import { afterEach, describe, expect, it, spyOn } from "bun:test"; import { handleWorkflowTest } from "./workflow/test.js"; import * as shared from "./workflow/shared.js"; import * as services from "../../domains/platform/services.js"; import type { CliParsedArgs, CliRuntime } from "../types.js"; import type { WorkflowTestReport } from "../../domains/platform/types.js"; const runtime = { selectedCorp: { id: "mock-corp" }, jsonMode: true } as CliRuntime; const args = (options: Record = {}): CliParsedArgs => ({ area: "workflow", action: "test", args: ["pipe"], options: { flow: "ID01-record", ...options }, raw: ["workflow", "test", "pipe", "--flow=ID01-record"] }); const completed: WorkflowTestReport = { jobId: "job", pipelineId: "pipe", status: "COMPLETED", outcome: "completed", timedOut: false, diagnosticsComplete: true, diagnosticsErrors: [], runType: "DEBUG", targetEntityId: "record", executionTree: [], failedExecutions: [], conditionDiagnoses: [] }; describe("workflow debug mutation and result contract", () => { const spies: Array<{ mockRestore(): void }> = []; afterEach(() => { while (spies.length) spies.pop()!.mockRestore(); }); function setup(report = completed) { const client = spyOn(shared, "createWorkflowDetailClient").mockResolvedValue({ client: {} } as any); const detail = spyOn(services, "getWorkflowDetail").mockResolvedValue({ state: 1 } as any); const run = spyOn(services, "runWorkflowDebugTest").mockResolvedValue(report); spies.push(client, detail, run); return { client, detail, run }; } it("dry-run with auto-publish does not create clients or start a job even if --yes is also set", async () => { const { client, run } = setup(); const result = await handleWorkflowTest(args({ "dry-run": true, "auto-publish": true, yes: true }), runtime); const preview = JSON.parse(result.stdout); expect(preview.dryRun).toBe(true); expect(preview.changes.autoPublish).toBe(true); expect(preview.changes.warnings).not.toHaveLength(0); expect(client).not.toHaveBeenCalled(); expect(run).not.toHaveBeenCalled(); }); it("requires explicit mutation confirmation before creating clients", async () => { const { client, run } = setup(); await expect(handleWorkflowTest(args(), runtime)).rejects.toThrow("请先使用 --dry-run"); expect(client).not.toHaveBeenCalled(); expect(run).not.toHaveBeenCalled(); }); it("allows confirmed enabled-workflow debug with machine-readable risk information", async () => { const { run } = setup(); const result = await handleWorkflowTest(args({ yes: true }), runtime); expect(result.exitCode).toBe(0); const report = JSON.parse(result.stdout); expect(report.productionEnabled).toBe(true); expect(report.warnings).not.toHaveLength(0); expect(run).toHaveBeenCalledTimes(1); }); it("retains an unknown production state in the result when the status check fails", async () => { const { detail } = setup(); detail.mockRejectedValue(new Error("status unavailable")); const result = await handleWorkflowTest(args({ yes: true }), runtime); const report = JSON.parse(result.stdout); expect(report.productionEnabled).toBeNull(); expect(report.warnings.join(" ")).toContain("启用状态未知"); }); for (const jsonMode of [true, false]) { it(`returns a failing exit code for failed and incomplete runs (json=${jsonMode})`, async () => { setup({ ...completed, status: "FAILED", outcome: "failed" }); const result = await handleWorkflowTest(args({ yes: true }), { ...runtime, jsonMode }); expect(result.exitCode).toBe(1); expect(result.stdout).toContain("FAILED"); }); } });