Versions 127 to 318
new file, 74 lines
1+import assert from "node:assert/strict"; 2+import { createHash } from "node:crypto"; 3+import { test } from "node:test"; 4+import { applyGitDeltaChunks } from "../src/git-delta-stream.ts"; 5+import { GitObjectBytes } from "../src/git-object-bytes.ts"; 6+import { memoryStorage } from "./helpers.ts"; 8+const integer = (value: number) => { 11+ const part = value % 128; 12+ value = Math.floor(value / 128); 13+ bytes.push(part | (value ? 128 : 0)); 17+test("disk delta output has exact Git identity and partial failures roll back", (t) => { 18+ const storage = memoryStorage(); 19+ t.after(() => storage.db.close()); 20+ const objects = new GitObjectBytes(storage); 21+ const bytes = Uint8Array.from({ length: 200000 }, (_, i) => i % 251); 22+ const base = objects.put("blob", (write) => write(bytes), 200000); 23+ const instructions = Uint8Array.from([...integer(bytes.length), ...integer(131075), 0xc0, 2, 3, 4, 5, 6]); 24+ const delta = objects.put("blob", (write) => write(instructions), 100); 25+ const result = objects.put( 28+ applyGitDeltaChunks(delta, base, write, 200000); 32+ const expected = Buffer.concat([bytes.subarray(0, 131072), new Uint8Array([4, 5, 6])]); 33+ assert.equal(result.oid, createHash("sha1").update(`blob ${expected.length}\0`).update(expected).digest("hex")); 34+ assert.deepEqual(result.read(65530, 100), new Uint8Array(expected.subarray(65530, 65630))); 35+ assert.equal(result.byteLength, expected.length); 36+ const counts = () => [ 37+ storage.db.prepare("SELECT COUNT(*) AS n FROM git_object_bytes").get()!.n, 38+ storage.db.prepare("SELECT COUNT(*) AS n FROM git_object_meta").get()!.n, 40+ const bad = objects.put("blob", (write) => write(Uint8Array.from([...instructions, 0])), 101); 41+ const before = counts(); 47+ applyGitDeltaChunks(bad, base, write, 200000); 51+ /invalid Git delta instruction/, 53+ assert.deepEqual(counts(), before); 60+ throw Error("injected producer failure"); 64+ /injected producer failure/, 66+ assert.deepEqual(counts(), before); 67+ assert.throws(() => objects.put("blob", (write) => write(bytes), 100000), /exceeds byte limit/); 68+ assert.deepEqual(counts(), before); 69+ assert.deepEqual(base.read(0, 100), bytes.slice(0, 100)); 70+ assert.equal(objects.put("blob", () => {}, 0).oid, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"); 72+ assert.deepEqual(counts(), [0, 0]); 73+ assert.throws(() => base.read(0, 1), /closed/);