@@ -4,8 +4,8 @@ import path from "node:path";
44import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
55import { createPatternFileHelper } from "../helpers/pattern-file.js" ;
66import { waitForChildClose , waitForDead , waitForPidFile } from "../helpers/process-wait.js" ;
7- import { createDeferred } from "../helpers/promise.js" ;
8- import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js" ;
7+ import { createDeferred , withTestTimeout } from "../helpers/promise.js" ;
8+ import { createTempDirTracker , useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js" ;
99
1010const commands = vi . hoisted ( ( ) => ( { prepare : vi . fn ( ) , prepareE2e : vi . fn ( ) , reader : vi . fn ( ) } ) ) ;
1111vi . mock ( "../../scripts/lib/managed-child-process.mts" , ( ) => ( {
@@ -175,25 +175,30 @@ syncBuiltinESMExports();\n`,
175175 ] as const ) (
176176 "blocks %s CLI readers until successful build and preserves SIGTERM" ,
177177 async ( _name , script , args , mode : "private-qa" | "runtime" = "private-qa" ) => {
178- for ( const outcome of [ 0 , 7 , "SIGTERM" ] as const ) {
179- const root = tempDirs . make ( "plugin-build-cli-" ) ;
180- const pidFile = path . join ( root , "build.pid" ) ;
181- const readersFile = path . join ( root , "readers" ) ;
182- const builder = path . join ( root , "build.mjs" ) ;
183- const preload = path . join ( root , "preload.mjs" ) ;
184- fs . writeFileSync (
185- builder ,
186- `import fs from 'node:fs';
178+ const outcomes = [ 0 , 7 , "SIGTERM" ] as const ;
179+ // Rows share hooks and module state; only their independent process trees overlap.
180+ const results = await Promise . allSettled (
181+ outcomes . map ( async ( outcome ) => {
182+ const scenarioDirs = createTempDirTracker ( ) ;
183+ const root = scenarioDirs . make ( "plugin-build-cli-" ) ;
184+ try {
185+ const pidFile = path . join ( root , "build.pid" ) ;
186+ const readersFile = path . join ( root , "readers" ) ;
187+ const builder = path . join ( root , "build.mjs" ) ;
188+ const preload = path . join ( root , "preload.mjs" ) ;
189+ fs . writeFileSync (
190+ builder ,
191+ `import fs from 'node:fs';
187192process.on('SIGTERM', () => process.exit(0));
188193process.stdin.once('data', () => process.exit(${ typeof outcome === "number" ? outcome : 0 } ));
189194fs.writeFileSync(${ JSON . stringify ( pidFile ) } , String(process.pid));
190195process.stdin.resume();\n` ,
191- ) ;
192- // Keep the real managed process owner and CLI scheduler. Replace only
193- // heavyweight build/test executables at Node's child-process boundary.
194- fs . writeFileSync (
195- preload ,
196- `import cp from 'node:child_process';
196+ ) ;
197+ // Keep the real managed process owner and CLI scheduler. Replace only
198+ // heavyweight build/test executables at Node's child-process boundary.
199+ fs . writeFileSync (
200+ preload ,
201+ `import cp from 'node:child_process';
197202import fs from 'node:fs';
198203import { syncBuiltinESMExports } from 'node:module';
199204const spawn = cp.spawn;
@@ -206,48 +211,74 @@ cp.spawn = (bin, args, options) => {
206211 return spawn(bin, args, options);
207212};
208213syncBuiltinESMExports();\n` ,
209- ) ;
210- const child = spawn (
211- process . execPath ,
212- [ "--import" , preload , path . resolve ( script ) , ...args ] ,
213- {
214- cwd : _name === "single" ? path . resolve ( "extensions/qa-lab" ) : process . cwd ( ) ,
215- env : { ...process . env , OPENCLAW_EXTENSION_BATCH_PARALLEL : "2" } ,
216- stdio : [ "pipe" , "pipe" , "pipe" ] ,
217- } ,
218- ) ;
219- let output = "" ;
220- child . stdout . on ( "data" , ( data ) => {
221- output += data ;
222- } ) ;
223- child . stderr . on ( "data" , ( data ) => {
224- output += data ;
225- } ) ;
226- const closed = waitForChildClose ( child ) ;
227- let buildPid : number | undefined ;
228- try {
229- buildPid = await waitForPidFile ( pidFile , 5_000 ) ;
230- expect ( fs . existsSync ( readersFile ) ) . toBe ( false ) ;
231- if ( outcome === "SIGTERM" ) child . kill ( "SIGTERM" ) ;
232- else child . stdin . end ( "finish\n" ) ;
233- expect ( await closed , output ) . toEqual ( {
234- code : outcome === "SIGTERM" ? 143 : outcome ,
235- signal : null ,
236- } ) ;
237- expect ( fs . existsSync ( readersFile ) , output ) . toBe ( outcome === 0 ) ;
238- expect ( output . match ( new RegExp ( `preparing ${ mode } runtime` , "gu" ) ) ) . toHaveLength ( 1 ) ;
239- await waitForDead ( buildPid , 5_000 ) ;
240- } finally {
241- if ( child . exitCode === null && child . signalCode === null ) child . kill ( "SIGKILL" ) ;
242- if ( buildPid ) {
214+ ) ;
215+ const child = spawn (
216+ process . execPath ,
217+ [ "--import" , preload , path . resolve ( script ) , ...args ] ,
218+ {
219+ cwd : _name === "single" ? path . resolve ( "extensions/qa-lab" ) : process . cwd ( ) ,
220+ env : { ...process . env , OPENCLAW_EXTENSION_BATCH_PARALLEL : "2" } ,
221+ stdio : [ "pipe" , "pipe" , "pipe" ] ,
222+ } ,
223+ ) ;
224+ let output = "" ;
225+ child . stdout . on ( "data" , ( data ) => {
226+ output += data ;
227+ } ) ;
228+ child . stderr . on ( "data" , ( data ) => {
229+ output += data ;
230+ } ) ;
231+ // Observe timeout failures immediately, but retain the actual close event
232+ // so failed assertions still join cleanup before this outcome settles.
233+ const closed = waitForChildClose ( child ) . catch ( ( error : unknown ) => error ) ;
234+ const stopped = createDeferred ( ) ;
235+ child . once ( "close" , ( ) => stopped . resolve ( ) ) ;
236+ let buildPid : number | undefined ;
243237 try {
244- process . kill ( buildPid , "SIGKILL" ) ;
245- } catch {
246- /* Already exited. */
238+ buildPid = await waitForPidFile ( pidFile , 5_000 ) ;
239+ expect ( fs . existsSync ( readersFile ) , `outcome=${ outcome } ` ) . toBe ( false ) ;
240+ if ( outcome === "SIGTERM" ) child . kill ( "SIGTERM" ) ;
241+ else child . stdin . end ( "finish\n" ) ;
242+ expect ( await closed , `outcome=${ outcome } \n${ output } ` ) . toEqual ( {
243+ code : outcome === "SIGTERM" ? 143 : outcome ,
244+ signal : null ,
245+ } ) ;
246+ expect ( fs . existsSync ( readersFile ) , `outcome=${ outcome } \n${ output } ` ) . toBe (
247+ outcome === 0 ,
248+ ) ;
249+ expect (
250+ output . match ( new RegExp ( `preparing ${ mode } runtime` , "gu" ) ) ,
251+ `outcome=${ outcome } ` ,
252+ ) . toHaveLength ( 1 ) ;
253+ await waitForDead ( buildPid , 5_000 ) ;
254+ } finally {
255+ if ( child . exitCode === null && child . signalCode === null ) child . kill ( "SIGKILL" ) ;
256+ if ( ! buildPid && fs . existsSync ( pidFile ) ) {
257+ buildPid = await waitForPidFile ( pidFile , 5_000 ) ;
258+ }
259+ if ( buildPid ) {
260+ try {
261+ process . kill ( buildPid , "SIGKILL" ) ;
262+ } catch {
263+ /* Already exited. */
264+ }
265+ }
266+ try {
267+ await withTestTimeout ( stopped . promise , 5_000 , `outcome=${ outcome } : CLI cleanup` ) ;
268+ } finally {
269+ if ( buildPid ) await waitForDead ( buildPid , 5_000 ) ;
270+ }
247271 }
272+ } finally {
273+ scenarioDirs . cleanup ( ) ;
248274 }
249- await closed ;
250- }
275+ } ) ,
276+ ) ;
277+ for ( const [ index , result ] of results . entries ( ) ) {
278+ expect . soft ( result , `outcome=${ outcomes [ index ] } ` ) . toEqual ( {
279+ status : "fulfilled" ,
280+ value : undefined ,
281+ } ) ;
251282 }
252283 } ,
253284 ) ;
0 commit comments