|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +// Mock child_process before importing the service so execFile is intercepted. |
| 4 | +// Mock util so promisify(execFile) === execFile (the mock). |
| 5 | +vi.mock('child_process', () => ({ execFile: vi.fn() })); |
| 6 | +vi.mock('util', () => ({ promisify: (fn: unknown) => fn })); |
| 7 | + |
| 8 | +import { execFile } from 'child_process'; |
| 9 | +import { PRStatusChecker, type TrackedPR } from '../../../src/services/pr-status-checker.js'; |
| 10 | + |
| 11 | +const mockExecFile = execFile as unknown as ReturnType<typeof vi.fn>; |
| 12 | + |
| 13 | +const fakePR: TrackedPR = { |
| 14 | + featureId: 'feat-1', |
| 15 | + projectPath: '/fake/project', |
| 16 | + prNumber: 42, |
| 17 | + prUrl: 'https://github.com/owner/repo/pull/42', |
| 18 | + branchName: 'feature/test', |
| 19 | + lastCheckedAt: Date.now(), |
| 20 | + reviewState: 'pending', |
| 21 | + iterationCount: 0, |
| 22 | +}; |
| 23 | + |
| 24 | +describe('PRStatusChecker', () => { |
| 25 | + let checker: PRStatusChecker; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + checker = new PRStatusChecker(); |
| 29 | + mockExecFile.mockReset(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('fetchJobLogs', () => { |
| 33 | + it('returns null for non-GHA ciProvider without calling execFile', async () => { |
| 34 | + const result = await checker.fetchJobLogs(fakePR, 12345, 'other'); |
| 35 | + expect(result).toBeNull(); |
| 36 | + expect(mockExecFile).not.toHaveBeenCalled(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns null for unrecognized ciProvider', async () => { |
| 40 | + const result = await checker.fetchJobLogs(fakePR, 12345, 'jenkins'); |
| 41 | + expect(result).toBeNull(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('calls gh api for GHA job logs', async () => { |
| 45 | + const rawLogs = [ |
| 46 | + '2024-01-01T00:00:00.0000000Z ##[group]Run npm test', |
| 47 | + '2024-01-01T00:00:01.0000000Z npm test output line 1', |
| 48 | + '2024-01-01T00:00:02.0000000Z ##[error]Tests failed: 3 failures', |
| 49 | + '2024-01-01T00:00:03.0000000Z ##[endgroup]', |
| 50 | + ].join('\n'); |
| 51 | + |
| 52 | + mockExecFile.mockResolvedValueOnce({ stdout: rawLogs, stderr: '' }); |
| 53 | + |
| 54 | + const result = await checker.fetchJobLogs(fakePR, 9999, 'github-actions'); |
| 55 | + |
| 56 | + expect(mockExecFile).toHaveBeenCalledWith( |
| 57 | + 'gh', |
| 58 | + ['api', 'repos/{owner}/{repo}/actions/jobs/9999/logs'], |
| 59 | + expect.objectContaining({ cwd: fakePR.projectPath }) |
| 60 | + ); |
| 61 | + expect(result).not.toBeNull(); |
| 62 | + expect(result).toContain('##[group]Run npm test'); |
| 63 | + expect(result).toContain('##[error]Tests failed: 3 failures'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('defaults to github-actions ciProvider when not specified', async () => { |
| 67 | + mockExecFile.mockResolvedValueOnce({ stdout: 'some log', stderr: '' }); |
| 68 | + const result = await checker.fetchJobLogs(fakePR, 1); |
| 69 | + expect(mockExecFile).toHaveBeenCalled(); |
| 70 | + expect(result).not.toBeNull(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns null on gh api failure', async () => { |
| 74 | + mockExecFile.mockRejectedValueOnce(new Error('gh: not found')); |
| 75 | + const result = await checker.fetchJobLogs(fakePR, 1, 'github-actions'); |
| 76 | + expect(result).toBeNull(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('truncates to last 200 lines of failing step', async () => { |
| 80 | + // Build a group containing 300 lines after the ##[error] marker |
| 81 | + const logLines: string[] = []; |
| 82 | + logLines.push('2024-01-01T00:00:00.0000000Z ##[group]Run big step'); |
| 83 | + logLines.push('2024-01-01T00:00:01.0000000Z ##[error]Something broke'); |
| 84 | + for (let i = 0; i < 300; i++) { |
| 85 | + logLines.push(`2024-01-01T00:00:02.0000000Z output line ${i}`); |
| 86 | + } |
| 87 | + logLines.push('2024-01-01T00:00:03.0000000Z ##[endgroup]'); |
| 88 | + |
| 89 | + mockExecFile.mockResolvedValueOnce({ stdout: logLines.join('\n'), stderr: '' }); |
| 90 | + |
| 91 | + const result = await checker.fetchJobLogs(fakePR, 1, 'github-actions'); |
| 92 | + expect(result).not.toBeNull(); |
| 93 | + |
| 94 | + const resultLines = result!.split('\n'); |
| 95 | + expect(resultLines.length).toBeLessThanOrEqual(200); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns last 200 lines when no ##[error] marker found', async () => { |
| 99 | + const logLines: string[] = []; |
| 100 | + for (let i = 0; i < 500; i++) { |
| 101 | + logLines.push(`2024-01-01T00:00:00.0000000Z output line ${i}`); |
| 102 | + } |
| 103 | + |
| 104 | + mockExecFile.mockResolvedValueOnce({ stdout: logLines.join('\n'), stderr: '' }); |
| 105 | + |
| 106 | + const result = await checker.fetchJobLogs(fakePR, 1, 'github-actions'); |
| 107 | + expect(result).not.toBeNull(); |
| 108 | + const resultLines = result!.split('\n'); |
| 109 | + expect(resultLines.length).toBeLessThanOrEqual(200); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('fetchFailedChecks — GHA fallback', () => { |
| 114 | + it('uses job logs as fallback when check run output is empty', async () => { |
| 115 | + const checkRunsJson = JSON.stringify([ |
| 116 | + { |
| 117 | + id: 777, |
| 118 | + name: 'test / unit', |
| 119 | + status: 'completed', |
| 120 | + conclusion: 'failure', |
| 121 | + output: { title: null, summary: null, text: null }, |
| 122 | + }, |
| 123 | + ]); |
| 124 | + |
| 125 | + const jobLogContent = [ |
| 126 | + '2024-01-01T00:00:00.0000000Z ##[group]Run unit tests', |
| 127 | + '2024-01-01T00:00:01.0000000Z ##[error]assertion failed', |
| 128 | + '2024-01-01T00:00:02.0000000Z ##[endgroup]', |
| 129 | + ].join('\n'); |
| 130 | + |
| 131 | + // First call: check-runs API; second call: actions/jobs logs API |
| 132 | + mockExecFile.mockResolvedValueOnce({ stdout: checkRunsJson, stderr: '' }); |
| 133 | + mockExecFile.mockResolvedValueOnce({ stdout: jobLogContent, stderr: '' }); |
| 134 | + |
| 135 | + const results = await checker.fetchFailedChecks(fakePR, 'abc123', 'github-actions'); |
| 136 | + |
| 137 | + expect(results).toHaveLength(1); |
| 138 | + expect(results[0].name).toBe('test / unit'); |
| 139 | + expect(results[0].output).toContain('##[error]assertion failed'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('does not fetch job logs for non-GHA provider', async () => { |
| 143 | + const checkRunsJson = JSON.stringify([ |
| 144 | + { |
| 145 | + id: 888, |
| 146 | + name: 'build', |
| 147 | + status: 'completed', |
| 148 | + conclusion: 'failure', |
| 149 | + output: { title: null, summary: null, text: null }, |
| 150 | + }, |
| 151 | + ]); |
| 152 | + |
| 153 | + mockExecFile.mockResolvedValueOnce({ stdout: checkRunsJson, stderr: '' }); |
| 154 | + |
| 155 | + const results = await checker.fetchFailedChecks(fakePR, 'abc123', 'other'); |
| 156 | + |
| 157 | + expect(mockExecFile).toHaveBeenCalledTimes(1); |
| 158 | + expect(results).toHaveLength(1); |
| 159 | + expect(results[0].output).toBe(''); |
| 160 | + }); |
| 161 | + |
| 162 | + it('skips fallback when API output is sufficient', async () => { |
| 163 | + const richSummary = 'A'.repeat(200); |
| 164 | + const checkRunsJson = JSON.stringify([ |
| 165 | + { |
| 166 | + id: 999, |
| 167 | + name: 'lint', |
| 168 | + status: 'completed', |
| 169 | + conclusion: 'failure', |
| 170 | + output: { title: 'Lint failed', summary: richSummary, text: null }, |
| 171 | + }, |
| 172 | + ]); |
| 173 | + |
| 174 | + mockExecFile.mockResolvedValueOnce({ stdout: checkRunsJson, stderr: '' }); |
| 175 | + |
| 176 | + const results = await checker.fetchFailedChecks(fakePR, 'def456', 'github-actions'); |
| 177 | + |
| 178 | + expect(mockExecFile).toHaveBeenCalledTimes(1); |
| 179 | + expect(results[0].output).toContain('Lint failed'); |
| 180 | + }); |
| 181 | + }); |
| 182 | +}); |
0 commit comments