@@ -421,6 +421,58 @@ describe("buildQaRuntimeEnv", () => {
421421 await expect ( readdir ( tempParent ) ) . resolves . toStrictEqual ( [ ] ) ;
422422 } ) ;
423423
424+ it ( "cleans up temp QA gateway roots when repo CLI discovery fails before startup" , async ( ) => {
425+ const tempParent = await tempDirs . makeTempDir ( "qa-gateway-cli-discovery-fail-" ) ;
426+ const emptyRepo = await tempDirs . makeTempDir ( "qa-gateway-empty-repo-" ) ;
427+ qaTempPathState . preferredTmpDir = tempParent ;
428+
429+ await expect (
430+ startQaGatewayChild ( {
431+ repoRoot : emptyRepo ,
432+ useRepoCli : true ,
433+ transportBaseUrl : "http://127.0.0.1:43123" ,
434+ } ) ,
435+ ) . rejects . toThrow ( "OpenClaw CLI entry not found" ) ;
436+
437+ await expect ( readdir ( tempParent ) ) . resolves . toStrictEqual ( [ ] ) ;
438+ } ) ;
439+
440+ it . each ( [
441+ {
442+ failure : "bundled plugin staging cannot copy root package metadata" ,
443+ packageContents : undefined ,
444+ expectedError : / E N O E N T / u,
445+ } ,
446+ {
447+ failure : "host version resolution cannot parse staged package metadata" ,
448+ packageContents : "{" ,
449+ expectedError : / J S O N / u,
450+ } ,
451+ ] ) ( "cleans staged QA runtime roots when $failure" , async ( { packageContents, expectedError } ) => {
452+ const tempParent = await tempDirs . makeTempDir ( "qa-gateway-staged-runtime-fail-" ) ;
453+ const repoRoot = await tempDirs . makeTempDir ( "qa-gateway-staged-runtime-repo-" ) ;
454+ const stagedRuntimeParent = path . join ( repoRoot , ".artifacts" , "qa-runtime" ) ;
455+ qaTempPathState . preferredTmpDir = tempParent ;
456+
457+ if ( packageContents !== undefined ) {
458+ await writeFile ( path . join ( repoRoot , "package.json" ) , packageContents , "utf8" ) ;
459+ }
460+
461+ await expect (
462+ startQaGatewayChild ( {
463+ repoRoot,
464+ transport : {
465+ requiredPluginIds : [ ] ,
466+ createGatewayConfig : ( ) => ( { } ) ,
467+ } ,
468+ transportBaseUrl : "http://127.0.0.1:43123" ,
469+ } ) ,
470+ ) . rejects . toThrow ( expectedError ) ;
471+
472+ await expect ( readdir ( tempParent ) ) . resolves . toStrictEqual ( [ ] ) ;
473+ await expect ( readdir ( stagedRuntimeParent ) ) . resolves . toStrictEqual ( [ ] ) ;
474+ } ) ;
475+
424476 it ( "reports command spawn errors instead of leaking unhandled child errors" , async ( ) => {
425477 const preferredTempParent = await tempDirs . makeTempDir ( "qa-gateway-default-spawn-fail-" ) ;
426478 const commandTempParent = await tempDirs . makeTempDir ( "qa-gateway-command-spawn-fail-" ) ;
0 commit comments