77
88import { describe , it , expect , beforeEach , vi , afterEach } from 'vitest' ;
99import { spawn } from 'child_process' ;
10- import { unlinkSync , existsSync } from 'fs' ;
11- import { join } from 'path' ;
12- import { tmpdir } from 'os' ;
10+ import { createWriteStream } from 'fs' ;
1311import { DefaultCommandRunner } from '../../src/execution/commandRunner.js' ;
1412import { ProgressReporter } from '../../src/execution/progressReporter.js' ;
1513import { MockLogger } from '../utils/MockLogger.js' ;
@@ -22,6 +20,13 @@ vi.mock('child_process', () => {
2220 } ;
2321} ) ;
2422
23+ // Mock fs.createWriteStream
24+ vi . mock ( 'fs' , ( ) => {
25+ return {
26+ createWriteStream : vi . fn ( ) ,
27+ } ;
28+ } ) ;
29+
2530describe ( 'DefaultCommandRunner' , ( ) => {
2631 let commandRunner : DefaultCommandRunner ;
2732 let mockLogger : MockLogger ;
@@ -490,28 +495,32 @@ describe('DefaultCommandRunner', () => {
490495 } ) ;
491496
492497 it ( 'should write output to file when outputFilePath provided' , async ( ) => {
493- const outputFile = join ( tmpdir ( ) , `test-output-${ Date . now ( ) } .txt` ) ;
498+ const mockWriteStream = {
499+ write : vi . fn ( ) ,
500+ end : vi . fn ( ) ,
501+ } ;
502+ vi . mocked ( createWriteStream ) . mockReturnValue (
503+ mockWriteStream as unknown as ReturnType < typeof createWriteStream >
504+ ) ;
494505
495- try {
496- const promise = commandRunner . execute ( 'echo' , [ 'test output' ] , {
497- outputFilePath : outputFile ,
498- commandName : 'Test Command' ,
499- } ) ;
506+ const promise = commandRunner . execute ( 'echo' , [ 'test output' ] , {
507+ outputFilePath : '/tmp/test-output.txt' ,
508+ commandName : 'Test Command' ,
509+ } ) ;
500510
501- setTimeout ( ( ) => {
502- stdoutHandlers . forEach ( handler => handler ( Buffer . from ( 'test output\n' ) ) ) ;
503- exitHandlers . forEach ( handler => handler ( 0 , null ) ) ;
504- } , 10 ) ;
511+ setTimeout ( ( ) => {
512+ stdoutHandlers . forEach ( handler => handler ( Buffer . from ( 'test output\n' ) ) ) ;
513+ exitHandlers . forEach ( handler => handler ( 0 , null ) ) ;
514+ } , 10 ) ;
505515
506- await promise ;
516+ await promise ;
507517
508- // File should exist and contain output
509- expect ( existsSync ( outputFile ) ) . toBe ( true ) ;
510- } finally {
511- if ( existsSync ( outputFile ) ) {
512- unlinkSync ( outputFile ) ;
513- }
514- }
518+ // Verify createWriteStream was called with the output file path
519+ expect ( createWriteStream ) . toHaveBeenCalledWith ( '/tmp/test-output.txt' ) ;
520+ // Verify output was written to the stream
521+ expect ( mockWriteStream . write ) . toHaveBeenCalledWith ( 'test output\n' ) ;
522+ // Verify stream was closed
523+ expect ( mockWriteStream . end ) . toHaveBeenCalled ( ) ;
515524 } ) ;
516525
517526 it ( 'should handle timeout' , async ( ) => {
0 commit comments