File
Blob: test/fetch-acks.worker.test.ts
| 1 | import { it, expect } from "vitest"; |
| 2 | import { env, exports as workerExports } from "cloudflare:workers"; |
| 3 | import { pktLine, delimPkt, flushPkt, concatChunks } from "@/worker/git"; |
| 4 | import { uniqueRepoId, runDOWithRetry } from "./util/test-helpers"; |
| 5 | import { setupRepoForTests } from "./util/repoSeed"; |
| 6 | |
| 7 | function buildFetchBody({ |
| 8 | wants, |
| 9 | haves, |
| 10 | done, |
| 11 | }: { |
| 12 | wants: string[]; |
| 13 | haves?: string[]; |
| 14 | done?: boolean; |
| 15 | }) { |
| 16 | const chunks: Uint8Array[] = []; |
| 17 | chunks.push(pktLine("command=fetch\n")); |
| 18 | chunks.push(delimPkt()); |
| 19 | for (const w of wants) chunks.push(pktLine(`want ${w}\n`)); |
| 20 | for (const h of haves || []) chunks.push(pktLine(`have ${h}\n`)); |
| 21 | if (done) chunks.push(pktLine("done\n")); |
| 22 | chunks.push(flushPkt()); |
| 23 | return concatChunks(chunks); |
| 24 | } |
| 25 | |
| 26 | it("upload-pack fetch returns acknowledgments before the final packfile response", async () => { |
| 27 | const owner = "o"; |
| 28 | const repo = uniqueRepoId("r"); |
| 29 | await setupRepoForTests(env, owner, repo); |
| 30 | const repoId = `${owner}/${repo}`; |
| 31 | // Seed tiny repo and get commit OID via DO instance |
| 32 | const id = env.REPO_DO.idFromName(repoId); |
| 33 | const { commitOid } = await runDOWithRetry( |
| 34 | () => env.REPO_DO.get(id), |
| 35 | async (instance) => instance.seedMinimalRepo() |
| 36 | ); |
| 37 | |
| 38 | const url = `https://example.com/${owner}/${repo}/git-upload-pack`; |
| 39 | |
| 40 | // Negotiation (done=false) should return only acknowledgments. |
| 41 | const negotiateBody = buildFetchBody({ wants: [commitOid], done: false }); |
| 42 | const negotiateRes = await workerExports.default.fetch(url, { |
| 43 | method: "POST", |
| 44 | headers: { |
| 45 | "Content-Type": "application/x-git-upload-pack-request", |
| 46 | "Git-Protocol": "version=2", |
| 47 | }, |
| 48 | body: negotiateBody, |
| 49 | } as any); |
| 50 | expect(negotiateRes.status).toBe(200); |
| 51 | const negotiateBytes = new Uint8Array(await negotiateRes.arrayBuffer()); |
| 52 | const negotiateText = new TextDecoder().decode(negotiateBytes); |
| 53 | expect(negotiateText.includes("acknowledgments\n")).toBe(true); |
| 54 | expect(negotiateText.includes("packfile\n")).toBe(false); |
| 55 | |
| 56 | // The final fetch (done=true) should return the packfile without acknowledgments. |
| 57 | const fetchBody = buildFetchBody({ wants: [commitOid], done: true }); |
| 58 | const fetchRes = await workerExports.default.fetch(url, { |
| 59 | method: "POST", |
| 60 | headers: { |
| 61 | "Content-Type": "application/x-git-upload-pack-request", |
| 62 | "Git-Protocol": "version=2", |
| 63 | }, |
| 64 | body: fetchBody, |
| 65 | } as any); |
| 66 | expect(fetchRes.status).toBe(200); |
| 67 | expect(fetchRes.headers.get("Content-Type") || "").toContain("git-upload-pack-result"); |
| 68 | const fetchBytes = new Uint8Array(await fetchRes.arrayBuffer()); |
| 69 | const fetchText = new TextDecoder().decode(fetchBytes); |
| 70 | expect(fetchText.includes("acknowledgments\n")).toBe(false); |
| 71 | expect(fetchText.includes("packfile\n")).toBe(true); |
| 72 | }); |