Files
OmniRoute/tests/unit/glm-provider-model-import-route.test.ts
diegosouzapw dff836ae26 fix(ci): resolve remaining CI failures (typecheck + build-reorg test drift + #3100 dedup)
Full CI surfaced real failures that local subsets missed (gh-merged PRs bypass
the hooks that run these gates):
- typecheck:core (Lint job): 3 now-unused @ts-expect-error in mcp-server/server.ts
  (#3077 dynamic tool loops) → @ts-ignore (lenient, no TS2578).
- pack-artifact-policy.test.ts: build-reorg (#3124) renamed app/->dist/; the test
  still asserted app/ paths + REQUIRED order (it sorts alphabetically).
- electron-packaging.test.ts: extraResources from .next/electron-standalone ->
  .build/electron-standalone (#3124).
- glm-provider-model-import-route.test.ts: two GLM connections shared one apiKey,
  so #3100 (#3023) dedup collapsed them → only one discovery fetch. Distinct keys.

Remaining CI flakes (batch expiration, ModelSync self-fetch) pass in isolation —
concurrency/port flakiness under --test-concurrency=4, not real failures.
2026-06-03 22:12:17 -03:00

439 lines
14 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";
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" },
});
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 },
})
);
}
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" },
});
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" },
});
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" },
});
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" },
});
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: {},
});
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: {},
});
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" },
});
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" },
});
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" },
});
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" },
});
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;
}
});