@@ -451,4 +451,72 @@ describe("MediaApi.uploadMedia direct URL uploads", () => {
451451 expect ( tokenManager [ "getAccessToken" ] ) . not . toHaveBeenCalled ( ) ;
452452 expect ( client [ "request" ] ) . not . toHaveBeenCalled ( ) ;
453453 } ) ;
454+
455+ it ( "rejects promptly when a capture clone keeps body cancellation pending" , async ( ) => {
456+ fetchWithSsrFGuardMock . mockReset ( ) ;
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+ ) ;
465+ const captureClone = response . clone ( ) ;
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 ;
480+ } ) ;
481+ } ) ;
482+
483+ const client = mockApiClient ( ) ;
484+ const tokenManager = mockTokenManager ( ) ;
485+ 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" ) ;
494+
495+ try {
496+ await cancellationStarted ;
497+ expect ( cancellationSettled ) . toBe ( false ) ;
498+
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+ }
521+ } ) ;
454522} ) ;
0 commit comments