|
| 1 | +import assert from 'assert'; |
| 2 | +import { startProcess, readProcessOutput } from '../dist/tools/improved-process-tools.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Proper test for read_process_output on completed processes |
| 6 | + * |
| 7 | + * This test should: |
| 8 | + * - FAIL when the bug exists (current behavior) |
| 9 | + * - PASS when the bug is fixed (desired behavior) |
| 10 | + */ |
| 11 | +async function testReadCompletedProcessOutput() { |
| 12 | + console.log('Testing read_process_output on completed process...'); |
| 13 | + |
| 14 | + // Start echo command with delay, but timeout before echo happens |
| 15 | + const startResult = await startProcess({ |
| 16 | + // Cross-platform delay + output using Node |
| 17 | + command: 'node -e "setTimeout(() => console.log(\'SUCCESS MESSAGE\'), 1000)"', |
| 18 | + timeout_ms: 500 // Returns before the output happens |
| 19 | + }); |
| 20 | + |
| 21 | + // Extract PID |
| 22 | + const pidMatch = startResult.content[0].text.match(/Process started with PID (\d+)/); |
| 23 | + assert(pidMatch, 'Should get PID from start_process'); |
| 24 | + const pid = parseInt(pidMatch[1]); |
| 25 | + |
| 26 | + // Wait for the actual command to complete |
| 27 | + await new Promise(resolve => setTimeout(resolve, 2000)); |
| 28 | + |
| 29 | + // Try to read the output - this should work when fixed |
| 30 | + const readResult = await readProcessOutput({ pid, timeout_ms: 1000 }); |
| 31 | + |
| 32 | + // ASSERT: Should be able to read from completed process |
| 33 | + assert(!readResult.isError, |
| 34 | + 'Should be able to read from completed process without error'); |
| 35 | + |
| 36 | + // ASSERT: Should contain the echo output |
| 37 | + assert(readResult.content[0].text.includes('SUCCESS MESSAGE'), |
| 38 | + 'Should contain the echo output from completed process'); |
| 39 | + |
| 40 | + console.log('✅ Successfully read from completed process'); |
| 41 | + console.log('✅ Retrieved echo output:', readResult.content[0].text); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Test immediate completion scenario |
| 46 | + */ |
| 47 | +async function testImmediateCompletion() { |
| 48 | + console.log('Testing immediate completion...'); |
| 49 | + |
| 50 | + const startResult = await startProcess({ |
| 51 | + command: 'node -e "console.log(\'IMMEDIATE OUTPUT\')"', |
| 52 | + timeout_ms: 2000 |
| 53 | + }); |
| 54 | + |
| 55 | + // Extract PID |
| 56 | + const pidMatch = startResult.content[0].text.match(/Process started with PID (\d+)/); |
| 57 | + assert(pidMatch, 'Should get PID from start_process'); |
| 58 | + const pid = parseInt(pidMatch[1]); |
| 59 | + |
| 60 | + // Small delay to ensure process completed |
| 61 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 62 | + |
| 63 | + // Should be able to read from immediately completed process |
| 64 | + const readResult = await readProcessOutput({ pid, timeout_ms: 1000 }); |
| 65 | + |
| 66 | + assert(!readResult.isError, |
| 67 | + 'Should be able to read from immediately completed process'); |
| 68 | + |
| 69 | + assert(readResult.content[0].text.includes('IMMEDIATE OUTPUT'), |
| 70 | + 'Should contain immediate output from completed process'); |
| 71 | + |
| 72 | + console.log('✅ Successfully read from immediately completed process'); |
| 73 | +} |
| 74 | + |
| 75 | +// Run tests |
| 76 | +async function runTests() { |
| 77 | + try { |
| 78 | + await testReadCompletedProcessOutput(); |
| 79 | + await testImmediateCompletion(); |
| 80 | + console.log('\n🎉 All tests passed - read_process_output works on completed processes!'); |
| 81 | + return true; |
| 82 | + } catch (error) { |
| 83 | + console.log('\n❌ Test failed:', error.message); |
| 84 | + console.log('\n💡 This indicates the bug still exists:'); |
| 85 | + console.log(' read_process_output cannot read from completed processes'); |
| 86 | + console.log(' Expected behavior: Should return completion info and final output'); |
| 87 | + return false; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +runTests() |
| 92 | + .then(success => { |
| 93 | + process.exit(success ? 0 : 1); |
| 94 | + }) |
| 95 | + .catch(error => { |
| 96 | + console.error('Test error:', error); |
| 97 | + process.exit(1); |
| 98 | + }); |
0 commit comments