{"draft":null,"from":32,"to":318,"files":[{"path":"server/test/git-object-bytes.test.ts","added":74,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/git-object-bytes.test.ts\n@@ -0,0 +1,74 @@\n+import assert from \"node:assert/strict\";\n+import { createHash } from \"node:crypto\";\n+import { test } from \"node:test\";\n+import { applyGitDeltaChunks } from \"../src/git-delta-stream.ts\";\n+import { GitObjectBytes } from \"../src/git-object-bytes.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+const integer = (value: number) => {\n+\tconst bytes = [];\n+\tdo {\n+\t\tconst part = value % 128;\n+\t\tvalue = Math.floor(value / 128);\n+\t\tbytes.push(part | (value ? 128 : 0));\n+\t} while (value);\n+\treturn bytes;\n+};\n+test(\"disk delta output has exact Git identity and partial failures roll back\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst objects = new GitObjectBytes(storage);\n+\tconst bytes = Uint8Array.from({ length: 200000 }, (_, i) => i % 251);\n+\tconst base = objects.put(\"blob\", (write) => write(bytes), 200000);\n+\tconst instructions = Uint8Array.from([...integer(bytes.length), ...integer(131075), 0xc0, 2, 3, 4, 5, 6]);\n+\tconst delta = objects.put(\"blob\", (write) => write(instructions), 100);\n+\tconst result = objects.put(\n+\t\t\"blob\",\n+\t\t(write) => {\n+\t\t\tapplyGitDeltaChunks(delta, base, write, 200000);\n+\t\t},\n+\t\t200000,\n+\t);\n+\tconst expected = Buffer.concat([bytes.subarray(0, 131072), new Uint8Array([4, 5, 6])]);\n+\tassert.equal(result.oid, createHash(\"sha1\").update(`blob ${expected.length}\\0`).update(expected).digest(\"hex\"));\n+\tassert.deepEqual(result.read(65530, 100), new Uint8Array(expected.subarray(65530, 65630)));\n+\tassert.equal(result.byteLength, expected.length);\n+\tconst counts = () => [\n+\t\tstorage.db.prepare(\"SELECT COUNT(*) AS n FROM git_object_bytes\").get()!.n,\n+\t\tstorage.db.prepare(\"SELECT COUNT(*) AS n FROM git_object_meta\").get()!.n,\n+\t];\n+\tconst bad = objects.put(\"blob\", (write) => write(Uint8Array.from([...instructions, 0])), 101);\n+\tconst before = counts();\n+\tassert.throws(\n+\t\t() =>\n+\t\t\tobjects.put(\n+\t\t\t\t\"blob\",\n+\t\t\t\t(write) => {\n+\t\t\t\t\tapplyGitDeltaChunks(bad, base, write, 200000);\n+\t\t\t\t},\n+\t\t\t\t200000,\n+\t\t\t),\n+\t\t/invalid Git delta instruction/,\n+\t);\n+\tassert.deepEqual(counts(), before);\n+\tassert.throws(\n+\t\t() =>\n+\t\t\tobjects.put(\n+\t\t\t\t\"blob\",\n+\t\t\t\t(write) => {\n+\t\t\t\t\twrite(bytes);\n+\t\t\t\t\tthrow Error(\"injected producer failure\");\n+\t\t\t\t},\n+\t\t\t\t200000,\n+\t\t\t),\n+\t\t/injected producer failure/,\n+\t);\n+\tassert.deepEqual(counts(), before);\n+\tassert.throws(() => objects.put(\"blob\", (write) => write(bytes), 100000), /exceeds byte limit/);\n+\tassert.deepEqual(counts(), before);\n+\tassert.deepEqual(base.read(0, 100), bytes.slice(0, 100));\n+\tassert.equal(objects.put(\"blob\", () => {}, 0).oid, \"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391\");\n+\tobjects.dispose();\n+\tassert.deepEqual(counts(), [0, 0]);\n+\tassert.throws(() => base.read(0, 1), /closed/);\n+});"}],"unmatched":[]}