@@ -8,6 +8,39 @@ import { describeQwenVideo } from "./media-understanding-provider.js";
|
8 | 8 | |
9 | 9 | installPinnedHostnameTestHooks(); |
10 | 10 | |
| 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 | + |
11 | 44 | describe("describeQwenVideo", () => { |
12 | 45 | it("builds the expected OpenAI-compatible video payload", async () => { |
13 | 46 | const { fetchFn, getRequest } = createRequestCaptureJsonFetch({ |
@@ -74,4 +107,42 @@ describe("describeQwenVideo", () => {
|
74 | 107 | `data:video/mp4;base64,${Buffer.from("video-bytes").toString("base64")}`, |
75 | 108 | ); |
76 | 109 | }); |
| 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 | +}); |
77 | 148 | }); |