|
| 1 | +import { CohereClient, CohereClientV2 } from "../../src"; |
| 2 | + |
| 3 | +function stubFetch(): { fetch: typeof fetch; headers: () => Headers } { |
| 4 | + let captured: Headers = new Headers(); |
| 5 | + const fetchStub = (async (_input: unknown, init?: RequestInit) => { |
| 6 | + captured = new Headers(init?.headers as HeadersInit); |
| 7 | + return new Response(JSON.stringify({ id: "1", message: { role: "assistant" }, finish_reason: "COMPLETE" }), { |
| 8 | + status: 200, |
| 9 | + headers: { "Content-Type": "application/json" }, |
| 10 | + }); |
| 11 | + }) as unknown as typeof fetch; |
| 12 | + return { fetch: fetchStub, headers: () => captured }; |
| 13 | +} |
| 14 | + |
| 15 | +describe("optional auth", () => { |
| 16 | + beforeEach(() => { |
| 17 | + delete process.env.CO_API_KEY; |
| 18 | + }); |
| 19 | + |
| 20 | + it("omits the Authorization header when the token is empty", async () => { |
| 21 | + const stub = stubFetch(); |
| 22 | + const client = new CohereClient({ token: "", fetch: stub.fetch }); |
| 23 | + await client.v2.chat({ model: "command", messages: [] }); |
| 24 | + expect(stub.headers().has("Authorization")).toBe(false); |
| 25 | + }); |
| 26 | + |
| 27 | + // Regeneration canary. `authProvider` is not part of BaseClientOptions or |
| 28 | + // CohereClient.Options -- withOptionalAuth() smuggles it through the options bag, and it |
| 29 | + // survives only because generated normalizeClientOptionsWithAuth() in src/BaseClient.ts both |
| 30 | + // (a) preserves unknown properties from the caller's options and (b) uses `??=` rather than an |
| 31 | + // unconditional assignment. src/BaseClient.ts is generated and is NOT in .fernignore, so a |
| 32 | + // future regeneration could break either half. Because the property is untyped at that |
| 33 | + // boundary there would be no compile error -- auth would silently switch back on and empty |
| 34 | + // tokens would send `Bearer `. This test fails loudly instead. |
| 35 | + it("honors a caller-supplied authProvider (guards the generated options seam)", async () => { |
| 36 | + const stub = stubFetch(); |
| 37 | + const sentinel = { |
| 38 | + getAuthRequest: async () => ({ headers: { Authorization: "Bearer sentinel-provider" } }), |
| 39 | + }; |
| 40 | + const client = new CohereClient({ authProvider: sentinel, fetch: stub.fetch } as unknown as Record< |
| 41 | + string, |
| 42 | + never |
| 43 | + >); |
| 44 | + await client.v2.chat({ model: "command", messages: [] }); |
| 45 | + expect(stub.headers().get("Authorization")).toBe("Bearer sentinel-provider"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("omits the Authorization header when the token is empty on the v2 client", async () => { |
| 49 | + const stub = stubFetch(); |
| 50 | + const client = new CohereClientV2({ token: "", fetch: stub.fetch }); |
| 51 | + await client.chat({ model: "command", messages: [] }); |
| 52 | + expect(stub.headers().has("Authorization")).toBe(false); |
| 53 | + }); |
| 54 | + |
| 55 | + it("still sends the Authorization header when a token is provided", async () => { |
| 56 | + const stub = stubFetch(); |
| 57 | + const client = new CohereClient({ token: "test-token", fetch: stub.fetch }); |
| 58 | + await client.v2.chat({ model: "command", messages: [] }); |
| 59 | + expect(stub.headers().get("Authorization")).toBe("Bearer test-token"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("invokes a token supplier exactly once per request", async () => { |
| 63 | + let calls = 0; |
| 64 | + const stub = stubFetch(); |
| 65 | + const client = new CohereClient({ |
| 66 | + token: async () => { |
| 67 | + calls += 1; |
| 68 | + return "test-token"; |
| 69 | + }, |
| 70 | + fetch: stub.fetch, |
| 71 | + }); |
| 72 | + await client.v2.chat({ model: "command", messages: [] }); |
| 73 | + expect(stub.headers().get("Authorization")).toBe("Bearer test-token"); |
| 74 | + expect(calls).toBe(1); |
| 75 | + }); |
| 76 | + |
| 77 | + it("does not re-read a token supplier after the empty check passes", async () => { |
| 78 | + // A supplier whose value changes between calls must not be able to turn a valid token into |
| 79 | + // an empty `Bearer ` header. |
| 80 | + const values = ["real-token", ""]; |
| 81 | + const stub = stubFetch(); |
| 82 | + const client = new CohereClient({ token: async () => values.shift() ?? "", fetch: stub.fetch }); |
| 83 | + await client.v2.chat({ model: "command", messages: [] }); |
| 84 | + expect(stub.headers().get("Authorization")).toBe("Bearer real-token"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("falls back to CO_API_KEY when no token is provided", async () => { |
| 88 | + process.env.CO_API_KEY = "env-token"; |
| 89 | + const stub = stubFetch(); |
| 90 | + const client = new CohereClient({ fetch: stub.fetch }); |
| 91 | + await client.v2.chat({ model: "command", messages: [] }); |
| 92 | + expect(stub.headers().get("Authorization")).toBe("Bearer env-token"); |
| 93 | + }); |
| 94 | +}); |
0 commit comments