|
8 | 8 | discardUsageResponseBody, |
9 | 9 | fetchJson, |
10 | 10 | parseFiniteNumber, |
| 11 | + readUsageJson, |
11 | 12 | } from "./provider-usage.fetch.shared.js"; |
12 | 13 |
|
13 | 14 | function requireFetchCall( |
@@ -148,4 +149,88 @@ describe("provider usage fetch shared helpers", () => { |
148 | 149 |
|
149 | 150 | expect(snapshot.error).toBe("HTTP 429"); |
150 | 151 | }); |
| 152 | + |
| 153 | + describe("readUsageJson", () => { |
| 154 | + it("parses a normal-sized JSON response", async () => { |
| 155 | + const response = new Response( |
| 156 | + JSON.stringify({ windows: [{ label: "5h", usedPercent: 42 }] }), |
| 157 | + { status: 200, headers: { "Content-Type": "application/json" } }, |
| 158 | + ); |
| 159 | + |
| 160 | + await expect(readUsageJson("anthropic", response)).resolves.toEqual({ |
| 161 | + ok: true, |
| 162 | + data: { windows: [{ label: "5h", usedPercent: 42 }] }, |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it("parses UTF-8 BOM-prefixed JSON with fetch-compatible semantics", async () => { |
| 167 | + const bom = new Uint8Array([0xef, 0xbb, 0xbf]); |
| 168 | + const json = new TextEncoder().encode(JSON.stringify({ windows: [] })); |
| 169 | + const combined = new Uint8Array(bom.length + json.length); |
| 170 | + combined.set(bom); |
| 171 | + combined.set(json, bom.length); |
| 172 | + const response = new Response(combined, { |
| 173 | + status: 200, |
| 174 | + headers: { "Content-Type": "application/json" }, |
| 175 | + }); |
| 176 | + |
| 177 | + await expect(readUsageJson("anthropic", response)).resolves.toEqual({ |
| 178 | + ok: true, |
| 179 | + data: { windows: [] }, |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + it("rejects an oversized JSON response and cancels the stream", async () => { |
| 184 | + let pullCount = 0; |
| 185 | + const cancel = vi.fn(async () => undefined); |
| 186 | + const oversizedStream = new ReadableStream<Uint8Array>({ |
| 187 | + pull(controller) { |
| 188 | + pullCount += 1; |
| 189 | + controller.enqueue(new Uint8Array(pullCount === 1 ? 16 * 1024 * 1024 + 1 : 1)); |
| 190 | + }, |
| 191 | + cancel, |
| 192 | + }); |
| 193 | + const response = new Response(oversizedStream, { |
| 194 | + status: 200, |
| 195 | + headers: { "Content-Type": "application/json" }, |
| 196 | + }); |
| 197 | + |
| 198 | + await expect(readUsageJson("anthropic", response)).resolves.toEqual({ |
| 199 | + ok: false, |
| 200 | + snapshot: expect.objectContaining({ |
| 201 | + provider: "anthropic", |
| 202 | + error: "Malformed usage response", |
| 203 | + }), |
| 204 | + }); |
| 205 | + expect(pullCount).toBeLessThanOrEqual(2); |
| 206 | + expect(cancel).toHaveBeenCalledOnce(); |
| 207 | + }); |
| 208 | + |
| 209 | + it("handles a JSON parse error gracefully", async () => { |
| 210 | + const response = new Response("not-json", { |
| 211 | + status: 200, |
| 212 | + headers: { "Content-Type": "application/json" }, |
| 213 | + }); |
| 214 | + |
| 215 | + await expect(readUsageJson("openai", response)).resolves.toEqual({ |
| 216 | + ok: false, |
| 217 | + snapshot: expect.objectContaining({ |
| 218 | + provider: "openai", |
| 219 | + error: "Malformed usage response", |
| 220 | + }), |
| 221 | + }); |
| 222 | + }); |
| 223 | + |
| 224 | + it("preserves provider name in malformed error snapshots", async () => { |
| 225 | + const response = new Response("", { |
| 226 | + status: 200, |
| 227 | + headers: { "Content-Type": "application/json" }, |
| 228 | + }); |
| 229 | + |
| 230 | + await expect(readUsageJson("deepseek", response)).resolves.toEqual({ |
| 231 | + ok: false, |
| 232 | + snapshot: expect.objectContaining({ provider: "deepseek" }), |
| 233 | + }); |
| 234 | + }); |
| 235 | + }); |
151 | 236 | }); |
0 commit comments