@@ -3,7 +3,11 @@ import { mkdir, mkdtemp, realpath, rm, writeFile } from "node:fs/promises";
33import os from "node:os" ;
44import path from "node:path" ;
55import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
6- import { readRemoteMediaBufferSpy , telegramBotDepsForTest } from "./bot.media.e2e-harness.js" ;
6+ import {
7+ readRemoteMediaBufferSpy ,
8+ resetReadRemoteMediaBufferMock ,
9+ telegramBotDepsForTest ,
10+ } from "./bot.media.e2e-harness.js" ;
711import {
812 TELEGRAM_TEST_TIMINGS ,
913 cacheStickerSpy ,
@@ -270,6 +274,24 @@ describe("telegram local Bot API media", () => {
270274 await rm ( tempRoot , { recursive : true , force : true } ) ;
271275 }
272276 } ) ;
277+
278+ it ( "cancels response body on non-ok status before throwing" , async ( ) => {
279+ const cancel = vi . fn ( ) ;
280+ const body = new ReadableStream < Uint8Array > ( { cancel } ) ;
281+ const response = new Response ( body , { status : 404 , statusText : "Not Found" } ) ;
282+ const fetchImpl = vi . fn ( ) . mockResolvedValue ( response ) ;
283+
284+ resetReadRemoteMediaBufferMock ( ) ;
285+
286+ await expect (
287+ readRemoteMediaBufferSpy ( {
288+ url : "https://example.com/file.jpg" ,
289+ fetchImpl : fetchImpl as unknown as typeof fetch ,
290+ } ) ,
291+ ) . rejects . toThrow ( "Failed to fetch media from https://example.com/file.jpg" ) ;
292+
293+ expect ( cancel ) . toHaveBeenCalledOnce ( ) ;
294+ } ) ;
273295} ) ;
274296
275297describe ( "telegram text fragments" , ( ) => {
0 commit comments