mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-27 01:22:10 +03:00
* fix(ci): route the Gemini Web b64_json download error through sanitizeErrorMessage() open-sse/handlers/imageGeneration/providers/geminiWeb.ts embedded a raw err.message in the b64_json download-failure response, tripping check:error-helper (Hard Rule #12) on release/v3.8.50. Refs #9985. * fix(tests): drain test-drift base-reds left by #10603/#10537 and a stale qwen-web catalog id Several base-reds on release/v3.8.50 (#9985) share one root cause: a legitimate product change landed without updating the test asserting the old behavior. - tests/unit/glm-provider-model-import-route.test.ts (12 tests) and tests/unit/model-sync-route.test.ts (2 tests) predate #10603, which made upstream model sync opt-in (isAutoFetchModelsEnabled() now requires providerSpecificData.autoFetchModels === true) and made manual custom-model overrides survive a sync instead of being demoted. Updated both files to opt in / assert the new preserve-manual-overrides behavior, with a comment citing #10603. - tests/unit/antigravity-model-aliases.test.ts predates #10537, which retired the collapsed 'gemini-3.7-flash' alias (upstream 'gemini-3.7-flash-tiered') in favor of the three directly-callable tiered ids. Dropped the retired id from EXPECTED_FLASH_TIERS. - open-sse/config/freeModelCatalog.data.ts: the qwen-web free-catalog entry still listed the retired 'qwen3.8-max-preview' id instead of the current 'qwen3.8-max' (open-sse/config/providers/registry/qwen/web/index.ts and the executor's compat alias both confirm 'qwen3.8-max' is canonical). Real data drift, not test drift. - src/i18n/messages/zh-TW.json: providers.autoFetchModelsTooltip (added by #10603) used the mainland term 緩存 instead of the zh-TW glossary-canonical 快取, tripping the i18n-glossary-consistency-check base-red. - src/lib/oauth/providers/zed-hosted.ts: removed an unused default export (the named export already covers every consumer) — shaves one symbol off the check:dead-code ratchet (419 -> 418; baseline 415, 3 still outstanding). Refs #9985. --------- Co-authored-by: Markus Hartung <mail@hartmark.se>
445 lines
15 KiB
TypeScript
445 lines
15 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
// #10603 made upstream model sync opt-in (isAutoFetchModelsEnabled() now requires
|
|
// providerSpecificData.autoFetchModels === true) so remote discovery doesn't overwrite
|
|
// manual catalog overrides by default. Every connection below sets it explicitly so the
|
|
// mocked `fetch` in each test actually gets called — without it, the route short-circuits
|
|
// to the local/cached catalog before ever reaching the network call these tests assert on.
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-glm-models-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const providersDb = await import("../../src/lib/db/providers.ts");
|
|
const modelsRoute = await import("../../src/app/api/providers/[id]/models/route.ts");
|
|
|
|
async function resetStorage() {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
}
|
|
|
|
test.after(() => {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
});
|
|
|
|
test("GLM import uses international coding endpoint when apiRegion is international", async () => {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "glm",
|
|
authType: "apikey",
|
|
name: "glm-intl",
|
|
apiKey: "glm-key",
|
|
providerSpecificData: { apiRegion: "international", autoFetchModels: true },
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
assert.equal(String(url), "https://api.z.ai/api/coding/paas/v4/models");
|
|
assert.equal(init.headers.Authorization, "Bearer glm-key");
|
|
assert.equal(init.headers["x-api-key"], undefined);
|
|
assert.equal(init.headers["anthropic-version"], undefined);
|
|
return Response.json({ data: [{ id: "glm-5", name: "GLM 5" }] });
|
|
};
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
assert.deepEqual(await response.json(), {
|
|
provider: "glm",
|
|
connectionId: connection.id,
|
|
models: [{ id: "glm-5", name: "GLM 5" }],
|
|
source: "api",
|
|
});
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("GLM import normalizes custom coding models URLs without duplicating endpoints", async () => {
|
|
await resetStorage();
|
|
// Distinct apiKeys per connection: createProviderConnection dedups by decrypted
|
|
// key value (#3023), so a shared key would collapse these two custom-endpoint
|
|
// connections into one and the second would serve from the first's cached
|
|
// catalog (only one discovery fetch instead of two).
|
|
const cases = [
|
|
{
|
|
baseUrl: "https://api.z.ai/api/coding/paas/v4",
|
|
expectedUrl: "https://api.z.ai/api/coding/paas/v4/models",
|
|
apiKey: "glm-key-0",
|
|
},
|
|
{
|
|
baseUrl: "https://api.z.ai/api/coding/paas/v4/models",
|
|
expectedUrl: "https://api.z.ai/api/coding/paas/v4/models",
|
|
apiKey: "glm-key-1",
|
|
},
|
|
];
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
const seenUrls: string[] = [];
|
|
const connections = [];
|
|
|
|
for (const [index, testCase] of cases.entries()) {
|
|
connections.push(
|
|
await providersDb.createProviderConnection({
|
|
provider: "glm",
|
|
authType: "apikey",
|
|
name: `glm-custom-${index}`,
|
|
apiKey: testCase.apiKey,
|
|
providerSpecificData: { baseUrl: testCase.baseUrl, autoFetchModels: true },
|
|
})
|
|
);
|
|
}
|
|
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
const expected = cases[seenUrls.length];
|
|
assert.ok(expected, `unexpected GLM discovery call to ${String(url)}`);
|
|
assert.equal(String(url), expected.expectedUrl);
|
|
assert.equal(init.headers.Authorization, `Bearer ${expected.apiKey}`);
|
|
assert.equal(init.headers["x-api-key"], undefined);
|
|
assert.equal(init.headers["anthropic-version"], undefined);
|
|
seenUrls.push(String(url));
|
|
return Response.json({ data: [{ id: "glm-5", name: "GLM 5" }] });
|
|
};
|
|
|
|
try {
|
|
for (const connection of connections) {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
}
|
|
|
|
assert.deepEqual(
|
|
seenUrls,
|
|
cases.map((testCase) => testCase.expectedUrl)
|
|
);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("GLM import falls back to Anthropic model discovery when coding discovery fails", async () => {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "glm",
|
|
authType: "apikey",
|
|
name: "glm-discovery-fallback",
|
|
apiKey: "glm-key",
|
|
providerSpecificData: { apiRegion: "international", autoFetchModels: true },
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
const seenUrls: string[] = [];
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
seenUrls.push(String(url));
|
|
if (seenUrls.length === 1) {
|
|
assert.equal(String(url), "https://api.z.ai/api/coding/paas/v4/models");
|
|
assert.equal(init.headers.Authorization, "Bearer glm-key");
|
|
assert.equal(init.headers["x-api-key"], undefined);
|
|
return new Response(JSON.stringify({ error: "bad gateway" }), { status: 502 });
|
|
}
|
|
|
|
assert.equal(String(url), "https://api.z.ai/api/anthropic/v1/models");
|
|
assert.equal(init.headers.Authorization, undefined);
|
|
assert.equal(init.headers["x-api-key"], "glm-key");
|
|
assert.equal(init.headers["anthropic-version"], "2023-06-01");
|
|
return Response.json({ data: [{ id: "glm-5.1", name: "GLM 5.1" }] });
|
|
};
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
assert.deepEqual(await response.json(), {
|
|
provider: "glm",
|
|
connectionId: connection.id,
|
|
models: [{ id: "glm-5.1", name: "GLM 5.1" }],
|
|
source: "api",
|
|
});
|
|
assert.deepEqual(seenUrls, [
|
|
"https://api.z.ai/api/coding/paas/v4/models",
|
|
"https://api.z.ai/api/anthropic/v1/models",
|
|
]);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("GLM import preserves auth failures instead of falling back across transports", async () => {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "glm",
|
|
authType: "apikey",
|
|
name: "glm-auth-fail",
|
|
apiKey: "bad-key",
|
|
providerSpecificData: { apiRegion: "international", autoFetchModels: true },
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
const seenUrls: string[] = [];
|
|
globalThis.fetch = async (url) => {
|
|
seenUrls.push(String(url));
|
|
return new Response(JSON.stringify({ error: "invalid api key" }), { status: 401 });
|
|
};
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models?refresh=true`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 401);
|
|
assert.deepEqual(seenUrls, ["https://api.z.ai/api/coding/paas/v4/models"]);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("GLMT import shares the GLM coding models endpoint and surfaces provider metadata correctly", async () => {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "glmt",
|
|
authType: "apikey",
|
|
name: "glmt-intl",
|
|
apiKey: "glmt-key",
|
|
providerSpecificData: { apiRegion: "international", autoFetchModels: true },
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
assert.equal(String(url), "https://api.z.ai/api/coding/paas/v4/models");
|
|
assert.equal(init.headers.Authorization, "Bearer glmt-key");
|
|
return Response.json({ data: [{ id: "glm-5.1", name: "GLM 5.1" }] });
|
|
};
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
assert.deepEqual(await response.json(), {
|
|
provider: "glmt",
|
|
connectionId: connection.id,
|
|
models: [{ id: "glm-5.1", name: "GLM 5.1" }],
|
|
source: "api",
|
|
});
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("GLM import uses China coding endpoint when apiRegion is china", async () => {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "glm",
|
|
authType: "apikey",
|
|
name: "glm-cn",
|
|
apiKey: "glm-cn-key",
|
|
providerSpecificData: { apiRegion: "china", autoFetchModels: true },
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
assert.equal(String(url), "https://open.bigmodel.cn/api/coding/paas/v4/models");
|
|
assert.equal(init.headers.Authorization, "Bearer glm-cn-key");
|
|
return Response.json({ data: [{ id: "glm-5", name: "GLM 5" }] });
|
|
};
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
const body = (await response.json()) as any;
|
|
assert.equal(body.provider, "glm");
|
|
assert.equal(body.models.length, 1);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("GLM China provider import uses the specialized GLM discovery path", async () => {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "glm-cn",
|
|
authType: "apikey",
|
|
name: "glm-cn-provider",
|
|
apiKey: "glm-cn-key",
|
|
providerSpecificData: { autoFetchModels: true },
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
assert.equal(String(url), "https://open.bigmodel.cn/api/coding/paas/v4/models");
|
|
assert.equal(init.headers.Authorization, "Bearer glm-cn-key");
|
|
assert.equal(init.headers["x-api-key"], undefined);
|
|
return Response.json({ data: [{ id: "glm-5", name: "GLM 5" }] });
|
|
};
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
const body = (await response.json()) as any;
|
|
assert.equal(body.provider, "glm-cn");
|
|
assert.deepEqual(body.models, [{ id: "glm-5", name: "GLM 5" }]);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("GLM import defaults to international endpoint when apiRegion is missing", async () => {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "glm",
|
|
authType: "apikey",
|
|
name: "glm-default",
|
|
apiKey: "glm-key",
|
|
providerSpecificData: { autoFetchModels: true },
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(String(url), "https://api.z.ai/api/coding/paas/v4/models");
|
|
return Response.json({ data: [{ id: "glm-5", name: "GLM 5" }] });
|
|
};
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("GLM import defaults to international endpoint when apiRegion is invalid", async () => {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "glm",
|
|
authType: "apikey",
|
|
name: "glm-bogus",
|
|
apiKey: "glm-key",
|
|
providerSpecificData: { apiRegion: "bogus", autoFetchModels: true },
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async (url) => {
|
|
assert.equal(String(url), "https://api.z.ai/api/coding/paas/v4/models");
|
|
return Response.json({ data: [{ id: "glm-5", name: "GLM 5" }] });
|
|
};
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("GLM import prefers apiKey over accessToken and sends only Authorization Bearer", async () => {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "glm",
|
|
authType: "apikey",
|
|
name: "glm-both-tokens",
|
|
apiKey: "glm-api-key",
|
|
accessToken: "glm-access-token",
|
|
providerSpecificData: { apiRegion: "international", autoFetchModels: true },
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
assert.equal(init.headers.Authorization, "Bearer glm-api-key");
|
|
assert.equal(init.headers["x-api-key"], undefined);
|
|
return Response.json({ data: [] });
|
|
};
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("GLM import falls back to accessToken when apiKey is absent", async () => {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "glm",
|
|
authType: "apikey",
|
|
name: "glm-access-only",
|
|
accessToken: "glm-access-token",
|
|
providerSpecificData: { apiRegion: "international", autoFetchModels: true },
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
assert.equal(init.headers.Authorization, "Bearer glm-access-token");
|
|
return Response.json({ data: [] });
|
|
};
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("GLM import falls back to the local catalog on upstream non-OK status codes", async () => {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "glm",
|
|
authType: "apikey",
|
|
name: "glm-error",
|
|
apiKey: "glm-key",
|
|
providerSpecificData: { apiRegion: "international", autoFetchModels: true },
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async () => new Response("bad gateway", { status: 502 });
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
const body = (await response.json()) as any;
|
|
assert.equal(body.provider, "glm");
|
|
assert.equal(body.connectionId, connection.id);
|
|
assert.equal(body.source, "local_catalog");
|
|
assert.match(body.warning, /API unavailable/i);
|
|
assert.ok(body.models.some((model) => model.id === "glm-5.1"));
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|