@@ -8,44 +8,48 @@ import { describe, it, vi, expect } from 'vitest';
88import { runPoc } from './poc.js' ;
99
1010describe ( 'runPoc' , ( ) => {
11- it ( 'should write file and execute it' , async ( ) => {
12- const mockFs = {
13- mkdir : vi . fn ( async ( ) => undefined ) ,
14- writeFile : vi . fn ( async ( ) => undefined ) ,
15- } ;
11+ it ( 'should execute the file at the given path' , async ( ) => {
1612 const mockPath = {
17- join : ( ... args : string [ ] ) => args . join ( '/' ) ,
13+ dirname : ( p : string ) => p . substring ( 0 , p . lastIndexOf ( '/' ) ) ,
1814 } ;
19- const mockExecAsync = vi . fn ( async ( ) => ( { stdout : 'output' , stderr : '' } ) ) ;
15+ const mockExecAsync = vi . fn ( async ( cmd : string ) => {
16+ if ( cmd . startsWith ( 'npm install' ) ) {
17+ return { stdout : '' , stderr : '' } ;
18+ }
19+ return { stdout : 'output' , stderr : '' } ;
20+ } ) ;
2021
2122 const result = await runPoc (
22- { code : 'console.log(" test") ' } ,
23- { fs : mockFs as any , path : mockPath as any , execAsync : mockExecAsync as any }
23+ { filePath : '/tmp/ test.js ' } ,
24+ { fs : { } as any , path : mockPath as any , execAsync : mockExecAsync as any }
2425 ) ;
2526
26- expect ( mockFs . mkdir ) . toHaveBeenCalledTimes ( 1 ) ;
27- expect ( mockFs . writeFile ) . toHaveBeenCalledTimes ( 1 ) ;
2827 expect ( mockExecAsync ) . toHaveBeenCalledTimes ( 2 ) ;
28+ expect ( mockExecAsync ) . toHaveBeenNthCalledWith (
29+ 1 ,
30+ 'npm install --registry=https://registry.npmjs.org/' ,
31+ { cwd : '/tmp' }
32+ ) ;
33+ expect ( mockExecAsync ) . toHaveBeenNthCalledWith ( 2 , 'node /tmp/test.js' ) ;
2934 expect ( ( result . content [ 0 ] as any ) . text ) . toBe (
3035 JSON . stringify ( { stdout : 'output' , stderr : '' } )
3136 ) ;
3237 } ) ;
3338
3439 it ( 'should handle execution errors' , async ( ) => {
35- const mockFs = {
36- mkdir : vi . fn ( async ( ) => undefined ) ,
37- writeFile : vi . fn ( async ( ) => undefined ) ,
38- } ;
3940 const mockPath = {
40- join : ( ... args : string [ ] ) => args . join ( '/' ) ,
41+ dirname : ( p : string ) => p . substring ( 0 , p . lastIndexOf ( '/' ) ) ,
4142 } ;
42- const mockExecAsync = vi . fn ( async ( ) => {
43- throw new Error ( 'Execution failed' ) ;
43+ const mockExecAsync = vi . fn ( async ( cmd : string ) => {
44+ if ( cmd . startsWith ( 'node' ) ) {
45+ throw new Error ( 'Execution failed' ) ;
46+ }
47+ return { stdout : '' , stderr : '' } ;
4448 } ) ;
4549
4650 const result = await runPoc (
47- { code : 'error' } ,
48- { fs : mockFs as any , path : mockPath as any , execAsync : mockExecAsync as any }
51+ { filePath : '/tmp/ error.js ' } ,
52+ { fs : { } as any , path : mockPath as any , execAsync : mockExecAsync as any }
4953 ) ;
5054
5155 expect ( result . isError ) . toBe ( true ) ;
0 commit comments