feat(core-audit): withAudit wrapper + Audited<F> brand

This commit is contained in:
2026-05-12 21:40:28 +02:00
parent c7bd9a2f8a
commit 103e06d20a
3 changed files with 71 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
import { describe, it, expect, expectTypeOf, vi } from "vitest";
import { withAudit, type Audited } from "@/with-audit";
import type { IAuditLog } from "@/audit-log.interface";
function makeAuditLog(): IAuditLog {
return {
record: vi.fn().mockResolvedValue(undefined),
eraseSubject: vi.fn().mockResolvedValue(undefined),
};
}
describe("withAudit", () => {
it("returns an Audited<F>", () => {
const auditLog = makeAuditLog();
const fn = async (input: { id: string }) => ({ ok: true });
const wrapped = withAudit(auditLog, fn);
expectTypeOf(wrapped).toMatchTypeOf<Audited<typeof fn>>();
});
it("passes input and output through unchanged", async () => {
const auditLog = makeAuditLog();
const fn = async (input: { id: string }) => ({ ok: true, id: input.id });
const wrapped = withAudit(auditLog, fn);
const result = await wrapped({ id: "abc" });
expect(result).toEqual({ ok: true, id: "abc" });
});
it("propagates errors", async () => {
const auditLog = makeAuditLog();
const err = new Error("boom");
const wrapped = withAudit(auditLog, async () => {
throw err;
});
await expect(wrapped()).rejects.toBe(err);
});
});