feat(core-audit): withAudit wraps and attaches runtime __audited marker

This commit is contained in:
2026-05-12 22:42:16 +02:00
parent 77fddcde78
commit 1d8b30045a
2 changed files with 30 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
import { describe, it, expect, expectTypeOf, vi } from "vitest";
import { withAudit, type Audited } from "@/with-audit";
import type { IAuditLog } from "@/audit-log.interface";
import { isAudited } from "@repo/core-shared/conformance";
function makeAuditLog(): IAuditLog {
return {
@@ -17,6 +18,22 @@ describe("withAudit", () => {
expectTypeOf(wrapped).toMatchTypeOf<Audited<typeof fn>>();
});
it("attaches __audited as a non-enumerable property on the wrapped function", () => {
const auditLog = makeAuditLog();
const fn = async () => ({ ok: true });
const wrapped = withAudit(auditLog, fn);
expect(isAudited(wrapped)).toBe(true);
expect(Object.keys(wrapped)).not.toContain("__audited");
});
it("does NOT pollute the original input function with the brand", () => {
const auditLog = makeAuditLog();
const fn = async () => ({ ok: true });
const wrapped = withAudit(auditLog, fn);
expect(isAudited(fn)).toBe(false);
expect(wrapped).not.toBe(fn);
});
it("passes input and output through unchanged", async () => {
const auditLog = makeAuditLog();
const fn = async (input: { id: string }) => ({ ok: true, id: input.id });