Files
OmniRoute/tests/unit/models-catalog-model-exposure-list.test.ts
Diego Rodrigues de Sa e Souza 34e2f84c04 feat(api): explicit model exposure allow/deny list for /v1/models (#11481) (#11997)
Adds opt-in modelVisibilityAllowlist/modelVisibilityDenylist settings so an operator can curate exactly which models GET /v1/models advertises, mirrored into auto/* combo candidate pools (the same trap #6512 fixed for hidePaidModels). Default off, no behavior change for anyone who doesn't opt in.

TDD: 4 new test files, 22/22 passing (16 node:test + 6 vitest) + regression sweep across virtual-auto-combo/hide-paid/hide-auto-no-think suites (21/21).

Rebased onto the updated tip (a sibling #9133 landed first, same file) — kept both rebaseline annotations in file-size-baseline.json and set the value to the real measured line count after both merged.
2026-08-29 15:40:32 -03:00

94 lines
3.2 KiB
TypeScript

/**
* #11481 — `modelVisibilityAllowlist`/`modelVisibilityDenylist` filter the
* unified `/v1/models` catalog. Mirrors models-catalog-hide-paid.test.ts.
* Rule #18 regression guard for the toggle.
*/
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-model-exposure-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const settingsDb = await import("../../src/lib/db/settings.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const v1ModelsCatalog = await import("../../src/app/api/v1/models/catalog.ts");
async function fetchCatalog(): Promise<Array<{ id: string; type?: string }>> {
const res = await v1ModelsCatalog.getUnifiedModelsResponse(
new Request("http://localhost/api/v1/models", { method: "GET" })
);
assert.equal(res.status, 200);
const body = (await res.json()) as { data: Array<{ id: string; type?: string }> };
return body.data;
}
test.after(() => {
core.resetDbInstance();
try {
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
} catch {
/* best-effort */
}
});
test("default (both lists empty) — catalog is unchanged", async () => {
const defaults = await settingsDb.getSettings();
assert.deepEqual(defaults.modelVisibilityAllowlist, []);
assert.deepEqual(defaults.modelVisibilityDenylist, []);
await providersDb.createProviderConnection({
provider: "openai",
authType: "apikey",
name: "openai-main-exposure",
apiKey: "sk-test",
isActive: true,
});
const ids = (await fetchCatalog()).map((m) => m.id);
assert.ok(
ids.some((id) => /^(openai|oa)\/gpt-4o(-mini)?$/.test(id)),
"expected openai chat models present with no exposure list configured"
);
});
test("modelVisibilityDenylist hides an exact-id match from /v1/models", async () => {
await settingsDb.updateSettings({
modelVisibilityAllowlist: [],
modelVisibilityDenylist: ["openai/gpt-4o-mini"],
});
const ids = (await fetchCatalog()).map((m) => m.id);
assert.ok(!ids.includes("openai/gpt-4o-mini"), "denied model must not appear in the catalog");
assert.ok(
ids.some((id) => id === "openai/gpt-4o"),
"a non-denied sibling model must remain"
);
await settingsDb.updateSettings({ modelVisibilityDenylist: [] });
});
test("modelVisibilityAllowlist restricts the chat catalog to exactly the listed models", async () => {
await settingsDb.updateSettings({
modelVisibilityDenylist: [],
modelVisibilityAllowlist: ["openai/gpt-4o-mini"],
});
const chatIds = (await fetchCatalog())
.filter((m) => m.type === undefined || m.type === "chat")
.map((m) => m.id)
.filter((id) => id.startsWith("openai/") || id.startsWith("oa/"));
assert.deepEqual(
chatIds.filter((id) => id !== "openai/gpt-4o-mini"),
[],
`only the allow-listed model may remain, found: ${chatIds.join(", ")}`
);
assert.ok(chatIds.includes("openai/gpt-4o-mini"), "the allow-listed model itself must remain");
await settingsDb.updateSettings({ modelVisibilityAllowlist: [] });
});