import { beforeEach, describe, expect, it, vi } from "vitest"; import { SystemRole } from "@capakraken/shared"; vi.mock("@capakraken/application", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, countPlanningEntries: vi.fn().mockResolvedValue({ countsByRoleId: new Map() }), getDashboardBudgetForecast: vi.fn().mockResolvedValue([]), getDashboardPeakTimes: vi.fn().mockResolvedValue([]), listAssignmentBookings: vi.fn().mockResolvedValue([]), }; }); import { countPlanningEntries } from "@capakraken/application"; import { executeTool } from "../router/assistant-tools.js"; import { createToolContext } from "./assistant-tools-master-data-mutation-test-helpers.js"; describe("assistant client create and update tools - errors", () => { beforeEach(() => { vi.clearAllMocks(); vi.mocked(countPlanningEntries).mockResolvedValue({ countsByRoleId: new Map() }); }); it("returns a stable error when creating a client with a duplicate code", async () => { const ctx = createToolContext( { client: { findUnique: vi.fn(async ({ where, }: { where: { id?: string; code?: string }; }) => { if (where.code === "ACM") { return { id: "client_existing", code: "ACM", name: "Existing Client" }; } return null; }), }, }, { userRole: SystemRole.MANAGER }, ); const result = await executeTool( "create_client", JSON.stringify({ name: "Acme Mobility", code: "ACM" }), ctx, ); expect(JSON.parse(result.content)).toEqual( expect.objectContaining({ error: "A client with this code already exists.", }), ); }); it("returns a stable error when creating a client with a missing parent", async () => { const ctx = createToolContext( { client: { findUnique: vi.fn().mockResolvedValue(null), }, }, { userRole: SystemRole.MANAGER }, ); const result = await executeTool( "create_client", JSON.stringify({ name: "Acme Mobility", parentId: "client_missing" }), ctx, ); expect(JSON.parse(result.content)).toEqual( expect.objectContaining({ error: "Parent client not found with the given criteria.", }), ); }); it("returns a stable error when updating a missing client", async () => { const ctx = createToolContext( { client: { findUnique: vi.fn().mockResolvedValue(null), }, }, { userRole: SystemRole.MANAGER }, ); const result = await executeTool( "update_client", JSON.stringify({ id: "client_missing", name: "Acme Mobility" }), ctx, ); expect(JSON.parse(result.content)).toEqual( expect.objectContaining({ error: "Client not found with the given criteria.", }), ); }); });