Files
STTracker/packages/plugin-sdk/test/csv.test.ts
T
2026-10-30 15:48:59 +01:00

25 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, test } from "bun:test";
import { parseCsv, toCsv } from "../src/csv.ts";
describe("csv codec", () => {
test("round-trips awkward cells", () => {
const x = [["a", "b,c", 'q"uote', "line\nbreak", "cr\r\nlf", "", "🐉"], ["", "", "", "", "", "", ""], ["only"]];
expect(parseCsv(toCsv(x))).toEqual(x);
});
test("toCsv uses CRLF and quotes as needed", () => {
expect(toCsv([["a", "b c"], ["x,y", 'z"']])).toBe('a,b c\r\n"x,y","z"""\r\n');
});
test("strips BOM and detects semicolon / tab", () => {
expect(parseCsv("a;b\n1;2\n")).toEqual([["a", "b"], ["1", "2"]]);
expect(parseCsv("a\tb\n1\t2")).toEqual([["a", "b"], ["1", "2"]]);
});
test("explicit delimiter wins; delimiters inside quoted header are ignored", () => {
expect(parseCsv('"a;b;c",d\n1,2', {})).toEqual([["a;b;c", "d"], ["1", "2"]]);
expect(parseCsv("a,b;c\n1,2;3", { delimiter: ";" })).toEqual([["a,b", "c"], ["1,2", "3"]]);
});
test("handles CRLF, blank trailing lines, and empty input", () => {
expect(parseCsv("a,b\r\n1,2\r\n\r\n")).toEqual([["a", "b"], ["1", "2"]]);
expect(parseCsv("")).toEqual([]);
});
});