| 1 | import { selectSettingsValue, settingsOptionValues } from "./settingsSelectTestUtils"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { JSDOM } from "jsdom"; |
| 4 | import React, { act } from "react"; |
| 5 | import { createRoot } from "react-dom/client"; |
| 6 | import { ProviderCatalogPicker, type CatalogChoice } from "../components/ProviderCatalogPicker"; |
| 7 | import { LocaleProvider } from "../lib/i18n"; |
| 8 | import data from "../lib/providerCatalog.generated.json"; |
| 9 | const dom = new JSDOM('<div id="root"></div>', {url: "http://localhost/", pretendToBeVisual: true}); |
| 10 | Object.assign(globalThis, {window: dom.window, document: dom.window.document, HTMLElement: dom.window.HTMLElement, Node: dom.window.Node, Event: dom.window.Event, localStorage: dom.window.localStorage, IS_REACT_ACT_ENVIRONMENT: true}); |
| 11 | Object.defineProperty(globalThis, "navigator", {configurable: true, value: dom.window.navigator}); |
| 12 | window.matchMedia = (() => ({matches: true, addEventListener(){}, removeEventListener(){}})) as any; |
| 13 | const rootEl = document.getElementById("root")!; |
| 14 | const root = createRoot(rootEl); |
| 15 | const choices: CatalogChoice[] = Object.entries(data.catalogs).map(([id, catalog]) => ({ |
| 16 | id, catalog, label: id, keyEnv: `${catalog.region}_KEY`, keySet: false, models: [`${id}-model`], status: "available", statusLabel: "", actionLabel: "Add", canAdd: true, |
| 17 | })); |
| 18 | let installed = ""; |
| 19 | let submittedFormat: string | undefined; |
| 20 | const render = (busy = false, rows = choices) => root.render(<LocaleProvider><ProviderCatalogPicker choices={rows} busy={busy} onConnect={(id, _key, _url, format) => {installed = id; submittedFormat = format;}} onView={() => {}} onReset={() => {throw new Error("unexpected reset");}} /></LocaleProvider>); |
| 21 | await act(async () => {render();}); |
| 22 | const select = (dimension: string) => rootEl.querySelector<HTMLButtonElement>(`button.settings-select[id$="-${dimension}"]`)!; |
| 23 | async function change(dimension: string, value: string) { |
| 24 | await selectSettingsValue(select(dimension), value); |
| 25 | } |
| 26 | async function brand(name: string) { |
| 27 | await act(async () => {Array.from(rootEl.querySelectorAll<HTMLButtonElement>(".provider-catalog__brand")).find(el=>el.textContent===name)!.click();}); |
| 28 | } |
| 29 | assert.equal(rootEl.querySelectorAll(".provider-catalog__brand").length, new Set(choices.map(c=>c.catalog.brandId)).size); |
| 30 | await brand("智谱 / Z.AI"); |
| 31 | await change("region","cn"); |
| 32 | await change("product","coding"); |
| 33 | await change("format","anthropic"); |
| 34 | assert.equal(rootEl.querySelector<HTMLInputElement>('input[id$="-url"]')!.value,"https://open.bigmodel.cn/api/anthropic"); |
| 35 | await act(async () => { rootEl.querySelector<HTMLButtonElement>(".btn--primary")!.click(); }); |
| 36 | assert.equal(installed,"glm-coding-plan-cn-anthropic"); |
| 37 | await change("product","api"); |
| 38 | assert.equal(select("format").value,"openai"); |
| 39 | assert.equal((await settingsOptionValues(select("format"))).length,3,"all common protocols are offered"); |
| 40 | assert.equal(select("format").disabled,false); |
| 41 | await change("format","responses"); |
| 42 | await act(async () => { rootEl.querySelector<HTMLButtonElement>(".btn--primary")!.click(); }); |
| 43 | assert.equal(installed,"glm-cn"); |
| 44 | assert.equal(submittedFormat,"responses","custom protocol reaches connection creation"); |
| 45 | await change("format","openai"); |
| 46 | await change("region","global"); |
| 47 | assert.equal(rootEl.querySelector<HTMLInputElement>('input[id$="-url"]')!.value,"https://api.z.ai/api/paas/v4"); |
| 48 | await brand("OpenAI"); |
| 49 | await change("format","responses"); |
| 50 | await act(async () => {render(true);}); |
| 51 | assert.equal(select("format").disabled,true,"mutation lock disables protocol changes"); |
| 52 | await act(async () => {render(false,choices.map(c=>c.id==="openai-responses"?{...c,canAdd:false,status:"installed",statusLabel:"Added"}:c));}); |
| 53 | assert.equal(rootEl.querySelector<HTMLButtonElement>(".btn--primary")!.disabled,true,"updated installed status cannot submit again"); |
| 54 | assert.equal(select("format").value,"responses","host refresh preserves explicit selection"); |
| 55 | await change("format","openai"); |
| 56 | assert.equal(rootEl.querySelector<HTMLButtonElement>(".btn--primary")!.disabled,false,"another uninstalled route stays available"); |
| 57 | await brand("OpenCode"); |
| 58 | await change("product","go"); |
| 59 | await change("format","anthropic"); |
| 60 | assert.ok((await settingsOptionValues(select("variant"))).length>1,"model-scoped routes stay reachable without duplicating brands"); |
| 61 | await brand("LM Studio"); |
| 62 | assert.equal(rootEl.querySelector('button.settings-select[id$="-region"]'),null,"single-platform providers avoid unnecessary choices"); |
| 63 | assert.match(rootEl.textContent!,/local installation/); |
| 64 | await change("format","responses"); |
| 65 | assert.equal(rootEl.querySelector<HTMLInputElement>('input[id$="-url"]')!.value,"http://localhost:1234/v1"); |
| 66 | await change("format","anthropic"); |
| 67 | assert.equal(rootEl.querySelector<HTMLInputElement>('input[id$="-url"]')!.value,"http://localhost:1234"); |
| 68 | await brand("PPIO / 派欧云"); |
| 69 | assert.equal(rootEl.querySelector<HTMLInputElement>('input[id$="-url"]')!.value,"https://api.ppio.com/openai"); |
| 70 | await change("format","anthropic"); |
| 71 | assert.equal(rootEl.querySelector<HTMLInputElement>('input[id$="-url"]')!.value,"https://api.ppio.com/anthropic"); |
| 72 | await act(async () => { rootEl.querySelector<HTMLButtonElement>(".btn--primary")!.click(); }); |
| 73 | assert.equal(submittedFormat,"anthropic"); |
| 74 | // A legacy host can put the Anthropic preset first. New official connections |
| 75 | // must still start with Chat Completions, while explicit choices survive refresh. |
| 76 | const deepseekChoice = choices.find(c => c.catalog.brandId === "deepseek")!; |
| 77 | await act(async () => {render(false, [{...deepseekChoice, catalog: {...deepseekChoice.catalog, format: "anthropic"}}]);}); |
| 78 | assert.equal(select("format").value, "openai"); |
| 79 | assert.equal(rootEl.querySelector<HTMLInputElement>('input[id$="-url"]')!.value, "https://api.deepseek.com/v1"); |
| 80 | assert.deepEqual(await settingsOptionValues(select("format")), ["openai", "responses", "anthropic"]); |
| 81 | await act(async () => { rootEl.querySelector<HTMLButtonElement>(".btn--primary")!.click(); }); |
| 82 | assert.equal(submittedFormat, "openai"); |
| 83 | await change("format", "anthropic"); |
| 84 | await act(async () => {render(false, [deepseekChoice]);}); |
| 85 | assert.equal(select("format").value, "anthropic"); |
| 86 | assert.equal(rootEl.querySelector<HTMLInputElement>('input[id$="-url"]')!.value, "https://api.deepseek.com/anthropic"); |
| 87 | await act(async () => root.unmount()); |
| 88 | console.log("PASS provider catalog: grouping, constrained selection, exact install, refresh state, local setup"); |
| 89 |