|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { createOpenAIChatAdapter } from "../src/adapters/openai-chat"; |
| 5 | +import { gatherRoutedModels } from "../src/codex/catalog"; |
| 6 | +import { clearModelCache } from "../src/codex/model-cache"; |
| 7 | +import { buildInitProviders } from "../src/cli/init"; |
| 8 | +import { buildModelsRequest } from "../src/oauth"; |
| 9 | +import { KEY_LOGIN_PROVIDERS, validateApiKey } from "../src/oauth/key-providers"; |
| 10 | +import { |
| 11 | + deriveInitProviders, |
| 12 | + deriveProviderPresets, |
| 13 | + providerConfigSeed, |
| 14 | +} from "../src/providers/derive"; |
| 15 | +import { PROVIDER_REGISTRY, type ProviderRegistryEntry } from "../src/providers/registry"; |
| 16 | +import { routeModel } from "../src/router"; |
| 17 | +import type { OcxConfig, OcxProviderConfig } from "../src/types"; |
| 18 | +import { formatProviderDisplayName, isCatalogProviderId } from "../gui/src/provider-icons"; |
| 19 | +import { withStubbedProviderFetch } from "./helpers/catalog-provider-fetch"; |
| 20 | + |
| 21 | +const FIXTURE = readFileSync(join(import.meta.dir, "fixtures/apertis-models.json"), "utf8"); |
| 22 | +const BASE_URL = "https://api.apertis.ai/v1"; |
| 23 | +const API_KEY = "apertis-test-key"; |
| 24 | +const originalFetch = globalThis.fetch; |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + globalThis.fetch = originalFetch; |
| 28 | + clearModelCache("apertis"); |
| 29 | +}); |
| 30 | + |
| 31 | +function registryEntry(): ProviderRegistryEntry { |
| 32 | + const entry = PROVIDER_REGISTRY.find(row => row.id === "apertis"); |
| 33 | + if (!entry) throw new Error("missing apertis registry entry"); |
| 34 | + return entry; |
| 35 | +} |
| 36 | + |
| 37 | +function providerConfig(overrides: Partial<OcxProviderConfig> = {}): OcxConfig { |
| 38 | + return { |
| 39 | + port: 10100, |
| 40 | + defaultProvider: "apertis", |
| 41 | + providers: { |
| 42 | + apertis: { |
| 43 | + adapter: "openai-chat", |
| 44 | + baseUrl: BASE_URL, |
| 45 | + authMode: "key", |
| 46 | + apiKey: API_KEY, |
| 47 | + liveModels: true, |
| 48 | + // Discovery is fixture-only; this avoids platform-specific public-DNS classification. |
| 49 | + allowPrivateNetwork: true, |
| 50 | + ...overrides, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +describe("Apertis provider", () => { |
| 57 | + test("registers a fixed OpenAI transport with live discovery", () => { |
| 58 | + expect(registryEntry()).toMatchObject({ |
| 59 | + id: "apertis", |
| 60 | + label: "Apertis", |
| 61 | + adapter: "openai-chat", |
| 62 | + baseUrl: BASE_URL, |
| 63 | + authKind: "key", |
| 64 | + dashboardUrl: "https://apertis.ai/setting?tab=keys", |
| 65 | + liveModels: true, |
| 66 | + preserveCustomDestination: true, |
| 67 | + }); |
| 68 | + expect(registryEntry()).not.toHaveProperty("modelDiscovery"); |
| 69 | + }); |
| 70 | + |
| 71 | + test("derives CLI and dashboard presets without persisting registry trust policy", () => { |
| 72 | + const entry = registryEntry(); |
| 73 | + expect(buildInitProviders()).toEqual(deriveInitProviders()); |
| 74 | + expect(KEY_LOGIN_PROVIDERS.apertis).toMatchObject({ |
| 75 | + adapter: "openai-chat", |
| 76 | + baseUrl: BASE_URL, |
| 77 | + dashboardUrl: entry.dashboardUrl, |
| 78 | + liveModels: true, |
| 79 | + }); |
| 80 | + expect(buildInitProviders().find(row => row.id === "apertis")).toMatchObject({ |
| 81 | + kind: "key", |
| 82 | + adapter: "openai-chat", |
| 83 | + baseUrl: BASE_URL, |
| 84 | + }); |
| 85 | + expect(deriveProviderPresets().find(row => row.id === "apertis")).toMatchObject({ |
| 86 | + auth: "key", |
| 87 | + dashboardUrl: entry.dashboardUrl, |
| 88 | + }); |
| 89 | + |
| 90 | + const seed = providerConfigSeed(entry); |
| 91 | + expect(seed).toMatchObject({ |
| 92 | + adapter: "openai-chat", |
| 93 | + baseUrl: BASE_URL, |
| 94 | + authMode: "key", |
| 95 | + liveModels: true, |
| 96 | + }); |
| 97 | + expect(seed).not.toHaveProperty("modelDiscovery"); |
| 98 | + expect(seed).not.toHaveProperty("preserveCustomDestination"); |
| 99 | + expect(KEY_LOGIN_PROVIDERS.apertis).not.toHaveProperty("modelDiscovery"); |
| 100 | + expect(KEY_LOGIN_PROVIDERS.apertis).not.toHaveProperty("preserveCustomDestination"); |
| 101 | + expect(formatProviderDisplayName("apertis")).toBe("Apertis"); |
| 102 | + expect(isCatalogProviderId("apertis")).toBe(true); |
| 103 | + }); |
| 104 | + |
| 105 | + test("lists and validates models through the documented Bearer-authenticated endpoint", async () => { |
| 106 | + expect(buildModelsRequest(providerConfig().providers.apertis!, API_KEY, "apertis")).toEqual({ |
| 107 | + url: `${BASE_URL}/models`, |
| 108 | + headers: { Authorization: `Bearer ${API_KEY}` }, |
| 109 | + }); |
| 110 | + |
| 111 | + globalThis.fetch = (async (input, init) => { |
| 112 | + expect(String(input)).toBe(`${BASE_URL}/models`); |
| 113 | + expect(new Headers(init?.headers).get("authorization")).toBe(`Bearer ${API_KEY}`); |
| 114 | + expect(init?.redirect).toBe("error"); |
| 115 | + return new Response(FIXTURE, { |
| 116 | + status: 200, |
| 117 | + headers: { "content-type": "application/json" }, |
| 118 | + }); |
| 119 | + }) as typeof fetch; |
| 120 | + |
| 121 | + expect(await validateApiKey("apertis", KEY_LOGIN_PROVIDERS.apertis!, API_KEY)).toBe(true); |
| 122 | + }); |
| 123 | + |
| 124 | + test("discovers OpenAI-shaped model ids", async () => { |
| 125 | + globalThis.fetch = (async (input, init) => { |
| 126 | + expect(String(input)).toBe(`${BASE_URL}/models`); |
| 127 | + expect(new Headers(init?.headers).get("authorization")).toBe(`Bearer ${API_KEY}`); |
| 128 | + expect(init?.redirect).toBe("manual"); |
| 129 | + return new Response(FIXTURE, { |
| 130 | + status: 200, |
| 131 | + headers: { "content-type": "application/json" }, |
| 132 | + }); |
| 133 | + }) as typeof fetch; |
| 134 | + |
| 135 | + const models = await gatherRoutedModels(withStubbedProviderFetch(providerConfig())); |
| 136 | + expect(models.filter(row => row.provider === "apertis").map(row => row.id)).toEqual([ |
| 137 | + "claude-sonnet-4.5", |
| 138 | + "gpt-4.1", |
| 139 | + ]); |
| 140 | + }); |
| 141 | + |
| 142 | + test("routes chat completions to the fixed provider host", () => { |
| 143 | + const route = routeModel(providerConfig(), "apertis/gpt-4.1"); |
| 144 | + const request = createOpenAIChatAdapter(route.provider).buildRequest({ |
| 145 | + modelId: route.modelId, |
| 146 | + context: { messages: [{ role: "user", content: "ping", timestamp: 0 }] }, |
| 147 | + stream: true, |
| 148 | + options: {}, |
| 149 | + }); |
| 150 | + const body = JSON.parse(String(request.body)) as Record<string, unknown>; |
| 151 | + |
| 152 | + expect(request.url).toBe(`${BASE_URL}/chat/completions`); |
| 153 | + expect(request.headers.Authorization).toBe(`Bearer ${API_KEY}`); |
| 154 | + expect(body.model).toBe("gpt-4.1"); |
| 155 | + }); |
| 156 | + |
| 157 | + test("does not retarget same-named custom providers", () => { |
| 158 | + const customConfig = providerConfig({ baseUrl: "https://custom.example/v1" }); |
| 159 | + const route = routeModel(customConfig, "apertis/custom-model"); |
| 160 | + expect(route.provider).toMatchObject({ |
| 161 | + adapter: "openai-chat", |
| 162 | + baseUrl: "https://custom.example/v1", |
| 163 | + authMode: "key", |
| 164 | + }); |
| 165 | + expect(buildModelsRequest(customConfig.providers.apertis!, "custom-key", "apertis")).toEqual({ |
| 166 | + url: "https://custom.example/v1/models", |
| 167 | + headers: { Authorization: "Bearer custom-key" }, |
| 168 | + }); |
| 169 | + |
| 170 | + const customAdapter = routeModel(providerConfig({ |
| 171 | + adapter: "anthropic", |
| 172 | + baseUrl: "https://custom.example/anthropic", |
| 173 | + }), "apertis/custom-model"); |
| 174 | + expect(customAdapter.provider).toMatchObject({ |
| 175 | + adapter: "anthropic", |
| 176 | + baseUrl: "https://custom.example/anthropic", |
| 177 | + authMode: "key", |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments