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(), commitDispoImportBatch: vi.fn(), countPlanningEntries: vi.fn().mockResolvedValue({ countsByRoleId: new Map() }), createEstimateExport: vi.fn(), createEstimatePlanningHandoff: vi.fn(), createEstimateRevision: vi.fn(), assessDispoImportReadiness: vi.fn(), loadResourceDailyAvailabilityContexts: vi.fn().mockResolvedValue(new Map()), getDashboardDemand: vi.fn().mockResolvedValue([]), getDashboardBudgetForecast: vi.fn().mockResolvedValue([]), getDashboardOverview: vi.fn(), getDashboardSkillGapSummary: vi.fn().mockResolvedValue({ roleGaps: [], totalOpenPositions: 0, skillSupplyTop10: [], resourcesByRole: [], }), getDashboardProjectHealth: vi.fn().mockResolvedValue([]), getDashboardPeakTimes: vi.fn().mockResolvedValue([]), getDashboardTopValueResources: vi.fn().mockResolvedValue([]), getEstimateById: vi.fn(), listAssignmentBookings: vi.fn().mockResolvedValue([]), stageDispoImportBatch: vi.fn(), submitEstimateVersion: vi.fn(), updateEstimateDraft: vi.fn(), }; }); import { executeTool } from "../router/assistant-tools.js"; import { createToolContext } from "./assistant-tools-allocation-planning-test-helpers.js"; describe("assistant allocation create tools", () => { beforeEach(() => { vi.clearAllMocks(); }); it("returns a stable assistant error for duplicate allocations", async () => { const ctx = createToolContext( { resource: { findUnique: vi .fn() .mockResolvedValueOnce({ id: "resource_1", eid: "EMP-001", displayName: "Carol Danvers", }) .mockResolvedValueOnce({ id: "resource_1", eid: "EMP-001", displayName: "Carol Danvers", lcrCents: 7000, availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 }, }), findFirst: vi.fn().mockResolvedValue(null), }, project: { findUnique: vi .fn() .mockResolvedValueOnce(null) .mockResolvedValueOnce({ id: "project_1", name: "Project One", shortCode: "PROJ-1", status: "ACTIVE", responsiblePerson: "Peter Parker", }), findFirst: vi.fn().mockResolvedValue(null), }, assignment: { findMany: vi.fn().mockResolvedValue([ { id: "assignment_existing", resourceId: "resource_1", projectId: "project_1", startDate: new Date("2026-06-01T00:00:00.000Z"), endDate: new Date("2026-06-05T00:00:00.000Z"), status: "PROPOSED", resource: { id: "resource_1", displayName: "Carol Danvers", eid: "EMP-001" }, project: { id: "project_1", name: "Project One", shortCode: "PROJ-1" }, }, ]), }, }, { userRole: SystemRole.ADMIN, permissions: [PermissionKey.MANAGE_ALLOCATIONS], }, ); const result = await executeTool( "create_allocation", JSON.stringify({ resourceId: "resource_1", projectId: "project_1", startDate: "2026-06-01", endDate: "2026-06-05", hoursPerDay: 6, }), ctx, ); expect(JSON.parse(result.content)).toEqual({ error: "Allocation already exists for this resource/project/dates. No new allocation created.", }); }); it("returns a stable assistant error when allocation creation receives an invalid start date", async () => { const ctx = createToolContext( { resource: { findUnique: vi .fn() .mockResolvedValueOnce(null) .mockResolvedValueOnce({ id: "resource_1", displayName: "Carol Danvers", eid: "EMP-001", chapter: "Delivery", isActive: true, fte: 1, lcrCents: 7000, availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 }, }), findFirst: vi.fn().mockResolvedValue(null), }, project: { findUnique: vi .fn() .mockResolvedValueOnce(null) .mockResolvedValueOnce({ id: "project_1", name: "Project One", shortCode: "PROJ-1", status: "ACTIVE", responsiblePerson: "Peter Parker", }), findFirst: vi.fn().mockResolvedValue(null), }, }, { userRole: SystemRole.ADMIN, permissions: [PermissionKey.MANAGE_ALLOCATIONS], }, ); const result = await executeTool( "create_allocation", JSON.stringify({ resourceId: "resource_1", projectId: "project_1", startDate: "2026-13-01", endDate: "2026-06-05", hoursPerDay: 6, }), ctx, ); expect(JSON.parse(result.content)).toEqual({ error: "Invalid startDate: 2026-13-01", }); }); });