import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { fetchPack } from '../../server/utils/git-wire/upload-pack' import { fakeGithubFetch, GitFixture, localReceivePackFactory } from '../utils/git-wire' import { ReceivePackSession } from '../../server/utils/git-wire/receive-pack' import { ZERO_SHA } from '../../server/utils/git-wire/refs' async function drain(gen: AsyncGenerator): Promise { const parts: Buffer[] = [] for await (const c of gen) parts.push(c) return Buffer.concat(parts) } describe('fetchPack negotiation edge cases', () => { let fx: GitFixture let realFetch: typeof globalThis.fetch beforeEach(() => { fx = new GitFixture() realFetch = globalThis.fetch }) afterEach(() => { globalThis.fetch = realFetch fx.cleanup() }) function firstBytes(buf: Buffer, n = 4): string { return buf.subarray(0, n).toString('ascii') } it('pack begins with the PACK signature when a have is a real common commit', async () => { const gh = fx.initBare('gh.git') const work = fx.initWork('work') const base = fx.commit(work, 'a.txt', 'hello') fx.pushTo(work, gh, 'HEAD:refs/heads/main') const head = fx.commit(work, 'b.txt', 'world') fx.pushTo(work, gh, 'HEAD:refs/heads/main') globalThis.fetch = fakeGithubFetch(new Map([['owner/repo', gh]])) as unknown as typeof globalThis.fetch const { pack } = await fetchPack({ repoFullName: 'owner/repo', token: 't', want: head, haves: [base], maxBytes: 1 << 30 }) expect(firstBytes(await drain(pack))).toBe('PACK') }) it('pack begins with PACK when a have is unknown to GitHub (diverged knot ref)', async () => { const gh = fx.initBare('gh.git') const work = fx.initWork('work') const base = fx.commit(work, 'a.txt', 'hello') fx.pushTo(work, gh, 'HEAD:refs/heads/main') const head = fx.commit(work, 'b.txt', 'world') fx.pushTo(work, gh, 'HEAD:refs/heads/main') // A commit GitHub has never seen (exists only on a throwaway repo). The // knot could advertise such a tip; we must not let it corrupt the stream. const other = fx.initWork('other') const stranger = fx.commit(other, 'x.txt', 'stranger') globalThis.fetch = fakeGithubFetch(new Map([['owner/repo', gh]])) as unknown as typeof globalThis.fetch const { pack } = await fetchPack({ repoFullName: 'owner/repo', token: 't', want: head, haves: [stranger, base], maxBytes: 1 << 30 }) expect(firstBytes(await drain(pack))).toBe('PACK') }) it('pushes into a knot that advertises a stranger tip alongside the base', async () => { const gh = fx.initBare('gh.git') const work = fx.initWork('work') const base = fx.commit(work, 'a.txt', 'hello') fx.pushTo(work, gh, 'HEAD:refs/heads/main') const head = fx.commit(work, 'b.txt', 'world') fx.pushTo(work, gh, 'HEAD:refs/heads/main') // Knot has the base on main plus an unrelated tip on another ref, so its // advertisement (and therefore our have-set) mixes a real common commit // with one GitHub can't resolve. const knot = fx.initBare('knot.git') fx.pushTo(work, knot, `${base}:refs/heads/main`) const other = fx.initWork('other') const stranger = fx.commit(other, 'x.txt', 'stranger') fx.pushTo(other, knot, `${stranger}:refs/heads/wip`) globalThis.fetch = fakeGithubFetch(new Map([['owner/repo', gh]])) as unknown as typeof globalThis.fetch const session = await ReceivePackSession.open(localReceivePackFactory(knot)) const haves = [...new Set(session.tips.values())].filter(s => s !== ZERO_SHA) const { pack } = await fetchPack({ repoFullName: 'owner/repo', token: 't', want: head, haves, maxBytes: 1 << 30 }) await session.push([{ ref: 'refs/heads/main', old: base, next: head }], pack) expect(fx.revParse(knot, 'refs/heads/main')).toBe(head) }) })