{"draft":null,"from":175,"to":318,"files":[{"path":"server/test/git-disk-clone.test.ts","added":249,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/git-disk-clone.test.ts\n@@ -0,0 +1,249 @@\n+import assert from \"node:assert/strict\";\n+import { randomBytes } from \"node:crypto\";\n+import { test } from \"node:test\";\n+import git, { type HttpClient } from \"isomorphic-git\";\n+import { GitDiskIntake } from \"../src/git-disk-intake.ts\";\n+import { GitFileContents } from \"../src/git-file-contents.ts\";\n+import { GitIntakeTrees } from \"../src/git-intake-trees.ts\";\n+import { pushedSince } from \"../src/ingest.ts\";\n+import { MemoryFS } from \"../src/memory-fs.ts\";\n+import { commitShared } from \"../src/push.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+const packet = (text: string | Uint8Array) => {\n+\tconst body = Buffer.from(text);\n+\treturn Buffer.concat([Buffer.from((body.length + 4).toString(16).padStart(4, \"0\")), body]);\n+};\n+\n+for (const mode of [\"success\", \"limit\", \"fatal\", \"server-intake\", \"text-intake\", \"binary-limit\"])\n+\ttest(`actual disk clone handles ${mode}`, async (t) => {\n+\t\tconst rejectPack = mode === \"limit\";\n+\t\tconst source = new MemoryFS(),\n+\t\t\tdir = \"/repo\";\n+\t\tawait git.init({ fs: source, dir, defaultBranch: \"main\" });\n+\t\tconst blob = mode === \"text-intake\" ? Buffer.from(\"x\".repeat(16383) + \"🚀é中\".repeat(20000)) : randomBytes(400000);\n+\t\tif (mode === \"binary-limit\") blob[0] = 255;\n+\t\tconst blobOid = await git.writeBlob({ fs: source, dir, blob });\n+\t\tconst tree = await git.writeTree({\n+\t\t\tfs: source,\n+\t\t\tdir,\n+\t\t\ttree: [{ mode: \"100644\", path: \"large.bin\", oid: blobOid, type: \"blob\" }],\n+\t\t});\n+\t\tconst author = {\n+\t\t\tname: \"Capacity fixture\",\n+\t\t\temail: \"fixture@example.invalid\",\n+\t\t\ttimestamp: 1,\n+\t\t\ttimezoneOffset: 0,\n+\t\t};\n+\t\tconst base = await git.writeCommit({\n+\t\t\tfs: source,\n+\t\t\tdir,\n+\t\t\tcommit: {\n+\t\t\t\ttree,\n+\t\t\t\tparent: [],\n+\t\t\t\tauthor,\n+\t\t\t\tcommitter: author,\n+\t\t\t\tmessage: \"Base\\n\\nabso-version: 0\\n\",\n+\t\t\t},\n+\t\t});\n+\t\tconst head = await git.writeCommit({\n+\t\t\tfs: source,\n+\t\t\tdir,\n+\t\t\tcommit: {\n+\t\t\t\ttree,\n+\t\t\t\tparent: [base],\n+\t\t\t\tauthor,\n+\t\t\t\tcommitter: author,\n+\t\t\t\tmessage: \"Fixture\\n\",\n+\t\t\t},\n+\t\t});\n+\t\tconst packed = (\n+\t\t\tawait git.packObjects({\n+\t\t\t\tfs: source,\n+\t\t\t\tdir,\n+\t\t\t\toids: [head, base, tree, blobOid],\n+\t\t\t\twrite: false,\n+\t\t\t})\n+\t\t).packfile!;\n+\t\tconst advertisement = Buffer.concat([\n+\t\t\tpacket(\"# service=git-upload-pack\\n\"),\n+\t\t\tBuffer.from(\"0000\"),\n+\t\t\tpacket(`${head} HEAD\\0symref=HEAD:refs/heads/main side-band-64k shallow\\n`),\n+\t\t\tpacket(`${head} refs/heads/main\\n`),\n+\t\t\tBuffer.from(\"0000\"),\n+\t\t]);\n+\t\tlet uploads = 0;\n+\t\tlet uploadChunksRead = 0;\n+\t\tlet uploadClosed = false;\n+\t\tconst uploadParts = [packet(\"NAK\\n\")];\n+\t\tfor (let at = 0; at < packed.length; at += 16000) {\n+\t\t\tuploadParts.push(packet(Buffer.concat([Buffer.from([1]), packed.subarray(at, at + 16000)])));\n+\t\t\tuploadParts.push(\n+\t\t\t\tpacket(Buffer.concat([Buffer.from([2]), Buffer.from(\"Counting objects: 50% (1/2)\\n\".repeat(200))])),\n+\t\t\t);\n+\t\t\tif (mode === \"fatal\" && at === 16000) {\n+\t\t\t\tuploadParts.push(packet(Buffer.concat([Buffer.from([3]), Buffer.from(\"fixture remote failure\")])));\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t\tuploadParts.push(Buffer.from(\"0000\"));\n+\t\tconst http: HttpClient = {\n+\t\t\trequest: async ({ url, method }) => {\n+\t\t\t\tconst body = method === \"POST\" ? Buffer.concat(uploadParts) : advertisement;\n+\t\t\t\tif (method === \"POST\") uploads++;\n+\t\t\t\treturn {\n+\t\t\t\t\turl,\n+\t\t\t\t\tmethod: method ?? \"GET\",\n+\t\t\t\t\tstatusCode: 200,\n+\t\t\t\t\tstatusMessage: \"OK\",\n+\t\t\t\t\theaders: {},\n+\t\t\t\t\tbody: (async function* () {\n+\t\t\t\t\t\ttry {\n+\t\t\t\t\t\t\tfor (let at = 0; at < body.length; at += 997) {\n+\t\t\t\t\t\t\t\tif (method === \"POST\") uploadChunksRead++;\n+\t\t\t\t\t\t\t\tyield body.subarray(at, at + 997);\n+\t\t\t\t\t\t\t}\n+\t\t\t\t\t\t} finally {\n+\t\t\t\t\t\t\tif (method === \"POST\") uploadClosed = true;\n+\t\t\t\t\t\t}\n+\t\t\t\t\t})(),\n+\t\t\t\t};\n+\t\t\t},\n+\t\t};\n+\t\tconst storage = memoryStorage();\n+\t\tconst intake = new GitDiskIntake(storage, rejectPack ? 70000 : 2000000, 1000000);\n+\t\tconst ingest = intake.ingest.bind(intake);\n+\t\tintake.ingest = async (...args) => {\n+\t\t\tawait new Promise((resolve) => setTimeout(resolve, 25));\n+\t\t\tassert.ok(uploadChunksRead < 64, `pack source read ahead ${uploadChunksRead} chunks before disk consumption`);\n+\t\t\treturn ingest(...args);\n+\t\t};\n+\t\tconst cache = {};\n+\t\tintake.attach(cache);\n+\t\tconst contents = new GitFileContents(storage, 1000000, 2000000);\n+\t\tconst fs = new MemoryFS(contents);\n+\t\tconst write = fs.promises.writeFile;\n+\t\tfs.promises.writeFile = async (path, ...args) => {\n+\t\t\tassert.ok(!/\\.(pack|idx)$/.test(path), \"clone must not persist a collected pack or index\");\n+\t\t\treturn write(path, ...args);\n+\t\t};\n+\t\ttry {\n+\t\t\tif (mode === \"server-intake\" || mode === \"text-intake\" || mode === \"binary-limit\") {\n+\t\t\t\tconst trees = new GitIntakeTrees(storage);\n+\t\t\t\tif (mode === \"text-intake\" || mode === \"binary-limit\") {\n+\t\t\t\t\tconst original = GitDiskIntake.prototype.read;\n+\t\t\t\t\tt.mock.method(GitDiskIntake.prototype, \"read\", function (this: GitDiskIntake, oid: string) {\n+\t\t\t\t\t\tif (oid === blobOid) throw Error(\"whole blob read forbidden\");\n+\t\t\t\t\t\treturn original.call(this, oid);\n+\t\t\t\t\t});\n+\t\t\t\t}\n+\t\t\t\tconst artifacts = {\n+\t\t\t\t\tget: async () => ({\n+\t\t\t\t\t\tinfo: async () => ({ remote: \"https://fixture.invalid/repo\" }),\n+\t\t\t\t\t\tcreateToken: async () => ({ plaintext: \"invented-test-token\" }),\n+\t\t\t\t\t\t[Symbol.dispose]() {},\n+\t\t\t\t\t}),\n+\t\t\t\t};\n+\t\t\t\tconst found = await pushedSince(\n+\t\t\t\t\tartifacts as never,\n+\t\t\t\t\t\"fixture\",\n+\t\t\t\t\tundefined,\n+\t\t\t\t\tasync (bytes) => {\n+\t\t\t\t\t\tassert.notEqual(mode, \"binary-limit\", \"oversized binary must not reach the store\");\n+\t\t\t\t\t\tassert.deepEqual(Buffer.from(bytes), blob);\n+\t\t\t\t\t\treturn \"fixture-blob\";\n+\t\t\t\t\t},\n+\t\t\t\t\tmode === \"binary-limit\" ? () => \"previous-blob\" : undefined,\n+\t\t\t\t\tundefined,\n+\t\t\t\t\t{\n+\t\t\t\t\t\tstorage,\n+\t\t\t\t\t\tmaxPackBytes: 2000000,\n+\t\t\t\t\t\tmaxObjectBytes: 1000000,\n+\t\t\t\t\t\tmaxBinaryBytes: mode === \"binary-limit\" ? 100000 : undefined,\n+\t\t\t\t\t\thttp,\n+\t\t\t\t\t\tretainFileChunks: (files, path, chunks, oid) => trees.retainChunks(files, path, chunks, oid),\n+\t\t\t\t\t},\n+\t\t\t\t);\n+\t\t\t\tassert.equal(found!.base, 0);\n+\t\t\t\tassert.equal(found!.commits.length, 1);\n+\t\t\t\tif (mode === \"text-intake\") assert.equal(found!.commits[0]!.files[\"large.bin\"], blob.toString(\"utf8\"));\n+\t\t\t\telse if (mode === \"binary-limit\") {\n+\t\t\t\t\tassert.equal(found!.commits[0]!.blobs[\"large.bin\"], \"previous-blob\");\n+\t\t\t\t\tassert.deepEqual(found!.tooBig, [\"large.bin\"]);\n+\t\t\t\t} else assert.equal(found!.commits[0]!.blobs[\"large.bin\"], \"fixture-blob\");\n+\t\t\t\ttrees.dispose();\n+\t\t\t\tfor (const table of [\"git_pack_spool\", \"git_pack_resolve\", \"git_object_meta\", \"git_object_bytes\"])\n+\t\t\t\t\tassert.equal(storage.db.prepare(`SELECT COUNT(*) AS n FROM ${table}`).get()!.n, 0);\n+\t\t\t\treturn;\n+\t\t\t}\n+\t\t\tconst clone = git.clone({\n+\t\t\t\tfs,\n+\t\t\t\tdir,\n+\t\t\t\thttp,\n+\t\t\t\tcache,\n+\t\t\t\turl: \"https://fixture.invalid/repo\",\n+\t\t\t\tref: \"main\",\n+\t\t\t\tsingleBranch: true,\n+\t\t\t\tnoCheckout: true,\n+\t\t\t});\n+\t\t\tif (mode !== \"success\") {\n+\t\t\t\tawait assert.rejects(clone, rejectPack ? /pack exceeds byte limit/ : /fixture remote failure/);\n+\t\t\t\tassert.equal(uploadClosed, true);\n+\t\t\t\tfor (const table of [\"git_pack_spool\", \"git_pack_resolve\", \"git_object_meta\", \"git_object_bytes\"])\n+\t\t\t\t\tassert.equal(storage.db.prepare(`SELECT COUNT(*) AS n FROM ${table}`).get()!.n, 0);\n+\t\t\t\treturn;\n+\t\t\t}\n+\t\t\tawait clone;\n+\t\t\tintake.assertReady();\n+\t\t\tassert.equal((await git.log({ fs, dir, cache, ref: \"main\" })).length, 2);\n+\t\t\tlet walked = 0;\n+\t\t\tawait git.walk({\n+\t\t\t\tfs,\n+\t\t\t\tdir,\n+\t\t\t\tcache,\n+\t\t\t\ttrees: [git.TREE({ ref: head })],\n+\t\t\t\tmap: async (path, [entry]) => {\n+\t\t\t\t\tif (path === \"large.bin\") {\n+\t\t\t\t\t\tassert.deepEqual(Buffer.from((await entry!.content())!), blob);\n+\t\t\t\t\t\twalked++;\n+\t\t\t\t\t}\n+\t\t\t\t\treturn true;\n+\t\t\t\t},\n+\t\t\t});\n+\t\t\tassert.equal(walked, 1);\n+\t\t\tassert.equal(uploads, 1);\n+\t\t\tassert.equal(await git.resolveRef({ fs, dir, ref: \"main\" }), head);\n+\t\t\tassert.equal((await git.readCommit({ fs, dir, cache, oid: head })).commit.tree, tree);\n+\t\t\tassert.deepEqual(Buffer.from((await git.readBlob({ fs, dir, cache, oid: blobOid })).blob), blob);\n+\t\t\tconst exported = await commitShared(\n+\t\t\t\tfs,\n+\t\t\t\tdir,\n+\t\t\t\thead,\n+\t\t\t\t{\n+\t\t\t\t\tversion: 1,\n+\t\t\t\t\tfiles: { \"new.txt\": \"Disk export fixture\" },\n+\t\t\t\t\tblobs: { \"large.bin\": \"fixture-blob\" },\n+\t\t\t\t},\n+\t\t\t\t() => blob,\n+\t\t\t\tundefined,\n+\t\t\t\tcache,\n+\t\t\t);\n+\t\t\tconst next = await git.readCommit({ fs, dir, cache, oid: exported.commit });\n+\t\t\tassert.deepEqual(next.commit.parent, [head]);\n+\t\t\tassert.deepEqual(\n+\t\t\t\tBuffer.from((await git.readBlob({ fs, dir, cache, oid: exported.commit, filepath: \"large.bin\" })).blob),\n+\t\t\t\tblob,\n+\t\t\t);\n+\t\t\tassert.equal(\n+\t\t\t\tBuffer.from(\n+\t\t\t\t\t(await git.readBlob({ fs, dir, cache, oid: exported.commit, filepath: \"new.txt\" })).blob,\n+\t\t\t\t).toString(),\n+\t\t\t\t\"Disk export fixture\",\n+\t\t\t);\n+\t\t\tfor (const entry of fs.entries.values()) if (entry.kind === \"file\") assert.equal(entry.data.byteLength, 0);\n+\t\t} finally {\n+\t\t\tcontents.dispose();\n+\t\t\tintake.dispose();\n+\t\t\tstorage.db.close();\n+\t\t}\n+\t});"}],"unmatched":[]}