import { describe, expect, it } from "vitest"; import { createValidationReport, detectDuplicateRows } from "./dedupe.js"; import type { ImportRowResult } from "./index.js"; describe("duplicate detection contracts", () => { it("reports deterministic duplicate groups and row diagnostics", () => { const rows = [ { rowNumber: 10, normalized: { email: " ada@example.com ", name: "Ada" } }, { rowNumber: 12, normalized: { email: "ben@example.com", name: "Ben" } }, { rowNumber: 14, normalized: { email: "ada@example.com", name: "Ada Lovelace" } }, { rowNumber: 16, normalized: { email: "ben@example.com", name: "Benjamin" } }, { rowNumber: 18, normalized: { email: "case@example.com", name: "Case" } }, { rowNumber: 20, normalized: { email: "CASE@example.com", name: "Case" } }, ]; const first = detectDuplicateRows(rows, ["email"]); const second = detectDuplicateRows(rows, ["email"]); expect(first.groups).toEqual([ { id: "duplicate-group-0001", key: [{ field: "email", valuePreview: "ada@example.com" }], rowNumbers: [10, 14], }, { id: "duplicate-group-0002", key: [{ field: "email", valuePreview: "ben@example.com" }], rowNumbers: [12, 16], }, ]); expect(second).toEqual(first); expect(first.summary).toMatchObject({ totalRows: 6, duplicateGroups: 2, duplicateRows: 4 }); expect(first.rowResults.filter((row) => !row.ok).map((row) => row.rowNumber)).toEqual([10, 12, 14, 16]); expect(first.summary.diagnostics).toHaveLength(4); expect(first.summary.diagnostics[0]).toMatchObject({ severity: "error", code: "duplicate_key", rowNumber: 10, valuePreview: "duplicate-group-0001", }); }); it("returns no duplicate errors for empty key configuration", () => { const report = detectDuplicateRows( [ { rowNumber: 1, normalized: { email: "a@example.com" } }, { rowNumber: 2, normalized: { email: "a@example.com" } }, ], [], ); expect(report.groups).toEqual([]); expect(report.summary).toEqual({ totalRows: 2, duplicateGroups: 0, duplicateRows: 0, diagnostics: [] }); expect(report.rowResults).toEqual([ { ok: true, rowNumber: 1, normalized: { email: "a@example.com" }, warnings: undefined }, { ok: true, rowNumber: 2, normalized: { email: "a@example.com" }, warnings: undefined }, ]); expect(report.errorReport).toBeUndefined(); }); it("ignores blank keys unless another configured key field is present", () => { const report = detectDuplicateRows( [ { rowNumber: 1, normalized: { email: " ", accountId: "tenant-a" } }, { rowNumber: 2, normalized: { email: null, accountId: "tenant-a" } }, { rowNumber: 3, normalized: { email: "", accountId: "" } }, { rowNumber: 4, normalized: { email: "", accountId: null } }, { rowNumber: 5, normalized: { email: "", accountId: "tenant-b" } }, ], ["email", "accountId"], ); expect(report.groups).toEqual([ { id: "duplicate-group-0001", key: [{ field: "accountId", valuePreview: "tenant-a" }], rowNumbers: [1, 2], }, ]); expect(report.summary).toMatchObject({ duplicateGroups: 1, duplicateRows: 2 }); expect(report.rowResults.filter((row) => !row.ok).map((row) => row.rowNumber)).toEqual([1, 2]); }); it("redacts secret-like values from duplicate diagnostics and reports", () => { const report = detectDuplicateRows( [ { rowNumber: 1, normalized: { apiToken: "token=super-secret", email: "a@example.com" } }, { rowNumber: 2, normalized: { apiToken: " token=super-secret ", email: "b@example.com" } }, ], ["apiToken"], ); const serialized = JSON.stringify(report); expect(report.groups[0]).toMatchObject({ key: [{ field: "apiToken", valuePreview: "token= [REDACTED]" }], rowNumbers: [1, 2], }); expect(report.summary.diagnostics[0]?.message).toContain("apiToken= [REDACTED]"); expect(serialized).not.toContain("super-secret"); expect(serialized).not.toContain("rawFile"); expect(serialized).not.toContain("requestBody"); expect(serialized).not.toContain("buffer"); }); it("builds redacted validation report summaries without raw rejected rows", () => { const rowResults: ImportRowResult[] = [ { ok: true, rowNumber: 1, normalized: { email: "a@example.com" } }, { ok: false, rowNumber: 2, errors: [{ severity: "error", code: "required", message: "password= [REDACTED]", rowNumber: 2 }] }, ]; const duplicateReport = detectDuplicateRows( [ { rowNumber: 1, normalized: { email: "a@example.com" } }, { rowNumber: 3, normalized: { email: "a@example.com" } }, ], ["email"], ); const report = createValidationReport("csv", rowResults, duplicateReport); const serialized = JSON.stringify(report); expect(report.summary).toMatchObject({ format: "csv", totalRows: 2, acceptedRows: 1, rejectedRows: 1, duplicateGroups: 1, duplicateRows: 2, }); expect(report.diagnostics).toHaveLength(3); expect(report.errorReport).toMatchObject({ rejectedRows: 1 }); expect(serialized).not.toContain("rejectedValues"); expect(serialized).not.toContain("rawRows"); expect(serialized).not.toContain("requestBody"); expect(serialized).not.toContain("super-secret"); }); });