The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import { filePreviewDelimiter, parseDelimitedPreview } from "./delimitedPreview.ts"; |
| 3 | |
| 4 | describe("delimited file previews", () => { |
| 5 | it("falls back to the file name under generic MIME types", () => { |
| 6 | expect(filePreviewDelimiter({ name: "a.csv", mimeType: "binary/octet-stream" })).toBe(","); |
| 7 | expect(filePreviewDelimiter({ name: "a.tsv", mimeType: "application/unknown" })).toBe("\t"); |
| 8 | }); |
| 9 | it("preserves quoted separators, escaped quotes, blank cells and line breaks", () => { |
| 10 | expect( |
| 11 | parseDelimitedPreview('\ufeffname,notes,empty\r\n"A, B","say ""hi""\nagain",\r\n', ","), |
| 12 | ).toEqual({ |
| 13 | rows: [ |
| 14 | ["name", "notes", "empty"], |
| 15 | ["A, B", 'say "hi"\nagain', ""], |
| 16 | ], |
| 17 | truncated: false, |
| 18 | }); |
| 19 | expect(parseDelimitedPreview("one\ttwo\n\tthree", "\t").rows).toEqual([ |
| 20 | ["one", "two"], |
| 21 | ["", "three"], |
| 22 | ]); |
| 23 | }); |
| 24 | it("detects semicolon-separated CSV from its first records outside quotes", () => { |
| 25 | expect(parseDelimitedPreview("a;b\r\n1;2,5", ",").rows).toEqual([ |
| 26 | ["a", "b"], |
| 27 | ["1", "2,5"], |
| 28 | ]); |
| 29 | expect(parseDelimitedPreview('name,notes\nAlice,"a;b;c"', ",").rows).toEqual([ |
| 30 | ["name", "notes"], |
| 31 | ["Alice", "a;b;c"], |
| 32 | ]); |
| 33 | expect(parseDelimitedPreview('"a, b";notes\nAlice;"say ""hi""\nagain"', ",").rows).toEqual([ |
| 34 | ["a, b", "notes"], |
| 35 | ["Alice", 'say "hi"\nagain'], |
| 36 | ]); |
| 37 | expect(parseDelimitedPreview("a,b;c", ",").rows).toEqual([["a", "b;c"]]); |
| 38 | expect(parseDelimitedPreview("id,notes;more;extra\n1,plain\n2,ordinary", ",").rows).toEqual([ |
| 39 | ["id", "notes;more;extra"], |
| 40 | ["1", "plain"], |
| 41 | ["2", "ordinary"], |
| 42 | ]); |
| 43 | expect( |
| 44 | parseDelimitedPreview("id,notes;more;extra\n1,plain;other;last\n2,ordinary;next;end", ",") |
| 45 | .rows, |
| 46 | ).toEqual([ |
| 47 | ["id", "notes;more;extra"], |
| 48 | ["1", "plain;other;last"], |
| 49 | ["2", "ordinary;next;end"], |
| 50 | ]); |
| 51 | expect(parseDelimitedPreview('12",name\n24",a;b;c', ",").rows).toEqual([ |
| 52 | ['12"', "name"], |
| 53 | ['24"', "a;b;c"], |
| 54 | ]); |
| 55 | expect(parseDelimitedPreview("a;b\tvalue", "\t").rows).toEqual([["a;b", "value"]]); |
| 56 | }); |
| 57 | describe.each([",", "\t"] as const)("with delimiter %j", (delimiter) => { |
| 58 | it("preserves a final quoted empty record without requiring a line ending", () => { |
| 59 | for (const ending of ["", "\n", "\r\n"]) { |
| 60 | expect(parseDelimitedPreview(`name\r\nAlice\r\n""${ending}`, delimiter)).toEqual({ |
| 61 | rows: [["name"], ["Alice"], [""]], |
| 62 | truncated: false, |
| 63 | }); |
| 64 | } |
| 65 | expect(parseDelimitedPreview('""', delimiter)).toEqual({ |
| 66 | rows: [[""]], |
| 67 | truncated: false, |
| 68 | }); |
| 69 | }); |
| 70 | it("does not invent records for empty input or a trailing line ending", () => { |
| 71 | for (const prefix of ["", "\ufeff"]) { |
| 72 | expect(parseDelimitedPreview(prefix, delimiter).rows).toEqual([]); |
| 73 | expect(parseDelimitedPreview(`${prefix}name\r\n`, delimiter).rows).toEqual([["name"]]); |
| 74 | expect(parseDelimitedPreview(`${prefix}""`, delimiter).rows).toEqual([[""]]); |
| 75 | } |
| 76 | }); |
| 77 | it("keeps a quoted empty record at the row limit", () => { |
| 78 | const text = `${"name\n".repeat(99)}""`; |
| 79 | const preview = parseDelimitedPreview(text, delimiter); |
| 80 | expect(preview.rows).toHaveLength(100); |
| 81 | expect(preview.rows.at(-1)).toEqual([""]); |
| 82 | expect(preview.truncated).toBe(false); |
| 83 | expect(parseDelimitedPreview(`${text}\nextra`, delimiter).truncated).toBe(true); |
| 84 | }); |
| 85 | }); |
| 86 | it("bounds rows, columns and cell length and reports partial content", () => { |
| 87 | const table = parseDelimitedPreview( |
| 88 | Array.from({ length: 101 }, () => |
| 89 | Array.from({ length: 31 }, () => "x".repeat(2001)).join(","), |
| 90 | ).join("\n"), |
| 91 | ",", |
| 92 | ); |
| 93 | expect(table.truncated).toBe(true); |
| 94 | expect(table.rows).toHaveLength(100); |
| 95 | expect(table.rows[0]).toHaveLength(30); |
| 96 | expect(table.rows[0]?.[0]).toHaveLength(2000); |
| 97 | expect(parseDelimitedPreview('a,"unfinished', ",").truncated).toBe(true); |
| 98 | }); |
| 99 | describe.each([",", "\t"] as const)("Unicode cells with delimiter %j", (delimiter) => { |
| 100 | it.each([false, true])("does not split surrogate pairs in quoted=%s cells", (quoted) => { |
| 101 | const prefix = "x".repeat(1999); |
| 102 | const value = `${prefix}😀tail`; |
| 103 | const input = `${quoted ? `"${value}"` : value}${delimiter}next\nlast`; |
| 104 | expect(parseDelimitedPreview(input, delimiter)).toEqual({ |
| 105 | rows: [[prefix, "next"], ["last"]], |
| 106 | truncated: true, |
| 107 | }); |
| 108 | }); |
| 109 | it("keeps pairs that fit and truncates later characters", () => { |
| 110 | const value = `${"x".repeat(1998)}😀`; |
| 111 | expect(parseDelimitedPreview(value, delimiter)).toEqual({ |
| 112 | rows: [[value]], |
| 113 | truncated: false, |
| 114 | }); |
| 115 | expect(parseDelimitedPreview(`${value}tail`, delimiter)).toEqual({ |
| 116 | rows: [[value]], |
| 117 | truncated: true, |
| 118 | }); |
| 119 | }); |
| 120 | it("keeps escaped quotes and BMP text at the same limit", () => { |
| 121 | const prefix = "x".repeat(1999); |
| 122 | expect(parseDelimitedPreview(`"${prefix}""tail"`, delimiter)).toEqual({ |
| 123 | rows: [[`${prefix}"`]], |
| 124 | truncated: true, |
| 125 | }); |
| 126 | expect(parseDelimitedPreview(`${prefix}éextra`, delimiter)).toEqual({ |
| 127 | rows: [[`${prefix}é`]], |
| 128 | truncated: true, |
| 129 | }); |
| 130 | }); |
| 131 | }); |
| 132 | it("reads worker pages beyond the old row, column and cell limits", () => { |
| 133 | const text = Array.from({ length: 128 }, (_, row) => |
| 134 | Array.from({ length: 40 }, (_, column) => `${row}:${column}`).join(","), |
| 135 | ).join("\n"); |
| 136 | const result = parseDelimitedPreview(text, ",", { |
| 137 | maxRows: 128, |
| 138 | maxColumns: 40, |
| 139 | maxCellLength: 4096, |
| 140 | }); |
| 141 | expect(result.rows).toHaveLength(128); |
| 142 | expect(result.rows.at(-1)?.at(-1)).toBe("127:39"); |
| 143 | expect(result.truncated).toBe(false); |
| 144 | expect( |
| 145 | parseDelimitedPreview(`"${"x".repeat(3000)}"`, ",", { |
| 146 | maxRows: 1, |
| 147 | maxColumns: 1, |
| 148 | maxCellLength: 4096, |
| 149 | }).rows[0]?.[0], |
| 150 | ).toHaveLength(3000); |
| 151 | }); |
| 152 | }); |