@@ -583,16 +583,28 @@ async function invokeAgent(
583583 } ,
584584) {
585585 const respond = options ?. respond ?? vi . fn ( ) ;
586- await agentHandlers . agent ( {
587- params,
588- respond : respond as never ,
589- context : options ?. context ?? makeContext ( ) ,
590- req : { type : "req" , id : options ?. reqId ?? "agent-test-req" , method : "agent" } ,
591- client : options ?. client ?? null ,
592- isWebchatConnect : options ?. isWebchatConnect ?? ( ( ) => false ) ,
593- } ) ;
594- if ( options ?. flushDispatch !== false ) {
595- await waitForAcceptedRunDispatch ( respond ) ;
586+ // Most cases only need to cross the accepted-ack timer; keep tests that own
587+ // timer semantics on their explicit clock while avoiding a real sleep here.
588+ const ownsDispatchTimers = options ?. flushDispatch !== false && ! vi . isFakeTimers ( ) ;
589+ if ( ownsDispatchTimers ) {
590+ vi . useFakeTimers ( { toFake : [ "setTimeout" , "clearTimeout" ] } ) ;
591+ }
592+ try {
593+ await agentHandlers . agent ( {
594+ params,
595+ respond : respond as never ,
596+ context : options ?. context ?? makeContext ( ) ,
597+ req : { type : "req" , id : options ?. reqId ?? "agent-test-req" , method : "agent" } ,
598+ client : options ?. client ?? null ,
599+ isWebchatConnect : options ?. isWebchatConnect ?? ( ( ) => false ) ,
600+ } ) ;
601+ if ( options ?. flushDispatch !== false ) {
602+ await waitForAcceptedRunDispatch ( respond ) ;
603+ }
604+ } finally {
605+ if ( ownsDispatchTimers ) {
606+ vi . useRealTimers ( ) ;
607+ }
596608 }
597609 return respond ;
598610}
0 commit comments