test(extensions): use real chutes response mocks

This commit is contained in:
Vincent Koc
2026-06-23 22:00:32 +08:00
parent fa0427347a
commit fa263affd5
2 changed files with 46 additions and 48 deletions
+11 -4
View File
@@ -15,6 +15,14 @@ function restoreEnvVar(name: string, value: string | undefined): void {
}
}
function jsonResponse(payload: unknown, init: ResponseInit = {}): Response {
return new Response(JSON.stringify(payload), {
status: 200,
headers: { "Content-Type": "application/json" },
...init,
});
}
async function runChutesCatalog(params: { apiKey?: string; discoveryApiKey?: string }) {
const provider = await registerSingleProviderPlugin(plugin);
const result = await provider.catalog?.run({
@@ -44,10 +52,9 @@ async function withRealChutesDiscovery<T>(
delete process.env.VITEST;
delete process.env.NODE_ENV;
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ data: [{ id: "chutes/private-model" }] }),
});
const fetchMock = vi
.fn()
.mockResolvedValue(jsonResponse({ data: [{ id: "chutes/private-model" }] }));
globalThis.fetch = fetchMock as unknown as typeof fetch;
try {
+35 -44
View File
@@ -15,6 +15,14 @@ function restoreEnvVar(name: string, value: string | undefined): void {
}
}
function jsonResponse(payload: unknown, init: ResponseInit = {}): Response {
return new Response(JSON.stringify(payload), {
status: 200,
headers: { "Content-Type": "application/json" },
...init,
});
}
async function withLiveChutesDiscovery<T>(
fetchMock: ReturnType<typeof vi.fn>,
run: () => Promise<T>,
@@ -45,12 +53,11 @@ async function withLiveChutesDiscovery<T>(
function createAuthEchoFetchMock() {
return vi.fn().mockImplementation((_url, init?: { headers?: HeadersInit }) => {
const auth = readAuthorizationHeader(init);
return Promise.resolve({
ok: true,
json: async () => ({
return Promise.resolve(
jsonResponse({
data: [{ id: auth ? `${auth}-model` : "public-model" }],
}),
});
);
});
}
@@ -124,9 +131,8 @@ describe("chutes-models", () => {
});
it("discoverChutesModels correctly maps API response when not in test env", async () => {
const mockFetch = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
const mockFetch = vi.fn().mockResolvedValue(
jsonResponse({
data: [
{ id: "zai-org/GLM-4.7-TEE" },
{
@@ -140,7 +146,7 @@ describe("chutes-models", () => {
{ id: "new-provider/simple-model" },
],
}),
});
);
await withLiveChutesDiscovery(mockFetch, async () => {
const models = await discoverChutesModels("test-token-real-fetch");
expect(models.length).toBeGreaterThan(0);
@@ -158,9 +164,8 @@ describe("chutes-models", () => {
});
it("falls back from malformed live token metadata", async () => {
const mockFetch = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
const mockFetch = vi.fn().mockResolvedValue(
jsonResponse({
data: [
{
id: "provider/bad-window",
@@ -174,7 +179,7 @@ describe("chutes-models", () => {
},
],
}),
});
);
await withLiveChutesDiscovery(mockFetch, async () => {
const models = await discoverChutesModels("malformed-token-metadata");
@@ -195,14 +200,10 @@ describe("chutes-models", () => {
it("discoverChutesModels retries without auth on 401", async () => {
const mockFetch = vi.fn().mockImplementation((_url, init?: { headers?: HeadersInit }) => {
if (readAuthorizationHeader(init) === "Bearer test-token-error") {
return Promise.resolve({
ok: false,
status: 401,
});
return Promise.resolve(new Response("", { status: 401 }));
}
return Promise.resolve({
ok: true,
json: async () => ({
return Promise.resolve(
jsonResponse({
data: [
{
id: "Qwen/Qwen3-32B",
@@ -232,7 +233,7 @@ describe("chutes-models", () => {
},
],
}),
});
);
});
await withLiveChutesDiscovery(mockFetch, async () => {
const models = await discoverChutesModels("test-token-error");
@@ -242,10 +243,7 @@ describe("chutes-models", () => {
});
it("does not cache fallback static catalog for non-OK responses", async () => {
const mockFetch = vi.fn().mockResolvedValue({
ok: false,
status: 503,
});
const mockFetch = vi.fn().mockResolvedValue(new Response("", { status: 503 }));
await withLiveChutesDiscovery(mockFetch, async () => {
const first = await discoverChutesModels("chutes-fallback-token");
@@ -260,27 +258,24 @@ describe("chutes-models", () => {
const mockFetch = vi.fn().mockImplementation((_url, init?: { headers?: HeadersInit }) => {
const auth = readAuthorizationHeader(init);
if (auth === "Bearer chutes-token-a") {
return Promise.resolve({
ok: true,
json: async () => ({
return Promise.resolve(
jsonResponse({
data: [{ id: "private/model-a" }],
}),
});
);
}
if (auth === "Bearer chutes-token-b") {
return Promise.resolve({
ok: true,
json: async () => ({
return Promise.resolve(
jsonResponse({
data: [{ id: "private/model-b" }],
}),
});
);
}
return Promise.resolve({
ok: true,
json: async () => ({
return Promise.resolve(
jsonResponse({
data: [{ id: "public/model" }],
}),
});
);
});
await withLiveChutesDiscovery(mockFetch, async () => {
const modelsA = await discoverChutesModels("chutes-token-a");
@@ -325,17 +320,13 @@ describe("chutes-models", () => {
it("does not cache 401 fallback under the failed token key", async () => {
const mockFetch = vi.fn().mockImplementation((_url, init?: { headers?: HeadersInit }) => {
if (readAuthorizationHeader(init) === "Bearer failed-token") {
return Promise.resolve({
ok: false,
status: 401,
});
return Promise.resolve(new Response("", { status: 401 }));
}
return Promise.resolve({
ok: true,
json: async () => ({
return Promise.resolve(
jsonResponse({
data: [{ id: "public/model" }],
}),
});
);
});
await withLiveChutesDiscovery(mockFetch, async () => {
await discoverChutesModels("failed-token");