@@ -252,14 +252,23 @@ async function withProxiedWorld<T>(
252252 forwarded : ProviderScenarioForwardedRequest [ ] ;
253253 } ) => Promise < T > ,
254254 options : ParityWorldOptions = { } ,
255+ dependencies : {
256+ createWorld ?: typeof createParityWorld ;
257+ createProxy ?: typeof createProviderScenarioProxy ;
258+ } = { } ,
255259) : Promise < T > {
256- const world = await createParityWorld ( options ) ;
260+ let world : ParityWorld | undefined ;
257261 let proxy : Awaited < ReturnType < typeof createProviderScenarioProxy > > | undefined ;
258262 let clientStateDir : string | undefined ;
259263 try {
260- const activeProxy = await createProviderScenarioProxy ( world . daemon , {
261- clientToken : PROXY_TOKEN ,
262- } ) ;
264+ const activeWorld = await ( dependencies . createWorld ?? createParityWorld ) ( options ) ;
265+ world = activeWorld ;
266+ const activeProxy = await ( dependencies . createProxy ?? createProviderScenarioProxy ) (
267+ activeWorld . daemon ,
268+ {
269+ clientToken : PROXY_TOKEN ,
270+ } ,
271+ ) ;
263272 proxy = activeProxy ;
264273 const proxyBaseUrl = loopbackProxyBaseUrl ( activeProxy . port ) ;
265274 const activeClientStateDir = fs . mkdtempSync (
@@ -283,13 +292,13 @@ async function withProxiedWorld<T>(
283292 return responseFromClientError ( error ) ;
284293 }
285294 } ;
286- return await run ( { world, proxied, forwarded : activeProxy . forwarded } ) ;
295+ return await run ( { world : activeWorld , proxied, forwarded : activeProxy . forwarded } ) ;
287296 } finally {
288297 try {
289- if ( proxy ) await proxy . close ( ) ;
298+ await proxy ? .close ( ) ;
290299 } finally {
291300 try {
292- await world . close ( ) ;
301+ await world ? .close ( ) ;
293302 } finally {
294303 if ( clientStateDir ) fs . rmSync ( clientStateDir , { recursive : true , force : true } ) ;
295304 }
@@ -416,17 +425,58 @@ test(
416425 PARALLEL_PROVIDER_SCENARIO_TIMEOUT_MS ,
417426) ;
418427
428+ test (
429+ 'closes the daemon when proxy startup fails' ,
430+ async ( t ) => {
431+ if ( await skipWhenLoopbackUnavailable ( t , 'daemon proxy startup cleanup coverage' ) ) return ;
432+
433+ let closeCalls = 0 ;
434+ const startupError = new Error ( 'simulated proxy listen failure' ) ;
435+ await assert . rejects (
436+ withProxiedWorld (
437+ async ( ) => {
438+ throw new Error ( 'the scenario callback must not run after proxy startup fails' ) ;
439+ } ,
440+ { } ,
441+ {
442+ createWorld : async ( options ) => {
443+ const world = await createParityWorld ( options ) ;
444+ return {
445+ ...world ,
446+ close : async ( ) => {
447+ closeCalls += 1 ;
448+ await world . close ( ) ;
449+ } ,
450+ } ;
451+ } ,
452+ createProxy : async ( ) => {
453+ throw startupError ;
454+ } ,
455+ } ,
456+ ) ,
457+ ( error ) => error === startupError ,
458+ ) ;
459+ assert . equal ( closeCalls , 1 ) ;
460+ } ,
461+ PARALLEL_PROVIDER_SCENARIO_TIMEOUT_MS ,
462+ ) ;
463+
419464test (
420465 'recording proxy retains upstream errors without exposing them in the response' ,
421466 async ( t ) => {
422467 if ( await skipWhenLoopbackUnavailable ( t , 'daemon proxy error handling coverage' ) ) return ;
423468
424- const world = await createParityWorld ( ) ;
469+ let world : ParityWorld | undefined ;
425470 let proxy : Awaited < ReturnType < typeof createProviderScenarioProxy > > | undefined ;
426471 try {
427- proxy = await createProviderScenarioProxy ( world . daemon , { clientToken : PROXY_TOKEN } ) ;
428- await world . close ( ) ;
429- const response = await fetch ( `${ loopbackProxyBaseUrl ( proxy . port ) } /rpc` , {
472+ const activeWorld = await createParityWorld ( ) ;
473+ world = activeWorld ;
474+ const activeProxy = await createProviderScenarioProxy ( activeWorld . daemon , {
475+ clientToken : PROXY_TOKEN ,
476+ } ) ;
477+ proxy = activeProxy ;
478+ await activeWorld . close ( ) ;
479+ const response = await fetch ( `${ loopbackProxyBaseUrl ( activeProxy . port ) } /rpc` , {
430480 method : 'POST' ,
431481 headers : {
432482 authorization : `Bearer ${ PROXY_TOKEN } ` ,
@@ -437,12 +487,12 @@ test(
437487 assert . equal ( response . status , 502 ) ;
438488 assert . equal ( await response . text ( ) , 'upstream request failed' ) ;
439489
440- assert . equal ( proxy . forwarded [ 0 ] ?. upstreamErrorCode , 'ECONNREFUSED' ) ;
490+ assert . equal ( activeProxy . forwarded [ 0 ] ?. upstreamErrorCode , 'ECONNREFUSED' ) ;
441491 } finally {
442492 try {
443- if ( proxy ) await proxy . close ( ) ;
493+ await proxy ? .close ( ) ;
444494 } finally {
445- await world . close ( ) ;
495+ await world ? .close ( ) ;
446496 }
447497 }
448498 } ,
0 commit comments