{"draft":null,"from":4,"to":318,"files":[{"path":"server/test/git-blob-text.test.ts","added":58,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/git-blob-text.test.ts\n@@ -0,0 +1,58 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { gitBlobText, InvalidGitUtf8 } from \"../src/git-blob-text.ts\";\n+import { GitIntakeTrees } from \"../src/git-intake-trees.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+function source(bytes: Uint8Array) {\n+\treturn {\n+\t\tbyteLength: bytes.length,\n+\t\tread(start: number, length: number) {\n+\t\t\tassert.ok(length <= 16384);\n+\t\t\treturn bytes.slice(start, start + length);\n+\t\t},\n+\t};\n+}\n+\n+test(\"Git text chunks preserve BOM and split UTF-8 semantics with bounded pieces\", () => {\n+\tfor (const text of [\"\", \"\\ufefftext\", \"x\".repeat(16383) + \"🚀é中\".repeat(20000), \"nul\\0text\"]) {\n+\t\tconst bytes = new TextEncoder().encode(text);\n+\t\tconst chunks = [...gitBlobText(source(bytes))];\n+\t\tassert.ok(chunks.every((chunk) => chunk.length <= 16384));\n+\t\tassert.equal(chunks.join(\"\"), new TextDecoder(\"utf-8\", { fatal: true, ignoreBOM: false }).decode(bytes));\n+\t}\n+});\n+\n+test(\"late invalid UTF-8 rolls back retained chunks without publishing a file\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst trees = new GitIntakeTrees(storage);\n+\tconst files: Record<string, string> = {};\n+\tconst bytes = new Uint8Array(100000).fill(97);\n+\tbytes[bytes.length - 1] = 0xf0;\n+\tassert.throws(() => trees.retainChunks(files, \"bad\", gitBlobText(source(bytes)), \"a\".repeat(40)), InvalidGitUtf8);\n+\tassert.deepEqual(Object.keys(files), []);\n+\tassert.equal(\n+\t\tstorage.db.prepare(\"SELECT COUNT(*) AS n FROM writer_state WHERE namespace = ?\").get(trees.namespace)!.n,\n+\t\t0,\n+\t);\n+\tbytes[bytes.length - 1] = 98;\n+\ttrees.retainChunks(files, \"good\", gitBlobText(source(bytes)), \"a\".repeat(40));\n+\tassert.equal(files.good, new TextDecoder().decode(bytes));\n+\ttrees.dispose();\n+});\n+\n+test(\"Git text reading preserves storage errors instead of classifying them as binary\", () => {\n+\tconst failure = new TypeError(\"storage failure\");\n+\tassert.throws(\n+\t\t() => [\n+\t\t\t...gitBlobText({\n+\t\t\t\tbyteLength: 1,\n+\t\t\t\tread: () => {\n+\t\t\t\t\tthrow failure;\n+\t\t\t\t},\n+\t\t\t}),\n+\t\t],\n+\t\t(error) => error === failure,\n+\t);\n+});"}],"unmatched":[]}