Files
OmniRoute/tests/unit/cc-discovery-alias-api.test.ts
Diego Rodrigues de Sa e Souza cd9a631464 feat: Claude Code discovery aliases (surface non-Claude models in the /model picker) (#8666)
* feat(db): cc discovery alias gate storage + EXPOSE_CC_DISCOVERY_ALIASES flag

Adds the gate for claude/<provider>/<model> discovery-alias mirror ids on
the /v1/models catalog: a new runtime feature flag (env forces on and wins
over the dashboard DB override), per-provider and per-model "on"/"off"/null
overrides stored in key_value under the ccDiscoveryAliases namespace, and a
pure precedence resolver (model > provider > global). Catalog wiring is a
separate follow-up task; this only lands the gate + storage.

* feat(sse): synthesize claude/ discovery aliases for the model catalog

* feat(api): advertise cc discovery aliases on /v1/models behind the 3-level gate

* feat(sse): resolve claude/ discovery aliases on the request path

* fix(sse): import getComboByName from db/combos, not the localDb barrel

* fix(sse): cover custom-node prefixes and the Codex WS bridge in cc discovery alias resolution

* feat(dashboard): cc discovery alias toggles + flag-screen env warning

Adds the operator-facing UI/API layer for the Claude Code discovery-alias
gate (claude/<provider>/<model> mirror ids on /v1/models): REST endpoint
for provider/model overrides, a provider-detail card with 3-state
(inherit/on/off) toggles, an info button on the Claude Code tool card
linking to Feature Flags, and an env-source warning on the
EXPOSE_CC_DISCOVERY_ALIASES flag card when it's forced on via env.

* feat(api): cc discovery usage metrics

* fix(api): record cc alias metric in the production wrapper + atomic counter upsert

* docs: document cc discovery aliases (Claude Code guide + feature flag catalog)

* fix(sse): don't mirror built-in auto/* combos as discovery aliases (advertised-but-unroutable)

* i18n(vi): translate the discovery-alias strings instead of shipping placeholders

vi is the one locale with a strict "no internal missing markers" test, so the 17
__MISSING__ entries this branch added (the provider ccAlias panel, the info
button, the feature-flag description and the env warning) would have turned that
test red the moment the base itself was repaired. Translated, keeping every ICU
placeholder ({modelId}, {error}) and the literal claude/<provider>/<model> id
shape intact.

* chore(quality): raise the frozen caps this feature legitimately grows

catalog.ts 1615 -> 1639: the alias synthesis is wired into the catalog builder,
which is where the per-key-filtered list is assembled — the only place the mirror
entries can be appended after model hiding has been applied.

localDb.ts 808 -> 810: two re-export lines for the new ccDiscoveryAliases db
module, which is exactly what the "Adding a New DB Module" recipe prescribes.

* refactor(dashboard,api): keep the complexity ratchets flat

The feature added four cyclomatic violations and one cognitive one, which the
ratchets reject — the baseline only moves when a metric improves. Split the new
code instead:

- appendCcDiscoveryAliases: the four skip-guards become isMirrorableId().
- resolveCcDiscoveryAliasStripWith: alias parsing and gate resolution become
  parseCcAliasTarget() and resolveGateFor(), replacing a chain of ternaries that
  each re-tested isComboAlias.
- FeatureFlagCard: the env-precedence warning becomes its own component instead
  of a conditional branch inside an already-large render.
- ProviderCcAliasSection: the loader moves to useCcAliasData(), and the override
  list and add-row become ModelOverrideList / AddOverrideRow, bringing both
  oversized functions back under the 80-line rule.

Behavior unchanged — the 74 discovery-alias tests pass untouched. Both ratchets
now sit exactly at baseline (2188 / 971).
2026-07-26 17:19:19 -03:00

126 lines
4.4 KiB
TypeScript

import { describe, it, beforeEach, after } from "node:test";
import assert from "node:assert/strict";
import os from "node:os";
import path from "node:path";
import fs from "node:fs";
// Set DATA_DIR to a temp dir before any imports that touch the DB.
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-test-cc-alias-api-"));
process.env.DATA_DIR = tmpDir;
process.env.REQUIRE_API_KEY = "false";
const core = await import("../../src/lib/db/core.ts");
const route = await import("../../src/app/api/providers/[id]/cc-alias/route.ts");
function resetDb() {
core.resetDbInstance();
fs.rmSync(tmpDir, { recursive: true, force: true });
fs.mkdirSync(tmpDir, { recursive: true });
}
function makeParams(id: string): { params: Promise<{ id: string }> } {
return { params: Promise.resolve({ id }) };
}
function putRequest(body: unknown): Request {
return new Request("http://localhost/api/providers/anthropic/cc-alias", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
}
function getRequest(): Request {
return new Request("http://localhost/api/providers/anthropic/cc-alias", { method: "GET" });
}
describe("PUT/GET /api/providers/[id]/cc-alias", () => {
beforeEach(() => {
resetDb();
});
after(() => {
core.resetDbInstance();
fs.rmSync(tmpDir, { recursive: true, force: true });
delete process.env.REQUIRE_API_KEY;
});
it("PUT provider on -> GET reflects the override", async () => {
const putRes = await route.PUT(
putRequest({ scope: "provider", value: "on" }),
makeParams("anthropic")
);
assert.equal(putRes.status, 200);
const putBody = await putRes.json();
assert.deepEqual(putBody, { success: true });
const getRes = await route.GET(getRequest(), makeParams("anthropic"));
assert.equal(getRes.status, 200);
const getBody = await getRes.json();
assert.equal(getBody.provider, "on");
assert.deepEqual(getBody.models, {});
});
it("PUT model off -> GET reflects the override, scoped to that provider only", async () => {
const putRes = await route.PUT(
putRequest({ scope: "model", modelId: "claude-opus-4", value: "off" }),
makeParams("anthropic")
);
assert.equal(putRes.status, 200);
const getRes = await route.GET(getRequest(), makeParams("anthropic"));
const getBody = await getRes.json();
assert.equal(getBody.provider, null);
assert.deepEqual(getBody.models, { "claude-opus-4": "off" });
// A model override under a different provider must not leak in.
const otherRes = await route.GET(getRequest(), makeParams("openai"));
const otherBody = await otherRes.json();
assert.deepEqual(otherBody.models, {});
});
it("PUT value:null clears an existing override back to inherit", async () => {
await route.PUT(putRequest({ scope: "provider", value: "on" }), makeParams("anthropic"));
await route.PUT(putRequest({ scope: "provider", value: null }), makeParams("anthropic"));
const getRes = await route.GET(getRequest(), makeParams("anthropic"));
const getBody = await getRes.json();
assert.equal(getBody.provider, null);
});
it("PUT with invalid body -> 400", async () => {
const res = await route.PUT(
putRequest({ scope: "provider", value: "maybe" }),
makeParams("anthropic")
);
assert.equal(res.status, 400);
});
it("PUT with missing scope -> 400", async () => {
const res = await route.PUT(putRequest({ value: "on" }), makeParams("anthropic"));
assert.equal(res.status, 400);
});
it("PUT scope:model without modelId -> 400", async () => {
const res = await route.PUT(
putRequest({ scope: "model", value: "on" }),
makeParams("anthropic")
);
assert.equal(res.status, 400);
});
it("PUT with invalid JSON body -> 400 without leaking a stack trace", async () => {
const badRequest = new Request("http://localhost/api/providers/anthropic/cc-alias", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: "{not json",
});
const res = await route.PUT(badRequest, makeParams("anthropic"));
assert.equal(res.status, 400);
const body = await res.json();
const text = JSON.stringify(body);
assert.equal(text.includes(" at "), false, "error body must not leak a stack trace");
assert.equal(text.toLowerCase().includes(".ts:"), false, "error body must not leak file:line");
});
});