Files
OmniRoute/tests/unit/8326-compatible-id-regex.test.ts

124 lines
4.7 KiB
TypeScript

/**
* Regression test for #8326 — the "compatible provider" connection id
* validator/matcher required a literal "-chat-" segment, so it rejected 3
* of the 4 id shapes actually generated by
* src/app/api/provider-nodes/route.ts:
* - openai-compatible-chat-<uuid> (accepted before the fix — control)
* - openai-compatible-responses-<uuid> (rejected before the fix)
* - anthropic-compatible-<uuid> (rejected before the fix)
* - anthropic-compatible-cc-<uuid> (rejected before the fix)
*
* Covers both call sites that shared the drifted regexes:
* - GET /v1/providers/:provider/models (src/app/api/v1/providers/[provider]/models/route.ts)
* - getProviderDisplayName (src/lib/display/names.ts)
* and the new shared matcher itself (src/shared/utils/compatibleProviderId.ts),
* including the landmine check that widening the matcher must NOT start
* matching plain built-in provider ids like "openai" / "anthropic".
*
* Run: node --import tsx/esm --test tests/unit/8326-compatible-id-regex.test.ts
*/
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-8326-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const routeModule = await import("../../src/app/api/v1/providers/[provider]/models/route.ts");
const { isCompatibleProviderConnectionId } = await import(
"../../src/shared/utils/compatibleProviderId.ts"
);
const { getProviderDisplayName } = await import("../../src/lib/display/names.ts");
function makeRequest(provider: string) {
return new Request(`http://localhost/api/v1/providers/${encodeURIComponent(provider)}/models`);
}
async function callGET(provider: string) {
return routeModule.GET(makeRequest(provider), {
params: Promise.resolve({ provider }),
});
}
test.beforeEach(() => {
core.resetDbInstance();
});
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
});
const UUID = "02669115-2545-4896-b003-cb4dac09d441";
const GENERATED_SHAPES = [
["openai-compatible-chat-" + UUID, "openai/chat (control — worked before the fix)"],
["openai-compatible-responses-" + UUID, "openai/responses"],
["anthropic-compatible-" + UUID, "anthropic (segment-less)"],
["anthropic-compatible-cc-" + UUID, "anthropic/cc"],
] as const;
test("isCompatibleProviderConnectionId matches all 4 shapes actually generated by provider-nodes/route.ts", () => {
for (const [id, label] of GENERATED_SHAPES) {
assert.equal(isCompatibleProviderConnectionId(id), true, `expected match for ${label}: ${id}`);
}
});
test("isCompatibleProviderConnectionId does NOT widen to match plain built-in provider ids", () => {
for (const id of ["openai", "anthropic", "gemini", "openrouter"]) {
assert.equal(
isCompatibleProviderConnectionId(id),
false,
`must not match plain built-in provider id: ${id}`
);
}
});
test("isCompatibleProviderConnectionId does NOT match unrelated look-alike prefixes", () => {
assert.equal(isCompatibleProviderConnectionId("custom-compatible-chat-" + UUID), false);
assert.equal(isCompatibleProviderConnectionId(null), false);
assert.equal(isCompatibleProviderConnectionId(undefined), false);
});
test("GET /v1/providers/:provider/models accepts all 4 generated compatible connection id shapes", async () => {
for (const [id, label] of GENERATED_SHAPES) {
const res = await callGET(id);
if (res.status === 400) {
const body = await res.json();
assert.notEqual(
body.error?.code,
"invalid_provider",
`${label} (${id}) must bypass the unknown-provider 400 gate`
);
}
}
});
test("GET /v1/providers/:provider/models still rejects unrelated look-alike prefixes", async () => {
const res = await callGET("custom-compatible-chat-" + UUID);
assert.equal(res.status, 400);
const body = await res.json();
assert.equal(body.error?.code, "invalid_provider");
});
test("getProviderDisplayName simplifies all 4 generated compatible id shapes", () => {
assert.equal(
getProviderDisplayName("openai-compatible-chat-" + UUID),
"Compatible (openai)"
);
assert.equal(
getProviderDisplayName("openai-compatible-responses-" + UUID),
"Compatible (openai)"
);
assert.equal(getProviderDisplayName("anthropic-compatible-" + UUID), "Compatible (anthropic)");
assert.equal(getProviderDisplayName("anthropic-compatible-cc-" + UUID), "CC Compatible");
});
test("getProviderDisplayName does not simplify plain built-in provider ids", () => {
assert.equal(getProviderDisplayName("openai"), "openai");
assert.equal(getProviderDisplayName("anthropic"), "anthropic");
});