44 lines · 3.0 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { createHash } from "node:crypto"; |
| 3 | import { test } from "node:test"; |
| 4 | import { commitBody, gitObject, mergeTrees, packfile, parseReport, treeBody } from "../gitwrite.ts"; |
| 5 | |
| 6 | const blob = (n: number) => n.toString(16).padStart(40, "0"); |
| 7 | |
| 8 | test("hashes objects as git does", async () => { |
| 9 | assert.equal((await gitObject("tree", treeBody([]))).hash, "4b825dc642cb6eb9a060e54bf8d69288fbee4904"); |
| 10 | const body = commitBody({ tree: "4b825dc642cb6eb9a060e54bf8d69288fbee4904", parents: [], author: { name: "A", email: "a@example.com" }, at: 0, message: "m" }); |
| 11 | const expected = createHash("sha1").update(Buffer.concat([Buffer.from(`commit ${body.length}\0`), Buffer.from(body)])).digest("hex"); |
| 12 | assert.equal((await gitObject("commit", body)).hash, expected); |
| 13 | }); |
| 14 | |
| 15 | test("orders a directory as if its name ended in a slash", () => { |
| 16 | const names = (body: Uint8Array) => [...Buffer.from(body).toString("latin1").matchAll(/\d+ ([^\0]+)\0/g)].map((match) => match[1]); |
| 17 | assert.deepEqual(names(treeBody([{ name: "a", mode: "40000", hash: blob(1) }, { name: "a.txt", mode: "100644", hash: blob(2) }, { name: "a-b", mode: "100644", hash: blob(3) }])), ["a-b", "a.txt", "a"]); |
| 18 | }); |
| 19 | |
| 20 | test("writes a version 2 pack with a trailing checksum", async () => { |
| 21 | const pack = await packfile([await gitObject("tree", treeBody([]))]); |
| 22 | assert.equal(Buffer.from(pack.subarray(0, 4)).toString(), "PACK"); |
| 23 | assert.deepEqual([...pack.subarray(4, 12)], [0, 0, 0, 2, 0, 0, 0, 1]); |
| 24 | assert.equal(Buffer.from(pack.subarray(-20)).toString("hex"), createHash("sha1").update(pack.subarray(0, -20)).digest("hex")); |
| 25 | }); |
| 26 | |
| 27 | test("reads a receive-pack report", () => { |
| 28 | const line = (text: string) => `${(text.length + 4).toString(16).padStart(4, "0")}${text}`; |
| 29 | assert.deepEqual(parseReport(new TextEncoder().encode(`${line("unpack ok\n")}${line("ng refs/heads/main failed to update ref\n")}0000`)), ["unpack ok", "ng refs/heads/main failed to update ref"]); |
| 30 | }); |
| 31 | |
| 32 | test("merges trees path by path and reports files both sides changed", async () => { |
| 33 | const trees: Record<string, ArtifactsTreeEntry[]> = { |
| 34 | base: [{ name: "a", mode: "100644", hash: blob(1), type: "blob" }, { name: "b", mode: "100644", hash: blob(2), type: "blob" }], |
| 35 | ours: [{ name: "a", mode: "100644", hash: blob(11), type: "blob" }, { name: "b", mode: "100644", hash: blob(2), type: "blob" }], |
| 36 | theirs: [{ name: "a", mode: "100644", hash: blob(1), type: "blob" }, { name: "c", mode: "100644", hash: blob(3), type: "blob" }], |
| 37 | clash: [{ name: "a", mode: "100644", hash: blob(12), type: "blob" }, { name: "b", mode: "100644", hash: blob(2), type: "blob" }], |
| 38 | }; |
| 39 | const read = async (hash: string) => trees[hash] ?? null; |
| 40 | const merged = await mergeTrees(read, "base", "ours", "theirs"); |
| 41 | assert.deepEqual(merged.conflicts, []); |
| 42 | assert.equal(merged.tree, (await gitObject("tree", treeBody([{ name: "a", mode: "100644", hash: blob(11) }, { name: "c", mode: "100644", hash: blob(3) }]))).hash); |
| 43 | assert.deepEqual((await mergeTrees(read, "base", "ours", "clash")).conflicts, ["a"]); |
| 44 | }); |