@@ -297,6 +297,56 @@ describe("downloadMSTeamsGraphMedia hosted content $value fallback", () => {
297297 expect ( failedMessageResponse . bodyUsed ) . toBe ( true ) ;
298298 } ) ;
299299
300+ it ( "releases a cloned failed collection body without awaiting stalled cancellation" , async ( ) => {
301+ const failedCollectionResponse = new Response ( "server error" , { status : 500 } ) ;
302+ const captureClone = failedCollectionResponse . clone ( ) ;
303+ const body = failedCollectionResponse . body ;
304+ if ( ! body ) {
305+ throw new Error ( "expected a readable collection error body" ) ;
306+ }
307+ const originalCancel = body . cancel . bind ( body ) ;
308+ let cancellation : Promise < void > | undefined ;
309+ let cancellationSettled = false ;
310+ const cancellationStarted = new Promise < void > ( ( resolve ) => {
311+ vi . spyOn ( body , "cancel" ) . mockImplementation ( ( reason ) => {
312+ cancellation = originalCancel ( reason ) . finally ( ( ) => {
313+ cancellationSettled = true ;
314+ } ) ;
315+ resolve ( ) ;
316+ return cancellation ;
317+ } ) ;
318+ } ) ;
319+ const release = vi . fn ( async ( ) => { } ) ;
320+ vi . mocked ( fetchWithSsrFGuard ) . mockImplementation ( async ( params : GuardedFetchParams ) => {
321+ if ( params . url . endsWith ( "/hostedContents" ) ) {
322+ return guardedFetchResult ( params , failedCollectionResponse , release ) ;
323+ }
324+ return guardedFetchResult ( params , mockFetchResponse ( { body : { } , attachments : [ ] } ) ) ;
325+ } ) ;
326+
327+ const operation = downloadMSTeamsGraphMedia ( {
328+ messageUrl : "https://graph.microsoft.com/v1.0/chats/c/messages/msg-cloned-collection" ,
329+ tokenProvider : { getAccessToken : vi . fn ( async ( ) => "test-token" ) } ,
330+ maxBytes : 10 * 1024 * 1024 ,
331+ } ) ;
332+
333+ try {
334+ await cancellationStarted ;
335+ expect ( release ) . toHaveBeenCalledOnce ( ) ;
336+ expect ( failedCollectionResponse . bodyUsed ) . toBe ( true ) ;
337+ expect ( cancellationSettled ) . toBe ( false ) ;
338+ await expect ( operation ) . resolves . toMatchObject ( {
339+ media : [ ] ,
340+ hostedCount : 0 ,
341+ hostedStatus : 500 ,
342+ } ) ;
343+ } finally {
344+ void captureClone . body ?. cancel ( ) . catch ( ( ) => undefined ) ;
345+ await cancellation ?. catch ( ( ) => undefined ) ;
346+ await operation . catch ( ( ) => undefined ) ;
347+ }
348+ } ) ;
349+
300350 it ( "ignores unexpected inline bytes and still fetches bounded $value" , async ( ) => {
301351 const fetchCalls : string [ ] = [ ] ;
302352 const base64Png = Buffer . from ( [ 0x89 , 0x50 , 0x4e , 0x47 ] ) . toString ( "base64" ) ;
0 commit comments