Skip to content

Commit b620df8

Browse files
committed
test(discovery): cover failure activation states
1 parent 404a521 commit b620df8

3 files changed

Lines changed: 367 additions & 4 deletions

File tree

‎gui/tests/models-empty-provider.test.tsx‎

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import { afterEach, beforeEach, expect, test } from "bun:test";
2+
import http2 from "node:http2";
23
import { renderToStaticMarkup } from "react-dom/server";
34
import { LanguageProvider } from "../src/i18n/provider";
45
import { EmptyProviderHint } from "../src/pages/Models";
56
import type { ProviderDiscoverySummary } from "../src/models-groups";
7+
import { gatherRoutedModels } from "../../src/codex/catalog";
8+
import {
9+
clearModelCache,
10+
getProviderDiscoveryStatus,
11+
markProviderDiscoveryFailed,
12+
type ProviderModelDiscoveryStatus,
13+
} from "../../src/codex/model-cache";
14+
import { handleManagementAPI } from "../../src/server/management-api";
615

716
let previousLanguage: unknown;
17+
const originalFetch = globalThis.fetch;
818

919
beforeEach(() => {
1020
previousLanguage = (globalThis.navigator as { language?: unknown } | undefined)?.language;
@@ -15,6 +25,8 @@ beforeEach(() => {
1525
});
1626

1727
afterEach(() => {
28+
globalThis.fetch = originalFetch;
29+
clearModelCache();
1830
Object.defineProperty(globalThis.navigator, "language", {
1931
configurable: true,
2032
value: previousLanguage,
@@ -29,6 +41,55 @@ function renderHint(liveModels: boolean, discovery?: ProviderDiscoverySummary):
2941
);
3042
}
3143

44+
async function providerDto(
45+
provider: string,
46+
adapter: "openai-chat" | "cursor" = "openai-chat",
47+
): Promise<Record<string, unknown>> {
48+
const requestUrl = new URL("http://127.0.0.1/api/providers");
49+
const response = await handleManagementAPI(
50+
new Request(requestUrl),
51+
requestUrl,
52+
{
53+
providers: {
54+
[provider]: {
55+
adapter,
56+
baseUrl: adapter === "cursor" ? "https://api2.cursor.sh" : "https://api.example.test/v1",
57+
models: [],
58+
},
59+
},
60+
},
61+
);
62+
const providers = await response!.json() as Array<Record<string, unknown>>;
63+
return providers[0] ?? {};
64+
}
65+
66+
async function withCursorDiscoveryServer<T>(
67+
status: number,
68+
run: (baseUrl: string) => Promise<T>,
69+
): Promise<T> {
70+
const server = http2.createServer();
71+
server.on("stream", stream => {
72+
stream.respond({ ":status": status, "content-type": "application/proto" });
73+
stream.end();
74+
});
75+
await new Promise<void>((resolve, reject) => {
76+
const onError = (error: Error) => reject(error);
77+
server.once("error", onError);
78+
server.listen(0, "127.0.0.1", () => {
79+
server.off("error", onError);
80+
resolve();
81+
});
82+
});
83+
84+
const address = server.address();
85+
if (!address || typeof address === "string") throw new Error("HTTP/2 fixture did not bind a TCP port");
86+
try {
87+
return await run(`http://127.0.0.1:${address.port}`);
88+
} finally {
89+
await new Promise<void>(resolve => server.close(() => resolve()));
90+
}
91+
}
92+
3293
test("empty live-discovery provider renders endpoint guidance and a settings link", () => {
3394
const html = renderHint(true, { status: "ok" });
3495
expect(html).toContain("No models were discovered");
@@ -62,6 +123,185 @@ test("failed discovery renders each server-owned reason without provider detail"
62123
}
63124
});
64125

126+
test("HTTP 401 discovery exposes HTTP status and badge", async () => {
127+
const provider = "activation-http-401";
128+
globalThis.fetch = (async () => new Response(null, { status: 401 })) as typeof fetch;
129+
130+
await gatherRoutedModels({
131+
providers: {
132+
[provider]: {
133+
adapter: "openai-chat",
134+
baseUrl: "https://93.184.216.34/v1",
135+
apiKey: "sk-test",
136+
},
137+
},
138+
});
139+
140+
const discovery = { status: "failed", reason: "http", httpStatus: 401 } as const;
141+
expect(getProviderDiscoveryStatus(provider)).toEqual(discovery);
142+
expect(await providerDto(provider)).toMatchObject({ discovery });
143+
const html = renderHint(true, discovery);
144+
expect(html).toContain("Discovery failed");
145+
expect(html).toContain("HTTP 401");
146+
expect(html).toContain('href="#providers"');
147+
});
148+
149+
test("destination-blocked discovery exposes blocked status and badge", async () => {
150+
const provider = "activation-blocked";
151+
let fetchCalls = 0;
152+
globalThis.fetch = (async () => {
153+
fetchCalls += 1;
154+
return Response.json({ data: [] });
155+
}) as typeof fetch;
156+
157+
const models = await gatherRoutedModels({
158+
providers: {
159+
[provider]: {
160+
adapter: "openai-chat",
161+
baseUrl: "http://198.18.0.1/v1",
162+
apiKey: "sk-test",
163+
models: ["static-fallback"],
164+
},
165+
},
166+
});
167+
168+
const discovery = { status: "failed", reason: "blocked" } as const;
169+
expect(fetchCalls).toBe(0);
170+
expect(models.map(model => model.id)).toEqual(["static-fallback"]);
171+
expect(getProviderDiscoveryStatus(provider)).toEqual(discovery);
172+
expect(await providerDto(provider)).toMatchObject({ discovery });
173+
const html = renderHint(true, discovery);
174+
expect(html).toContain("Discovery failed");
175+
expect(html).toContain("blocked by the destination policy");
176+
expect(html).toContain('href="#providers"');
177+
});
178+
179+
test("invalid JSON or malformed model data exposes invalid-response status and badge", async () => {
180+
const fixtures = [
181+
{ name: "invalid-json", body: "{not-json" },
182+
{ name: "missing-data", body: JSON.stringify({ models: [] }) },
183+
{ name: "malformed-data", body: JSON.stringify({ data: [{ id: 42 }] }) },
184+
];
185+
186+
for (const fixture of fixtures) {
187+
const provider = `activation-${fixture.name}`;
188+
globalThis.fetch = (async () => new Response(fixture.body, {
189+
status: 200,
190+
headers: { "content-type": "application/json" },
191+
})) as typeof fetch;
192+
const models = await gatherRoutedModels({
193+
providers: {
194+
[provider]: {
195+
adapter: "openai-chat",
196+
baseUrl: "https://93.184.216.34/v1",
197+
apiKey: "sk-test",
198+
models: ["static-fallback"],
199+
},
200+
},
201+
});
202+
203+
const discovery = { status: "failed", reason: "invalid_response" } as const;
204+
expect(models.map(model => model.id)).toEqual(["static-fallback"]);
205+
expect(getProviderDiscoveryStatus(provider)).toEqual(discovery);
206+
expect(await providerDto(provider)).toMatchObject({ discovery });
207+
const html = renderHint(true, discovery);
208+
expect(html).toContain("Discovery failed");
209+
expect(html).toContain("returned an invalid response");
210+
clearModelCache(provider);
211+
}
212+
});
213+
214+
test("network discovery failure exposes sanitized network status and badge", async () => {
215+
const provider = "activation-network";
216+
const sentinel = "SENTINEL-PRIVATE-URL-https://secret.invalid/account";
217+
globalThis.fetch = (async () => {
218+
throw new TypeError(sentinel);
219+
}) as typeof fetch;
220+
221+
await gatherRoutedModels({
222+
providers: {
223+
[provider]: {
224+
adapter: "openai-chat",
225+
baseUrl: "https://93.184.216.34/v1",
226+
apiKey: "sk-test",
227+
},
228+
},
229+
});
230+
231+
const discovery = { status: "failed", reason: "network" } as const;
232+
expect(getProviderDiscoveryStatus(provider)).toEqual(discovery);
233+
const dto = await providerDto(provider);
234+
expect(dto).toMatchObject({ discovery });
235+
const html = renderHint(true, discovery);
236+
expect(html).toContain("Discovery failed");
237+
expect(html).toContain("due to a network error");
238+
expect(JSON.stringify(dto)).not.toContain(sentinel);
239+
expect(html).not.toContain(sentinel);
240+
});
241+
242+
test("Cursor discovery failure exposes provider status and badge", async () => {
243+
const provider = "activation-cursor";
244+
const rawDetail = "HTTP 401";
245+
const models = await withCursorDiscoveryServer(401, baseUrl => gatherRoutedModels({
246+
providers: {
247+
[provider]: {
248+
adapter: "cursor",
249+
baseUrl,
250+
apiKey: "bad-token",
251+
models: ["auto"],
252+
},
253+
},
254+
}));
255+
256+
const discovery = { status: "failed", reason: "provider" } as const;
257+
expect(models.map(model => model.id)).toEqual(["auto"]);
258+
expect(getProviderDiscoveryStatus(provider)).toEqual(discovery);
259+
const dto = await providerDto(provider, "cursor");
260+
expect(dto).toMatchObject({ discovery });
261+
const html = renderHint(true, discovery);
262+
expect(html).toContain("Discovery failed");
263+
expect(html).toContain("provider reported a model discovery error");
264+
expect(JSON.stringify(dto)).not.toContain(rawDetail);
265+
expect(html).not.toContain(rawDetail);
266+
});
267+
268+
test("successful discovery clears every prior failure reason", async () => {
269+
const provider = "activation-reset";
270+
const failures: Array<Extract<ProviderModelDiscoveryStatus, { status: "failed" }>> = [
271+
{ status: "failed", reason: "blocked" },
272+
{ status: "failed", reason: "http", httpStatus: 401 },
273+
{ status: "failed", reason: "invalid_response" },
274+
{ status: "failed", reason: "network" },
275+
{ status: "failed", reason: "provider" },
276+
];
277+
globalThis.fetch = (async () => Response.json({ data: [] })) as typeof fetch;
278+
279+
for (const { status: _status, ...failure } of failures) {
280+
markProviderDiscoveryFailed(provider, failure);
281+
await gatherRoutedModels({
282+
modelCacheTtlMs: 0,
283+
providers: {
284+
[provider]: {
285+
adapter: "openai-chat",
286+
baseUrl: "https://93.184.216.34/v1",
287+
apiKey: "sk-test",
288+
},
289+
},
290+
});
291+
292+
const discovery = { status: "ok" } as const;
293+
expect(getProviderDiscoveryStatus(provider)).toEqual(discovery);
294+
expect(await providerDto(provider)).toMatchObject({ discovery });
295+
const html = renderHint(true, discovery);
296+
expect(html).toContain("No models were discovered");
297+
expect(html).not.toContain("Discovery failed");
298+
}
299+
300+
clearModelCache(provider);
301+
expect(getProviderDiscoveryStatus(provider)).toBeUndefined();
302+
expect(await providerDto(provider)).not.toHaveProperty("discovery");
303+
});
304+
65305
test("empty static provider explains that live discovery is disabled", () => {
66306
const html = renderHint(false);
67307
expect(html).toContain("Live model discovery is off");

0 commit comments

Comments
 (0)