import { describe, expect, it } from "vitest"; import { AUDIT_PDF_HARD_ROW_LIMIT, buildAuditPdfDocument, serializeAuditEventsToPdf, summarizeAuditPdfFilters, type AuditPdfEvent, type AuditPdfReportInput, } from "./audit-pdf.js"; import { ExportPdfRowLimitExceededError } from "./pdf/index.js"; function makeEvent(over: Partial = {}): 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: "deadbeef0123456789abcdef0123456789abcdef0123456789abcdef01234567", ...over, }; } function makeInput(over: Partial = {}): AuditPdfReportInput { return { tenantId: "tenant_1", from: "2026-05-01", to: "2026-05-17", filterSummary: "regulatoryScope=soc2", integrity: { ok: true, inspected: 1, firstBreakId: null, reason: null }, events: [makeEvent()], generatedAt: new Date("2026-05-17T12:34:56.000Z"), generatedBy: "ops-bot", ...over, }; } function collectText(node: unknown): string { if (typeof node === "string" || typeof node === "number") { return String(node); } if (!node || typeof node !== "object") { return ""; } const props = (node as { props?: { children?: unknown } }).props; const children = props?.children; if (Array.isArray(children)) { return children.map(collectText).join(" "); } return collectText(children); } describe("summarizeAuditPdfFilters", () => { it("joins regulatoryScope, actorType and outcome", () => { expect(summarizeAuditPdfFilters({ regulatoryScope: ["soc2", "gdpr"], actorType: "admin", outcome: "blocked" })).toBe( "regulatoryScope=soc2|gdpr actorType=admin outcome=blocked" ); }); it("returns empty when nothing is set", () => { expect(summarizeAuditPdfFilters({})).toBe(""); }); }); describe("buildAuditPdfDocument", () => { it("returns a React element with the expected title", () => { const doc = buildAuditPdfDocument(makeInput({ title: "Custom title" })); expect(doc).toBeTruthy(); expect((doc as unknown as { props: { title: string } }).props.title).toBe("Custom title"); }); it("accepts localized labels for visible report copy", () => { const doc = buildAuditPdfDocument(makeInput({ labels: { title: "Audit-Compliance-Bericht", tenant: "Konto", platformTenant: "Plattform", range: "Zeitraum", filters: "Filter", generatedBy: "Erstellt von", generatedAt: "Erstellt am", hashChainIntegrity: "Hash-Chain-Integritaet", events: (count) => `Ereignisse (${count})`, integrityOk: (inspected) => `verifyAuditChain OK - ${inspected} Ereignisse geprueft.`, columns: { actorType: "Akteur", outcome: "Ergebnis", regulatoryScope: "Geltungsbereich", }, }, })); const text = collectText(doc); expect((doc as unknown as { props: { title: string } }).props.title).toBe("Audit-Compliance-Bericht"); expect(text).toContain("Konto: tenant_1"); expect(text).toContain("Hash-Chain-Integritaet"); expect(text).toContain("Ereignisse (1)"); expect(text).toContain("Akteur"); expect(text).toContain("Ergebnis"); }); }); describe("serializeAuditEventsToPdf", () => { it("renders a real PDF byte stream", async () => { const bytes = await serializeAuditEventsToPdf(makeInput()); expect(bytes).toBeInstanceOf(Uint8Array); expect(bytes.length).toBeGreaterThan(0); // PDFs start with %PDF const header = Buffer.from(bytes.slice(0, 4)).toString("utf8"); expect(header).toBe("%PDF"); }); it("throws when event count exceeds the hard cap", async () => { const tooMany: AuditPdfEvent[] = Array.from({ length: AUDIT_PDF_HARD_ROW_LIMIT + 1 }, (_, i) => makeEvent({ id: `e_${i}` })); await expect(serializeAuditEventsToPdf(makeInput({ events: tooMany }))).rejects.toBeInstanceOf(ExportPdfRowLimitExceededError); }); });