| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { FLUSH, concat, pkt, utf8 } from "@diffinery/git-core"; |
| 3 | import { parseReceivePack, parseReceivePackHead } from "../../src/git/protocol.js"; |
| 4 | |
| 5 | const old = "0".repeat(40); |
| 6 | const oid = "a".repeat(40); |
| 7 | const body = (options: string[] | null) => |
| 8 | concat([ |
| 9 | pkt(`${old} ${oid} refs/heads/main\0report-status${options ? " push-options" : ""} atomic\n`), |
| 10 | pkt(`${old} ${oid} refs/heads/next\n`), |
| 11 | FLUSH, |
| 12 | ...(options ? [...options.map((o) => pkt(`${o}\n`)), FLUSH] : []), |
| 13 | utf8("PACK...."), |
| 14 | ]); |
| 15 | |
| 16 | describe("the start of a staged push", () => { |
| 17 | it("gives the commands, push options and where the pack starts, as parseReceivePack does", () => { |
| 18 | for (const options of [null, ["diffinery-import", "allow-secrets=a reason"]]) { |
| 19 | const b = body(options); |
| 20 | const head = parseReceivePackHead(b)!; |
| 21 | const whole = parseReceivePack(b); |
| 22 | expect(head.req.commands).toEqual(whole.commands); |
| 23 | expect([...head.req.capabilities]).toEqual([...whole.capabilities]); |
| 24 | expect(head.req.pushOptions).toEqual(whole.pushOptions); |
| 25 | expect(b.subarray(head.packOffset)).toEqual(whole.pack); |
| 26 | } |
| 27 | }); |
| 28 | it("asks for more when the head is cut short, wherever it is cut", () => { |
| 29 | const b = body(["diffinery-import"]); |
| 30 | const full = parseReceivePackHead(b)!.packOffset; |
| 31 | for (let n = 0; n < full; n++) expect(parseReceivePackHead(b.subarray(0, n))).toBeNull(); |
| 32 | expect(parseReceivePackHead(b.subarray(0, full))?.packOffset).toBe(full); |
| 33 | }); |
| 34 | }); |