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 org unit mutation tools - success", () => { beforeEach(() => { vi.clearAllMocks(); vi.mocked(countPlanningEntries).mockResolvedValue({ countsByRoleId: new Map() }); }); it("routes org unit mutations through their backing router", async () => { const db = { orgUnit: { findUnique: vi.fn().mockImplementation(async (args?: { where?: { id?: string } }) => { if (args?.where?.id === "ou_1") { return { id: "ou_1", name: "Operations", shortName: "OPS", level: 5, parentId: null, sortOrder: 1, isActive: true, }; } return null; }), create: vi.fn().mockResolvedValue({ id: "ou_1", name: "Operations", shortName: "OPS", level: 5, parentId: null, sortOrder: 1, isActive: true, }), update: vi.fn().mockResolvedValue({ id: "ou_1", name: "Operations EU", shortName: null, level: 5, parentId: null, sortOrder: 4, isActive: false, }), }, auditLog: { create: vi.fn().mockResolvedValue({ id: "audit_1" }), }, }; const ctx = createToolContext(db, { userRole: SystemRole.ADMIN }); const createOrgUnitResult = await executeTool( "create_org_unit", JSON.stringify({ name: "Operations", shortName: "OPS", level: 5, sortOrder: 1 }), ctx, ); const updateOrgUnitResult = await executeTool( "update_org_unit", JSON.stringify({ id: "ou_1", name: "Operations EU", shortName: null, sortOrder: 4, isActive: false }), ctx, ); expect(JSON.parse(createOrgUnitResult.content)).toEqual(expect.objectContaining({ success: true, orgUnitId: "ou_1", message: "Created org unit: Operations", })); expect(JSON.parse(updateOrgUnitResult.content)).toEqual(expect.objectContaining({ success: true, orgUnitId: "ou_1", message: "Updated org unit: Operations EU", })); expect(db.orgUnit.create).toHaveBeenCalled(); expect(db.orgUnit.update).toHaveBeenCalled(); expect(db.auditLog.create).toHaveBeenCalled(); }); it("returns the expected assistant payloads for org unit mutations", async () => { const db = { orgUnit: { findUnique: vi.fn(async ({ where, }: { where: { id: string }; }) => ( where.id === "ou_1" ? { id: "ou_1", name: "Delivery", shortName: "DEL", level: 5, parentId: null, sortOrder: 1, isActive: true, } : null )), create: vi.fn().mockResolvedValue({ id: "ou_1", name: "Delivery", shortName: "DEL", level: 5, parentId: null, sortOrder: 1, isActive: true, }), update: vi.fn().mockResolvedValue({ id: "ou_1", name: "Delivery Europe", shortName: "DEU", level: 5, parentId: null, sortOrder: 2, isActive: true, }), }, auditLog: { create: vi.fn().mockResolvedValue({ id: "audit_1" }), }, }; const ctx = createToolContext(db, { userRole: SystemRole.ADMIN }); const createOrgUnitResult = await executeTool( "create_org_unit", JSON.stringify({ name: "Delivery", shortName: "DEL", level: 5, sortOrder: 1 }), ctx, ); const updateOrgUnitResult = await executeTool( "update_org_unit", JSON.stringify({ id: "ou_1", name: "Delivery Europe", shortName: "DEU", sortOrder: 2 }), ctx, ); expect(JSON.parse(createOrgUnitResult.content)).toEqual(expect.objectContaining({ success: true, message: "Created org unit: Delivery", })); expect(JSON.parse(updateOrgUnitResult.content)).toEqual(expect.objectContaining({ success: true, message: "Updated org unit: Delivery Europe", })); }); });