@@ -3427,6 +3427,62 @@ describe("createTelegramBot", () => {
34273427 expect ( onUpdateId . mock . calls . map ( ( call ) => call [ 0 ] ) ) . toEqual ( [ 701 ] ) ;
34283428 } ) ;
34293429
3430+ it ( "retries a deferred spooled update after its queued turn is abandoned" , async ( ) => {
3431+ configureOpenDm ( ) ;
3432+ let queuedLifecycle : GetReplyOptions [ "turnAdoptionLifecycle" ] ;
3433+ replySpy
3434+ . mockImplementationOnce ( async ( _ctx : MsgContext , opts ?: GetReplyOptions ) => {
3435+ queuedLifecycle = opts ?. turnAdoptionLifecycle ;
3436+ queuedLifecycle ?. onDeferred ?.( ) ;
3437+ return undefined ;
3438+ } )
3439+ . mockImplementationOnce ( async ( _ctx : MsgContext , opts ?: GetReplyOptions ) => {
3440+ await opts ?. turnAdoptionLifecycle ?. onAdopted ?.( ) ;
3441+ return { text : "recovered" } ;
3442+ } ) ;
3443+ const { onUpdateId } = setupUpdateOffsetTracker ( {
3444+ lastUpdateId : 701 ,
3445+ } ) ;
3446+ const messageHandler = getMessageHandler ( ) ;
3447+
3448+ const firstReplayPromise = dispatchSpooledPrivateText ( messageHandler , {
3449+ updateId : 702 ,
3450+ messageId : 702 ,
3451+ text : "retry after queued turn abandonment" ,
3452+ } ) ;
3453+ await vi . waitFor ( ( ) => {
3454+ expect ( queuedLifecycle ?. onAbandoned ) . toEqual ( expect . any ( Function ) ) ;
3455+ } ) ;
3456+ queuedLifecycle ?. onAbandoned ?.( ) ;
3457+ const firstReplay = await firstReplayPromise ;
3458+ const firstDeferredWork = requireValue ( firstReplay . deferredWork , "first deferred spooled work" ) ;
3459+ await expect ( firstDeferredWork . task ) . resolves . toMatchObject ( { kind : "failed-retryable" } ) ;
3460+ await flushTelegramTestMicrotasks ( ) ;
3461+ expect ( onUpdateId ) . not . toHaveBeenCalled ( ) ;
3462+
3463+ const secondReplay = await dispatchSpooledPrivateText ( messageHandler , {
3464+ updateId : 702 ,
3465+ messageId : 702 ,
3466+ text : "retry after queued turn abandonment" ,
3467+ } ) ;
3468+ const secondDeferredWork = requireValue (
3469+ secondReplay . deferredWork ,
3470+ "second deferred spooled work" ,
3471+ ) ;
3472+ await expect ( secondDeferredWork . task ) . resolves . toEqual ( { kind : "completed" } ) ;
3473+ await flushTelegramTestMicrotasks ( ) ;
3474+ expect ( replySpy ) . toHaveBeenCalledTimes ( 2 ) ;
3475+ expect ( onUpdateId . mock . calls . map ( ( call ) => call [ 0 ] ) ) . toEqual ( [ 702 ] ) ;
3476+
3477+ const duplicateReplay = await dispatchSpooledPrivateText ( messageHandler , {
3478+ updateId : 702 ,
3479+ messageId : 702 ,
3480+ text : "retry after queued turn abandonment" ,
3481+ } ) ;
3482+ expect ( duplicateReplay . deferredWork ) . toBeUndefined ( ) ;
3483+ expect ( replySpy ) . toHaveBeenCalledTimes ( 2 ) ;
3484+ } ) ;
3485+
34303486 it ( "skips replayed update ids even when the semantic update key differs" , async ( ) => {
34313487 const { onUpdateId, run : runMiddlewareChain } = setupUpdateOffsetTracker ( {
34323488 lastUpdateId : 300 ,
0 commit comments