@@ -452,44 +452,71 @@ describe("MediaApi.uploadMedia direct URL uploads", () => {
452452 expect ( client [ "request" ] ) . not . toHaveBeenCalled ( ) ;
453453 } ) ;
454454
455- it ( "does not hang when the rejected response body cancel never settles " , async ( ) => {
455+ it ( "rejects promptly when a capture clone keeps body cancellation pending " , async ( ) => {
456456 fetchWithSsrFGuardMock . mockReset ( ) ;
457- const { response, release } = mockGuardedResponse ( "server error" , { status : 500 } ) ;
457+ const response = new Response (
458+ new ReadableStream < Uint8Array > ( {
459+ start ( controller ) {
460+ controller . enqueue ( new TextEncoder ( ) . encode ( "server error" ) ) ;
461+ } ,
462+ } ) ,
463+ { status : 500 } ,
464+ ) ;
458465 const captureClone = response . clone ( ) ;
459- let cancelStarted = false ;
460- vi . spyOn ( response . body ! , "cancel" ) . mockImplementation ( ( ) => {
461- cancelStarted = true ;
462- // Mimic a debug-capture tee branch: cancellation only settles after the
463- // sibling clone branch is cancelled.
464- return new Promise < void > ( ( resolve ) => {
465- void captureClone . body
466- ?. cancel ( )
467- . catch ( ( ) => undefined )
468- . then ( resolve ) ;
466+ const release = vi . fn ( async ( ) => { } ) ;
467+ fetchWithSsrFGuardMock . mockResolvedValueOnce ( { response, release } ) ;
468+
469+ const body = response . body ! ;
470+ const originalCancel = body . cancel . bind ( body ) ;
471+ let cancellation : Promise < void > | undefined ;
472+ let cancellationSettled = false ;
473+ const cancellationStarted = new Promise < void > ( ( resolve ) => {
474+ vi . spyOn ( body , "cancel" ) . mockImplementation ( ( reason ) => {
475+ cancellation = originalCancel ( reason ) . finally ( ( ) => {
476+ cancellationSettled = true ;
477+ } ) ;
478+ resolve ( ) ;
479+ return cancellation ;
469480 } ) ;
470481 } ) ;
471482
472483 const client = mockApiClient ( ) ;
473484 const tokenManager = mockTokenManager ( ) ;
474485 const api = new MediaApi ( client , tokenManager ) ;
486+ const upload = api . uploadMedia (
487+ "c2c" ,
488+ "user-openid" ,
489+ MediaFileType . IMAGE ,
490+ { appId : "app-id" , clientSecret : "client-secret" } ,
491+ { url : "https://cdn.example.com/server-error.png" } ,
492+ ) ;
493+ const cancellationPending = Symbol ( "capture cancellation pending" ) ;
475494
476- const start = Date . now ( ) ;
477- await expect (
478- api . uploadMedia (
479- "c2c" ,
480- "user-openid" ,
481- MediaFileType . IMAGE ,
482- { appId : "app-id" , clientSecret : "client-secret" } ,
483- { url : "https://cdn.example.com/server-error.png" } ,
484- ) ,
485- ) . rejects . toThrow ( "Direct-upload media URL returned HTTP 500" ) ;
495+ try {
496+ await cancellationStarted ;
497+ expect ( cancellationSettled ) . toBe ( false ) ;
486498
487- // The rejection must return promptly; without the fire-and-forget fix this
488- // would wait indefinitely for the capture sibling branch to cancel.
489- expect ( Date . now ( ) - start ) . toBeLessThan ( 1000 ) ;
490- expect ( cancelStarted ) . toBe ( true ) ;
491- expect ( release ) . toHaveBeenCalledOnce ( ) ;
492- expect ( tokenManager [ "getAccessToken" ] ) . not . toHaveBeenCalled ( ) ;
493- expect ( client [ "request" ] ) . not . toHaveBeenCalled ( ) ;
499+ const result = await Promise . race ( [
500+ upload . then (
501+ ( ) => undefined ,
502+ ( error : unknown ) => error ,
503+ ) ,
504+ new Promise < symbol > ( ( resolve ) => {
505+ setImmediate ( ( ) => resolve ( cancellationPending ) ) ;
506+ } ) ,
507+ ] ) ;
508+
509+ expect ( result ) . not . toBe ( cancellationPending ) ;
510+ expect ( result ) . toMatchObject ( {
511+ message : "Direct-upload media URL returned HTTP 500" ,
512+ } ) ;
513+ expect ( release ) . toHaveBeenCalledOnce ( ) ;
514+ expect ( tokenManager [ "getAccessToken" ] ) . not . toHaveBeenCalled ( ) ;
515+ expect ( client [ "request" ] ) . not . toHaveBeenCalled ( ) ;
516+ } finally {
517+ void captureClone . body ?. cancel ( ) . catch ( ( ) => undefined ) ;
518+ await cancellation ?. catch ( ( ) => undefined ) ;
519+ await upload . catch ( ( ) => undefined ) ;
520+ }
494521 } ) ;
495522} ) ;
0 commit comments