feat(core-audit): withAudit wrapper + Audited<F> brand
This commit is contained in:
36
packages/core-audit/src/with-audit.test.ts
Normal file
36
packages/core-audit/src/with-audit.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user