| 1 | import { test } from "vitest"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { readFileSync } from "node:fs"; |
| 4 | import { parseProviderDescriptors } from "./provider-descriptors.mjs"; |
| 5 | interface TestRow { |
| 6 | kind: string; id: string; label: string; default_model: string; |
| 7 | env_vars: unknown[]; retired: boolean; selectable: boolean; |
| 8 | web?: { order: number; label?: string; env?: string }; |
| 9 | tui_wire_tag: string; catalog_id: string; catalog_source_id: string; |
| 10 | } |
| 11 | interface TestData { |
| 12 | schema_version: number; providers: TestRow[]; |
| 13 | constant_refs: Record<string, { provider: string; field: string }>; |
| 14 | } |
| 15 | const data: TestData = JSON.parse(readFileSync(new URL("../../crates/config/assets/provider_descriptors.json", import.meta.url), "utf8")); |
| 16 | |
| 17 | test("shared descriptor projection preserves OAuth explanation and released stable website identities", () => { |
| 18 | const facts = parseProviderDescriptors(data); |
| 19 | assert.ok(facts); |
| 20 | assert.equal(facts.defaultModel, "deepseek-flash"); |
| 21 | assert.equal(Object.keys(facts.labels).length, 50); |
| 22 | assert.equal(facts.labels["siliconflow-CN"].id, "siliconflow-CN"); |
| 23 | assert.equal(facts.labels.openai.label, "OpenAI-compatible"); |
| 24 | assert.match(facts.labels["openai-codex"].env, /ChatGPT OAuth/); |
| 25 | assert.deepEqual(["antigravity", "custom", "deepseek-cn"].filter(k => k in facts.labels), []); |
| 26 | }); |
| 27 | test("retired metadata cannot advertise the tombstone even with hostile public overrides", () => { |
| 28 | const fixture = structuredClone(data); |
| 29 | const retired = fixture.providers.find(r => r.kind === "Antigravity")!; |
| 30 | retired.web = { order: 0, label: "Enable retired route", env: "bad" }; |
| 31 | const facts = parseProviderDescriptors(fixture); |
| 32 | assert.ok(facts); |
| 33 | assert.equal(facts.labels.openai.id, "openai"); |
| 34 | retired.retired = false; |
| 35 | assert.equal(parseProviderDescriptors(fixture)?.labels.openai.id, "openai"); |
| 36 | }); |
| 37 | test("missing rows, duplicate identities, absent public labels and malformed defaults fail closed", () => { |
| 38 | const mutations: Array<(data: TestData) => void> = [ |
| 39 | d => d.schema_version = 2, |
| 40 | d => d.providers = [], |
| 41 | d => d.providers.push(d.providers[0]), |
| 42 | d => delete d.providers.find(r => r.kind === "Deepseek")!.web, |
| 43 | d => d.providers.find(r => r.kind === "Deepseek")!.env_vars = [123], |
| 44 | d => d.constant_refs.DEFAULT_TEXT_MODEL.provider = "not-installed", |
| 45 | d => d.constant_refs.DEFAULT_TEXT_MODEL.field = "base_url", |
| 46 | d => d.providers.find(r => r.kind === "Openai")!.web!.order = 0, |
| 47 | ]; |
| 48 | for (const mutate of mutations) { |
| 49 | const fixture = structuredClone(data); mutate(fixture); |
| 50 | assert.equal(parseProviderDescriptors(fixture), null); |
| 51 | } |
| 52 | assert.equal(parseProviderDescriptors("not JSON"), null); |
| 53 | }); |
| 54 | test("default and public rows follow supplied exact source without borrowing build labels", () => { |
| 55 | const fixture = structuredClone(data); |
| 56 | const row = fixture.providers.find(r => r.kind === "Deepseek")!; |
| 57 | row.default_model = "exact-remote"; row.label = "Exact remote label"; row.env_vars = ["EXACT_REMOTE_ENV"]; |
| 58 | const facts = parseProviderDescriptors(JSON.stringify(fixture)); |
| 59 | assert.ok(facts); |
| 60 | assert.equal(facts.defaultModel, "exact-remote"); |
| 61 | assert.deepEqual(facts.labels.deepseek, { id: "deepseek", label: "Exact remote label", env: "EXACT_REMOTE_ENV" }); |
| 62 | }); |
| 63 |