113 lines
3.5 KiB
TypeScript
113 lines
3.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { LEAD_CSV_COLUMNS, escapeCsvValue, leadsCsvFilename, serializeLeadsCsv } from "./leadsCsv";
|
|
|
|
describe("escapeCsvValue", () => {
|
|
it("returns an empty string for null and undefined", () => {
|
|
expect(escapeCsvValue(null)).toBe("");
|
|
expect(escapeCsvValue(undefined)).toBe("");
|
|
});
|
|
|
|
it("renders Date as an ISO 8601 string", () => {
|
|
const value = new Date("2026-05-20T13:45:00.000Z");
|
|
expect(escapeCsvValue(value)).toBe("2026-05-20T13:45:00.000Z");
|
|
});
|
|
|
|
it("does not quote values without separators or quotes", () => {
|
|
expect(escapeCsvValue("plain")).toBe("plain");
|
|
expect(escapeCsvValue(42)).toBe("42");
|
|
});
|
|
|
|
it("quotes and doubles inner quotes for values containing commas, quotes, or newlines", () => {
|
|
expect(escapeCsvValue('a,b')).toBe('"a,b"');
|
|
expect(escapeCsvValue('he said "hi"')).toBe('"he said ""hi"""');
|
|
expect(escapeCsvValue("line1\nline2")).toBe('"line1\nline2"');
|
|
expect(escapeCsvValue("line1\r\nline2")).toBe('"line1\r\nline2"');
|
|
});
|
|
});
|
|
|
|
describe("serializeLeadsCsv", () => {
|
|
it("emits exactly the documented header row, even for an empty input", () => {
|
|
expect(serializeLeadsCsv([])).toBe(`${LEAD_CSV_COLUMNS.join(",")}\n`);
|
|
});
|
|
|
|
it("aligns each row's columns with the header order", () => {
|
|
const csv = serializeLeadsCsv([
|
|
{
|
|
id: "lead_1",
|
|
email: "alice@example.com",
|
|
firstName: "Alice",
|
|
lastName: null,
|
|
phone: null,
|
|
company: "Acme",
|
|
source: "landing-pro",
|
|
status: "qualified",
|
|
utmSource: "google",
|
|
utmMedium: "cpc",
|
|
utmCampaign: "spring",
|
|
utmTerm: null,
|
|
utmContent: null,
|
|
gclid: null,
|
|
fbclid: null,
|
|
referrer: null,
|
|
landingPage: "/pricing",
|
|
score: 5,
|
|
createdAt: new Date("2026-05-20T09:00:00.000Z"),
|
|
lastTouchAt: new Date("2026-05-20T09:15:00.000Z"),
|
|
},
|
|
]);
|
|
const lines = csv.split("\n");
|
|
expect(lines[0]).toBe(LEAD_CSV_COLUMNS.join(","));
|
|
expect(lines[1]).toBe(
|
|
[
|
|
"lead_1",
|
|
"alice@example.com",
|
|
"Alice",
|
|
"",
|
|
"",
|
|
"Acme",
|
|
"landing-pro",
|
|
"qualified",
|
|
"google",
|
|
"cpc",
|
|
"spring",
|
|
"",
|
|
"",
|
|
"",
|
|
"",
|
|
"",
|
|
"/pricing",
|
|
"5",
|
|
"2026-05-20T09:00:00.000Z",
|
|
"2026-05-20T09:15:00.000Z",
|
|
].join(","),
|
|
);
|
|
expect(lines.at(-1)).toBe("");
|
|
});
|
|
|
|
it("escapes commas, quotes, and newlines inside string fields", () => {
|
|
const csv = serializeLeadsCsv([
|
|
{ id: "lead_2", email: 'quoted"name@example.com', firstName: "Comma, Name", lastName: "Multi\nLine" },
|
|
]);
|
|
expect(csv).toContain('"quoted""name@example.com"');
|
|
expect(csv).toContain('"Comma, Name"');
|
|
expect(csv).toContain('"Multi\nLine"');
|
|
});
|
|
|
|
it("treats absent keys identically to nulls", () => {
|
|
const csv = serializeLeadsCsv([{ id: "lead_3", email: "x@y.z" }]);
|
|
const rowLine = csv.split("\n")[1] ?? "";
|
|
const cells = rowLine.split(",");
|
|
expect(cells.length).toBe(LEAD_CSV_COLUMNS.length);
|
|
expect(cells[0]).toBe("lead_3");
|
|
expect(cells[1]).toBe("x@y.z");
|
|
for (let i = 2; i < cells.length; i++) expect(cells[i]).toBe("");
|
|
});
|
|
});
|
|
|
|
describe("leadsCsvFilename", () => {
|
|
it("emits UTC-stamped YYYYMMDD suffix", () => {
|
|
expect(leadsCsvFilename(new Date("2026-05-20T23:00:00.000Z"))).toBe("marketing-leads-20260520.csv");
|
|
expect(leadsCsvFilename(new Date("2026-01-05T00:00:00.000Z"))).toBe("marketing-leads-20260105.csv");
|
|
});
|
|
});
|