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 demand fill error mapping", () => { beforeEach(() => { vi.clearAllMocks(); }); it("returns a stable assistant error when demand filling cannot resolve the demand", async () => { const ctx = createToolContext( { demandRequirement: { findUnique: vi.fn().mockResolvedValue(null), }, resource: { findUnique: vi.fn() .mockResolvedValueOnce(null) .mockResolvedValueOnce({ id: "resource_1", displayName: "Carol Danvers", eid: "EMP-001", chapter: "Delivery", isActive: true, }), findFirst: vi.fn().mockResolvedValue(null), }, }, { userRole: SystemRole.ADMIN, permissions: [PermissionKey.MANAGE_ALLOCATIONS], }, ); const result = await executeTool( "fill_demand", JSON.stringify({ demandId: "demand_missing", resourceId: "resource_1" }), ctx, ); expect(JSON.parse(result.content)).toEqual({ error: "Demand not found with the given criteria.", }); }); it("returns a stable assistant error when demand filling violates lifecycle preconditions", async () => { const ctx = createToolContext( { demandRequirement: { findUnique: vi.fn().mockResolvedValue({ id: "demand_1", projectId: "project_1", startDate: new Date("2026-05-01T00:00:00.000Z"), endDate: new Date("2026-05-15T00:00:00.000Z"), hoursPerDay: 6, role: "Designer", roleId: "role_1", headcount: 1, status: "COMPLETED", metadata: {}, }), }, resource: { findUnique: vi.fn() .mockResolvedValueOnce(null) .mockResolvedValueOnce({ id: "resource_1", displayName: "Carol Danvers", eid: "EMP-001", chapter: "Delivery", isActive: true, }), findFirst: vi.fn().mockResolvedValue(null), }, }, { userRole: SystemRole.ADMIN, permissions: [PermissionKey.MANAGE_ALLOCATIONS], }, ); const result = await executeTool( "fill_demand", JSON.stringify({ demandId: "demand_1", resourceId: "resource_1" }), ctx, ); expect(JSON.parse(result.content)).toEqual({ error: "Demand cannot be filled in its current status.", }); }); });