import { describe, expect, it, vi } from "vitest"; import { auditEvent, withAuditMiddleware } from "./audit.js"; describe("foundation audit helpers", () => { it("normalizes empty optional fields and falls back to system actor type", async () => { const client = { auditEvent: { create: vi.fn(async (args) => args.data) } }; await auditEvent( { action: "resource.updated", tenantId: undefined, actorId: undefined, actorType: " ", targetType: undefined, targetId: undefined, requestId: undefined, metadata: undefined, }, client, ); expect(client.auditEvent.create).toHaveBeenCalledWith({ data: { tenantId: null, actorId: null, actorType: "system", action: "resource.updated", targetType: null, targetId: null, requestId: null, metadata: null, }, }); }); it("preserves trimmed actor types and skips null middleware events", async () => { const client = { auditEvent: { create: vi.fn(async (args) => args.data) } }; await auditEvent({ action: "resource.created", actorType: " user " }, client); expect(client.auditEvent.create).toHaveBeenCalledWith({ data: expect.objectContaining({ action: "resource.created", actorType: "user" }), }); client.auditEvent.create.mockClear(); const handler = withAuditMiddleware(async () => ({ id: "result_1" }), () => null, client); await expect(handler()).resolves.toEqual({ id: "result_1" }); expect(client.auditEvent.create).not.toHaveBeenCalled(); }); });