import { beforeEach, describe, expect, it, vi } from "vitest"; import { VacationType } from "@capakraken/db"; 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-planning-read-test-helpers.js"; describe("assistant planning availability read tools", () => { beforeEach(() => { vi.clearAllMocks(); }); it("routes availability checks through the allocation and vacation router paths", async () => { const db = { resource: { findUnique: vi.fn().mockResolvedValue({ id: "res_1", eid: "EMP-001", displayName: "Bruce Banner", fte: 1, availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 }, countryId: null, federalState: null, metroCityId: null, country: null, metroCity: null, }), findFirst: vi.fn(), }, assignment: { findMany: vi.fn().mockResolvedValue([ { id: "assign_1", startDate: new Date("2026-04-01T00:00:00.000Z"), endDate: new Date("2026-04-01T00:00:00.000Z"), hoursPerDay: 4, status: "CONFIRMED", project: { name: "Gelddruckmaschine", shortCode: "GDM" }, }, ]), }, vacation: { findMany: vi.fn().mockResolvedValue([ { id: "vac_1", resourceId: "res_1", status: "APPROVED", type: VacationType.ANNUAL, startDate: new Date("2026-04-02T00:00:00.000Z"), endDate: new Date("2026-04-02T00:00:00.000Z"), isHalfDay: true, halfDayPart: "AFTERNOON", resource: { id: "res_1", displayName: "Bruce Banner", eid: "EMP-001", lcrCents: 8_000, }, requestedBy: null, approvedBy: null, }, ]), }, }; const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER, permissions: [PermissionKey.VIEW_PLANNING], }); const result = await executeTool( "check_resource_availability", JSON.stringify({ resourceId: "res_1", startDate: "2026-04-01", endDate: "2026-04-02" }), ctx, ); expect(db.assignment.findMany).toHaveBeenCalledWith({ where: { resourceId: "res_1", status: { not: "CANCELLED" }, startDate: { lte: new Date("2026-04-02T00:00:00.000Z") }, endDate: { gte: new Date("2026-04-01T00:00:00.000Z") }, }, select: { id: true, startDate: true, endDate: true, hoursPerDay: true, status: true, project: { select: { name: true, shortCode: true } }, }, orderBy: { startDate: "asc" }, }); expect(JSON.parse(result.content)).toEqual( expect.objectContaining({ resource: "Bruce Banner", workingDays: 2, periodAvailableHours: 16, periodBookedHours: 4, periodRemainingHours: 12, availableHoursPerDay: 6, isFullyAvailable: false, existingAllocations: [ { project: "Gelddruckmaschine (GDM)", hoursPerDay: 4, status: "CONFIRMED", start: "2026-04-01", end: "2026-04-01", }, ], vacations: [ { type: VacationType.ANNUAL, start: "2026-04-02", end: "2026-04-02", isHalfDay: true, }, ], }), ); }); });