|
| 1 | +import { afterEach, expect, test } from "bun:test"; |
| 2 | +import { BaseApiClient } from "../../src/internal/providers/base-client.ts"; |
| 3 | +import { type FetchLike, resolveFetch, setDefaultFetch } from "../../src/internal/transport.ts"; |
| 4 | + |
| 5 | +class TestClient extends BaseApiClient { |
| 6 | + protected baseUrl = "https://example.test"; |
| 7 | + protected errorPrefix = "test"; |
| 8 | + protected paginationStrategy = "page" as const; |
| 9 | + |
| 10 | + protected headers(): Record<string, string> { |
| 11 | + return { "Content-Type": "application/json", Authorization: "Bearer k" }; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + setDefaultFetch(undefined); |
| 17 | +}); |
| 18 | + |
| 19 | +test("resolveFetch falls back to the current globalThis.fetch when nothing is installed", async () => { |
| 20 | + const originalFetch = globalThis.fetch; |
| 21 | + globalThis.fetch = (async () => |
| 22 | + new Response(JSON.stringify({ id: "from-global" }), { |
| 23 | + status: 200, |
| 24 | + })) as unknown as typeof fetch; |
| 25 | + |
| 26 | + try { |
| 27 | + const res = await new TestClient().get("/x"); |
| 28 | + expect(res).toEqual({ id: "from-global" }); |
| 29 | + } finally { |
| 30 | + globalThis.fetch = originalFetch; |
| 31 | + } |
| 32 | +}); |
| 33 | + |
| 34 | +test("setDefaultFetch routes provider client requests through the installed implementation", async () => { |
| 35 | + const captured: Array<{ |
| 36 | + url: string; |
| 37 | + method?: string; |
| 38 | + headers: Record<string, string>; |
| 39 | + }> = []; |
| 40 | + const installed: FetchLike = async (input, init) => { |
| 41 | + captured.push({ |
| 42 | + url: typeof input === "string" ? input : input instanceof URL ? input.href : input.url, |
| 43 | + method: init?.method, |
| 44 | + headers: (init?.headers ?? {}) as Record<string, string>, |
| 45 | + }); |
| 46 | + return new Response(JSON.stringify({ id: "from-installed" }), { |
| 47 | + status: 200, |
| 48 | + }); |
| 49 | + }; |
| 50 | + setDefaultFetch(installed); |
| 51 | + |
| 52 | + const res = await new TestClient().post("/agents", { name: "a" }); |
| 53 | + expect(res).toEqual({ id: "from-installed" }); |
| 54 | + expect(captured).toEqual([ |
| 55 | + { |
| 56 | + url: "https://example.test/agents", |
| 57 | + method: "POST", |
| 58 | + headers: { |
| 59 | + "Content-Type": "application/json", |
| 60 | + Authorization: "Bearer k", |
| 61 | + }, |
| 62 | + }, |
| 63 | + ]); |
| 64 | +}); |
| 65 | + |
| 66 | +test("installed implementation still gets ApiError semantics from the client (non-ok response)", async () => { |
| 67 | + setDefaultFetch(async () => new Response("missing", { status: 404 })); |
| 68 | + |
| 69 | + const err = (await new TestClient().get("/missing").catch((error: unknown) => error)) as Error & { |
| 70 | + statusCode?: number; |
| 71 | + }; |
| 72 | + expect(err.statusCode).toBe(404); |
| 73 | +}); |
| 74 | + |
| 75 | +test("setDefaultFetch(undefined) resets back to the global fetch", async () => { |
| 76 | + setDefaultFetch(async () => new Response("{}", { status: 200 })); |
| 77 | + setDefaultFetch(undefined); |
| 78 | + |
| 79 | + const originalFetch = globalThis.fetch; |
| 80 | + let globalHit = false; |
| 81 | + globalThis.fetch = (async () => { |
| 82 | + globalHit = true; |
| 83 | + return new Response("{}", { status: 200 }); |
| 84 | + }) as unknown as typeof fetch; |
| 85 | + |
| 86 | + try { |
| 87 | + await resolveFetch()("https://example.test/ping"); |
| 88 | + expect(globalHit).toBe(true); |
| 89 | + } finally { |
| 90 | + globalThis.fetch = originalFetch; |
| 91 | + } |
| 92 | +}); |
0 commit comments