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, getDashboardBudgetForecast: vi.fn().mockResolvedValue([]), getDashboardPeakTimes: vi.fn().mockResolvedValue([]), listAssignmentBookings: vi.fn().mockResolvedValue([]), }; }); import { executeTool } from "../router/assistant-tools.js"; import { createToolContext } from "./assistant-tools-user-admin-test-helpers.js"; describe("assistant user admin resource linking conflict errors", () => { beforeEach(() => { vi.clearAllMocks(); }); it("returns a stable error when linking a resource that is already linked to another user", async () => { const ctx = createToolContext( { user: { findUnique: vi.fn().mockResolvedValue({ id: "user_1" }), }, resource: { findUnique: vi.fn().mockResolvedValue({ id: "resource_1", userId: "user_2" }), updateMany: vi.fn(), }, }, SystemRole.ADMIN, ); const result = await executeTool( "link_user_resource", JSON.stringify({ userId: "user_1", resourceId: "resource_1" }), ctx, ); expect(JSON.parse(result.content)).toEqual({ error: "Resource is already linked to another user.", }); }); it("returns a stable retry error when the resource link changes during update", async () => { const ctx = createToolContext( { user: { findUnique: vi.fn().mockResolvedValue({ id: "user_1" }), }, resource: { findUnique: vi.fn().mockResolvedValue({ id: "resource_1", userId: null }), updateMany: vi.fn().mockResolvedValueOnce({ count: 0 }).mockResolvedValueOnce({ count: 0 }), }, }, SystemRole.ADMIN, ); const result = await executeTool( "link_user_resource", JSON.stringify({ userId: "user_1", resourceId: "resource_1" }), ctx, ); expect(JSON.parse(result.content)).toEqual({ error: "Resource link changed during update. Please retry.", }); }); });