|
| 1 | +/** |
| 2 | + * WorkflowExecutor CodeRabbit invocation tests |
| 3 | + * |
| 4 | + * Regression guards for #763/#764: |
| 5 | + * - non-zero CodeRabbit subprocess exits must not pass silently |
| 6 | + * - workflow-executor WSL probing mirrors Layer 2 behavior |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +const childProcess = require('child_process'); |
| 12 | +const os = require('os'); |
| 13 | +const path = require('path'); |
| 14 | + |
| 15 | +const { WorkflowExecutor } = require('../../../.aiox-core/core/orchestration/workflow-executor'); |
| 16 | + |
| 17 | +const expectedDefaultCliPath = path.join(os.homedir(), '/.local/bin/coderabbit'); |
| 18 | + |
| 19 | +describe('WorkflowExecutor CodeRabbit analysis', () => { |
| 20 | + let originalPlatform; |
| 21 | + let spawnSyncSpy; |
| 22 | + let execSpy; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + originalPlatform = process.platform; |
| 26 | + spawnSyncSpy = jest.spyOn(childProcess, 'spawnSync').mockImplementation((cmd) => { |
| 27 | + if (cmd === 'wsl') { |
| 28 | + return { status: 0, stdout: 'Ubuntu\n', stderr: '' }; |
| 29 | + } |
| 30 | + return { status: 0, stdout: '', stderr: '' }; |
| 31 | + }); |
| 32 | + execSpy = jest.spyOn(childProcess, 'exec').mockImplementation((command, options, callback) => { |
| 33 | + callback(null, '', ''); |
| 34 | + return { pid: 1234 }; |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + Object.defineProperty(process, 'platform', { value: originalPlatform }); |
| 40 | + spawnSyncSpy.mockRestore(); |
| 41 | + execSpy.mockRestore(); |
| 42 | + }); |
| 43 | + |
| 44 | + const setPlatform = (value) => { |
| 45 | + Object.defineProperty(process, 'platform', { value }); |
| 46 | + }; |
| 47 | + |
| 48 | + const runAnalysis = (config = {}, projectRoot = process.cwd()) => { |
| 49 | + const executor = new WorkflowExecutor(projectRoot, { saveState: false, debug: false }); |
| 50 | + return executor.runCodeRabbitAnalysis({ |
| 51 | + self_healing: { timeout_minutes: 1 }, |
| 52 | + ...config, |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + it('returns an informative failure when CodeRabbit exits non-zero', async () => { |
| 57 | + setPlatform('darwin'); |
| 58 | + execSpy.mockImplementation((command, options, callback) => { |
| 59 | + const error = new Error('Command failed'); |
| 60 | + error.code = 137; |
| 61 | + error.stdout = ''; |
| 62 | + error.stderr = ''; |
| 63 | + callback(error, '', ''); |
| 64 | + return { pid: 1234 }; |
| 65 | + }); |
| 66 | + |
| 67 | + const result = await runAnalysis({ installation_mode: 'native' }); |
| 68 | + |
| 69 | + expect(result.success).toBe(false); |
| 70 | + expect(result.error).toMatch(/CodeRabbit CLI exited with code 137/); |
| 71 | + expect(result.error).toContain('stdout:'); |
| 72 | + expect(result.error).toContain('stderr:'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('surfaces a clear error when WSL binary is missing (ENOENT)', async () => { |
| 76 | + setPlatform('darwin'); |
| 77 | + spawnSyncSpy.mockImplementation((cmd) => { |
| 78 | + if (cmd === 'wsl') { |
| 79 | + return { error: Object.assign(new Error('ENOENT'), { code: 'ENOENT' }), status: null }; |
| 80 | + } |
| 81 | + return { status: 0, stdout: '', stderr: '' }; |
| 82 | + }); |
| 83 | + |
| 84 | + const result = await runAnalysis({ installation_mode: 'wsl' }); |
| 85 | + |
| 86 | + expect(spawnSyncSpy).toHaveBeenCalledWith('wsl', ['-l'], expect.any(Object)); |
| 87 | + expect(result.success).toBe(false); |
| 88 | + expect(result.error).toMatch(/CodeRabbit CLI requires WSL/); |
| 89 | + expect(result.error).toMatch(/wsl --install/); |
| 90 | + expect(result.error).toMatch(/installation-troubleshooting/); |
| 91 | + expect(execSpy).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('surfaces a clear error when WSL has no usable distribution', async () => { |
| 95 | + setPlatform('darwin'); |
| 96 | + spawnSyncSpy.mockImplementation((cmd) => { |
| 97 | + if (cmd === 'wsl') { |
| 98 | + return { status: 1, stdout: '', stderr: 'no distros\n' }; |
| 99 | + } |
| 100 | + return { status: 0, stdout: '', stderr: '' }; |
| 101 | + }); |
| 102 | + |
| 103 | + const result = await runAnalysis({ installation_mode: 'wsl' }); |
| 104 | + |
| 105 | + expect(spawnSyncSpy).toHaveBeenCalledWith('wsl', ['-l'], expect.any(Object)); |
| 106 | + expect(result.success).toBe(false); |
| 107 | + expect(result.error).toMatch(/CodeRabbit CLI requires WSL/); |
| 108 | + expect(execSpy).not.toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('does not probe WSL when installation_mode=native on Windows', async () => { |
| 112 | + setPlatform('win32'); |
| 113 | + |
| 114 | + await runAnalysis({ installation_mode: 'native' }); |
| 115 | + |
| 116 | + expect(execSpy).toHaveBeenCalled(); |
| 117 | + const command = execSpy.mock.calls[0][0]; |
| 118 | + expect(command).toBe(`${expectedDefaultCliPath} --prompt-only -t uncommitted`); |
| 119 | + const wslProbeCalls = spawnSyncSpy.mock.calls.filter((c) => c[0] === 'wsl'); |
| 120 | + expect(wslProbeCalls).toHaveLength(0); |
| 121 | + }); |
| 122 | + |
| 123 | + it('does not probe WSL for default native mode on macOS', async () => { |
| 124 | + setPlatform('darwin'); |
| 125 | + |
| 126 | + await runAnalysis(); |
| 127 | + |
| 128 | + expect(execSpy).toHaveBeenCalled(); |
| 129 | + const command = execSpy.mock.calls[0][0]; |
| 130 | + expect(command).toBe(`${expectedDefaultCliPath} --prompt-only -t uncommitted`); |
| 131 | + const wslProbeCalls = spawnSyncSpy.mock.calls.filter((c) => c[0] === 'wsl'); |
| 132 | + expect(wslProbeCalls).toHaveLength(0); |
| 133 | + }); |
| 134 | +}); |
0 commit comments