11// Covers approval handler runtime adapter creation and lazy wiring.
22import { describe , expect , it , vi } from "vitest" ;
3+ import { createDeferred , withTestTimeout } from "../../test/helpers/promise.js" ;
34import {
45 createChannelApprovalNativeRuntimeAdapter ,
56 createChannelApprovalHandlerFromCapability ,
@@ -439,14 +440,12 @@ describe("createLazyChannelApprovalNativeRuntimeAdapter", () => {
439440 } ) ;
440441
441442 it ( "unbinds in-flight wrapped entry when stop() fires between bindPending and activeEntries.set" , async ( ) => {
442- const bindGate = { resolve : ( ) => { } , promise : Promise . resolve ( ) } ;
443- const bindPromise = new Promise < void > ( ( resolve ) => {
444- bindGate . resolve = resolve ;
445- } ) ;
446- bindGate . promise = bindPromise ;
443+ const bindEntered = createDeferred ( ) ;
444+ const bindGate = createDeferred ( ) ;
447445 const deliverPending = vi . fn ( ) . mockResolvedValue ( { messageId : "in-flight" } ) ;
448446 const bindPending = vi . fn ( async ( ) => {
449- await bindPromise ;
447+ bindEntered . resolve ( ) ;
448+ await bindGate . promise ;
450449 return { bindingId : "bound-in-flight" } ;
451450 } ) ;
452451 const unbindPending = vi . fn ( ) ;
@@ -462,13 +461,7 @@ describe("createLazyChannelApprovalNativeRuntimeAdapter", () => {
462461 const request = makeExecApprovalRequest ( "exec:in-flight" ) ;
463462
464463 const inflight = approvalRuntime . handleRequested ( request ) ;
465- // Flush microtasks so deliverPending resolves and bindPending parks at the gate.
466- await new Promise ( ( r ) => {
467- setTimeout ( r , 0 ) ;
468- } ) ;
469- await new Promise ( ( r ) => {
470- setTimeout ( r , 0 ) ;
471- } ) ;
464+ await withTestTimeout ( bindEntered . promise , 1_000 , "in-flight approval binding did not start" ) ;
472465
473466 // stop() flips the stopped flag while bindPending is parked.
474467 await approvalRuntime . stop ( ) ;
@@ -485,14 +478,12 @@ describe("createLazyChannelApprovalNativeRuntimeAdapter", () => {
485478 } ) ;
486479
487480 it ( "invokes cancelDelivered when stop() fires between deliverPending and bindPending" , async ( ) => {
488- const deliverGate = { resolve : ( ) => { } , promise : Promise . resolve ( ) } ;
489- const deliverPromise = new Promise < void > ( ( resolve ) => {
490- deliverGate . resolve = resolve ;
491- } ) ;
492- deliverGate . promise = deliverPromise ;
481+ const deliverEntered = createDeferred ( ) ;
482+ const deliverGate = createDeferred ( ) ;
493483 const deliveredEntry = { messageId : "pre-bind" } ;
494484 const deliverPending = vi . fn ( async ( ) => {
495- await deliverPromise ;
485+ deliverEntered . resolve ( ) ;
486+ await deliverGate . promise ;
496487 return deliveredEntry ;
497488 } ) ;
498489 const bindPending = vi . fn ( ) . mockResolvedValue ( { bindingId : "should-not-bind" } ) ;
@@ -511,13 +502,11 @@ describe("createLazyChannelApprovalNativeRuntimeAdapter", () => {
511502 const request = makeExecApprovalRequest ( "exec:pre-bind" ) ;
512503
513504 const inflight = approvalRuntime . handleRequested ( request ) ;
514- // Flush microtasks so deliverPending is awaited and parked at the gate.
515- await new Promise ( ( r ) => {
516- setTimeout ( r , 0 ) ;
517- } ) ;
518- await new Promise ( ( r ) => {
519- setTimeout ( r , 0 ) ;
520- } ) ;
505+ await withTestTimeout (
506+ deliverEntered . promise ,
507+ 1_000 ,
508+ "pre-bind approval delivery did not start" ,
509+ ) ;
521510
522511 // stop() flips the stopped flag while deliverPending is still pending.
523512 await approvalRuntime . stop ( ) ;
@@ -536,15 +525,13 @@ describe("createLazyChannelApprovalNativeRuntimeAdapter", () => {
536525 } ) ;
537526
538527 it ( "invokes cancelDelivered when stop() fires after bindPending returned null" , async ( ) => {
539- const bindGate = { resolve : ( ) => { } , promise : Promise . resolve ( ) } ;
540- const bindPromise = new Promise < void > ( ( resolve ) => {
541- bindGate . resolve = resolve ;
542- } ) ;
543- bindGate . promise = bindPromise ;
528+ const bindEntered = createDeferred ( ) ;
529+ const bindGate = createDeferred ( ) ;
544530 const deliveredEntry = { messageId : "post-bind-null" } ;
545531 const deliverPending = vi . fn ( ) . mockResolvedValue ( deliveredEntry ) ;
546532 const bindPending = vi . fn ( async ( ) => {
547- await bindPromise ;
533+ bindEntered . resolve ( ) ;
534+ await bindGate . promise ;
548535 return null ;
549536 } ) ;
550537 const unbindPending = vi . fn ( ) ;
@@ -562,13 +549,7 @@ describe("createLazyChannelApprovalNativeRuntimeAdapter", () => {
562549 const request = makeExecApprovalRequest ( "exec:post-bind-null" ) ;
563550
564551 const inflight = approvalRuntime . handleRequested ( request ) ;
565- // Flush microtasks so deliverPending resolves and bindPending awaits the gate.
566- await new Promise ( ( r ) => {
567- setTimeout ( r , 0 ) ;
568- } ) ;
569- await new Promise ( ( r ) => {
570- setTimeout ( r , 0 ) ;
571- } ) ;
552+ await withTestTimeout ( bindEntered . promise , 1_000 , "null approval binding did not start" ) ;
572553
573554 // stop() flips the stopped flag while bindPending is parked; it then resolves to null.
574555 await approvalRuntime . stop ( ) ;
0 commit comments