File
Blob: test/parse-fetch-args.test.ts
| 1 | import { assert, test } from "vitest"; |
| 2 | import { pktLine, delimPkt, flushPkt, parseFetchArgs } from "@/worker/git"; |
| 3 | |
| 4 | function buildFetchBody({ |
| 5 | wants, |
| 6 | haves, |
| 7 | done, |
| 8 | }: { |
| 9 | wants: string[]; |
| 10 | haves: string[]; |
| 11 | done?: boolean; |
| 12 | }): Uint8Array { |
| 13 | const chunks: Uint8Array[] = []; |
| 14 | chunks.push(pktLine("command=fetch\n")); |
| 15 | chunks.push(delimPkt()); |
| 16 | for (const w of wants) chunks.push(pktLine(`want ${w}\n`)); |
| 17 | for (const h of haves) chunks.push(pktLine(`have ${h}\n`)); |
| 18 | if (done) chunks.push(pktLine("done\n")); |
| 19 | chunks.push(flushPkt()); |
| 20 | const total = chunks.reduce((n, c) => n + c.byteLength, 0); |
| 21 | const out = new Uint8Array(total); |
| 22 | let off = 0; |
| 23 | for (const c of chunks) { |
| 24 | out.set(c, off); |
| 25 | off += c.byteLength; |
| 26 | } |
| 27 | return out; |
| 28 | } |
| 29 | |
| 30 | const O1 = "0123456789abcdef0123456789abcdef01234567"; |
| 31 | const O2 = "89abcdef0123456789abcdef0123456789abcdef"; |
| 32 | |
| 33 | test("parseFetchArgs extracts wants/haves/done after delim", () => { |
| 34 | const body = buildFetchBody({ wants: [O1], haves: [O2], done: true }); |
| 35 | const res = parseFetchArgs(body); |
| 36 | assert.deepEqual(res.wants, [O1]); |
| 37 | assert.deepEqual(res.haves, [O2]); |
| 38 | assert.isTrue(res.done); |
| 39 | }); |
| 40 | |
| 41 | test("parseFetchArgs handles multiple wants/haves and missing done", () => { |
| 42 | const body = buildFetchBody({ wants: [O1, O2], haves: [O2, O1], done: false }); |
| 43 | const res = parseFetchArgs(body); |
| 44 | assert.deepEqual(new Set(res.wants), new Set([O1, O2])); |
| 45 | assert.deepEqual(res.haves, [O2, O1]); |
| 46 | assert.isFalse(res.done); |
| 47 | }); |