141 lines
4.8 KiB
TypeScript
141 lines
4.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { createValidationReport, detectDuplicateRows } from "./dedupe.js";
|
|
import type { ImportRowResult } from "./index.js";
|
|
|
|
describe("duplicate detection edge cases", () => {
|
|
it("builds composite keys from array values, stripping blank and null items", () => {
|
|
const report = detectDuplicateRows(
|
|
[
|
|
{ rowNumber: 1, normalized: { ids: [1, "a", null, " "] } },
|
|
{ rowNumber: 2, normalized: { ids: ["1", "a"] } },
|
|
{ rowNumber: 3, normalized: { ids: ["a", "1"] } },
|
|
],
|
|
["ids"],
|
|
);
|
|
|
|
// Rows 1 and 2 normalize to the same ["1","a"] signature (numeric 1 -> "1", blanks dropped);
|
|
// row 3 differs because element order changes the JSON signature.
|
|
expect(report.groups).toEqual([
|
|
{
|
|
id: "duplicate-group-0001",
|
|
key: [{ field: "ids", valuePreview: '["1","a"]' }],
|
|
rowNumbers: [1, 2],
|
|
},
|
|
]);
|
|
expect(report.summary).toMatchObject({ totalRows: 3, duplicateGroups: 1, duplicateRows: 2 });
|
|
});
|
|
|
|
it("skips rows whose only key field is an all-blank array", () => {
|
|
const report = detectDuplicateRows(
|
|
[
|
|
{ rowNumber: 1, normalized: { ids: [null, " "] } },
|
|
{ rowNumber: 2, normalized: { ids: [] } },
|
|
],
|
|
["ids"],
|
|
);
|
|
|
|
expect(report.groups).toEqual([]);
|
|
expect(report.summary).toMatchObject({ totalRows: 2, duplicateGroups: 0, duplicateRows: 0 });
|
|
expect(report.rowResults.every((row) => row.ok)).toBe(true);
|
|
});
|
|
|
|
it("stringifies numeric and boolean scalar key values", () => {
|
|
const numericReport = detectDuplicateRows(
|
|
[
|
|
{ rowNumber: 1, normalized: { score: 42 } },
|
|
{ rowNumber: 2, normalized: { score: 42 } },
|
|
{ rowNumber: 3, normalized: { score: 7 } },
|
|
],
|
|
["score"],
|
|
);
|
|
expect(numericReport.groups).toEqual([
|
|
{ id: "duplicate-group-0001", key: [{ field: "score", valuePreview: "42" }], rowNumbers: [1, 2] },
|
|
]);
|
|
|
|
const booleanReport = detectDuplicateRows(
|
|
[
|
|
{ rowNumber: 1, normalized: { active: true } },
|
|
{ rowNumber: 2, normalized: { active: true } },
|
|
],
|
|
["active"],
|
|
);
|
|
expect(booleanReport.groups).toEqual([
|
|
{ id: "duplicate-group-0001", key: [{ field: "active", valuePreview: "true" }], rowNumbers: [1, 2] },
|
|
]);
|
|
});
|
|
|
|
it("accepts DuplicateKeyField object descriptors as keys", () => {
|
|
const report = detectDuplicateRows(
|
|
[
|
|
{ rowNumber: 1, normalized: { email: "a@example.com" } },
|
|
{ rowNumber: 2, normalized: { email: "a@example.com" } },
|
|
],
|
|
[{ field: "email", label: "Email" }],
|
|
);
|
|
|
|
expect(report.groups).toEqual([
|
|
{ id: "duplicate-group-0001", key: [{ field: "email", valuePreview: "a@example.com" }], rowNumbers: [1, 2] },
|
|
]);
|
|
});
|
|
|
|
it("falls back to positional row numbers when rows omit rowNumber", () => {
|
|
const report = detectDuplicateRows(
|
|
[
|
|
{ normalized: { email: "a@example.com" } },
|
|
{ normalized: { email: "a@example.com" } },
|
|
{ normalized: { email: "b@example.com" } },
|
|
],
|
|
["email"],
|
|
);
|
|
|
|
expect(report.rowResults.map((row) => row.rowNumber)).toEqual([1, 2, 3]);
|
|
expect(report.groups).toEqual([
|
|
{ id: "duplicate-group-0001", key: [{ field: "email", valuePreview: "a@example.com" }], rowNumbers: [1, 2] },
|
|
]);
|
|
});
|
|
|
|
it("omits the error report when configured keys yield no duplicates", () => {
|
|
const report = detectDuplicateRows(
|
|
[
|
|
{ rowNumber: 1, normalized: { email: "a@example.com" } },
|
|
{ rowNumber: 2, normalized: { email: "b@example.com" } },
|
|
],
|
|
["email"],
|
|
);
|
|
|
|
expect(report.groups).toEqual([]);
|
|
expect(report.summary).toMatchObject({ duplicateGroups: 0, duplicateRows: 0, diagnostics: [] });
|
|
expect(report.errorReport).toBeUndefined();
|
|
expect(report.rowResults.every((row) => row.ok)).toBe(true);
|
|
});
|
|
|
|
it("omits the validation error report when there are no diagnostics", () => {
|
|
const rowResults: ImportRowResult[] = [
|
|
{ ok: true, rowNumber: 1, normalized: { email: "a@example.com" } },
|
|
{ ok: true, rowNumber: 2, normalized: { email: "b@example.com" }, warnings: undefined },
|
|
];
|
|
const duplicateReport = detectDuplicateRows(
|
|
[
|
|
{ rowNumber: 1, normalized: { email: "a@example.com" } },
|
|
{ rowNumber: 2, normalized: { email: "b@example.com" } },
|
|
],
|
|
["email"],
|
|
);
|
|
|
|
const report = createValidationReport("json", rowResults, duplicateReport);
|
|
|
|
expect(report.summary).toMatchObject({
|
|
format: "json",
|
|
totalRows: 2,
|
|
acceptedRows: 2,
|
|
rejectedRows: 0,
|
|
duplicateGroups: 0,
|
|
duplicateRows: 0,
|
|
});
|
|
expect(report.diagnostics).toEqual([]);
|
|
expect(report.duplicateGroups).toEqual([]);
|
|
expect(report.errorReport).toBeUndefined();
|
|
});
|
|
});
|