@@ -8,155 +8,155 @@ const CLI_PATH = resolve(__dirname, "../../dist/cli.js");
88
99/** Spawn the CLI and collect stdout/stderr/exit code. */
1010function runCli (
11- args : string [ ] ,
12- opts : { timeout ?: number } = { } ,
11+ args : string [ ] ,
12+ opts : { timeout ?: number } = { } ,
1313) : Promise < { stdout : string ; stderr : string ; code : number | null } > {
14- const timeout = opts . timeout ?? 5000 ;
15- return new Promise ( ( res ) => {
16- const cp = execFile ( "node" , [ CLI_PATH , ...args ] , { timeout } , ( err , stdout , stderr ) => {
17- const code = cp . exitCode ?? ( err && "code" in err ? ( err as { code : number } ) . code : null ) ;
18- res ( { stdout, stderr, code } ) ;
19- } ) ;
14+ const timeout = opts . timeout ?? 5000 ;
15+ return new Promise ( ( res ) => {
16+ const cp = execFile ( "node" , [ CLI_PATH , ...args ] , { timeout } , ( err , stdout , stderr ) => {
17+ const code = cp . exitCode ?? ( err && "code" in err ? ( err as { code : number } ) . code : null ) ;
18+ res ( { stdout, stderr, code } ) ;
2019 } ) ;
20+ } ) ;
2121}
2222
2323/**
2424 * Spawn the CLI expecting a long-running server. Returns the child
2525 * process plus helpers to read accumulated output and send signals.
2626 */
2727function spawnCli ( args : string [ ] ) : {
28- cp : ChildProcess ;
29- stdout : ( ) => string ;
30- stderr : ( ) => string ;
31- kill : ( signal ?: NodeJS . Signals ) => void ;
32- waitForOutput : ( match : RegExp , timeoutMs ?: number ) => Promise < void > ;
28+ cp : ChildProcess ;
29+ stdout : ( ) => string ;
30+ stderr : ( ) => string ;
31+ kill : ( signal ?: NodeJS . Signals ) => void ;
32+ waitForOutput : ( match : RegExp , timeoutMs ?: number ) => Promise < void > ;
3333} {
34- let out = "" ;
35- let err = "" ;
36- const cp = execFile ( "node" , [ CLI_PATH , ...args ] ) ;
37- cp . stdout ?. on ( "data" , ( d ) => {
38- out += d ;
39- } ) ;
40- cp . stderr ?. on ( "data" , ( d ) => {
41- err += d ;
34+ let out = "" ;
35+ let err = "" ;
36+ const cp = execFile ( "node" , [ CLI_PATH , ...args ] ) ;
37+ cp . stdout ?. on ( "data" , ( d ) => {
38+ out += d ;
39+ } ) ;
40+ cp . stderr ?. on ( "data" , ( d ) => {
41+ err += d ;
42+ } ) ;
43+
44+ const waitForOutput = ( match : RegExp , timeoutMs = 5000 ) : Promise < void > =>
45+ new Promise ( ( resolve , reject ) => {
46+ const deadline = setTimeout ( ( ) => {
47+ reject ( new Error ( `Timed out waiting for ${ match } — stdout: ${ out } , stderr: ${ err } ` ) ) ;
48+ } , timeoutMs ) ;
49+
50+ const check = ( ) => {
51+ if ( match . test ( out ) || match . test ( err ) ) {
52+ clearTimeout ( deadline ) ;
53+ resolve ( ) ;
54+ return ;
55+ }
56+ setTimeout ( check , 50 ) ;
57+ } ;
58+ check ( ) ;
4259 } ) ;
4360
44- const waitForOutput = ( match : RegExp , timeoutMs = 5000 ) : Promise < void > =>
45- new Promise ( ( resolve , reject ) => {
46- const deadline = setTimeout ( ( ) => {
47- reject ( new Error ( `Timed out waiting for ${ match } — stdout: ${ out } , stderr: ${ err } ` ) ) ;
48- } , timeoutMs ) ;
49-
50- const check = ( ) => {
51- if ( match . test ( out ) || match . test ( err ) ) {
52- clearTimeout ( deadline ) ;
53- resolve ( ) ;
54- return ;
55- }
56- setTimeout ( check , 50 ) ;
57- } ;
58- check ( ) ;
59- } ) ;
60-
61- return {
62- cp,
63- stdout : ( ) => out ,
64- stderr : ( ) => err ,
65- kill : ( signal : NodeJS . Signals = "SIGTERM" ) => cp . kill ( signal ) ,
66- waitForOutput,
67- } ;
61+ return {
62+ cp,
63+ stdout : ( ) => out ,
64+ stderr : ( ) => err ,
65+ kill : ( signal : NodeJS . Signals = "SIGTERM" ) => cp . kill ( signal ) ,
66+ waitForOutput,
67+ } ;
6868}
6969
7070function makeTmpDir ( ) : string {
71- return mkdtempSync ( join ( tmpdir ( ) , "cli-test-" ) ) ;
71+ return mkdtempSync ( join ( tmpdir ( ) , "cli-test-" ) ) ;
7272}
7373
7474function writeFixture ( dir : string , name : string ) : string {
75- const filePath = join ( dir , name ) ;
76- writeFileSync (
77- filePath ,
78- JSON . stringify ( {
79- fixtures : [
80- {
81- match : { userMessage : "hello" } ,
82- response : { content : "Hello from test fixture!" } ,
83- } ,
84- ] ,
85- } ) ,
86- "utf-8" ,
87- ) ;
88- return filePath ;
75+ const filePath = join ( dir , name ) ;
76+ writeFileSync (
77+ filePath ,
78+ JSON . stringify ( {
79+ fixtures : [
80+ {
81+ match : { userMessage : "hello" } ,
82+ response : { content : "Hello from test fixture!" } ,
83+ } ,
84+ ] ,
85+ } ) ,
86+ "utf-8" ,
87+ ) ;
88+ return filePath ;
8989}
9090
9191/* ================================================================== */
9292
9393describe ( "CLI: --help" , ( ) => {
94- it ( "prints usage text and exits with code 0" , async ( ) => {
95- const { stdout, code } = await runCli ( [ "--help" ] ) ;
96- expect ( stdout ) . toContain ( "Usage: mock-openai" ) ;
97- expect ( stdout ) . toContain ( "--port" ) ;
98- expect ( stdout ) . toContain ( "--fixtures" ) ;
99- expect ( code ) . toBe ( 0 ) ;
100- } ) ;
94+ it ( "prints usage text and exits with code 0" , async ( ) => {
95+ const { stdout, code } = await runCli ( [ "--help" ] ) ;
96+ expect ( stdout ) . toContain ( "Usage: mock-openai" ) ;
97+ expect ( stdout ) . toContain ( "--port" ) ;
98+ expect ( stdout ) . toContain ( "--fixtures" ) ;
99+ expect ( code ) . toBe ( 0 ) ;
100+ } ) ;
101101} ) ;
102102
103103describe ( "CLI: argument validation" , ( ) => {
104- it ( "rejects --port 99999 (out of range)" , async ( ) => {
105- const { stderr, code } = await runCli ( [ "--port" , "99999" ] ) ;
106- expect ( stderr ) . toContain ( "Invalid port" ) ;
107- expect ( code ) . toBe ( 1 ) ;
108- } ) ;
109-
110- it ( "rejects --port=-1 (negative)" , async ( ) => {
111- const { stderr, code } = await runCli ( [ "--port=-1" ] ) ;
112- expect ( stderr ) . toContain ( "Invalid port" ) ;
113- expect ( code ) . toBe ( 1 ) ;
114- } ) ;
115-
116- it ( "rejects --latency=-5 (negative)" , async ( ) => {
117- const { stderr, code } = await runCli ( [ "--latency=-5" ] ) ;
118- expect ( stderr ) . toContain ( "Invalid latency" ) ;
119- expect ( code ) . toBe ( 1 ) ;
120- } ) ;
121-
122- it ( "rejects --chunk-size 0 (below minimum)" , async ( ) => {
123- const { stderr, code } = await runCli ( [ "--chunk-size" , "0" ] ) ;
124- expect ( stderr ) . toContain ( "Invalid chunk-size" ) ;
125- expect ( code ) . toBe ( 1 ) ;
126- } ) ;
104+ it ( "rejects --port 99999 (out of range)" , async ( ) => {
105+ const { stderr, code } = await runCli ( [ "--port" , "99999" ] ) ;
106+ expect ( stderr ) . toContain ( "Invalid port" ) ;
107+ expect ( code ) . toBe ( 1 ) ;
108+ } ) ;
109+
110+ it ( "rejects --port=-1 (negative)" , async ( ) => {
111+ const { stderr, code } = await runCli ( [ "--port=-1" ] ) ;
112+ expect ( stderr ) . toContain ( "Invalid port" ) ;
113+ expect ( code ) . toBe ( 1 ) ;
114+ } ) ;
115+
116+ it ( "rejects --latency=-5 (negative)" , async ( ) => {
117+ const { stderr, code } = await runCli ( [ "--latency=-5" ] ) ;
118+ expect ( stderr ) . toContain ( "Invalid latency" ) ;
119+ expect ( code ) . toBe ( 1 ) ;
120+ } ) ;
121+
122+ it ( "rejects --chunk-size 0 (below minimum)" , async ( ) => {
123+ const { stderr, code } = await runCli ( [ "--chunk-size" , "0" ] ) ;
124+ expect ( stderr ) . toContain ( "Invalid chunk-size" ) ;
125+ expect ( code ) . toBe ( 1 ) ;
126+ } ) ;
127127} ) ;
128128
129129describe ( "CLI: fixture loading" , ( ) => {
130- let tmpDir : string ;
130+ let tmpDir : string ;
131131
132- beforeEach ( ( ) => {
133- tmpDir = makeTmpDir ( ) ;
134- } ) ;
132+ beforeEach ( ( ) => {
133+ tmpDir = makeTmpDir ( ) ;
134+ } ) ;
135135
136- afterEach ( ( ) => {
137- rmSync ( tmpDir , { recursive : true , force : true } ) ;
138- } ) ;
136+ afterEach ( ( ) => {
137+ rmSync ( tmpDir , { recursive : true , force : true } ) ;
138+ } ) ;
139139
140- it ( "starts server with a valid fixture file, then exits cleanly on SIGTERM" , async ( ) => {
141- const fixturePath = writeFixture ( tmpDir , "test.json" ) ;
142- const child = spawnCli ( [ "--fixtures" , fixturePath , "--port" , "0" ] ) ;
140+ it ( "starts server with a valid fixture file, then exits cleanly on SIGTERM" , async ( ) => {
141+ const fixturePath = writeFixture ( tmpDir , "test.json" ) ;
142+ const child = spawnCli ( [ "--fixtures" , fixturePath , "--port" , "0" ] ) ;
143143
144- await child . waitForOutput ( / l i s t e n i n g o n / i, 5000 ) ;
145- expect ( child . stdout ( ) ) . toContain ( "Loaded 1 fixture(s)" ) ;
144+ await child . waitForOutput ( / l i s t e n i n g o n / i, 5000 ) ;
145+ expect ( child . stdout ( ) ) . toContain ( "Loaded 1 fixture(s)" ) ;
146146
147- child . kill ( "SIGTERM" ) ;
147+ child . kill ( "SIGTERM" ) ;
148148
149- await new Promise < void > ( ( resolve ) => {
150- child . cp . on ( "close" , ( code ) => {
151- expect ( code ) . toBe ( 0 ) ;
152- resolve ( ) ;
153- } ) ;
154- } ) ;
149+ await new Promise < void > ( ( resolve ) => {
150+ child . cp . on ( "close" , ( code ) => {
151+ expect ( code ) . toBe ( 0 ) ;
152+ resolve ( ) ;
153+ } ) ;
155154 } ) ;
155+ } ) ;
156156
157- it ( "fails with error when --fixtures points to a non-existent path" , async ( ) => {
158- const { stderr, code } = await runCli ( [ "--fixtures" , "/nonexistent/path/to/fixtures" ] ) ;
159- expect ( stderr ) . toContain ( "Fixtures path not found" ) ;
160- expect ( code ) . toBe ( 1 ) ;
161- } ) ;
157+ it ( "fails with error when --fixtures points to a non-existent path" , async ( ) => {
158+ const { stderr, code } = await runCli ( [ "--fixtures" , "/nonexistent/path/to/fixtures" ] ) ;
159+ expect ( stderr ) . toContain ( "Fixtures path not found" ) ;
160+ expect ( code ) . toBe ( 1 ) ;
161+ } ) ;
162162} ) ;
0 commit comments