|
| 1 | +import { createServer } from "node:http"; |
| 2 | +import type { AddressInfo } from "node:net"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { configureAiTransportHost } from "../host.js"; |
| 5 | +import type { Context, Model } from "../types.js"; |
| 6 | +import { streamAnthropic } from "./anthropic.js"; |
| 7 | + |
| 8 | +type CapturedRequest = { |
| 9 | + method: string; |
| 10 | + path: string; |
| 11 | + authorization?: string; |
| 12 | + apiKey?: string; |
| 13 | +}; |
| 14 | + |
| 15 | +const context = { |
| 16 | + messages: [{ role: "user", content: "hello", timestamp: 1 }], |
| 17 | +} satisfies Context; |
| 18 | + |
| 19 | +function makeModel(overrides: Partial<Model<"anthropic-messages">>) { |
| 20 | + return { |
| 21 | + id: "claude-sonnet-4-6", |
| 22 | + name: "Claude Sonnet 4.6", |
| 23 | + provider: "anthropic", |
| 24 | + api: "anthropic-messages", |
| 25 | + baseUrl: "https://api.anthropic.com", |
| 26 | + reasoning: true, |
| 27 | + input: ["text"], |
| 28 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, |
| 29 | + contextWindow: 200_000, |
| 30 | + maxTokens: 4_096, |
| 31 | + ...overrides, |
| 32 | + } satisfies Model<"anthropic-messages">; |
| 33 | +} |
| 34 | + |
| 35 | +afterEach(() => { |
| 36 | + configureAiTransportHost({}); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("Anthropic SDK host fetch wiring", () => { |
| 40 | + it("routes every non-Cloudflare client branch through the host fetch", async () => { |
| 41 | + const requests: CapturedRequest[] = []; |
| 42 | + const server = createServer((request, response) => { |
| 43 | + requests.push({ |
| 44 | + method: request.method ?? "", |
| 45 | + path: request.url ?? "", |
| 46 | + authorization: request.headers.authorization, |
| 47 | + apiKey: request.headers["x-api-key"] as string | undefined, |
| 48 | + }); |
| 49 | + response.writeHead(401, { "content-type": "application/json" }); |
| 50 | + response.end( |
| 51 | + JSON.stringify({ |
| 52 | + type: "error", |
| 53 | + error: { type: "authentication_error", message: "test rejection" }, |
| 54 | + }), |
| 55 | + ); |
| 56 | + }); |
| 57 | + await new Promise<void>((resolve) => { |
| 58 | + server.listen(0, "127.0.0.1", () => resolve()); |
| 59 | + }); |
| 60 | + |
| 61 | + const address = server.address() as AddressInfo; |
| 62 | + const baseUrl = `http://127.0.0.1:${address.port}`; |
| 63 | + const hostFetch = vi.fn<typeof fetch>((input, init) => globalThis.fetch(input, init)); |
| 64 | + const buildModelFetch = vi.fn(() => hostFetch); |
| 65 | + configureAiTransportHost({ buildModelFetch }); |
| 66 | + |
| 67 | + const cases = [ |
| 68 | + { |
| 69 | + model: makeModel({ provider: "github-copilot", baseUrl }), |
| 70 | + apiKey: "copilot-token", |
| 71 | + }, |
| 72 | + { |
| 73 | + model: makeModel({ provider: "microsoft-foundry", baseUrl, authHeader: true }), |
| 74 | + apiKey: "foundry-token", |
| 75 | + }, |
| 76 | + { |
| 77 | + model: makeModel({ baseUrl }), |
| 78 | + apiKey: "sk-ant-oat01-oauth-token", // pragma: allowlist secret |
| 79 | + }, |
| 80 | + { |
| 81 | + model: makeModel({ baseUrl }), |
| 82 | + apiKey: "sk-ant-api03-api-key", // pragma: allowlist secret |
| 83 | + }, |
| 84 | + { |
| 85 | + model: makeModel({ provider: "kimi-coding", baseUrl }), |
| 86 | + apiKey: "kimi-api-key", |
| 87 | + thinkingEnabled: true, |
| 88 | + }, |
| 89 | + ]; |
| 90 | + |
| 91 | + try { |
| 92 | + for (const testCase of cases) { |
| 93 | + const result = await streamAnthropic(testCase.model, context, { |
| 94 | + apiKey: testCase.apiKey, |
| 95 | + maxRetries: 0, |
| 96 | + thinkingEnabled: testCase.thinkingEnabled, |
| 97 | + }).result(); |
| 98 | + expect(result.stopReason).toBe("error"); |
| 99 | + } |
| 100 | + } finally { |
| 101 | + await new Promise<void>((resolve, reject) => { |
| 102 | + server.close((error) => (error ? reject(error) : resolve())); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + expect(hostFetch).toHaveBeenCalledTimes(cases.length); |
| 107 | + expect(requests).toEqual([ |
| 108 | + { |
| 109 | + method: "POST", |
| 110 | + path: "/v1/messages", |
| 111 | + authorization: "Bearer copilot-token", |
| 112 | + apiKey: undefined, |
| 113 | + }, |
| 114 | + { |
| 115 | + method: "POST", |
| 116 | + path: "/v1/messages", |
| 117 | + authorization: "Bearer foundry-token", |
| 118 | + apiKey: undefined, |
| 119 | + }, |
| 120 | + { |
| 121 | + method: "POST", |
| 122 | + path: "/v1/messages", |
| 123 | + authorization: "Bearer sk-ant-oat01-oauth-token", // pragma: allowlist secret |
| 124 | + apiKey: undefined, |
| 125 | + }, |
| 126 | + { |
| 127 | + method: "POST", |
| 128 | + path: "/v1/messages", |
| 129 | + authorization: undefined, |
| 130 | + apiKey: "sk-ant-api03-api-key", // pragma: allowlist secret |
| 131 | + }, |
| 132 | + { |
| 133 | + method: "POST", |
| 134 | + path: "/v1/messages", |
| 135 | + authorization: undefined, |
| 136 | + apiKey: "kimi-api-key", |
| 137 | + }, |
| 138 | + ]); |
| 139 | + expect(buildModelFetch).toHaveBeenLastCalledWith(cases.at(-1)?.model, undefined, { |
| 140 | + sanitizeSse: false, |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments