import { describe, expect, it } from "bun:test"; import { getWorkflowSnapshot, updateWorkflowChooses, listWorkflowSnapshots } from "./api.js"; describe("workflow snapshot API", () => { it("normalizes null or missing snapshot items without losing pagination", async () => { for (const items of [null, undefined, []]) { const client = { get: async () => Response.json({ code: 200, data: { number: 2, size: 10, totalItems: 0, items } }) } as any; expect(await listWorkflowSnapshots(client, "fixture")).toEqual({ number: 2, size: 10, totalPages: 0, totalItems: 0, items: [] }); } }); it("keeps snapshot entries and rejects malformed items or business failures", async () => { const query = (body: unknown) => listWorkflowSnapshots({ get: async () => Response.json(body) } as any, "fixture"); expect((await query({ code: 200, data: { items: [{ id: "snapshot", pipelineId: "fixture" }] } })).items[0]?.id).toBe("snapshot"); for (const items of ["bad", {}, [{}]]) await expect(query({ code: 200, data: { items } })).rejects.toThrow(); await expect(query({ code: 403, msg: "denied", data: { items: null } })).rejects.toThrow("denied"); await expect(query({ code: 200, data: null })).rejects.toThrow(); }); it("uses the piper pipelines/snapshot endpoint first", async () => { const calls: string[] = []; const client = { get: async (path: string) => { calls.push(path); if (path === "api/easo/pipelines/snapshot/snap-1") { return new Response(JSON.stringify({ code: 200, data: { id: "snap-1", pipelineId: "pipe-1", version: "26.10.7" }, }), { status: 200, headers: { "content-type": "application/json" } }); } throw new Error("unexpected path"); }, } as any; const snapshot = await getWorkflowSnapshot(client, "snap-1", "pipe-1"); expect(calls).toEqual(["api/easo/pipelines/snapshot/snap-1"]); expect(snapshot.id).toBe("snap-1"); expect(snapshot.version).toBe("26.10.7"); }); it("refreshes chooses after the pipeline save", async () => { const calls: string[] = []; const client = { put: async (path: string) => { calls.push(path); return new Response(JSON.stringify({ code: 200, data: [] }), { status: 200, headers: { "content-type": "application/json" } }); }, } as any; await updateWorkflowChooses(client, "pipe-1"); expect(calls).toEqual(["api/easo/pipelines/pipe-1/chooses"]); }); });