@@ -2456,16 +2456,98 @@ describe('createAskableContext', () => {
24562456 it ( 'rejects immediately when the source request signal is already aborted' , async ( ) => {
24572457 const ctx = createAskableContext ( ) ;
24582458 const controller = new AbortController ( ) ;
2459+ const resolve = vi . fn ( ( ) => ( { data : 'ok' } ) ) ;
24592460 controller . abort ( ) ;
24602461
24612462 ctx . registerSource ( 'fast' , {
2462- resolve : ( _req ) => ( { data : 'ok' } ) ,
2463+ resolve,
24632464 } ) ;
24642465
24652466 await expect (
24662467 ( ctx as any ) . resolveSource ( 'fast' , { mode : 'summary' , signal : controller . signal } ) ,
24672468 ) . rejects . toThrow ( 'aborted' ) ;
2469+ expect ( resolve ) . not . toHaveBeenCalled ( ) ;
2470+
2471+ ctx . destroy ( ) ;
2472+ } ) ;
2473+
2474+ it ( 'aborts a pending source when no timeout is configured' , async ( ) => {
2475+ const ctx = createAskableContext ( ) ;
2476+ const controller = new AbortController ( ) ;
2477+ ctx . registerSource ( 'pending' , {
2478+ resolve : ( ) => new Promise ( ( ) => undefined ) ,
2479+ } ) ;
2480+
2481+ const result = ( ctx as any ) . resolveSource ( 'pending' , {
2482+ mode : 'summary' ,
2483+ signal : controller . signal ,
2484+ } ) ;
2485+ controller . abort ( ) ;
2486+
2487+ await expect ( result ) . rejects . toThrow ( 'Context source request aborted.' ) ;
2488+ ctx . destroy ( ) ;
2489+ } ) ;
2490+
2491+ it ( 'does not schedule a timer when no timeout is configured' , async ( ) => {
2492+ const timerSpy = vi . spyOn ( globalThis , 'setTimeout' ) ;
2493+ const ctx = createAskableContext ( ) ;
2494+ const controller = new AbortController ( ) ;
2495+ ctx . registerSource ( 'fast' , {
2496+ resolve : ( ) => ( { data : 'ok' } ) ,
2497+ } ) ;
2498+
2499+ await expect (
2500+ ( ctx as any ) . resolveSource ( 'fast' , {
2501+ mode : 'summary' ,
2502+ signal : controller . signal ,
2503+ } ) ,
2504+ ) . resolves . toMatchObject ( { data : { data : 'ok' } } ) ;
2505+ expect ( timerSpy ) . not . toHaveBeenCalled ( ) ;
2506+
2507+ timerSpy . mockRestore ( ) ;
2508+ ctx . destroy ( ) ;
2509+ } ) ;
2510+
2511+ it . each ( [
2512+ { outcome : 'resolve' , timeoutMs : undefined } ,
2513+ { outcome : 'reject' , timeoutMs : undefined } ,
2514+ { outcome : 'timeout' , timeoutMs : 10 } ,
2515+ { outcome : 'abort' , timeoutMs : 100 } ,
2516+ ] as const ) ( 'removes the abort listener after $outcome' , async ( { outcome, timeoutMs } ) => {
2517+ if ( timeoutMs !== undefined ) vi . useFakeTimers ( ) ;
2518+
2519+ const ctx = createAskableContext ( ) ;
2520+ const controller = new AbortController ( ) ;
2521+ const addSpy = vi . spyOn ( controller . signal , 'addEventListener' ) ;
2522+ const removeSpy = vi . spyOn ( controller . signal , 'removeEventListener' ) ;
2523+ ctx . registerSource ( 'source' , {
2524+ resolve : ( ) => {
2525+ if ( outcome === 'resolve' ) return { data : 'ok' } ;
2526+ if ( outcome === 'reject' ) throw new Error ( 'source failed' ) ;
2527+ return new Promise ( ( ) => undefined ) ;
2528+ } ,
2529+ } ) ;
2530+
2531+ const result = ( ctx as any ) . resolveSource ( 'source' , {
2532+ mode : 'summary' ,
2533+ signal : controller . signal ,
2534+ ...( timeoutMs !== undefined ? { timeoutMs } : { } ) ,
2535+ } ) ;
2536+ const resultExpectation = outcome === 'resolve'
2537+ ? expect ( result ) . resolves . toMatchObject ( { data : { data : 'ok' } } )
2538+ : expect ( result ) . rejects . toThrow ( ) ;
2539+
2540+ if ( outcome === 'abort' ) controller . abort ( ) ;
2541+ if ( outcome === 'timeout' ) await vi . advanceTimersByTimeAsync ( timeoutMs ! ) ;
2542+
2543+ await resultExpectation ;
2544+
2545+ const abortListener = addSpy . mock . calls . find ( ( [ type ] ) => type === 'abort' ) ?. [ 1 ] ;
2546+ expect ( abortListener ) . toBeDefined ( ) ;
2547+ expect ( removeSpy ) . toHaveBeenCalledWith ( 'abort' , abortListener ) ;
24682548
2549+ addSpy . mockRestore ( ) ;
2550+ removeSpy . mockRestore ( ) ;
24692551 ctx . destroy ( ) ;
24702552 } ) ;
24712553 } ) ;
0 commit comments