70 lines
3.0 KiB
TypeScript
70 lines
3.0 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
DEFAULT_AUDIT_RETENTION_DAYS,
|
|
GDPR_AUDIT_RETENTION_DAYS,
|
|
SOC2_AUDIT_RETENTION_DAYS,
|
|
resolveAuditRetentionPolicy,
|
|
runAuditRetentionJob,
|
|
} from "./audit-retention.js";
|
|
|
|
describe("resolveAuditRetentionPolicy", () => {
|
|
it("returns 7y for SOC2 + HIPAA tenants (HIPAA wins on label)", () => {
|
|
expect(resolveAuditRetentionPolicy({ tenantId: "t1", regulatoryScopes: ["soc2"] })).toEqual({ retentionDays: SOC2_AUDIT_RETENTION_DAYS, source: "soc2" });
|
|
expect(resolveAuditRetentionPolicy({ tenantId: "t1", regulatoryScopes: ["hipaa"] })).toEqual({ retentionDays: SOC2_AUDIT_RETENTION_DAYS, source: "hipaa" });
|
|
expect(resolveAuditRetentionPolicy({ tenantId: "t1", regulatoryScopes: ["soc2", "hipaa"] })).toEqual({ retentionDays: SOC2_AUDIT_RETENTION_DAYS, source: "hipaa" });
|
|
});
|
|
|
|
it("returns 6y for GDPR-only tenants", () => {
|
|
expect(resolveAuditRetentionPolicy({ tenantId: "t1", regulatoryScopes: ["gdpr"] })).toEqual({ retentionDays: GDPR_AUDIT_RETENTION_DAYS, source: "gdpr" });
|
|
});
|
|
|
|
it("returns 1y default for tenants with no regulatory scope", () => {
|
|
expect(resolveAuditRetentionPolicy({ tenantId: "t1", regulatoryScopes: [] })).toEqual({ retentionDays: DEFAULT_AUDIT_RETENTION_DAYS, source: "default" });
|
|
});
|
|
});
|
|
|
|
describe("runAuditRetentionJob", () => {
|
|
it("walks tenants, deletes rows, and emits a meta-audit for each non-empty sweep", async () => {
|
|
const now = new Date("2026-05-17T00:00:00.000Z");
|
|
const tenants = [
|
|
{ tenantId: "t1", regulatoryScopes: ["soc2"] },
|
|
{ tenantId: "t2", regulatoryScopes: [] },
|
|
];
|
|
const eventDeleter = vi.fn(async (tenantId: string, threshold: Date) => {
|
|
void threshold;
|
|
return tenantId === "t1" ? 42 : 0;
|
|
});
|
|
const auditWriter = vi.fn(async () => {});
|
|
const result = await runAuditRetentionJob({
|
|
now,
|
|
tenantLister: async () => tenants,
|
|
eventDeleter,
|
|
auditWriter,
|
|
});
|
|
expect(result).toEqual({ inspectedTenants: 2, deletedRows: 42, failures: [] });
|
|
expect(eventDeleter).toHaveBeenCalledTimes(2);
|
|
// SOC2 -> 7y threshold
|
|
const soc2Threshold = eventDeleter.mock.calls[0]?.[1];
|
|
expect(soc2Threshold).toBeInstanceOf(Date);
|
|
expect(soc2Threshold.toISOString()).toBe(new Date(now.getTime() - SOC2_AUDIT_RETENTION_DAYS * 86_400_000).toISOString());
|
|
// Only t1 had deletions -> only one meta-audit
|
|
expect(auditWriter).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("records failures without aborting other tenants", async () => {
|
|
const now = new Date("2026-05-17T00:00:00.000Z");
|
|
const tenants = [
|
|
{ tenantId: "t-fail", regulatoryScopes: [] },
|
|
{ tenantId: "t-ok", regulatoryScopes: [] },
|
|
];
|
|
const eventDeleter = vi.fn(async (tenantId: string) => {
|
|
if (tenantId === "t-fail") throw new Error("boom");
|
|
return 5;
|
|
});
|
|
const result = await runAuditRetentionJob({ now, tenantLister: async () => tenants, eventDeleter });
|
|
expect(result.deletedRows).toBe(5);
|
|
expect(result.failures).toEqual([{ tenantId: "t-fail", error: "boom" }]);
|
|
});
|
|
});
|