import { describe, expect, it, vi } from 'vitest'; import type { GitHubDeliveryState, GitHubDeliveryOperation } from '@wishforge/contracts'; import type { PortableCheckpoint } from '@wishforge/runtime'; import { advanceGitHubDelivery, restoreMergedGitHubSource } from '../src/github-delivery.js'; const candidate = 'a'.repeat(40), base = 'b'.repeat(40), merged = 'c'.repeat(40); const operation: GitHubDeliveryOperation = { id: 'gh-operation', revision: 1, status: 'open', headBranch: `wishforge/run/${candidate}`, baseBranch: 'main', expectedHeadSha: candidate, expectedBaseSha: base, mergeApproved: false, mergeAttempted: false, pullRequestUrl: 'https://github.com/fixture/repo/pull/1', }; function input(state: GitHubDeliveryState, request = vi.fn()) { return { state, api: { request }, git: { pushReviewBranch: vi.fn(async () => operation.headBranch) }, runId: 'run', revision: 1, leaseToken: 1, repository: '/owned/integration.git', candidateCommit: candidate, verifiedCommit: candidate, expectedBaseCommit: base, assertIntegrationGate: vi.fn(async () => {}), refreshGitCredential: vi.fn(async () => {}), }; } describe('independent GitHub worker delivery path', () => { it('never falls back to target-branch push when GitHub is unconfigured', async () => { const f = input({ required: true, enabled: false, reason: 'GITHUB_UNCONFIGURED' }); expect(await advanceGitHubDelivery(f)).toMatchObject({ kind: 'waiting', reason: 'github_unconfigured', detail: 'GITHUB_UNCONFIGURED', }); expect(f.git.pushReviewBranch).not.toHaveBeenCalled(); expect(f.api.request).not.toHaveBeenCalled(); expect(await advanceGitHubDelivery(input({ required: false, enabled: false }))).toEqual({ kind: 'standard_git' }); }); it('pushes only an immutable run review branch behind the integration gate then waits for approval', async () => { const events: string[] = []; const state = { required: true, enabled: true }; const request = vi.fn(async (route: string) => { events.push(route.endsWith('/create') ? 'create' : 'advance'); return { ...state, operation, reason: 'github_merge_approval' }; }); const f = input(state, request); f.assertIntegrationGate = vi.fn(async () => { events.push('gate'); }); f.refreshGitCredential = vi.fn(async () => { events.push('credential'); }); f.git.pushReviewBranch = vi.fn(async () => { events.push('review-branch'); return operation.headBranch; }); expect(await advanceGitHubDelivery(f)).toMatchObject({ kind: 'waiting', operationId: operation.id, reason: 'github_review', detail: expect.stringContaining('github_merge_approval'), }); expect(events).toEqual(['gate', 'credential', 'review-branch', 'gate', 'create', 'gate', 'advance']); expect(f.git.pushReviewBranch).toHaveBeenCalledWith({ repository: '/owned/integration.git', runId: 'run', candidateCommit: candidate, verifiedCommit: candidate, }); }); it('reconciles an existing operation without recreating a review branch or PR', async () => { const f = input( { required: true, enabled: true, operation }, vi.fn(async () => ({ required: true, enabled: true, operation: { ...operation, status: 'reverification_required', mergedCommitSha: merged }, })), ); expect(await advanceGitHubDelivery(f)).toMatchObject({ kind: 'merged', sourceCommit: merged, baseCommit: base, operationId: operation.id, }); expect(f.git.pushReviewBranch).not.toHaveBeenCalled(); expect(f.api.request).toHaveBeenCalledTimes(1); expect(f.api.request.mock.calls[0][0]).toMatch(/github\/advance$/); }); it('requires a fresh human workspace gate before any remote write', async () => { const f = input({ required: true, enabled: true }); f.assertIntegrationGate = vi.fn(async () => { throw new Error('human workspace input is stale'); }); await expect(advanceGitHubDelivery(f)).rejects.toThrow('stale'); expect(f.git.pushReviewBranch).not.toHaveBeenCalled(); expect(f.api.request).not.toHaveBeenCalled(); }); it('restores the actual merged SHA into a fresh workspace while preserving the original review base', async () => { const checkpoint: PortableCheckpoint = { schemaVersion: 1, runId: 'run', provider: 'codex', sourceCommit: merged, baseCommit: base, patch: '', patchSha256: 'd'.repeat(64), createdAt: new Date().toISOString(), }; const git = { create: vi.fn(async () => ({ workspace: '/owned/merged-fixture', baseCommit: merged })), checkpoint: vi.fn(async () => checkpoint), }; const result = await restoreMergedGitHubSource({ git, repositoryUrl: 'https://github.com/fixture/repo.git', targetBranch: 'main', root: '/owned/verifier', runId: 'run', provider: 'codex', merged: { kind: 'merged', sourceCommit: merged, baseCommit: base, operationId: operation.id }, assertIntegrationGate: async () => {}, refreshGitCredential: async () => {}, }); expect(git.create).toHaveBeenCalledWith( expect.objectContaining({ baseCommit: merged, targetBranch: 'main', runId: expect.stringMatching(/^run-merged-/), }), ); expect(result).toEqual({ workspace: '/owned/merged-fixture', checkpoint }); expect(git.checkpoint).toHaveBeenCalledWith('/owned/merged-fixture', base, 'run', 'codex'); }); });