import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; import { generateKeyPairSync, createPublicKey, verify } from 'node:crypto'; import { mkdtemp, rm, writeFile } from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import type { Actor, Run } from '@wishforge/contracts'; import { GitHubApp, operationMarker, type AppConfiguration } from '../packages/github/src/index.js'; import { GitHubIntegration } from '../apps/api/src/domain/github.js'; import { ControlPlane } from '../apps/api/src/domain/control-plane.js'; import { Connections } from '../apps/api/src/domain/connections.js'; import { MemoryStore } from './helpers/memory-store.js'; const owner: Actor = { tenantId: 'alpha', userId: 'owner', role: 'owner', kind: 'human' }; const worker: Actor = { ...owner, userId: 'worker', kind: 'worker' }, verifier: Actor = { ...owner, userId: 'verifier', kind: 'verifier' }; const head = 'a'.repeat(40), base = 'b'.repeat(40), final = 'c'.repeat(40); let directory: string, privateKey: string, config: AppConfiguration, configFile: string; const response = (value: unknown, status = 200) => new Response(JSON.stringify(value), { status, headers: { 'content-type': 'application/json' } }); beforeAll(async () => { directory = await mkdtemp(path.join(os.tmpdir(), 'wishforge-github-test-')); privateKey = generateKeyPairSync('rsa', { modulusLength: 2048 }).privateKey.export({ format: 'pem', type: 'pkcs8' }).toString(); await writeFile(path.join(directory, 'app.pem'), privateKey, { mode: 0o600 }); }); afterAll(async () => { await rm(directory, { recursive: true, force: true }); }); beforeEach(async () => { configFile = path.join(directory, `config-${Date.now()}-${Math.random()}.json`); config = { appId: '12345', privateKeyFile: path.join(directory, 'app.pem'), tenantInstallations: [{ tenantId: 'alpha', installationId: 9001, repositories: [{ owner: 'wishforge-fixture', name: 'catalog', repositoryId: 101, allowedBaseBranches: ['main'] }] }] }; await writeFile(configFile, JSON.stringify(config)); }); function fixture() { const calls: { route: string; method: string; body: any; authorization: string }[] = []; const state = { pr: undefined as any, createTimeout: false, mergeTimeout: false, baseSha: base, broadenToken: false, mergePending: false }; const fetcher = vi.fn(async (url: string | URL | Request, init?: RequestInit) => { const route = new URL(String(url)).pathname, method = init?.method ?? 'GET', body = init?.body ? JSON.parse(String(init.body)) : undefined; const authorization = String((init?.headers as Record)?.Authorization ?? ''); calls.push({ route, method, body, authorization }); if (route === '/app/installations/9001/access_tokens') return response({ token: 'fixture-installation-token-opaque-long-value', expires_at: new Date(Date.now() + 3600000).toISOString(), permissions: { contents: body.permissions.contents, pull_requests: body.permissions.pull_requests, metadata: 'read' }, repositories: [{ id: 101, full_name: 'wishforge-fixture/catalog' }, ...(state.broadenToken ? [{ id: 102, full_name: 'wishforge-fixture/other' }] : [])] }); if (route.endsWith('/git/ref/heads/main')) return response({ object: { type: 'commit', sha: state.baseSha } }); if (route.includes('/git/ref/heads/')) return response({ object: { type: 'commit', sha: head } }); if (route.endsWith('/pulls') && method === 'GET') return response(state.pr ? [state.pr] : []); if (route.endsWith('/pulls') && method === 'POST') { state.pr = { number: 7, html_url: 'https://github.com/wishforge-fixture/catalog/pull/7', state: 'open', head: { ref: body.head, sha: head, repo: { id: 101 } }, base: { ref: body.base, sha: base, repo: { id: 101 } }, merged: false, merged_at: null, merge_commit_sha: 'd'.repeat(40), draft: false, mergeable: true, mergeable_state: 'clean', body: body.body }; if (state.createTimeout) throw new Error('fixture network timeout after creation'); return response(state.pr, 201); } if (route.endsWith('/merge-async') && method === 'PUT') { if (!state.mergePending) Object.assign(state.pr, { state: 'closed', merged: true, merged_at: new Date().toISOString(), merge_commit_sha: final }); if (state.mergeTimeout) throw new Error('fixture timeout after merge'); return response({ status: state.mergePending ? 'pending' : 'merged', details: { uuid: 'fixture-request-id', sha: final } }, state.mergePending ? 202 : 200); } if (route.endsWith('/pulls/7')) return response(state.pr); throw new Error(`Unexpected fixture endpoint ${method} ${route}`); }) as unknown as typeof fetch; return { state, calls, app: new GitHubApp({ WF_GITHUB_APP_CONFIG_FILE: configFile }, fetcher), fetcher }; } async function domain(f = fixture()) { const db = new MemoryStore(); db.seed('alpha'); db.seed('beta'); const env = { NODE_ENV: 'test', WF_VAULT_KEY: 'a'.repeat(64), WF_GITHUB_APP_CONFIG_FILE: configFile }; const control = new ControlPlane(db, env); const connections = new Connections(db, env); await connections.create(owner, { provider: 'codex', label: 'fixture', apiKey: 'fixture-not-real' }); const project = await control.createProject(owner, { name: 'Catalog', repositoryUrl: 'https://github.com/wishforge-fixture/catalog.git' }); const submitted = await control.submit(owner, project.id, { goal: 'Add catalog search', acceptanceCriteria: ['Search returns exact rows'] }, 'github-test-submit'); await db.transaction('alpha', async tx => { const run = (await tx.get('run', submitted.run_id))!; run.verifiedCommit = head; run.sourceCommit = head; await tx.put('run', run); }); const github = new GitHubIntegration(db, env, f.app); const input = { operationId: 'fixture-operation', title: 'Add search', body: 'Protected command checks passed.', headBranch: `wishforge/${submitted.run_id}`, baseBranch: 'main', expectedHeadSha: head, expectedBaseSha: base }; return { ...f, db, control, github, project, runId: submitted.run_id, input }; } describe('GitHub App repository and credential contracts', () => { it('uses operator tenant mapping, repository-only token permissions and a short RS256 App JWT', async () => { const { app, calls } = fixture(); const scope = await app.scope('alpha', 'https://github.com/wishforge-fixture/catalog.git'); await app.token(scope, 'read'); await app.token(scope, 'write'); expect(calls[0].body).toEqual({ repository_ids: [101], permissions: { contents: 'read', pull_requests: 'read' } }); expect(calls[1].body.permissions.contents).toBe('write'); const jwt = calls[0].authorization.slice(7), [encodedHeader, encodedBody, signature] = jwt.split('.'); const claims = JSON.parse(Buffer.from(encodedBody, 'base64url').toString()); expect(JSON.parse(Buffer.from(encodedHeader, 'base64url').toString()).alg).toBe('RS256'); expect(claims.exp - claims.iat).toBe(600); expect(claims.iss).toBe('12345'); expect(verify('RSA-SHA256', Buffer.from(`${encodedHeader}.${encodedBody}`), createPublicKey(privateKey), Buffer.from(signature, 'base64url'))).toBe(true); await expect(app.scope('beta', 'https://github.com/wishforge-fixture/catalog')).rejects.toMatchObject({ code: 'GITHUB_REPOSITORY_DENIED' }); await expect(app.scope('alpha', 'https://attacker.invalid/wishforge-fixture/catalog')).rejects.toMatchObject({ code: 'GITHUB_REPOSITORY_DENIED' }); }); it('refuses broader token repository scope and altered API destination', async () => { const f = fixture(); const scope = await f.app.scope('alpha', 'https://github.com/wishforge-fixture/catalog'); f.state.broadenToken = true; await expect(f.app.token(scope, 'read')).rejects.toMatchObject({ code: 'GITHUB_TOKEN_SCOPE_INVALID' }); await expect(f.app.token({ ...scope, apiBaseUrl: 'https://attacker.invalid' }, 'read')).rejects.toMatchObject({ code: 'GITHUB_SCOPE_CHANGED' }); }); it('reports explicit unconfigured status without consulting gh caches or sending requests', async () => { const { db, project } = await domain(); expect(await new GitHubIntegration(db, {}).status(owner, project.id)).toMatchObject({ configured: false, enabled: false, reason: 'GITHUB_UNCONFIGURED' }); }); it('only issues builder read access and verifier write access for the live owned lease', async () => { const f = await domain(); const builderLease = await f.control.lease(worker, f.runId, 'builder'); await f.github.credential(worker, f.project, builderLease); expect(f.calls.at(-1)!.body.permissions.contents).toBe('read'); await expect(f.github.credential(verifier, f.project, builderLease)).rejects.toMatchObject({ code: 'LEASE_IDENTITY_MISMATCH' }); await f.control.releaseLease(worker, f.runId, builderLease.lease!.token); const verifiedLease = await f.control.lease(verifier, f.runId, 'verifier'); expect(await f.github.credential(verifier, f.project, verifiedLease)).toMatchObject({ username: 'x-access-token' }); expect(f.calls.at(-1)!.body.permissions.contents).toBe('write'); }); }); describe('durable PR operations and final commit validation', () => { it('exposes a lease-scoped verifier path that waits for approval and preserves the lease for final-SHA reverification', async () => { const f = await domain(); const lease = await f.control.lease(verifier, f.runId, 'verifier'); await f.control.checkpoint(verifier, f.runId, { sourceCommit: head, baseCommit: base }, lease.lease!.token); await expect(f.github.workerState(worker, f.runId, lease.lease!.token)).rejects.toMatchObject({ code: 'FORBIDDEN' }); const state = await f.github.workerCreate(verifier, f.runId, { leaseToken: lease.lease!.token, candidateCommit: head, expectedBaseCommit: base, headBranch: `wishforge/${f.runId}/${head}` }); expect(state.operation?.expectedHeadSha).toBe(head); expect((await f.github.workerAdvance(verifier, f.runId, { leaseToken: lease.lease!.token, operationId: state.operation!.id })).reason).toBe('github_merge_approval'); expect(f.calls.filter(call => call.method === 'PUT')).toHaveLength(0); await f.github.authorizeMerge(owner, state.operation!.id, { expectedHeadSha: head, expectedBaseSha: base }); const mergedState = await f.github.workerAdvance(verifier, f.runId, { leaseToken: lease.lease!.token, operationId: state.operation!.id }); expect(mergedState.operation?.mergedCommitSha).toBe(final); expect(await f.control.heartbeat(verifier, f.runId, lease.lease!.token)).toMatchObject({ status: 'running', stage: 'verify', sourceCommit: final }); expect((await f.control.run(owner, f.runId)).verifiedCommit).toBeUndefined(); }); it('rejects caller installation IDs and hides operations from nonmembers and other tenants', async () => { const f = await domain(); await expect(f.github.createPullRequest(owner, f.runId, { ...f.input, installationId: 9002 })).rejects.toThrow(); const operation = await f.github.createPullRequest(owner, f.runId, f.input); await expect(f.github.readPullRequest({ ...owner, userId: 'viewer', role: 'viewer' }, operation.id)).rejects.toMatchObject({ code: 'NOT_FOUND' }); await expect(f.github.readPullRequest({ ...owner, tenantId: 'beta' }, operation.id)).rejects.toMatchObject({ code: 'NOT_FOUND' }); }); it('reconciles an ambiguous create with one POST and marker/head/base identity', async () => { const f = await domain(); f.state.createTimeout = true; await expect(f.github.createPullRequest(owner, f.runId, f.input)).rejects.toMatchObject({ code: 'GITHUB_OUTCOME_UNKNOWN' }); const reconciled = await f.github.createPullRequest(owner, f.runId, f.input); expect(reconciled).toMatchObject({ status: 'open', snapshot: { number: 7, mergeCommitSha: null } }); expect(f.calls.filter(c => c.method === 'POST' && c.route.endsWith('/pulls'))).toHaveLength(1); expect(f.state.pr.body).toContain(operationMarker('alpha', f.runId, f.input.operationId)); }); it('does not resend an ambiguous create even if a follow-up listing is empty', async () => { const f = await domain(); f.state.createTimeout = true; await expect(f.github.createPullRequest(owner, f.runId, f.input)).rejects.toThrow(); f.state.pr = undefined; expect((await f.github.createPullRequest(owner, f.runId, f.input)).status).toBe('unknown'); expect(f.calls.filter(c => c.method === 'POST' && c.route.endsWith('/pulls'))).toHaveLength(1); }); it('blocks unapproved merge, binds human approval, uses no bypass and invalidates old source verification on final SHA change', async () => { const f = await domain(); const operation = await f.github.createPullRequest(owner, f.runId, f.input); await expect(f.github.merge(owner, operation.id)).rejects.toMatchObject({ code: 'GITHUB_MERGE_NOT_AUTHORIZED' }); await expect(f.github.authorizeMerge({ ...owner, kind: 'connector' }, operation.id, { expectedHeadSha: head, expectedBaseSha: base })).rejects.toMatchObject({ code: 'HUMAN_APPROVAL_REQUIRED' }); await f.github.authorizeMerge(owner, operation.id, { expectedHeadSha: head, expectedBaseSha: base }); expect(await f.github.merge(owner, operation.id)).toMatchObject({ status: 'reverification_required', snapshot: { merged: true, mergeCommitSha: final } }); const run = await f.control.run(owner, f.runId); expect(run).toMatchObject({ status: 'waiting', waitingReason: 'merged_sha_reverification', sourceCommit: final }); expect(run.verifiedCommit).toBeUndefined(); expect(f.calls.find(c => c.method === 'PUT')!.body).toEqual({ sha: head, merge_method: 'merge', merge_action: 'default', bypass_rules: false }); }); it('rejects a moving target branch without a merge request', async () => { const f = await domain(); const operation = await f.github.createPullRequest(owner, f.runId, f.input); await f.github.authorizeMerge(owner, operation.id, { expectedHeadSha: head, expectedBaseSha: base }); f.state.baseSha = 'f'.repeat(40); await expect(f.github.merge(owner, operation.id)).rejects.toMatchObject({ code: 'GITHUB_REF_CHANGED' }); expect(f.calls.filter(c => c.method === 'PUT')).toHaveLength(0); }); it('reconciles a lost merge receipt and never issues a second merge', async () => { const f = await domain(); const operation = await f.github.createPullRequest(owner, f.runId, f.input); await f.github.authorizeMerge(owner, operation.id, { expectedHeadSha: head, expectedBaseSha: base }); f.state.mergeTimeout = true; await expect(f.github.merge(owner, operation.id)).rejects.toMatchObject({ code: 'GITHUB_OUTCOME_UNKNOWN' }); expect((await f.github.merge(owner, operation.id)).status).toBe('reverification_required'); expect(f.calls.filter(c => c.method === 'PUT')).toHaveLength(1); }); it('does not confuse queued merges with delivery or resend pending requests', async () => { const f = await domain(); const operation = await f.github.createPullRequest(owner, f.runId, f.input); await f.github.authorizeMerge(owner, operation.id, { expectedHeadSha: head, expectedBaseSha: base }); f.state.mergePending = true; expect((await f.github.merge(owner, operation.id)).status).toBe('merge_pending'); await f.github.merge(owner, operation.id); expect(f.calls.filter(c => c.method === 'PUT')).toHaveLength(1); expect((await f.control.run(owner, f.runId)).status).not.toBe('succeeded'); }); it('permits explicit operator preauthorization without accepting a caller policy override', async () => { config.tenantInstallations[0].repositories[0].allowAutomaticMerge = true; await writeFile(configFile, JSON.stringify(config)); const f = await domain(); const operation = await f.github.createPullRequest(owner, f.runId, f.input); expect((await f.github.merge(owner, operation.id)).status).toBe('reverification_required'); }); });