|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockExecuteTool } = vi.hoisted(() => ({ mockExecuteTool: vi.fn() })) |
| 7 | + |
| 8 | +vi.mock('@/tools', () => ({ executeTool: mockExecuteTool })) |
| 9 | + |
| 10 | +import { |
| 11 | + fetchOpenPrSnapshot, |
| 12 | + fetchPrSnapshot, |
| 13 | + validateRepositoryCoordinates, |
| 14 | +} from '@/executor/handlers/pi/github-pr' |
| 15 | + |
| 16 | +const HEAD_SHA = 'a'.repeat(40) |
| 17 | +const BASE_SHA = 'b'.repeat(40) |
| 18 | + |
| 19 | +const COORDINATES = { |
| 20 | + owner: 'octo', |
| 21 | + repo: 'demo', |
| 22 | + pullNumber: 7, |
| 23 | + githubToken: 'ghp_secret', |
| 24 | +} |
| 25 | + |
| 26 | +function snapshot(overrides: Record<string, unknown> = {}) { |
| 27 | + return { |
| 28 | + title: 'Add feature', |
| 29 | + body: 'Does the thing', |
| 30 | + html_url: 'https://github.com/octo/demo/pull/7', |
| 31 | + state: 'open', |
| 32 | + head: { sha: HEAD_SHA }, |
| 33 | + base: { sha: BASE_SHA, ref: 'staging' }, |
| 34 | + ...overrides, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +describe('fetchPrSnapshot', () => { |
| 39 | + beforeEach(() => { |
| 40 | + vi.clearAllMocks() |
| 41 | + mockExecuteTool.mockResolvedValue({ success: true, output: snapshot() }) |
| 42 | + }) |
| 43 | + |
| 44 | + it('reads the pull request without its files, using the caller-supplied token', async () => { |
| 45 | + const result = await fetchPrSnapshot(COORDINATES) |
| 46 | + |
| 47 | + expect(mockExecuteTool).toHaveBeenCalledWith( |
| 48 | + 'github_pr_v2', |
| 49 | + { |
| 50 | + owner: 'octo', |
| 51 | + repo: 'demo', |
| 52 | + pullNumber: 7, |
| 53 | + includeFiles: false, |
| 54 | + apiKey: 'ghp_secret', |
| 55 | + }, |
| 56 | + { signal: undefined } |
| 57 | + ) |
| 58 | + expect(result).toMatchObject({ headSha: HEAD_SHA, baseSha: BASE_SHA, state: 'open' }) |
| 59 | + }) |
| 60 | + |
| 61 | + it('returns a closed or merged pull request instead of throwing', async () => { |
| 62 | + // This is the whole reason the state guard lives in the wrapper: a mode that |
| 63 | + // must report "the PR closed mid-run" as a result rather than as a failure |
| 64 | + // builds on this form, so folding the guard back in here would break it. |
| 65 | + mockExecuteTool.mockResolvedValue({ success: true, output: snapshot({ state: 'closed' }) }) |
| 66 | + |
| 67 | + await expect(fetchPrSnapshot(COORDINATES)).resolves.toMatchObject({ state: 'closed' }) |
| 68 | + }) |
| 69 | + |
| 70 | + it('surfaces a failed read rather than returning an empty snapshot', async () => { |
| 71 | + mockExecuteTool.mockResolvedValue({ success: false, error: 'Not Found' }) |
| 72 | + |
| 73 | + await expect(fetchPrSnapshot(COORDINATES)).rejects.toThrow('Failed to fetch PR #7: Not Found') |
| 74 | + }) |
| 75 | + |
| 76 | + it('rejects a head SHA that is not a full commit id', async () => { |
| 77 | + mockExecuteTool.mockResolvedValue({ success: true, output: snapshot({ head: { sha: 'abc' } }) }) |
| 78 | + |
| 79 | + await expect(fetchPrSnapshot(COORDINATES)).rejects.toThrow( |
| 80 | + /head\.sha must be a full commit SHA/ |
| 81 | + ) |
| 82 | + }) |
| 83 | +}) |
| 84 | + |
| 85 | +describe('fetchOpenPrSnapshot', () => { |
| 86 | + beforeEach(() => vi.clearAllMocks()) |
| 87 | + |
| 88 | + it('passes an open pull request through', async () => { |
| 89 | + mockExecuteTool.mockResolvedValue({ success: true, output: snapshot() }) |
| 90 | + |
| 91 | + await expect(fetchOpenPrSnapshot(COORDINATES)).resolves.toMatchObject({ state: 'open' }) |
| 92 | + }) |
| 93 | + |
| 94 | + it('refuses anything that is not open', async () => { |
| 95 | + mockExecuteTool.mockResolvedValue({ success: true, output: snapshot({ state: 'closed' }) }) |
| 96 | + |
| 97 | + await expect(fetchOpenPrSnapshot(COORDINATES)).rejects.toThrow( |
| 98 | + 'PR #7 is closed; only open PRs can be reviewed' |
| 99 | + ) |
| 100 | + }) |
| 101 | +}) |
| 102 | + |
| 103 | +describe('validateRepositoryCoordinates', () => { |
| 104 | + it('accepts ordinary GitHub coordinates', () => { |
| 105 | + expect(() => validateRepositoryCoordinates(COORDINATES)).not.toThrow() |
| 106 | + }) |
| 107 | + |
| 108 | + it.each([ |
| 109 | + ['a traversal in the owner', { owner: '../octo' }], |
| 110 | + ['a traversal in the repo', { repo: '..' }], |
| 111 | + ['a slash in the repo', { repo: 'demo/evil' }], |
| 112 | + ['a non-positive pull number', { pullNumber: 0 }], |
| 113 | + ['a fractional pull number', { pullNumber: 1.5 }], |
| 114 | + ])('rejects %s before any credential is used', (_label, overrides) => { |
| 115 | + expect(() => validateRepositoryCoordinates({ ...COORDINATES, ...overrides })).toThrow( |
| 116 | + /Invalid GitHub repository coordinates/ |
| 117 | + ) |
| 118 | + }) |
| 119 | +}) |
0 commit comments