|
| 1 | +/** |
| 2 | + * Remote provider-config resolver tests: pure extraction + the two-stage |
| 3 | + * fetch (client/configs → CDN) with TTL / dedup / fail-open semantics. |
| 4 | + * All HTTP goes through the test fetch seam — never the network. |
| 5 | + */ |
| 6 | +import { describe, expect, it, beforeEach, afterEach } from "bun:test"; |
| 7 | +import { |
| 8 | + __resetHighspeedProviderConfigForTests, |
| 9 | + __setProviderConfigFetchForTests, |
| 10 | + extractHighspeedBaseUrls, |
| 11 | + resolvedHighspeedLlmUrl, |
| 12 | + warmHighspeedProviderConfig, |
| 13 | +} from "./provider-config.js"; |
| 14 | + |
| 15 | +function jsonResponse(body: unknown, status = 200): Response { |
| 16 | + return new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } }); |
| 17 | +} |
| 18 | + |
| 19 | +function builtinDoc(zai: string, bigmodel: string): unknown { |
| 20 | + return { |
| 21 | + schemaVersion: 2, |
| 22 | + revision: 43, |
| 23 | + config: { |
| 24 | + providerConfigRules: { |
| 25 | + providerRules: [ |
| 26 | + { providerId: "account:zai-individual-coding-plan", config: { api: { baseUrl: "https://api.z.ai/api/anthropic" } } }, |
| 27 | + { providerId: "account:zai-highspeed-card", config: { api: { baseUrl: zai } } }, |
| 28 | + { providerId: "account:bigmodel-highspeed-card", config: { api: { baseUrl: bigmodel } } }, |
| 29 | + ], |
| 30 | + }, |
| 31 | + }, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +const CONFIGS_BODY = { code: 0, data: { configs: { builtin_provider_config_json: "https://cdn.example.test/builtin.json" } } }; |
| 36 | + |
| 37 | +/** Fetch impl routing client/configs → body, everything else (CDN) → doc. */ |
| 38 | +function twoStageFetch(opts: { configs?: unknown; cdn?: unknown; configsStatus?: number } = {}) { |
| 39 | + const calls: string[] = []; |
| 40 | + const impl = (async (input: RequestInfo | URL) => { |
| 41 | + const url = String(input instanceof Request ? input.url : input); |
| 42 | + calls.push(url); |
| 43 | + if (url.includes("/api/v1/client/configs")) { |
| 44 | + return jsonResponse(opts.configs ?? CONFIGS_BODY, opts.configsStatus ?? 200); |
| 45 | + } |
| 46 | + return jsonResponse(opts.cdn ?? builtinDoc("https://cdn.example.test/hs/anthropic", "https://cdn.example.test/hs/anthropic")); |
| 47 | + }) as unknown as typeof fetch; |
| 48 | + return { impl, calls }; |
| 49 | +} |
| 50 | + |
| 51 | +const defaultNow = () => Date.now(); |
| 52 | + |
| 53 | +beforeEach(() => { |
| 54 | + __resetHighspeedProviderConfigForTests(); |
| 55 | + __setProviderConfigFetchForTests(null); |
| 56 | +}); |
| 57 | + |
| 58 | +afterEach(() => { |
| 59 | + __resetHighspeedProviderConfigForTests(); |
| 60 | + __setProviderConfigFetchForTests(null); |
| 61 | +}); |
| 62 | + |
| 63 | +describe("extractHighspeedBaseUrls", () => { |
| 64 | + it("extracts both families from a zcode-builtin shaped document", () => { |
| 65 | + const bases = extractHighspeedBaseUrls(builtinDoc("https://a.example.test/hs", "https://b.example.test/hs")); |
| 66 | + expect(bases.zai).toBe("https://a.example.test/hs"); |
| 67 | + expect(bases.bigmodel).toBe("https://b.example.test/hs"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns {} for malformed shapes", () => { |
| 71 | + for (const bad of [null, 42, "x", {}, { config: {} }, { config: { providerConfigRules: {} } }, |
| 72 | + { config: { providerConfigRules: { providerRules: "nope" } } }, |
| 73 | + // entries with non-string / blank baseUrl are skipped, not fatal |
| 74 | + { config: { providerConfigRules: { providerRules: [ |
| 75 | + { providerId: "account:zai-highspeed-card", config: { api: { baseUrl: 7 } } }, |
| 76 | + { providerId: "account:bigmodel-highspeed-card", config: { api: { baseUrl: " " } } }, |
| 77 | + ] } } }]) { |
| 78 | + expect(extractHighspeedBaseUrls(bad)).toEqual({}); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it("ignores unrelated provider ids and keeps a partial extraction", () => { |
| 83 | + const doc = { config: { providerConfigRules: { providerRules: [ |
| 84 | + { providerId: "account:zai-individual-coding-plan", config: { api: { baseUrl: "https://api.z.ai/api/anthropic" } } }, |
| 85 | + { providerId: "account:zai-highspeed-card", config: { api: { baseUrl: "https://hs.example.test/anthropic" } } }, |
| 86 | + ] } } }; |
| 87 | + expect(extractHighspeedBaseUrls(doc)).toEqual({ zai: "https://hs.example.test/anthropic" }); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +describe("warmHighspeedProviderConfig + resolvedHighspeedLlmUrl", () => { |
| 92 | + it("resolves the CDN base and appends /v1/messages per family", async () => { |
| 93 | + const { impl, calls } = twoStageFetch({ cdn: builtinDoc("https://a.example.test/hs/anthropic/", "https://b.example.test/hs/anthropic") }); |
| 94 | + __setProviderConfigFetchForTests(impl); |
| 95 | + await warmHighspeedProviderConfig({ origin: "https://zcode.z.ai", appVersion: "3.17.2", now: defaultNow }); |
| 96 | + // two-stage: configs then CDN, with app_version/platform params |
| 97 | + expect(calls.length).toBe(2); |
| 98 | + expect(calls[0]).toContain("app_version=3.17.2"); |
| 99 | + expect(calls[0]).toContain("platform=win32-x64"); |
| 100 | + expect(calls[1]).toBe("https://cdn.example.test/builtin.json"); |
| 101 | + // trailing slash on the base is trimmed before the SDK-style path join |
| 102 | + expect(resolvedHighspeedLlmUrl("zai")).toBe("https://a.example.test/hs/anthropic/v1/messages"); |
| 103 | + expect(resolvedHighspeedLlmUrl("bigmodel")).toBe("https://b.example.test/hs/anthropic/v1/messages"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("serves the snapshot within TTL without refetching, refetches after expiry", async () => { |
| 107 | + let clock = 1_000_000; |
| 108 | + const { impl, calls } = twoStageFetch(); |
| 109 | + __setProviderConfigFetchForTests(impl); |
| 110 | + await warmHighspeedProviderConfig({ origin: "https://zcode.z.ai", appVersion: "v", now: () => clock }); |
| 111 | + expect(calls.length).toBe(2); |
| 112 | + await warmHighspeedProviderConfig({ origin: "https://zcode.z.ai", appVersion: "v", now: () => clock + 60_000 }); |
| 113 | + expect(calls.length).toBe(2); // cached |
| 114 | + clock += 60 * 60 * 1000 + 1; |
| 115 | + await warmHighspeedProviderConfig({ origin: "https://zcode.z.ai", appVersion: "v", now: () => clock }); |
| 116 | + expect(calls.length).toBe(4); // TTL expired → fresh two-stage fetch |
| 117 | + }); |
| 118 | + |
| 119 | + it("deduplicates concurrent warm calls into one in-flight fetch pair", async () => { |
| 120 | + const { impl, calls } = twoStageFetch(); |
| 121 | + __setProviderConfigFetchForTests(impl); |
| 122 | + await Promise.all([ |
| 123 | + warmHighspeedProviderConfig({ origin: "https://zcode.z.ai", appVersion: "v", now: defaultNow }), |
| 124 | + warmHighspeedProviderConfig({ origin: "https://zcode.z.ai", appVersion: "v", now: defaultNow }), |
| 125 | + ]); |
| 126 | + expect(calls.length).toBe(2); |
| 127 | + }); |
| 128 | + |
| 129 | + it("fails open on configs error / missing CDN field / CDN error — default stays", async () => { |
| 130 | + for (const opts of [ |
| 131 | + { configsStatus: 500 }, // configs HTTP failure |
| 132 | + { configs: { data: { configs: {} } } }, // no builtin_provider_config_json |
| 133 | + { cdn: { nonsense: true } }, // CDN doc without our entries |
| 134 | + ] as const) { |
| 135 | + __resetHighspeedProviderConfigForTests(); |
| 136 | + const { impl } = twoStageFetch(opts as { configs?: unknown; cdn?: unknown; configsStatus?: number }); |
| 137 | + __setProviderConfigFetchForTests(impl); |
| 138 | + await warmHighspeedProviderConfig({ origin: "https://zcode.z.ai", appVersion: "v", now: defaultNow }); |
| 139 | + expect(resolvedHighspeedLlmUrl("zai")).toBeNull(); |
| 140 | + expect(resolvedHighspeedLlmUrl("bigmodel")).toBeNull(); |
| 141 | + } |
| 142 | + }); |
| 143 | + |
| 144 | + it("does not cache a document lacking our entries — the next warm refetches", async () => { |
| 145 | + let cdnBody: unknown = { config: { providerConfigRules: { providerRules: [] } } }; |
| 146 | + const calls: string[] = []; |
| 147 | + __setProviderConfigFetchForTests((async (input: RequestInfo | URL) => { |
| 148 | + const url = String(input instanceof Request ? input.url : input); |
| 149 | + calls.push(url); |
| 150 | + return url.includes("/api/v1/client/configs") |
| 151 | + ? jsonResponse(CONFIGS_BODY) |
| 152 | + : jsonResponse(cdnBody); |
| 153 | + }) as unknown as typeof fetch); |
| 154 | + await warmHighspeedProviderConfig({ origin: "https://zcode.z.ai", appVersion: "v", now: defaultNow }); |
| 155 | + expect(calls.length).toBe(2); |
| 156 | + expect(resolvedHighspeedLlmUrl("zai")).toBeNull(); |
| 157 | + cdnBody = builtinDoc("https://later.example.test/hs", "https://later.example.test/hs"); |
| 158 | + await warmHighspeedProviderConfig({ origin: "https://zcode.z.ai", appVersion: "v", now: defaultNow }); |
| 159 | + expect(calls.length).toBe(4); // nothing was cached → retried immediately |
| 160 | + expect(resolvedHighspeedLlmUrl("zai")).toBe("https://later.example.test/hs/v1/messages"); |
| 161 | + }); |
| 162 | + |
| 163 | + it("never throws on network errors (warm resolves, default stays)", async () => { |
| 164 | + __setProviderConfigFetchForTests((async () => { throw new Error("boom"); }) as unknown as typeof fetch); |
| 165 | + await warmHighspeedProviderConfig({ origin: "https://zcode.z.ai", appVersion: "v", now: defaultNow }); |
| 166 | + expect(resolvedHighspeedLlmUrl("zai")).toBeNull(); |
| 167 | + }); |
| 168 | +}); |
0 commit comments