Public: anyone can read this; the crew can change it.
A .tsv file opens as a table the way .csv does #14109
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14080-a-tsv-file-opens-as-a-table-the-way-csv → main 2 commits fork 2 threads · 2 unresolved
1 file
changed +48
−0
test/render-tsv.test.ts added +48
| @@ -0,0 +1,48 @@ | ||
| 1 | /** | |
| 2 | * A .tsv file opens as a table: tab delimiter, ragged rows padded, hostile cells escaped, same row | |
| 3 | * window. | |
| 4 | */ | |
| 5 | import { expect, test } from "bun:test"; | |
| 6 | import { csv, ROW_WINDOW } from "../src/web/render/csv.ts"; | |
| 7 | import { fileKind } from "../src/web/render/kinds.ts"; | |
| 8 | ||
| 9 | const draw = (text: string, path = "d.tsv") => { | |
| 10 | const body = new TextEncoder().encode(text); | |
| 11 | const out = csv({ path, size: body.length, body, media: "/m", download: "/d", pathHref: `/o/r/blob/main/${path}`, all: false }); | |
| 12 | return { html: String(out.content), summary: out.summary ?? "" }; | |
| 13 | }; | |
| 14 | const rows = (page: string): string[] => page.split("<tr>").slice(1); | |
| 15 | const cellCount = (tr: string): number => (tr.match(/<t[dh][ >]/g) ?? []).length; | |
| 16 | ||
| 17 | test("a .tsv path is the table kind", () => { | |
| 18 | expect(fileKind("d.tsv", new TextEncoder().encode("a\tb\n"))).toBe("csv"); | |
| 19 | }); | |
| 20 | ||
| 21 | test("a ragged row pads to the header width and a quoted tab stays one cell", () => { | |
| 22 | const { html, summary } = draw('name\tnote\textra\nann\tshort\nbob\t"a\tb"\tz\n'); | |
outdated
|
||
| 23 | expect(summary).toContain("tab-separated"); | |
| 24 | expect(summary).toContain("× 3 columns"); | |
| 25 | const trs = rows(html); | |
| 26 | expect(trs.length).toBe(3); | |
| 27 | for (const tr of trs) expect(cellCount(tr)).toBe(3); | |
| 28 | expect(trs[1]).toContain("<td>short</td><td></td>"); | |
| 29 | expect(trs[2]).toContain("<td>a\tb</td>"); | |
| 30 | }); | |
| 31 | ||
| 32 | test("a hostile cell is escaped", () => { | |
| 33 | const { html } = draw("name\tnote\nann\t<script>alert(1)</script>\nbob\t<img src=x onerror=1>\n"); | |
| 34 | expect(html).not.toContain("<script>alert"); | |
| 35 | expect(html).not.toContain("<img src=x"); | |
| 36 | expect(html).toContain("<script>"); | |
| 37 | expect(html).toContain("<img"); | |
| 38 | }); | |
| 39 | ||
| 40 | test("a .tsv has the same row window as the same data in a .csv", () => { | |
| 41 | const n = ROW_WINDOW + 5; | |
| 42 | const lines = (d: string) => `a${d}b\n` + Array.from({ length: n }, (_, i) => `${i}${d}x`).join("\n") + "\n"; | |
| 43 | const t = draw(lines("\t")); | |
| 44 | const c = draw(lines(","), "d.csv"); | |
| 45 | expect(rows(t.html).length - 1).toBe(ROW_WINDOW); | |
| 46 | expect(rows(c.html).length).toBe(rows(t.html).length); | |
| 47 | expect(t.html).toContain(`Showing ${ROW_WINDOW.toLocaleString()} of ${n.toLocaleString()} rows`); | |
| 48 | }); |
new above
offline
Ragged padding is the point of this test but is never asserted. Count the
<td>/<th>per row (3 each) and check the quoteda\tbcell is one cell.