@@ -18,37 +18,45 @@ const valid = JSON.stringify({ |
18 | 18 |
|
19 | 19 | describe("fetchFreeModels", () => { |
20 | 20 | test("returns the non-deprecated zero-cost models from a valid response", async () => { |
21 | | - const load = (async () => new Response(valid)) as typeof fetch |
22 | | - const models = await fetchFreeModels({ fetch: load }) |
| 21 | + const models = await fetchFreeModels({ fetch: async () => new Response(valid) }) |
23 | 22 | expect(models.map((model) => model.id)).toEqual(["free"]) |
24 | 23 | }) |
25 | 24 |
|
26 | 25 | test("a stalled request settles within the deadline", async () => { |
27 | | - const load = ((_input: RequestInfo | URL, init?: RequestInit) => |
28 | | - new Promise<Response>((_resolve, reject) => { |
29 | | - init?.signal?.addEventListener("abort", () => reject(init.signal?.reason)) |
30 | | - })) as typeof fetch |
31 | 26 | const start = Date.now() |
32 | | - await expect(fetchFreeModels({ fetch: load, timeoutMs: 25 })).rejects.toThrow() |
| 27 | + await expect( |
| 28 | + fetchFreeModels({ |
| 29 | + timeoutMs: 25, |
| 30 | + fetch: (_input, init) => |
| 31 | + new Promise<Response>((_resolve, reject) => { |
| 32 | + init?.signal?.addEventListener("abort", () => reject(init.signal?.reason)) |
| 33 | + }), |
| 34 | + }), |
| 35 | + ).rejects.toThrow() |
33 | 36 | expect(Date.now() - start).toBeLessThan(5_000) |
34 | 37 | }) |
35 | 38 |
|
36 | 39 | test("a never-ending response body settles within the deadline", async () => { |
37 | | - const load = (async (_input: RequestInfo | URL, init?: RequestInit) => { |
38 | | - const body = new ReadableStream<Uint8Array>({ |
39 | | - start(controller) { |
40 | | - init?.signal?.addEventListener("abort", () => controller.error(init.signal?.reason)) |
41 | | - }, |
42 | | - }) |
43 | | - return new Response(body) |
44 | | - }) as typeof fetch |
45 | 40 | const start = Date.now() |
46 | | - await expect(fetchFreeModels({ fetch: load, timeoutMs: 25 })).rejects.toThrow() |
| 41 | + await expect( |
| 42 | + fetchFreeModels({ |
| 43 | + timeoutMs: 25, |
| 44 | + fetch: async (_input, init) => |
| 45 | + new Response( |
| 46 | + new ReadableStream<Uint8Array>({ |
| 47 | + start(controller) { |
| 48 | + init?.signal?.addEventListener("abort", () => controller.error(init.signal?.reason)) |
| 49 | + }, |
| 50 | + }), |
| 51 | + ), |
| 52 | + }), |
| 53 | + ).rejects.toThrow() |
47 | 54 | expect(Date.now() - start).toBeLessThan(5_000) |
48 | 55 | }) |
49 | 56 |
|
50 | 57 | test("an over-limit body is rejected before full buffering", async () => { |
51 | | - const load = (async () => new Response(valid)) as typeof fetch |
52 | | - await expect(fetchFreeModels({ fetch: load, maxBytes: 16 })).rejects.toThrow("exceeded 16 bytes") |
| 58 | + await expect(fetchFreeModels({ fetch: async () => new Response(valid), maxBytes: 16 })).rejects.toThrow( |
| 59 | + "exceeded 16 bytes", |
| 60 | + ) |
53 | 61 | }) |
54 | 62 | }) |