import { beforeEach, describe, expect, it, vi } from "vitest"; import { PermissionKey, SystemRole } from "@capakraken/shared"; vi.mock("@capakraken/application", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, approveEstimateVersion: vi.fn(), cloneEstimate: vi.fn(), createEstimateExport: vi.fn(), createEstimatePlanningHandoff: vi.fn(), createEstimateRevision: vi.fn(), getDashboardBudgetForecast: vi.fn().mockResolvedValue([]), getDashboardPeakTimes: vi.fn().mockResolvedValue([]), getEstimateById: vi.fn(), listAssignmentBookings: vi.fn().mockResolvedValue([]), submitEstimateVersion: vi.fn(), updateEstimateDraft: vi.fn(), }; }); import { createEstimatePlanningHandoff } from "@capakraken/application"; import { executeTool } from "../router/assistant-tools.js"; import { createToolContext, resetEstimateToolMocks, } from "./assistant-tools-estimate-test-helpers.js"; describe("assistant estimate planning handoff mutation errors", () => { beforeEach(() => { resetEstimateToolMocks(); }); it("returns stable assistant errors for create_estimate_planning_handoff mutations", async () => { const cases = [ { payload: { estimateId: "est_1" }, setup: () => vi.mocked(createEstimatePlanningHandoff).mockRejectedValueOnce(new Error("Linked project not found")), expected: "Project not found with the given criteria.", }, { payload: { estimateId: "est_1" }, setup: () => vi.mocked(createEstimatePlanningHandoff).mockRejectedValueOnce(new Error("Estimate has no approved version")), expected: "Estimate has no approved version.", }, { payload: { estimateId: "est_1" }, setup: () => vi.mocked(createEstimatePlanningHandoff).mockRejectedValueOnce( new Error("Planning handoff already exists for this approved version"), ), expected: "Planning handoff already exists for this approved version.", }, { payload: { estimateId: "est_1" }, setup: () => vi.mocked(createEstimatePlanningHandoff).mockRejectedValueOnce( new Error("Project window has no working days for demand line dl_1"), ), expected: "The linked project window has no working days for at least one demand line.", }, { payload: { estimateId: "est_1" }, setup: () => vi.mocked(createEstimatePlanningHandoff).mockRejectedValueOnce( new Error("Only approved versions can be handed off to planning"), ), expected: "Only approved versions can be handed off to planning.", }, { payload: { estimateId: "est_1" }, setup: () => vi.mocked(createEstimatePlanningHandoff).mockRejectedValueOnce( new Error("Estimate must be linked to a project before planning handoff"), ), expected: "Estimate must be linked to a project before planning handoff.", }, { payload: { estimateId: "est_1" }, setup: () => vi.mocked(createEstimatePlanningHandoff).mockRejectedValueOnce( new Error("Linked project has an invalid date range"), ), expected: "The linked project has an invalid date range for planning handoff.", }, ] as const; for (const testCase of cases) { testCase.setup(); const ctx = createToolContext({}, { userRole: SystemRole.MANAGER, permissions: [PermissionKey.MANAGE_ALLOCATIONS], }); const result = await executeTool( "create_estimate_planning_handoff", JSON.stringify(testCase.payload), ctx, ); expect(JSON.parse(result.content)).toEqual({ error: testCase.expected }); } }); });