From f1427a3f85147db63e4ea702ae82db4569efaddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hartmut=20N=C3=B6renberg?= Date: Wed, 1 Apr 2026 00:38:03 +0200 Subject: [PATCH] test(api): cover assistant estimate planning handoff errors --- ...s-estimate-planning-handoff-errors.test.ts | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 packages/api/src/__tests__/assistant-tools-estimate-planning-handoff-errors.test.ts diff --git a/packages/api/src/__tests__/assistant-tools-estimate-planning-handoff-errors.test.ts b/packages/api/src/__tests__/assistant-tools-estimate-planning-handoff-errors.test.ts new file mode 100644 index 0000000..4814c42 --- /dev/null +++ b/packages/api/src/__tests__/assistant-tools-estimate-planning-handoff-errors.test.ts @@ -0,0 +1,105 @@ +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 }); + } + }); +});