@@ -3,7 +3,7 @@ import { createServer as createHttpServer } from "node:http";
33import OpenAI from "openai" ;
44import { createRequire } from "node:module" ;
55import { tmpdir } from "node:os" ;
6- import { spawnSync } from "node:child_process" ;
6+ import { execFile } from "node:child_process" ;
77import { join , resolve } from "node:path" ;
88import { afterEach , describe , expect , it , vi } from "vitest" ;
99import {
@@ -59,6 +59,40 @@ function recognized(fixtures: Fixture[]): Fixture[] {
5959 return fixtures ;
6060}
6161
62+ interface ChildResult {
63+ status : number | null ;
64+ signal : NodeJS . Signals | null ;
65+ error : Error | undefined ;
66+ stdout : string ;
67+ stderr : string ;
68+ }
69+
70+ /** Run real children while the worker can acknowledge pending runner RPC messages. */
71+ function runChild (
72+ args : string [ ] ,
73+ options : { timeout : number ; cwd ?: string ; env ?: NodeJS . ProcessEnv } ,
74+ ) : Promise < ChildResult > {
75+ return new Promise ( ( resolve ) => {
76+ const child = execFile (
77+ process . execPath ,
78+ args ,
79+ { ...options , encoding : "utf8" } ,
80+ ( error , stdout , stderr ) => {
81+ resolve ( {
82+ status : error ? ( typeof error . code === "number" ? error . code : null ) : 0 ,
83+ signal : error ?. signal ?? null ,
84+ // A normal nonzero exit is a status, not a spawn failure. Preserve
85+ // startup, output-buffer and timeout errors for the existing checks.
86+ error : error && ( error . killed || typeof error . code === "string" ) ? error : undefined ,
87+ stdout,
88+ stderr,
89+ } ) ;
90+ } ,
91+ ) ;
92+ child . stdin ?. end ( ) ;
93+ } ) ;
94+ }
95+
6296const directories : string [ ] = [ ] ;
6397const response = { toolCalls : [ { name : "weather" , arguments : { city : "Paris" } } ] } ;
6498function fixtureFile ( entries : unknown [ ] ) {
@@ -533,12 +567,11 @@ describe("normal CLI startup misbehavior diagnostics", () => {
533567 describe . each ( [ false , true ] ) ( "strict=%s" , ( strict ) => {
534568 it . each ( [ "silent" , "warn" , "info" , "debug" ] ) (
535569 "rejects malformed config without --validate-on-load at log level %s" ,
536- ( logLevel ) => {
570+ async ( logLevel ) => {
537571 const { file } = fixtureFile ( [
538572 entry ( { faults : [ { fault : "tool-args-invalid-json" , rate : 2 } ] } ) ,
539573 ] ) ;
540- const result = spawnSync (
541- process . execPath ,
574+ const result = await runChild (
542575 [
543576 "--import" ,
544577 createRequire ( import . meta. url ) . resolve ( "tsx" ) ,
@@ -554,7 +587,7 @@ describe("normal CLI startup misbehavior diagnostics", () => {
554587 "--misbehavior" ,
555588 ...( strict ? [ "--strict" ] : [ ] ) ,
556589 ] ,
557- { encoding : "utf8" , timeout : 5000 } ,
590+ { timeout : 5000 } ,
558591 ) ;
559592 expect ( result . error ) . toBeUndefined ( ) ;
560593 expect ( result . signal ) . toBeNull ( ) ;
@@ -569,9 +602,8 @@ describe("normal CLI startup misbehavior diagnostics", () => {
569602} ) ;
570603
571604describe ( "actual validate command misbehavior diagnostics" , ( ) => {
572- function validate ( args : string [ ] , cwd ?: string ) {
573- const result = spawnSync (
574- process . execPath ,
605+ async function validate ( args : string [ ] , cwd ?: string ) {
606+ const result = await runChild (
575607 [
576608 "--import" ,
577609 createRequire ( import . meta. url ) . resolve ( "tsx" ) ,
@@ -580,7 +612,7 @@ describe("actual validate command misbehavior diagnostics", () => {
580612 "--misbehavior" ,
581613 ...args ,
582614 ] ,
583- { encoding : "utf8" , cwd, timeout : 15000 } ,
615+ { cwd, timeout : 15000 } ,
584616 ) ;
585617 expect ( result . error ) . toBeUndefined ( ) ;
586618 return result ;
@@ -617,15 +649,15 @@ describe("actual validate command misbehavior diagnostics", () => {
617649 } ,
618650 ] ) (
619651 "reports $rule with logical source, original index, path and value" ,
620- ( { config, rule, path, value, fixtureResponse } ) => {
652+ async ( { config, rule, path, value, fixtureResponse } ) => {
621653 const { directory } = fixtureFile ( [ ] ) ;
622654 mkdirSync ( join ( directory , "nested" ) ) ;
623655 writeFileSync (
624656 join ( directory , "nested" , "case.json" ) ,
625657 JSON . stringify ( { fixtures : [ null , entry ( config , fixtureResponse ) ] } ) ,
626658 ) ;
627659 for ( const json of [ false , true ] ) {
628- const result = validate ( [ ...( json ? [ "--json" ] : [ ] ) , directory ] ) ;
660+ const result = await validate ( [ ...( json ? [ "--json" ] : [ ] ) , directory ] ) ;
629661 expect ( result . status ) . toBe ( 1 ) ;
630662 const report = json
631663 ? ( JSON . parse ( result . stdout ) as {
@@ -645,11 +677,11 @@ describe("actual validate command misbehavior diagnostics", () => {
645677 } ,
646678 ) ;
647679
648- it ( "does not repeat probabilistic warnings in the cross-file pass" , ( ) => {
680+ it ( "does not repeat probabilistic warnings in the cross-file pass" , async ( ) => {
649681 const config = { faults : [ { fault : "tool-args-invalid-json" , rate : 0.5 } ] } ;
650682 const first = fixtureFile ( [ entry ( config ) ] ) ;
651683 const second = fixtureFile ( [ entry ( config ) ] ) ;
652- const result = validate ( [ "--json" , first . file , second . file ] ) ;
684+ const result = await validate ( [ "--json" , first . file , second . file ] ) ;
653685 expect ( result . status ) . toBe ( 0 ) ;
654686 const report = JSON . parse ( result . stdout ) as {
655687 files : { warnings : { index : number ; message : string } [ ] } [ ] ;
@@ -667,9 +699,9 @@ describe("actual validate command misbehavior diagnostics", () => {
667699 }
668700 } ) ;
669701
670- it ( "keeps a named relative source spelling" , ( ) => {
702+ it ( "keeps a named relative source spelling" , async ( ) => {
671703 const { directory } = fixtureFile ( [ entry ( "unknown-fault" ) ] ) ;
672- const result = validate ( [ "./fixtures.json" ] , directory ) ;
704+ const result = await validate ( [ "./fixtures.json" ] , directory ) ;
673705 expect ( result . stderr ) . toContain ( '"./fixtures.json": [misbehavior/bad-value]' ) ;
674706 expect ( result . stderr ) . toContain ( "fixtures[0].misbehavior" ) ;
675707 } ) ;
@@ -684,12 +716,12 @@ describe("actual validate command misbehavior diagnostics", () => {
684716 { rate : 0.5 , times : 1 , warn : false } ,
685717 ] ) (
686718 "warns only for the specified unseeded probabilistic case %j" ,
687- ( { rate, seed, times, warn } ) => {
719+ async ( { rate, seed, times, warn } ) => {
688720 const { file } = fixtureFile ( [
689721 entry ( { seed, faults : [ { fault : "tool-args-invalid-json" , rate, times } ] } ) ,
690722 ] ) ;
691723 for ( const strict of [ false , true ] ) {
692- const result = validate ( [ "--json" , ...( strict ? [ "--strict" ] : [ ] ) , file ] ) ;
724+ const result = await validate ( [ "--json" , ...( strict ? [ "--strict" ] : [ ] ) , file ] ) ;
693725 const report = JSON . parse ( result . stdout ) as {
694726 files : { warnings : { message : string } [ ] } [ ] ;
695727 } ;
@@ -707,11 +739,11 @@ describe("actual validate command misbehavior diagnostics", () => {
707739 it . each ( [
708740 { toolCalls : [ { name : "weather" , arguments : "{broken" } ] } ,
709741 { blocks : [ { type : "toolCall" , name : "weather" , arguments : "{broken" } ] } ,
710- ] ) ( "retains authored-invalid JSON as an error with the B5 hint %j" , ( fixtureResponse ) => {
742+ ] ) ( "retains authored-invalid JSON as an error with the B5 hint %j" , async ( fixtureResponse ) => {
711743 const { file } = fixtureFile ( [
712744 { match : { userMessage : "weather" } , response : fixtureResponse } ,
713745 ] ) ;
714- const result = validate ( [ "--json" , file ] ) ;
746+ const result = await validate ( [ "--json" , file ] ) ;
715747 expect ( result . status ) . toBe ( 1 ) ;
716748 const report = JSON . parse ( result . stdout ) as { files : { errors : { message : string } [ ] } [ ] } ;
717749 expect ( report . files [ 0 ] . errors ) . toEqual (
@@ -800,7 +832,7 @@ describe("shared validateFixtures misbehavior diagnostics", () => {
800832// Run real plugin hooks in a child runner: a failing beforeAll must not fail
801833// this parent suite. As in mcp-fakes-plugins, Jest uses Vitest's compatible globals.
802834describe ( "actual plugin misbehavior diagnostics" , ( ) => {
803- function runPlugin ( plugin : string , fixturePath : string , enableMisbehavior = true ) {
835+ async function runPlugin ( plugin : string , fixturePath : string , enableMisbehavior = true ) {
804836 const directory = mkdtempSync ( resolve ( ".aimock-plugin-loading-" ) ) ;
805837 directories . push ( directory ) ;
806838 const testFile = join ( directory , "plugin.test.ts" ) ;
@@ -833,10 +865,9 @@ it("serves the loaded fixture over HTTP", async () => {
833865 expect(body.choices[0].message).toBeDefined();
834866});` ,
835867 ) ;
836- const result = spawnSync (
837- process . execPath ,
868+ const result = await runChild (
838869 [ resolve ( "node_modules/vitest/vitest.mjs" ) , "run" , "--config" , configFile ] ,
839- { encoding : "utf8" , timeout : 15000 , env : { ...process . env , NO_COLOR : "1" , CI : "1" } } ,
870+ { timeout : 15000 , env : { ...process . env , NO_COLOR : "1" , CI : "1" } } ,
840871 ) ;
841872 expect ( result . error ) . toBeUndefined ( ) ;
842873 expect ( result . signal ) . toBeNull ( ) ;
@@ -881,9 +912,9 @@ it("serves the loaded fixture over HTTP", async () => {
881912 } ,
882913 ] ) (
883914 "propagates $rule from a $surface through beforeAll" ,
884- ( { surface, config, rule, path, value, fixtureResponse } ) => {
915+ async ( { surface, config, rule, path, value, fixtureResponse } ) => {
885916 const { file, directory } = fixtureFile ( [ entry ( config , fixtureResponse ) ] ) ;
886- const result = runPlugin ( plugin , surface === "file" ? file : directory ) ;
917+ const result = await runPlugin ( plugin , surface === "file" ? file : directory ) ;
887918 expect ( result . status , result . output ) . toBe ( 1 ) ;
888919 expect ( result . output ) . toContain ( "MisbehaviorConfigError:" ) ;
889920 expect ( result . output ) . toContain ( `[misbehavior/${ rule } ]` ) ;
@@ -896,18 +927,18 @@ it("serves the loaded fixture over HTTP", async () => {
896927 20000 ,
897928 ) ;
898929
899- it ( "ignores an invalid misbehavior key without enableMisbehavior, as 1.44.0" , ( ) => {
930+ it ( "ignores an invalid misbehavior key without enableMisbehavior, as 1.44.0" , async ( ) => {
900931 const { file } = fixtureFile ( [ entry ( { faults : [ ] , typo : "unexpected" } ) ] ) ;
901- const result = runPlugin ( plugin , file , false ) ;
932+ const result = await runPlugin ( plugin , file , false ) ;
902933 expect ( result . status , result . output ) . toBe ( 0 ) ;
903934 expect ( result . output ) . toMatch ( / T e s t s \s + 1 p a s s e d \( 1 \) / ) ;
904935 expect ( result . output ) . not . toContain ( "MisbehaviorConfigError" ) ;
905936 expect ( result . output ) . not . toContain ( "[aimock] Failed to load fixtures" ) ;
906937 } , 20000 ) ;
907938
908- it ( "starts and serves HTTP with a valid empty misbehavior override" , ( ) => {
939+ it ( "starts and serves HTTP with a valid empty misbehavior override" , async ( ) => {
909940 const { file } = fixtureFile ( [ entry ( { faults : [ ] } ) ] ) ;
910- const result = runPlugin ( plugin , file ) ;
941+ const result = await runPlugin ( plugin , file ) ;
911942 expect ( result . status , result . output ) . toBe ( 0 ) ;
912943 expect ( result . output ) . toMatch ( / T e s t s \s + 1 p a s s e d \( 1 \) / ) ;
913944 } , 20000 ) ;
0 commit comments