import assert from "node:assert/strict"; import { test } from "node:test"; import { dropBytes, parseReceiveCommands, parseReceiveResult, rejectPush, reportPush } from "../git.ts"; function pkt(line: string): string { return (line.length + 4).toString(16).padStart(4, "0") + line; } const a = "a".repeat(40); const b = "b".repeat(40); const zero = "0".repeat(40); test("reads ref updates up to the flush packet", () => { const body = pkt(`${a} ${b} refs/heads/main\0report-status side-band-64k\n`) + pkt(`${zero} ${a} refs/heads/new\n`) + pkt(`${b} ${zero} refs/tags/old\n`) + "0000PACK..."; assert.deepEqual(parseReceiveCommands(new TextEncoder().encode(body)), { updates: [ { ref: "refs/heads/main", from: a, to: b }, { ref: "refs/heads/new", from: null, to: a }, { ref: "refs/tags/old", from: b, to: null }, ], capabilities: ["report-status", "side-band-64k"], length: body.indexOf("0000PACK") + 4, }); }); test("asks for more bytes when the command list is cut off", () => { const body = pkt(`${a} ${b} refs/heads/main\n`).slice(0, 30); assert.equal(parseReceiveCommands(new TextEncoder().encode(body)), null); }); test("rejects a push with a reason per ref, inside side-band when the client asked for it", async () => { const commands = { updates: [{ ref: "refs/heads/main", from: a, to: b }, { ref: "refs/heads/tasks/52", from: a, to: b }], capabilities: ["report-status"] }; const plain = await rejectPush(commands, new Map([["refs/heads/main", "only tasks/52"]])).text(); assert.equal(plain, pkt("unpack ok\n") + pkt("ng refs/heads/main only tasks/52\n") + pkt("ng refs/heads/tasks/52 rejected with the other refs in this push\n") + "0000"); const banded = new Uint8Array(await rejectPush({ ...commands, capabilities: ["report-status", "side-band-64k"] }, new Map()).arrayBuffer()); const first = new TextDecoder().decode(banded.subarray(0, 4)); assert.equal(parseInt(first, 16), 4 + 1 + pkt("unpack ok\n").length); assert.equal(banded[4], 1); assert.equal(new TextDecoder().decode(banded.subarray(banded.length - 4)), "0000"); }); test("reports ok and ng per ref, with remote: lines on side-band 2 before the status", async () => { const commands = { updates: [{ ref: "refs/heads/tasks/52", from: a, to: b }], capabilities: ["report-status", "side-band-64k"], length: 0 }; const banded = new Uint8Array(await reportPush(commands, new Map([["refs/heads/tasks/52", null]]), ["recorded DEC-X"]).arrayBuffer()); assert.equal(banded[4], 2); assert.equal(new TextDecoder().decode(banded.subarray(5, parseInt(new TextDecoder().decode(banded.subarray(0, 4)), 16))), "recorded DEC-X\n"); assert.match(new TextDecoder().decode(banded), /ok refs\/heads\/tasks\/52\n/); }); test("reads a plain receive-pack report", () => { const report = pkt("unpack ok\n") + pkt("ok refs/quarantine/q\n") + pkt("ng refs/heads/main stale ref\n") + "0000"; assert.deepEqual(parseReceiveResult(new TextEncoder().encode(report)), { unpack: "ok", refs: new Map([["refs/quarantine/q", null], ["refs/heads/main", "stale ref"]]) }); }); test("drops a prefix that spans chunks", async () => { const stream = new ReadableStream({ start(controller) { for (const chunk of ["0123", "4567", "89"]) controller.enqueue(new TextEncoder().encode(chunk)); controller.close(); }, }); assert.equal(await new Response(dropBytes(stream, 6)).text(), "6789"); });