feat(core-audit): PayloadAuditLog.record impl (eraseSubject lands in Phase 3)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
41
packages/core-audit/src/payload-audit-log.test.ts
Normal file
41
packages/core-audit/src/payload-audit-log.test.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { PayloadAuditLog } from "./payload-audit-log";
|
||||
import type { AuditEntry } from "@repo/core-shared/audit";
|
||||
|
||||
const sample: AuditEntry = {
|
||||
actorId: "user_1",
|
||||
actorType: "user",
|
||||
actorRoles: ["admin"],
|
||||
action: "UPDATE",
|
||||
resource: { type: "articles", id: "abc" },
|
||||
changedFields: ["title", "body"],
|
||||
at: new Date("2026-05-11T10:00:00.000Z"),
|
||||
scope: { feature: "blog", environment: "production", tenant: "default" },
|
||||
from: { ipTruncated: "10.0.0.0", userAgent: "Mozilla/5.0" },
|
||||
containsPii: false,
|
||||
outcome: "success",
|
||||
};
|
||||
|
||||
describe("PayloadAuditLog.record", () => {
|
||||
it("maps AuditEntry → flat collection doc + calls payload.create", async () => {
|
||||
const mockCreate = vi.fn().mockResolvedValue({ id: "doc_1" });
|
||||
const mockGetPayload = vi.fn().mockResolvedValue({ create: mockCreate });
|
||||
const log = new PayloadAuditLog({} as never, mockGetPayload);
|
||||
|
||||
await log.record(sample);
|
||||
|
||||
expect(mockCreate).toHaveBeenCalledOnce();
|
||||
const call = mockCreate.mock.calls[0]![0] as { collection: string; data: Record<string, unknown> };
|
||||
expect(call.collection).toBe("audit-logs");
|
||||
expect(call.data.actorId).toBe("user_1");
|
||||
expect(call.data.action).toBe("UPDATE");
|
||||
expect(call.data.resourceType).toBe("articles");
|
||||
expect(call.data.resourceId).toBe("abc");
|
||||
expect(call.data.changedFields).toEqual(["title", "body"]);
|
||||
expect(call.data.scopeFeature).toBe("blog");
|
||||
expect(call.data.scopeTenant).toBe("default");
|
||||
expect(call.data.ipTruncated).toBe("10.0.0.0");
|
||||
expect(call.data.containsPii).toBe(false);
|
||||
expect(call.data.outcome).toBe("success");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user