138 lines
5.0 KiB
TypeScript
138 lines
5.0 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import type { AuditPdfEvent, AuditPdfIntegrity, AuditPdfReportInput } from "@hivegrid/exports/audit-pdf";
|
|
import { runAuditExportPdf, type AuditExportPdfDependencies } from "./export-pdf.js";
|
|
|
|
function makeEvent(over: Partial<AuditPdfEvent> = {}): AuditPdfEvent {
|
|
return {
|
|
id: "audit_1",
|
|
createdAt: new Date("2026-05-17T00:00:00.000Z"),
|
|
tenantId: "tenant_1",
|
|
actorType: "user",
|
|
actorId: "user_1",
|
|
actorEmail: null,
|
|
actorIp: null,
|
|
actorUserAgent: null,
|
|
action: "widget.created",
|
|
targetType: "widget",
|
|
targetId: "widget_1",
|
|
requestId: null,
|
|
sourceModule: "crm",
|
|
outcome: "success",
|
|
dataSensitivity: "internal",
|
|
regulatoryScope: ["soc2"],
|
|
metadata: null,
|
|
previousHash: null,
|
|
eventHash: "h1",
|
|
...over,
|
|
};
|
|
}
|
|
|
|
type TestState = {
|
|
writtenPath: string | null;
|
|
writtenBytes: Uint8Array | null;
|
|
renderInput: AuditPdfReportInput | null;
|
|
};
|
|
|
|
function makeDeps(opts: {
|
|
events: ReadonlyArray<AuditPdfEvent>;
|
|
integrity?: AuditPdfIntegrity;
|
|
}): {
|
|
deps: AuditExportPdfDependencies;
|
|
state: TestState;
|
|
logger: { info: ReturnType<typeof vi.fn>; error: ReturnType<typeof vi.fn> };
|
|
} {
|
|
const state: TestState = { writtenPath: null, writtenBytes: null, renderInput: null };
|
|
const logger = { info: vi.fn(), error: vi.fn() };
|
|
const deps: AuditExportPdfDependencies = {
|
|
loadEvents: async () => opts.events,
|
|
verify: () => opts.integrity ?? { ok: true, inspected: opts.events.length, firstBreakId: null, reason: null },
|
|
render: async (input) => {
|
|
state.renderInput = input;
|
|
// Fake PDF bytes — the renderer is unit-tested separately in
|
|
// @hivegrid/exports.
|
|
return new Uint8Array([0x25, 0x50, 0x44, 0x46, 0x2d, 0x31, 0x2e, 0x37]); // "%PDF-1.7"
|
|
},
|
|
writeOutput: async (path, bytes) => {
|
|
state.writtenPath = path;
|
|
state.writtenBytes = bytes;
|
|
},
|
|
logger,
|
|
now: () => new Date("2026-05-17T12:00:00.000Z"),
|
|
};
|
|
return { deps, state, logger };
|
|
}
|
|
|
|
describe("runAuditExportPdf", () => {
|
|
it("rejects empty --tenant", async () => {
|
|
const { deps, logger } = makeDeps({ events: [] });
|
|
const out = await runAuditExportPdf({ tenant: " ", out: "/tmp/x.pdf" }, deps);
|
|
expect(out.exitCode).toBe(1);
|
|
expect(logger.error).toHaveBeenCalledWith(expect.stringContaining("Missing --tenant"));
|
|
});
|
|
|
|
it("rejects missing --out", async () => {
|
|
const { deps, logger } = makeDeps({ events: [] });
|
|
const out = await runAuditExportPdf({ tenant: "t1", out: "" }, deps);
|
|
expect(out.exitCode).toBe(1);
|
|
expect(logger.error).toHaveBeenCalledWith(expect.stringContaining("Missing --out"));
|
|
});
|
|
|
|
it("renders + writes a PDF and exits 0 when the chain is intact", async () => {
|
|
const events = [makeEvent(), makeEvent({ id: "audit_2", action: "widget.updated", previousHash: "h1", eventHash: "h2" })];
|
|
const { deps, state, logger } = makeDeps({ events });
|
|
const out = await runAuditExportPdf(
|
|
{
|
|
tenant: "tenant_1",
|
|
from: "2026-05-01T00:00:00.000Z",
|
|
to: "2026-05-17T00:00:00.000Z",
|
|
regulatoryScope: "soc2",
|
|
out: "/tmp/report.pdf",
|
|
generatedBy: "ops-bot",
|
|
},
|
|
deps
|
|
);
|
|
expect(out.exitCode).toBe(0);
|
|
expect(out.rowCount).toBe(2);
|
|
expect(state.writtenPath).toBe("/tmp/report.pdf");
|
|
expect(state.writtenBytes?.[0]).toBe(0x25); // '%'
|
|
expect(state.renderInput?.tenantId).toBe("tenant_1");
|
|
expect(state.renderInput?.filterSummary).toContain("regulatoryScope=soc2");
|
|
expect(state.renderInput?.events).toHaveLength(2);
|
|
expect(state.renderInput?.integrity.ok).toBe(true);
|
|
expect(logger.info).toHaveBeenCalledWith(expect.stringContaining("audit-pdf written"));
|
|
});
|
|
|
|
it("treats tenant=__platform__ as null tenantId in the loader", async () => {
|
|
const events: AuditPdfEvent[] = [];
|
|
let captured: { tenantId: string | null } | null = null;
|
|
const { deps: baseDeps } = makeDeps({ events });
|
|
const deps: AuditExportPdfDependencies = {
|
|
...baseDeps,
|
|
loadEvents: async (args) => {
|
|
captured = { tenantId: args.tenantId };
|
|
return events;
|
|
},
|
|
};
|
|
await runAuditExportPdf({ tenant: "__platform__", out: "/tmp/x.pdf" }, deps);
|
|
expect(captured).toEqual({ tenantId: null });
|
|
});
|
|
|
|
it("exits 1 when integrity is broken — but still writes the file as evidence", async () => {
|
|
const events = [makeEvent()];
|
|
const { deps, state, logger } = makeDeps({
|
|
events,
|
|
integrity: { ok: false, inspected: 1, firstBreakId: "audit_1", reason: "hash-mismatch" },
|
|
});
|
|
const out = await runAuditExportPdf({ tenant: "t1", out: "/tmp/x.pdf" }, deps);
|
|
expect(out.exitCode).toBe(1);
|
|
expect(state.writtenPath).toBe("/tmp/x.pdf");
|
|
expect(logger.error).toHaveBeenCalledWith(expect.stringContaining("BROKEN CHAIN"));
|
|
});
|
|
|
|
it("throws when --from is not parseable", async () => {
|
|
const { deps } = makeDeps({ events: [] });
|
|
await expect(runAuditExportPdf({ tenant: "t1", from: "garbage", out: "/tmp/x.pdf" }, deps)).rejects.toThrow(/Invalid --from/);
|
|
});
|
|
});
|