|
| 1 | +import { beforeEach, describe, expect, test } from "bun:test"; |
| 2 | +import { XAI_OAUTH_PROXY_BASE_URL } from "@corbits/xai-provider"; |
| 3 | +import { |
| 4 | + FIRST_CLASS_PROVIDERS, |
| 5 | + OPENAI_API_MAX_COMPLETION_TOKENS_MODELS, |
| 6 | + firstClassProviderById, |
| 7 | +} from "../../packages/first-class-providers/src/index.js"; |
| 8 | +import { |
| 9 | + OPENCODE_GO_DEFAULT_MODEL, |
| 10 | + OPENCODE_GO_MODEL_IDS, |
| 11 | + isKnownGoModel, |
| 12 | +} from "../../packages/opencode-go/src/index.js"; |
| 13 | +import { |
| 14 | + ZEN_DEFAULT_MODEL, |
| 15 | + ZEN_MODEL_IDS, |
| 16 | + isKnownZenModel, |
| 17 | +} from "../../packages/zen/src/index.js"; |
| 18 | +import { |
| 19 | + CODEX_BASE_URL, |
| 20 | + CODEX_DEFAULT_MODELS, |
| 21 | +} from "../auth/codex/constants.js"; |
| 22 | +import type { CodexProfile } from "../auth/codex/store.js"; |
| 23 | +import type { XaiProfile } from "../auth/xai/store.js"; |
| 24 | +import { XAI_BASE_URL, XAI_DEFAULT_MODELS } from "../auth/xai/constants.js"; |
| 25 | +import { |
| 26 | + codexProfilesToCatalogEntries, |
| 27 | + codexProviderName, |
| 28 | +} from "../config/codex-providers.js"; |
| 29 | +import { |
| 30 | + xaiProfilesToCatalogEntries, |
| 31 | + xaiProviderName, |
| 32 | +} from "../config/xai-providers.js"; |
| 33 | +import { OAUTH_SURFACES } from "../tui/provider/choices.js"; |
| 34 | +import { |
| 35 | + resetGoModelDiscoveryForTests, |
| 36 | + selectableGoModelIds, |
| 37 | +} from "./opencode-go-models.js"; |
| 38 | +import { |
| 39 | + resetZenModelDiscoveryForTests, |
| 40 | + selectableZenModelIds, |
| 41 | +} from "./zen-models.js"; |
| 42 | + |
| 43 | +// CL-5691: provider/model identity unification. FIRST_CLASS_PROVIDERS is the |
| 44 | +// canonical static registry; the Codex/xAI live-fetch fallbacks stay separate |
| 45 | +// (they back live calls) but every auth path serving the same provider must |
| 46 | +// agree on identity metadata. These tests fail loudly on drift instead of |
| 47 | +// letting another list quietly diverge. |
| 48 | +describe("provider identity divergence", () => { |
| 49 | + beforeEach(() => { |
| 50 | + resetZenModelDiscoveryForTests(); |
| 51 | + resetGoModelDiscoveryForTests(); |
| 52 | + }); |
| 53 | + |
| 54 | + test("OpenAI API-key default and ChatGPT-OAuth fallback default agree", () => { |
| 55 | + const apiPath = firstClassProviderById("openai")?.paths?.find( |
| 56 | + (p) => p.id === "api", |
| 57 | + ); |
| 58 | + if (apiPath?.defaultModel === undefined) |
| 59 | + throw new Error("Expected the OpenAI API path to declare a defaultModel"); |
| 60 | + // Both auth paths serve OpenAI models, so the shared model must lead both |
| 61 | + // lists — the fallback default is models[0] by construction. |
| 62 | + const codexFallbacks: readonly string[] = CODEX_DEFAULT_MODELS; |
| 63 | + const codexDefault: string | undefined = codexFallbacks[0]; |
| 64 | + expect(codexDefault).toBe(apiPath.defaultModel); |
| 65 | + expect(codexFallbacks).toContain(apiPath.defaultModel); |
| 66 | + }); |
| 67 | + |
| 68 | + test("OpenAI API-key entry is self-consistent", () => { |
| 69 | + const apiPath = firstClassProviderById("openai")?.paths?.find( |
| 70 | + (p) => p.id === "api", |
| 71 | + ); |
| 72 | + expect(apiPath?.models).toContain(apiPath?.defaultModel); |
| 73 | + for (const model of apiPath?.maxCompletionTokensModels ?? []) { |
| 74 | + expect(apiPath?.models).toContain(model); |
| 75 | + } |
| 76 | + // The quirks reader resolves through the registry entry, so the exported |
| 77 | + // list must stay identical to it — never a second copy. |
| 78 | + expect([...OPENAI_API_MAX_COMPLETION_TOKENS_MODELS]).toEqual([ |
| 79 | + ...(apiPath?.maxCompletionTokensModels ?? []), |
| 80 | + ]); |
| 81 | + }); |
| 82 | + |
| 83 | + test("every first-class api-key default is a member of its models", () => { |
| 84 | + for (const def of FIRST_CLASS_PROVIDERS) { |
| 85 | + // Keyless entries (e.g. Ollama) carry an empty default and no models. |
| 86 | + if (def.defaultModel === undefined || def.defaultModel === "") continue; |
| 87 | + expect(def.models ?? []).toContain(def.defaultModel); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + test("Codex OAuth projection and surfaces track the live-fetch fallback", () => { |
| 92 | + expect(OAUTH_SURFACES.codex.baseURL).toBe(CODEX_BASE_URL); |
| 93 | + expect([...OAUTH_SURFACES.codex.models]).toEqual([...CODEX_DEFAULT_MODELS]); |
| 94 | + expect(OAUTH_SURFACES.codex.providerName("probe")).toBe( |
| 95 | + codexProviderName("probe"), |
| 96 | + ); |
| 97 | + const profile = { |
| 98 | + name: "probe", |
| 99 | + tokens: { access: "probe", refresh: "probe", expiresAt: 0 }, |
| 100 | + } as CodexProfile; |
| 101 | + const [entry] = codexProfilesToCatalogEntries([profile]); |
| 102 | + expect(entry?.baseURL).toBe(CODEX_BASE_URL); |
| 103 | + expect(entry?.models).toEqual([...CODEX_DEFAULT_MODELS]); |
| 104 | + expect(entry?.defaultModel).toBe(CODEX_DEFAULT_MODELS[0]); |
| 105 | + }); |
| 106 | + |
| 107 | + test("xAI OAuth projection and surfaces track the vendor fallback", () => { |
| 108 | + expect(XAI_BASE_URL).toBe(XAI_OAUTH_PROXY_BASE_URL); |
| 109 | + expect(OAUTH_SURFACES.xai.baseURL).toBe(XAI_OAUTH_PROXY_BASE_URL); |
| 110 | + expect([...OAUTH_SURFACES.xai.models]).toEqual([...XAI_DEFAULT_MODELS]); |
| 111 | + expect(OAUTH_SURFACES.xai.providerName("probe")).toBe( |
| 112 | + xaiProviderName("probe"), |
| 113 | + ); |
| 114 | + const profile = { |
| 115 | + name: "probe", |
| 116 | + tokens: { access: "probe", refresh: "probe", expiresAt: 0 }, |
| 117 | + } as XaiProfile; |
| 118 | + const [entry] = xaiProfilesToCatalogEntries([profile]); |
| 119 | + expect(entry?.baseURL).toBe(XAI_OAUTH_PROXY_BASE_URL); |
| 120 | + expect(entry?.models).toEqual([...XAI_DEFAULT_MODELS]); |
| 121 | + expect(entry?.defaultModel).toBe(XAI_DEFAULT_MODELS[0]); |
| 122 | + }); |
| 123 | + |
| 124 | + test("Zen registry entry tracks the packaged seed catalog", () => { |
| 125 | + const zen = firstClassProviderById("zen"); |
| 126 | + expect([...(zen?.models ?? [])]).toEqual([...ZEN_MODEL_IDS]); |
| 127 | + expect(zen?.defaultModel).toBe(ZEN_DEFAULT_MODEL); |
| 128 | + expect(ZEN_MODEL_IDS).toContain(ZEN_DEFAULT_MODEL); |
| 129 | + // Cold picker (no live snapshot yet) falls back to the same seed. |
| 130 | + expect([...selectableZenModelIds()]).toEqual([...ZEN_MODEL_IDS]); |
| 131 | + for (const id of ZEN_MODEL_IDS) { |
| 132 | + expect(isKnownZenModel(id)).toBe(true); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + test("OpenCode Go registry entry tracks the packaged seed catalog", () => { |
| 137 | + const go = firstClassProviderById("opencode-go"); |
| 138 | + expect([...(go?.models ?? [])]).toEqual([...OPENCODE_GO_MODEL_IDS]); |
| 139 | + expect(go?.defaultModel).toBe(OPENCODE_GO_DEFAULT_MODEL); |
| 140 | + expect(OPENCODE_GO_MODEL_IDS).toContain(OPENCODE_GO_DEFAULT_MODEL); |
| 141 | + expect([...selectableGoModelIds()]).toEqual([...OPENCODE_GO_MODEL_IDS]); |
| 142 | + for (const id of OPENCODE_GO_MODEL_IDS) { |
| 143 | + expect(isKnownGoModel(id)).toBe(true); |
| 144 | + } |
| 145 | + }); |
| 146 | +}); |
0 commit comments