import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; vi.mock("@/lib/config", () => ({ getBackendConfig: () => ({ apiBaseUrl: "http://backend.test" }), })); import { createWorkflowStepAction, deleteWorkspaceAction, exportAllWorkflowsAction, listWorkflowStepsAction, updateWorkflowStepAction, } from "./workspaces"; const REVIEW_STEP_NAME = "Review"; const STEP_COLOR = "bg-blue-500"; describe("exportAllWorkflowsAction", () => { beforeEach(() => { vi.stubGlobal( "fetch", vi.fn(async () => new Response("workflows: []", { status: 200 })), ); }); afterEach(() => { vi.unstubAllGlobals(); vi.restoreAllMocks(); }); const requestedUrl = () => { const fetchMock = fetch as unknown as ReturnType; expect(fetchMock).toHaveBeenCalledTimes(1); return new URL(String(fetchMock.mock.calls[0][0])); }; it("omits the ids param when no workflow IDs are passed (export all)", async () => { await exportAllWorkflowsAction("ws-1"); expect(requestedUrl().searchParams.has("ids")).toBe(false); }); it("restricts the export to the provided workflow IDs", async () => { await exportAllWorkflowsAction("ws-1", ["wf-1", "wf-3"]); expect(requestedUrl().searchParams.get("ids")).toBe("wf-1,wf-3"); }); it("sends an empty ids param so nothing is exported when the set is empty", async () => { await exportAllWorkflowsAction("ws-1", []); const url = requestedUrl(); expect(url.searchParams.has("ids")).toBe(true); expect(url.searchParams.get("ids")).toBe(""); }); }); describe("deleteWorkspaceAction", () => { beforeEach(() => { vi.stubGlobal( "fetch", vi.fn(async () => new Response("", { status: 204 })), ); }); afterEach(() => { vi.unstubAllGlobals(); vi.restoreAllMocks(); }); it("sends the workspace name as confirm_name in the DELETE body", async () => { await deleteWorkspaceAction("ws-1", "My Workspace"); const fetchMock = fetch as unknown as ReturnType; expect(fetchMock).toHaveBeenCalledTimes(1); const [url, init] = fetchMock.mock.calls[0]; expect(url).toBe("http://backend.test/api/v1/office/workspaces/ws-1"); expect(init.method).toBe("DELETE"); expect(JSON.parse(init.body as string)).toEqual({ confirm_name: "My Workspace" }); }); }); describe("workflow step WIP fields", () => { beforeEach(() => { vi.stubGlobal( "fetch", vi.fn(async () => Response.json({ id: "step-1", workflow_id: "wf-1", name: REVIEW_STEP_NAME, position: 1, color: STEP_COLOR, wip_limit: 2, pull_from_step_id: "step-0", created_at: "", updated_at: "", }), ), ); }); afterEach(() => { vi.unstubAllGlobals(); vi.restoreAllMocks(); }); it("preserves WIP fields returned from workflow step APIs", async () => { vi.stubGlobal( "fetch", vi.fn(async () => Response.json({ steps: [ { id: "step-1", workflow_id: "wf-1", name: REVIEW_STEP_NAME, position: 1, color: STEP_COLOR, wip_limit: 2, pull_from_step_id: "step-0", created_at: "", updated_at: "", }, ], }), ), ); const result = await listWorkflowStepsAction("wf-1"); expect(result.steps[0]).toMatchObject({ wip_limit: 2, pull_from_step_id: "step-0", }); }); it("sends WIP fields when creating a workflow step", async () => { await createWorkflowStepAction({ workflow_id: "wf-1", name: REVIEW_STEP_NAME, position: 1, color: STEP_COLOR, wip_limit: 2, pull_from_step_id: "step-0", } as Parameters[0]); const fetchMock = fetch as unknown as ReturnType; const [, init] = fetchMock.mock.calls[0]; expect(JSON.parse(init.body as string)).toMatchObject({ wip_limit: 2, pull_from_step_id: "step-0", }); }); it("sends WIP fields when updating a workflow step", async () => { await updateWorkflowStepAction("step-1", { wip_limit: 3, pull_from_step_id: "", } as Parameters[1]); const fetchMock = fetch as unknown as ReturnType; const [, init] = fetchMock.mock.calls[0]; expect(JSON.parse(init.body as string)).toMatchObject({ wip_limit: 3, pull_from_step_id: "", }); }); }); describe("workflow step cancellation fields", () => { beforeEach(() => { vi.stubGlobal( "fetch", vi.fn(async () => Response.json({ id: "step-1", workflow_id: "wf-1", name: REVIEW_STEP_NAME, position: 1, color: STEP_COLOR, cancel_triggers_turn_complete: true, created_at: "", updated_at: "", }), ), ); }); afterEach(() => { vi.unstubAllGlobals(); vi.restoreAllMocks(); }); it("preserves cancel completion policy returned from workflow step APIs", async () => { vi.stubGlobal( "fetch", vi.fn(async () => Response.json({ steps: [ { id: "step-1", workflow_id: "wf-1", name: REVIEW_STEP_NAME, position: 1, color: STEP_COLOR, cancel_triggers_turn_complete: true, created_at: "", updated_at: "", }, ], }), ), ); const result = await listWorkflowStepsAction("wf-1"); expect(result.steps[0].cancel_triggers_turn_complete).toBe(true); }); it("sends cancel completion policy when creating a workflow step", async () => { await createWorkflowStepAction({ workflow_id: "wf-1", name: REVIEW_STEP_NAME, position: 1, color: STEP_COLOR, cancel_triggers_turn_complete: true, } as Parameters[0]); const fetchMock = fetch as unknown as ReturnType; const [, init] = fetchMock.mock.calls[0]; expect(JSON.parse(init.body as string)).toMatchObject({ cancel_triggers_turn_complete: true, }); }); it("sends an explicit false when disabling cancel completion policy", async () => { await updateWorkflowStepAction("step-1", { cancel_triggers_turn_complete: false, } as Parameters[1]); const fetchMock = fetch as unknown as ReturnType; const [, init] = fetchMock.mock.calls[0]; expect(JSON.parse(init.body as string)).toMatchObject({ cancel_triggers_turn_complete: false, }); }); });