import { describe, expect, it } from "bun:test"; import { runWorkflowDebugTest } from "../services.js"; function setup(status = "COMPLETED", errors: string[] = []) { const posts: string[] = []; const response = (data: unknown) => new Response(JSON.stringify({ code: 200, data })); const client = { post: async (path: string) => { posts.push(path); return response("job"); }, get: async (path: string) => { if (path.endsWith("/level/failed")) { if (errors.includes("failures")) throw new Error("403 failures unavailable"); return response([]); } if (path.endsWith("/execution")) { if (errors.includes("tree")) throw new Error("403 execution unavailable"); return response([]); } if (errors.includes("job")) throw new Error("403 job unavailable"); return response({ id: "job", status }); }, } as any; return { client, posts }; } describe("workflow debug completion evidence", () => { it("reports completed only when all diagnostics were fetched", async () => { const { client } = setup(); const result = await runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record" }); expect(result.outcome).toBe("completed"); expect(result.timedOut).toBe(false); expect(result.diagnosticsComplete).toBe(true); }); it("reports a failed terminal job", async () => { const { client } = setup("FAILED"); const result = await runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record" }); expect(result.outcome).toBe("failed"); }); it("keeps timed-out jobs queryable and exposes missing diagnostic evidence", async () => { const { client } = setup("RUNNING", ["tree", "failures"]); const result = await runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record", timeoutMs: 0 }); expect(result.jobId).toBe("job"); expect(result.status).toBe("RUNNING"); expect(result.outcome).toBe("timed_out"); expect(result.timedOut).toBe(true); expect(result.diagnosticsComplete).toBe(false); expect(result.diagnosticsErrors.map(error => error.source)).toEqual(["executionTree", "failedExecutions"]); }); it("marks partial diagnostic reads as incomplete rather than clean success", async () => { const { client } = setup("COMPLETED", ["tree"]); const result = await runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record" }); expect(result.outcome).toBe("incomplete"); expect(result.diagnosticsErrors[0]?.message).toContain("403"); }); it("preserves job identity when polling fails and does not hide the error", async () => { const { client } = setup("RUNNING", ["job"]); const result = await runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record" }); expect(result.jobId).toBe("job"); expect(result.outcome).toBe("incomplete"); expect(result.diagnosticsErrors[0]?.source).toBe("job"); }); it("rejects invalid timing options before creating a mutating job", async () => { const { client, posts } = setup(); await expect(runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record", timeoutMs: -1 })).rejects.toThrow("timeoutMs"); expect(posts).toEqual([]); }); describe("numeric job state must agree with the watch pipeline", () => { const withState = (state: number) => { const { client } = setup(); const get = client.get; client.get = async (path: string) => { if (path.endsWith("/job")) { return new Response(JSON.stringify({ code: 200, data: { id: "job", state } }), { headers: { "content-type": "application/json" }, }); } return get(path); }; return client; }; it("reads state 0 as completed instead of polling until timeout", async () => { const result = await runWorkflowDebugTest(withState(0), "pipe", { flowCodeOrId: "ID01-record", timeoutMs: 100, pollIntervalMs: 1 }); expect(result.status).toBe("COMPLETED"); expect(result.outcome).toBe("completed"); expect(result.timedOut).toBe(false); }); it("reads state 1 as still running", async () => { const result = await runWorkflowDebugTest(withState(1), "pipe", { flowCodeOrId: "ID01-record", timeoutMs: 0 }); expect(result.status).toBe("RUNNING"); expect(result.outcome).toBe("timed_out"); }); }); it("retries a newly created job's HTTP 404 instead of reporting an authorization failure", async () => { const { client } = setup(); const get = client.get; let jobReads = 0; client.get = async (path: string) => { if (path.endsWith("/job") && jobReads++ === 0) throw { response: { status: 404 } }; return get(path); }; const result = await runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record", timeoutMs: 100, pollIntervalMs: 1 }); expect(result.outcome).toBe("completed"); expect(jobReads).toBe(2); }); });