Files
CapaKraken/packages/api/src/__tests__/assistant-tools-estimate-test-helpers.ts
T
Hartmut 3c0179fcec fix(api): wrap audit log writes inside their parent transactions
Prevents mutations from committing without an audit trail if the
auditLog.create call fails after the main write already succeeded.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-09 16:40:10 +02:00

55 lines
1.6 KiB
TypeScript

import {
approveEstimateVersion,
cloneEstimate,
createEstimateExport,
createEstimatePlanningHandoff,
createEstimateRevision,
getEstimateById,
submitEstimateVersion,
updateEstimateDraft,
} from "@capakraken/application";
import { PermissionKey, SystemRole } from "@capakraken/shared";
import { vi } from "vitest";
import type { ToolContext } from "../router/assistant-tools.js";
export function createToolContext(
db: Record<string, unknown>,
options?: {
permissions?: PermissionKey[];
userRole?: SystemRole;
},
): ToolContext {
const userRole = options?.userRole ?? SystemRole.ADMIN;
const mergedDb: Record<string, unknown> = { ...db };
mergedDb["$transaction"] = vi.fn(async (fn: (tx: unknown) => unknown) => fn(mergedDb));
return {
db: mergedDb as ToolContext["db"],
userId: "user_1",
userRole,
permissions: new Set(options?.permissions ?? []),
session: {
user: { email: "assistant@example.com", name: "Assistant User", image: null },
expires: "2026-03-29T00:00:00.000Z",
},
dbUser: {
id: "user_1",
systemRole: userRole,
permissionOverrides: null,
},
roleDefaults: null,
};
}
export function resetEstimateToolMocks() {
vi.clearAllMocks();
vi.mocked(approveEstimateVersion).mockReset();
vi.mocked(cloneEstimate).mockReset();
vi.mocked(createEstimateExport).mockReset();
vi.mocked(createEstimatePlanningHandoff).mockReset();
vi.mocked(createEstimateRevision).mockReset();
vi.mocked(getEstimateById).mockReset();
vi.mocked(submitEstimateVersion).mockReset();
vi.mocked(updateEstimateDraft).mockReset();
}