Files
Nexus/packages/api/src/__tests__/assistant-tools-allocation-cancel-errors.test.ts
T
Hartmut b41c1d2501
CI / Architecture Guardrails (push) Successful in 2m38s
CI / Assistant Split Regression (push) Successful in 3m33s
CI / Typecheck (push) Successful in 3m51s
CI / Lint (push) Successful in 5m2s
CI / E2E Tests (push) Has been cancelled
CI / Fresh-Linux Docker Deploy (push) Has been cancelled
CI / Release Images (push) Has been cancelled
CI / Build (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
rename(phase 1): CapaKraken → Nexus across code, UI, docs, CI (#61)
rename(phase 1): CapaKraken → Nexus across code, UI, docs, CI (#61)

Co-authored-by: Hartmut Nörenberg <hn@hartmut-noerenberg.com>
Co-committed-by: Hartmut Nörenberg <hn@hartmut-noerenberg.com>
2026-05-21 16:28:40 +02:00

97 lines
2.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { PermissionKey, SystemRole } from "@nexus/shared";
import { TRPCError } from "@trpc/server";
import {
createAssignment,
createToolContext,
executeTool,
} from "./assistant-tools-allocation-cancel-test-helpers.js";
describe("assistant allocation cancel error tools", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("returns a stable assistant error when allocation cancellation cannot resolve an assignment", async () => {
const ctx = createToolContext(
{
assignment: {
findUnique: vi
.fn()
.mockRejectedValue(
new TRPCError({ code: "NOT_FOUND", message: "Assignment not found" }),
),
},
},
{
userRole: SystemRole.ADMIN,
permissions: [PermissionKey.MANAGE_ALLOCATIONS],
},
);
const result = await executeTool(
"cancel_allocation",
JSON.stringify({ allocationId: "assignment_missing" }),
ctx,
);
expect(JSON.parse(result.content)).toEqual({
error: "Allocation not found with the given criteria.",
});
});
it("returns stable assistant errors when allocation cancellation fails during the update step", async () => {
const assignment = createAssignment();
const errors = [
new TRPCError({ code: "NOT_FOUND", message: "Assignment not found" }),
{
code: "P2025",
message: "Record not found",
meta: { modelName: "Assignment" },
},
] as const;
for (const error of errors) {
const tx = {
assignment: {
findUnique: vi.fn().mockResolvedValue(assignment),
update: vi.fn().mockRejectedValue(error),
},
auditLog: {
create: vi.fn(),
},
};
const ctx = createToolContext(
{
assignment: {
findUnique: vi.fn().mockResolvedValue(assignment),
},
webhook: {
findMany: vi.fn().mockResolvedValue([]),
},
$transaction: vi
.fn()
.mockImplementation(async (callback: (inner: typeof tx) => Promise<unknown>) =>
callback(tx),
),
},
{
userRole: SystemRole.ADMIN,
permissions: [PermissionKey.MANAGE_ALLOCATIONS],
},
);
const result = await executeTool(
"cancel_allocation",
JSON.stringify({ allocationId: "assignment_1" }),
ctx,
);
expect(JSON.parse(result.content)).toEqual({
error: "Allocation not found with the given criteria.",
});
}
});
});