Skip to content
refs
64 lines · 3.4 KBRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { dropBytes, parseReceiveCommands, parseReceiveResult, rejectPush, reportPush } from "../git.ts";
4
5function pkt(line: string): string {
6 return (line.length + 4).toString(16).padStart(4, "0") + line;
7}
8
9const a = "a".repeat(40);
10const b = "b".repeat(40);
11const zero = "0".repeat(40);
12
13test("reads ref updates up to the flush packet", () => {
14 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...";
15 assert.deepEqual(parseReceiveCommands(new TextEncoder().encode(body)), {
16 updates: [
17 { ref: "refs/heads/main", from: a, to: b },
18 { ref: "refs/heads/new", from: null, to: a },
19 { ref: "refs/tags/old", from: b, to: null },
20 ],
21 capabilities: ["report-status", "side-band-64k"],
22 length: body.indexOf("0000PACK") + 4,
23 });
24});
25
26test("asks for more bytes when the command list is cut off", () => {
27 const body = pkt(`${a} ${b} refs/heads/main\n`).slice(0, 30);
28 assert.equal(parseReceiveCommands(new TextEncoder().encode(body)), null);
29});
30
31test("rejects a push with a reason per ref, inside side-band when the client asked for it", async () => {
32 const commands = { updates: [{ ref: "refs/heads/main", from: a, to: b }, { ref: "refs/heads/tasks/52", from: a, to: b }], capabilities: ["report-status"] };
33 const plain = await rejectPush(commands, new Map([["refs/heads/main", "only tasks/52"]])).text();
34 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");
35
36 const banded = new Uint8Array(await rejectPush({ ...commands, capabilities: ["report-status", "side-band-64k"] }, new Map()).arrayBuffer());
37 const first = new TextDecoder().decode(banded.subarray(0, 4));
38 assert.equal(parseInt(first, 16), 4 + 1 + pkt("unpack ok\n").length);
39 assert.equal(banded[4], 1);
40 assert.equal(new TextDecoder().decode(banded.subarray(banded.length - 4)), "0000");
41});
42
43test("reports ok and ng per ref, with remote: lines on side-band 2 before the status", async () => {
44 const commands = { updates: [{ ref: "refs/heads/tasks/52", from: a, to: b }], capabilities: ["report-status", "side-band-64k"], length: 0 };
45 const banded = new Uint8Array(await reportPush(commands, new Map([["refs/heads/tasks/52", null]]), ["recorded DEC-X"]).arrayBuffer());
46 assert.equal(banded[4], 2);
47 assert.equal(new TextDecoder().decode(banded.subarray(5, parseInt(new TextDecoder().decode(banded.subarray(0, 4)), 16))), "recorded DEC-X\n");
48 assert.match(new TextDecoder().decode(banded), /ok refs\/heads\/tasks\/52\n/);
49});
50
51test("reads a plain receive-pack report", () => {
52 const report = pkt("unpack ok\n") + pkt("ok refs/quarantine/q\n") + pkt("ng refs/heads/main stale ref\n") + "0000";
53 assert.deepEqual(parseReceiveResult(new TextEncoder().encode(report)), { unpack: "ok", refs: new Map([["refs/quarantine/q", null], ["refs/heads/main", "stale ref"]]) });
54});
55
56test("drops a prefix that spans chunks", async () => {
57 const stream = new ReadableStream<Uint8Array>({
58 start(controller) {
59 for (const chunk of ["0123", "4567", "89"]) controller.enqueue(new TextEncoder().encode(chunk));
60 controller.close();
61 },
62 });
63 assert.equal(await new Response(dropBytes(stream, 6)).text(), "6789");
64});