{"draft":null,"from":116,"to":318,"files":[{"path":"server/test/git-pack-spool.test.ts","added":70,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/git-pack-spool.test.ts\n@@ -0,0 +1,70 @@\n+import assert from \"node:assert/strict\";\n+import { randomBytes } from \"node:crypto\";\n+import { test } from \"node:test\";\n+import git from \"isomorphic-git\";\n+import { GitPackSpool } from \"../src/git-pack-spool.ts\";\n+import { MemoryFS } from \"../src/memory-fs.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+test(\"cold pack recovery clears abandoned scratch without changing canonical tables\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tGitPackSpool.recover(storage);\n+\tstorage.sql.exec(\"CREATE TABLE canonical_test (value TEXT)\");\n+\tstorage.sql.exec(\"INSERT INTO canonical_test VALUES (?)\", \"preserved\");\n+\tstorage.sql.exec(\"INSERT INTO git_pack_spool VALUES (?, ?, ?)\", \"abandoned\", 0, new Uint8Array([1, 2]));\n+\tGitPackSpool.recover(storage);\n+\tassert.equal(storage.db.prepare(\"SELECT COUNT(*) AS n FROM git_pack_spool\").get()!.n, 0);\n+\tassert.equal(storage.db.prepare(\"SELECT value FROM canonical_test\").get()!.value, \"preserved\");\n+\tGitPackSpool.recover(storage);\n+});\n+\n+test(\"pack spool verifies real pack bytes and serves bounded ranges across chunk boundaries\", async (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst fs = new MemoryFS(),\n+\t\tdir = \"/repo\";\n+\tawait git.init({ fs, dir });\n+\tconst oids = [];\n+\tfor (let i = 0; i < 4; i++) oids.push(await git.writeBlob({ fs, dir, blob: randomBytes(180000) }));\n+\tconst packed = await git.packObjects({ fs, dir, oids, write: false });\n+\tconst bytes = packed.packfile!;\n+\tconst spool = new GitPackSpool(storage);\n+\tassert.throws(() => spool.read(0, 0), /not readable/);\n+\tasync function* source() {\n+\t\tfor (let at = 0; at < bytes.length; at += 997) yield bytes.subarray(at, at + 997);\n+\t}\n+\tassert.equal(await spool.ingest(source(), 1000000), Buffer.from(bytes.subarray(-20)).toString(\"hex\"));\n+\tassert.equal(spool.byteLength, bytes.length);\n+\tfor (const start of [0, 65530, 65536, 131070, bytes.length - 101]) {\n+\t\tconst length = Math.min(101, bytes.length - start);\n+\t\tassert.deepEqual(spool.read(start, length), new Uint8Array(bytes.subarray(start, start + length)));\n+\t}\n+\tassert.deepEqual(Buffer.concat([...spool.chunks()]), Buffer.from(bytes));\n+\tassert.throws(() => spool.read(0, 65537), /invalid pack byte range/);\n+\tassert.throws(() => spool.read(bytes.length, 1), /invalid pack byte range/);\n+\tconst max = storage.db.prepare(\"SELECT MAX(length(data)) AS n FROM git_pack_spool\").get()!.n;\n+\tassert.ok(Number(max) <= 65536);\n+\tspool.dispose();\n+\tassert.throws(() => spool.read(0, 0), /not readable/);\n+\tassert.equal(storage.db.prepare(\"SELECT COUNT(*) AS n FROM git_pack_spool\").get()!.n, 0);\n+\tfor (const mode of [\"corrupt\", \"limit\", \"source-error\"]) {\n+\t\tconst bad = new GitPackSpool(storage);\n+\t\tlet closed = false;\n+\t\tasync function* broken() {\n+\t\t\ttry {\n+\t\t\t\tconst data = Uint8Array.from(bytes);\n+\t\t\t\tif (mode === \"corrupt\") data[20]! ^= 1;\n+\t\t\t\tyield data.subarray(0, 100000);\n+\t\t\t\tif (mode === \"source-error\") throw Error(\"injected source failure\");\n+\t\t\t\tyield data.subarray(100000);\n+\t\t\t} finally {\n+\t\t\t\tclosed = true;\n+\t\t\t}\n+\t\t}\n+\t\tawait assert.rejects(bad.ingest(broken(), mode === \"limit\" ? 70000 : 1000000));\n+\t\tassert.equal(closed, true);\n+\t\tassert.throws(() => bad.read(0, 0), /not readable/);\n+\t\tassert.equal(storage.db.prepare(\"SELECT COUNT(*) AS n FROM git_pack_spool\").get()!.n, 0);\n+\t}\n+});"}],"unmatched":[]}