|
1 | 1 | import { expect, test } from "bun:test"; |
2 | 2 | import { routeModel } from "../src/router"; |
3 | | -import type { OcxConfig } from "../src/types"; |
| 3 | +import type { OcxConfig, OcxProviderConfig } from "../src/types"; |
4 | 4 |
|
5 | | -test("routing preserves resolved user URLs only for registry template providers", () => { |
6 | | - const config: OcxConfig = { |
| 5 | +const OVERRIDE_PROVIDERS = [ |
| 6 | + { id: "ollama", registryBaseUrl: "http://localhost:11434/v1" }, |
| 7 | + { id: "vllm", registryBaseUrl: "http://localhost:8000/v1" }, |
| 8 | + { id: "lm-studio", registryBaseUrl: "http://localhost:1234/v1" }, |
| 9 | + { id: "litellm", registryBaseUrl: "http://localhost:4000/v1" }, |
| 10 | +] as const; |
| 11 | + |
| 12 | +function configFor(providerName: string, provider: OcxProviderConfig): OcxConfig { |
| 13 | + return { |
7 | 14 | port: 10100, |
8 | | - defaultProvider: "azure-openai", |
9 | | - providers: { |
10 | | - "azure-openai": { |
11 | | - adapter: "azure-openai", |
12 | | - baseUrl: "https://myres.openai.azure.com/openai", |
13 | | - apiKey: "azure-key", |
14 | | - }, |
15 | | - "cloudflare-ai-gateway": { |
16 | | - adapter: "anthropic", |
17 | | - baseUrl: "https://gateway.ai.cloudflare.com/v1/my-account/my-gateway/anthropic", |
18 | | - apiKey: "cloudflare-key", |
19 | | - }, |
20 | | - anthropic: { |
21 | | - adapter: "anthropic", |
22 | | - baseUrl: "https://user-supplied.example.test/anthropic", |
23 | | - apiKey: "anthropic-key", |
24 | | - }, |
25 | | - }, |
| 15 | + defaultProvider: providerName, |
| 16 | + providers: { [providerName]: provider }, |
26 | 17 | }; |
| 18 | +} |
| 19 | + |
| 20 | +for (const { id, registryBaseUrl } of OVERRIDE_PROVIDERS) { |
| 21 | + test(`${id} trims and preserves a configured base URL override`, () => { |
| 22 | + const override = `http://${id}.lan:3210/v1`; |
| 23 | + const config = configFor(id, { |
| 24 | + adapter: "openai-chat", |
| 25 | + baseUrl: ` ${override} `, |
| 26 | + }); |
| 27 | + |
| 28 | + expect(routeModel(config, `${id}/model`).provider.baseUrl).toBe(override); |
| 29 | + }); |
| 30 | + |
| 31 | + test(`${id} accepts its resolved registry-default base URL`, () => { |
| 32 | + const config = configFor(id, { |
| 33 | + adapter: "openai-chat", |
| 34 | + baseUrl: registryBaseUrl, |
| 35 | + }); |
| 36 | + |
| 37 | + expect(routeModel(config, `${id}/model`).provider.baseUrl).toBe(registryBaseUrl); |
| 38 | + }); |
| 39 | + |
| 40 | + for (const [label, invalidBaseUrl] of [ |
| 41 | + ["blank", ""], |
| 42 | + ["whitespace-only", " \t"], |
| 43 | + ["unresolved placeholder", "http://{host}:8000/v1"], |
| 44 | + ] as const) { |
| 45 | + test(`${id} rejects a ${label} override instead of falling back`, () => { |
| 46 | + const config = configFor(id, { |
| 47 | + adapter: "openai-chat", |
| 48 | + baseUrl: invalidBaseUrl, |
| 49 | + }); |
| 50 | + |
| 51 | + expect(() => routeModel(config, `${id}/model`)) |
| 52 | + .toThrow(`Invalid baseUrl for provider "${id}": expected a nonblank URL without unresolved placeholders`); |
| 53 | + }); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +for (const { id, registryBaseUrl, adapter } of [ |
| 58 | + { id: "ollama-cloud", registryBaseUrl: "https://ollama.com/v1", adapter: "openai-chat" }, |
| 59 | + { id: "anthropic", registryBaseUrl: "https://api.anthropic.com", adapter: "anthropic" }, |
| 60 | +] as const) { |
| 61 | + test(`${id} keeps its fixed remote registry endpoint authoritative`, () => { |
| 62 | + const config = configFor(id, { |
| 63 | + adapter, |
| 64 | + baseUrl: "https://user-supplied.example.test/v1", |
| 65 | + }); |
| 66 | + |
| 67 | + expect(routeModel(config, `${id}/model`).provider.baseUrl).toBe(registryBaseUrl); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +for (const { id, adapter, registryTemplate, resolvedBaseUrl } of [ |
| 72 | + { |
| 73 | + id: "azure-openai", |
| 74 | + adapter: "azure-openai", |
| 75 | + registryTemplate: "https://{resource}.openai.azure.com/openai", |
| 76 | + resolvedBaseUrl: "https://myres.openai.azure.com/openai", |
| 77 | + }, |
| 78 | + { |
| 79 | + id: "cloudflare-ai-gateway", |
| 80 | + adapter: "anthropic", |
| 81 | + registryTemplate: "https://gateway.ai.cloudflare.com/v1/{account-id}/{gateway}/anthropic", |
| 82 | + resolvedBaseUrl: "https://gateway.ai.cloudflare.com/v1/my-account/my-gateway/anthropic", |
| 83 | + }, |
| 84 | +] as const) { |
| 85 | + test(`${id} keeps resolved template behavior unchanged`, () => { |
| 86 | + const config = configFor(id, { |
| 87 | + adapter, |
| 88 | + baseUrl: ` ${resolvedBaseUrl} `, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(routeModel(config, `${id}/model`).provider.baseUrl).toBe(resolvedBaseUrl); |
| 92 | + }); |
| 93 | + |
| 94 | + test(`${id} keeps unresolved template fallback behavior unchanged`, () => { |
| 95 | + const config = configFor(id, { |
| 96 | + adapter, |
| 97 | + baseUrl: registryTemplate, |
| 98 | + }); |
27 | 99 |
|
28 | | - expect(routeModel(config, "azure-openai/deployment").provider.baseUrl) |
29 | | - .toBe("https://myres.openai.azure.com/openai"); |
30 | | - expect(routeModel(config, "cloudflare-ai-gateway/claude-sonnet-5").provider.baseUrl) |
31 | | - .toBe("https://gateway.ai.cloudflare.com/v1/my-account/my-gateway/anthropic"); |
32 | | - expect(routeModel(config, "anthropic/claude-sonnet-5").provider.baseUrl) |
33 | | - .toBe("https://api.anthropic.com"); |
34 | | -}); |
| 100 | + expect(routeModel(config, `${id}/model`).provider.baseUrl).toBe(registryTemplate); |
| 101 | + }); |
| 102 | +} |
0 commit comments