@@ -8,6 +8,39 @@ import { describeQwenVideo } from "./media-understanding-provider.js";
88
99installPinnedHostnameTestHooks ( ) ;
1010
11+ function oversizedJsonResponse ( params : { chunkCount : number ; chunkSize : number } ) : {
12+ response : Response ;
13+ getReadCount : ( ) => number ;
14+ wasCanceled : ( ) => boolean ;
15+ } {
16+ const chunk = new Uint8Array ( params . chunkSize ) ;
17+ let readCount = 0 ;
18+ let canceled = false ;
19+ return {
20+ response : new Response (
21+ new ReadableStream < Uint8Array > ( {
22+ pull ( controller ) {
23+ if ( readCount >= params . chunkCount ) {
24+ controller . close ( ) ;
25+ return ;
26+ }
27+ readCount += 1 ;
28+ controller . enqueue ( chunk ) ;
29+ } ,
30+ cancel ( ) {
31+ canceled = true ;
32+ } ,
33+ } ) ,
34+ {
35+ status : 200 ,
36+ headers : { "Content-Type" : "application/json" } ,
37+ } ,
38+ ) ,
39+ getReadCount : ( ) => readCount ,
40+ wasCanceled : ( ) => canceled ,
41+ } ;
42+ }
43+
1144describe ( "describeQwenVideo" , ( ) => {
1245 it ( "builds the expected OpenAI-compatible video payload" , async ( ) => {
1346 const { fetchFn, getRequest } = createRequestCaptureJsonFetch ( {
@@ -74,4 +107,42 @@ describe("describeQwenVideo", () => {
74107 `data:video/mp4;base64,${ Buffer . from ( "video-bytes" ) . toString ( "base64" ) } ` ,
75108 ) ;
76109 } ) ;
110+
111+ it ( "bounds successful Qwen video JSON bodies instead of buffering the whole response" , async ( ) => {
112+ const streamed = oversizedJsonResponse ( { chunkCount : 64 , chunkSize : 1024 * 1024 } ) ;
113+
114+ await expect (
115+ describeQwenVideo ( {
116+ buffer : Buffer . from ( "video-bytes" ) ,
117+ fileName : "clip.mp4" ,
118+ mime : "video/mp4" ,
119+ apiKey : "test-key" ,
120+ timeoutMs : 1500 ,
121+ baseUrl : "https://example.com/v1" ,
122+ fetchFn : async ( ) => streamed . response ,
123+ } ) ,
124+ ) . rejects . toThrow ( "Qwen video description failed: JSON response exceeds 16777216 bytes" ) ;
125+
126+ expect ( streamed . getReadCount ( ) ) . toBeLessThan ( 64 ) ;
127+ expect ( streamed . wasCanceled ( ) ) . toBe ( true ) ;
128+ } ) ;
129+
130+ it ( "reports malformed Qwen video JSON with a provider-owned error" , async ( ) => {
131+ const response = new Response ( "not-json{" , {
132+ status : 200 ,
133+ headers : { "Content-Type" : "application/json" } ,
134+ } ) ;
135+
136+ await expect (
137+ describeQwenVideo ( {
138+ buffer : Buffer . from ( "video-bytes" ) ,
139+ fileName : "clip.mp4" ,
140+ mime : "video/mp4" ,
141+ apiKey : "test-key" ,
142+ timeoutMs : 1500 ,
143+ baseUrl : "https://example.com/v1" ,
144+ fetchFn : async ( ) => response ,
145+ } ) ,
146+ ) . rejects . toThrow ( "Qwen video description failed: malformed JSON response" ) ;
147+ } ) ;
77148} ) ;
0 commit comments