Spaces:
Sleeping
Sleeping
File size: 1,288 Bytes
05c5ed5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import { describe, it, expect } from "vitest";
import { formatCsvPreviewText, parseCsvPreview } from "./csv";
describe("parseCsvPreview", () => {
it("parses simple CSV and limits rows/cols", () => {
const csv = "a,b,c\n1,2,3\n4,5,6\n7,8,9\n";
const res = parseCsvPreview(Buffer.from(csv), { maxRows: 2, maxCols: 2 });
expect(res.header).toEqual(["a", "b"]);
expect(res.rows).toEqual([
["1", "2"],
["4", "5"],
]);
expect(res.columns).toBe(2);
expect(res.totalRows).toBe(4); // includes header
expect(res.markdownTable).toContain("| a | b |");
});
it("handles quoted fields and escaped quotes", () => {
const csv = 'name,desc\n"ACME, Inc.","He said ""hello"""\n';
const res = parseCsvPreview(Buffer.from(csv));
expect(res.header).toEqual(["name", "desc"]);
expect(res.rows[0]).toEqual(["ACME, Inc.", 'He said "hello"']);
});
it("formats preview text with summary metadata", () => {
const csv = "col1,col2\n1,2\n3,4\n";
const preview = parseCsvPreview(Buffer.from(csv));
const text = formatCsvPreviewText("sample.csv", preview);
expect(text).toContain("sample.csv");
expect(text).toContain("rows: 3");
expect(text).toContain("cols: 2");
expect(text).toContain("| col1 | col2 |");
});
});
|