95 lines
3.8 KiB
TypeScript
95 lines
3.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
createImportDryRunPreview,
|
|
detectImportFormatFromContentType,
|
|
redactDiagnosticMessage,
|
|
} from "./index.js";
|
|
|
|
describe("import preview edge cases", () => {
|
|
it("treats empty and all-blank arrays as missing required values", () => {
|
|
const output = createImportDryRunPreview({
|
|
format: "csv",
|
|
fields: [{ targetField: "tags", required: true, label: "Tags" }],
|
|
mappings: [{ sourceColumn: "Tags", targetField: "tags" }],
|
|
rows: [
|
|
{ rowNumber: 1, values: { Tags: [] } },
|
|
{ rowNumber: 2, values: { Tags: [" ", null] } },
|
|
{ rowNumber: 3, values: { Tags: ["member"] } },
|
|
],
|
|
});
|
|
|
|
expect(output.summary).toMatchObject({ totalRows: 3, acceptedRows: 1, rejectedRows: 2 });
|
|
expect(output.rows[0]).toMatchObject({
|
|
ok: false,
|
|
rowNumber: 1,
|
|
errors: [{ code: "missing_required_field", column: "tags", message: "Tags is required for import preview." }],
|
|
});
|
|
expect(output.rows[1]).toMatchObject({ ok: false, rowNumber: 2 });
|
|
expect(output.rows[2]).toMatchObject({ ok: true, rowNumber: 3, normalized: { tags: ["member"] } });
|
|
expect(output.errorReport).toMatchObject({ rejectedRows: 2 });
|
|
});
|
|
|
|
it("treats numeric zero and boolean false as present required values", () => {
|
|
const output = createImportDryRunPreview({
|
|
format: "json",
|
|
fields: [
|
|
{ targetField: "score", required: true, label: "Score" },
|
|
{ targetField: "active", required: true, label: "Active" },
|
|
],
|
|
mappings: [
|
|
{ sourceColumn: "Score", targetField: "score" },
|
|
{ sourceColumn: "Active", targetField: "active" },
|
|
],
|
|
rows: [{ rowNumber: 1, values: { Score: 0, Active: false } }],
|
|
});
|
|
|
|
expect(output.summary).toMatchObject({ totalRows: 1, acceptedRows: 1, rejectedRows: 0 });
|
|
expect(output.rows[0]).toMatchObject({ ok: true, rowNumber: 1, normalized: { score: 0, active: false } });
|
|
expect(output.errorReport).toBeUndefined();
|
|
});
|
|
|
|
it("normalizes present-but-null source values to null without rejecting optional fields", () => {
|
|
const output = createImportDryRunPreview({
|
|
format: "csv",
|
|
fields: [{ targetField: "email", required: true, label: "Email" }],
|
|
mappings: [
|
|
{ sourceColumn: "Email", targetField: "email", required: true, label: "Email" },
|
|
{ sourceColumn: "Note", targetField: "note" },
|
|
],
|
|
rows: [{ rowNumber: 1, values: { Email: "a@example.com", Note: null } }],
|
|
});
|
|
|
|
expect(output.rows[0]).toMatchObject({ ok: true, rowNumber: 1, normalized: { email: "a@example.com", note: null } });
|
|
expect(output.mappedRows[0]).toMatchObject({ rowNumber: 1, sourceColumns: ["Email", "Note"] });
|
|
});
|
|
|
|
it("falls back to the target field name when a required field has no label", () => {
|
|
const output = createImportDryRunPreview({
|
|
format: "csv",
|
|
fields: [{ targetField: "memberId", required: true }],
|
|
mappings: [{ sourceColumn: "ID", targetField: "memberId" }],
|
|
rows: [{ rowNumber: 4, values: { ID: "" } }],
|
|
});
|
|
|
|
expect(output.rows[0]).toMatchObject({
|
|
ok: false,
|
|
rowNumber: 4,
|
|
errors: [{ code: "missing_required_field", column: "memberId", message: "memberId is required for import preview." }],
|
|
});
|
|
});
|
|
|
|
it("returns undefined for content types that reduce to an empty subtype", () => {
|
|
expect(detectImportFormatFromContentType(";")).toBeUndefined();
|
|
expect(detectImportFormatFromContentType("; charset=utf-8")).toBeUndefined();
|
|
});
|
|
|
|
it("redacts secret matches that contain both colon and equals separators", () => {
|
|
const redacted = redactDiagnosticMessage("authorization=Bearer:token-value");
|
|
|
|
expect(redacted).toContain("authorization= [REDACTED]");
|
|
expect(redacted).not.toContain("Bearer");
|
|
expect(redacted).not.toContain("token-value");
|
|
});
|
|
});
|