|
| 1 | +const { expect } = require('chai') |
| 2 | + |
| 3 | +const { runCommandWithOutput } = require('../../../dist/src/cli/utils/process.js') |
| 4 | + |
| 5 | +describe('runCommandWithOutput', () => { |
| 6 | + it('resolves ok:true with captured stdout, stderr and exit code 0', async () => { |
| 7 | + const result = await runCommandWithOutput('sh', ['-c', 'echo out; echo err >&2']) |
| 8 | + |
| 9 | + expect(result).to.deep.equal({ ok: true, code: 0, stdout: 'out\n', stderr: 'err\n' }) |
| 10 | + }) |
| 11 | + |
| 12 | + it('resolves ok:true with non-zero exit code', async () => { |
| 13 | + const result = await runCommandWithOutput('sh', ['-c', 'exit 2']) |
| 14 | + |
| 15 | + expect(result.ok).to.equal(true) |
| 16 | + expect(result.code).to.equal(2) |
| 17 | + }) |
| 18 | + |
| 19 | + it('resolves ok:false reason:not-found when command does not exist (ENOENT)', async () => { |
| 20 | + const result = await runCommandWithOutput('__nostream_nonexistent_cmd__', []) |
| 21 | + |
| 22 | + expect(result).to.deep.equal({ ok: false, reason: 'not-found', stdout: '', stderr: '' }) |
| 23 | + }) |
| 24 | + |
| 25 | + it('resolves ok:false reason:timeout when the process exceeds timeoutMs', async () => { |
| 26 | + const result = await runCommandWithOutput('sleep', ['10'], { timeoutMs: 100 }) |
| 27 | + |
| 28 | + expect(result).to.deep.equal({ ok: false, reason: 'timeout', stdout: '', stderr: '' }) |
| 29 | + }) |
| 30 | + |
| 31 | + it('resolves ok:false reason:signal when the process is killed by a signal', async () => { |
| 32 | + const result = await runCommandWithOutput('sh', ['-c', 'kill -9 $$']) |
| 33 | + |
| 34 | + expect(result.ok).to.equal(false) |
| 35 | + expect(result.reason).to.equal('signal') |
| 36 | + }) |
| 37 | + |
| 38 | + it('does not double-settle when ENOENT fires both error and close', async () => { |
| 39 | + const result = await runCommandWithOutput('__nostream_nonexistent_cmd__', []) |
| 40 | + |
| 41 | + expect(result.ok).to.equal(false) |
| 42 | + expect(result.reason).to.equal('not-found') |
| 43 | + }) |
| 44 | +}) |
0 commit comments