Files
OmniRoute/tests/unit/managed-model-import.test.ts
InkshadeWoods fd76271515 fix(providers): make upstream model sync opt-in and preserve manual overrides (#10603)
* fix(providers): make upstream model sync opt-in and preserve manual overrides

(cherry picked from commit 0a84f5496896a95856e834112b3d813fa1b87d38)

* test(providers): cover upstream model sync controls

* fix(providers): fix pre-existing tests broken by opt-in model sync + sync i18n keys

The upstream model auto-fetch opt-in default flip made 3 pre-existing tests
short-circuit before reaching the paths they exercise, because their
connection fixtures never set providerSpecificData.autoFetchModels: true:

- tests/unit/provider-models-route-lan-guard.test.ts (#6939 SSRF-guard tests)
- tests/unit/openrouter-embeddings-catalog-6976.test.ts (live discovery merge/dedup)
- tests/unit/provider-models-route.test.ts (Kimi Coding auth-header test —
  this was mislabeled as base/catalog drift during review, but is the same
  root cause: without autoFetchModels the mocked fetch is never reached and
  the route falls back to local catalog data instead)

Also syncs the 11 new providers.autoFetchModels*/overridesUpstreamModel*/
resetToUpstreamDefaults* i18n keys from en.json/zh-CN.json to the remaining
40 locale files via a narrowly-scoped ad-hoc translation script (only these
11 keys — leaves each locale's pre-existing, unrelated missing-key backlog
untouched).

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(providers): fix remaining pre-existing tests broken by opt-in model sync

Rebase surfaced that the 'Kimi Coding' CI failure flagged as possible base
drift during review was actually the same root cause as the lan-guard and
openrouter-embeddings fixes: 29 pre-existing tests in
tests/unit/provider-models-route.test.ts (of 59 total) short-circuit under
the new autoFetchModels opt-in default because their connection fixtures
never set providerSpecificData.autoFetchModels: true, so they never reach
the live-fetch/validation paths they were written to exercise (fetch mocks
never called, base-URL validation never reached, live models never merged).

Adds providerSpecificData.autoFetchModels: true to each affected fixture.
No production code or test assertions changed — same TEST-fixture-only
pattern as the lan-guard and openrouter-embeddings fixes. All 59 tests in
the file now pass (was 30/59).

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
2026-08-18 10:58:40 -03:00

300 lines
10 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-managed-model-import-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const modelsDb = await import("../../src/lib/db/models.ts");
const localDb = await import("../../src/lib/localDb.ts");
const { importManagedModels } = await import("../../src/lib/providerModels/managedModelImport.ts");
const { mergeProviderModelListing } =
await import("../../src/lib/providers/mergeProviderModelListing.ts");
async function resetStorage() {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
test.beforeEach(async () => {
await resetStorage();
});
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
});
test("sync mode builds aliases from provider-level synced available models", async () => {
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", "conn-a", [
{ id: "shared/model-a", name: "Model A", source: "imported" },
]);
await importManagedModels({
providerId: "openrouter",
connectionId: "conn-b",
mode: "sync",
fetchedModels: [{ id: "shared/model-b", name: "Model B" }],
});
const aliases = await localDb.getModelAliases();
assert.equal(aliases["model-a"], "openrouter/shared/model-a");
assert.equal(aliases["model-b"], "openrouter/shared/model-b");
});
test("Crof managed import persists boolean reasoning effort metadata", async () => {
const result = await importManagedModels({
providerId: "crof",
connectionId: "conn-crof",
mode: "sync",
fetchedModels: [{ id: "crof-managed-model", reasoning_effort: true }],
});
const synced = await modelsDb.getSyncedAvailableModelsForConnection("crof", "conn-crof");
assert.deepEqual(synced[0]?.supportedThinkingEfforts, ["none", "low", "medium", "high", "max"]);
assert.equal(synced[0]?.supportsThinking, true);
assert.deepEqual(result.importedModels[0]?.supportedThinkingEfforts, [
"none",
"low",
"medium",
"high",
"max",
]);
assert.equal(result.importedModels[0]?.supportsThinking, true);
});
test("merge mode builds aliases from discovered models without pruning missing provider aliases", async () => {
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", "conn-a", [
{ id: "shared/model-a", name: "Model A", source: "imported" },
]);
await localDb.setModelAlias("existing", "openrouter/shared/existing");
await importManagedModels({
providerId: "openrouter",
connectionId: "conn-b",
mode: "merge",
fetchedModels: [{ id: "shared/model-b", name: "Model B" }],
});
const aliases = await localDb.getModelAliases();
assert.equal(aliases.existing, "openrouter/shared/existing");
assert.equal(aliases["model-a"], undefined);
assert.equal(aliases["model-b"], "openrouter/shared/model-b");
});
test("sync keeps a same-id manual model as the user-configurable metadata override", async () => {
await modelsDb.addCustomModel(
"openrouter",
"shared-model",
"Operator configuration",
"manual",
"responses",
["responses"],
"claude",
{},
true
);
await importManagedModels({
providerId: "openrouter",
connectionId: "openrouter-connection",
mode: "sync",
fetchedModels: [
{
id: "shared-model",
name: "Upstream name",
apiFormat: "chat-completions",
supportedEndpoints: ["chat"],
description: "Upstream description",
},
],
});
const customModels = (await modelsDb.getCustomModels("openrouter")) as Array<{
id: string;
apiFormat?: string;
targetFormat?: string;
supportedEndpoints?: string[];
supportsVision?: boolean;
}>;
assert.deepEqual(customModels, [
{
id: "shared-model",
name: "Operator configuration",
source: "manual",
apiFormat: "responses",
supportedEndpoints: ["responses"],
targetFormat: "claude",
supportsVision: true,
},
]);
const syncedModels = await modelsDb.getSyncedAvailableModels("openrouter");
const effectiveModel = mergeProviderModelListing({
providerId: "openrouter",
registryModels: [],
syncedModels,
customModels,
}).find((model) => model.id === "shared-model");
assert.equal(effectiveModel?.apiFormat, "responses");
assert.deepEqual(effectiveModel?.supportedEndpoints, ["responses"]);
assert.equal(effectiveModel?.targetFormat, "claude");
assert.equal(effectiveModel?.supportsVision, true);
assert.equal(effectiveModel?.description, "Upstream description");
assert.equal(await modelsDb.removeCustomModel("openrouter", "shared-model"), true);
const resetModel = mergeProviderModelListing({
providerId: "openrouter",
registryModels: [],
syncedModels,
customModels: await modelsDb.getCustomModels("openrouter"),
}).find((model) => model.id === "shared-model");
assert.equal(resetModel?.apiFormat, "chat-completions");
assert.deepEqual(resetModel?.supportedEndpoints, ["chat"]);
assert.equal(resetModel?.description, "Upstream description");
});
test("provider-level synced model deletion removes only that provider", async () => {
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", "conn-a", [
{ id: "shared/model-a", name: "Model A", source: "imported" },
]);
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", "conn-b", [
{ id: "shared/model-b", name: "Model B", source: "imported" },
]);
await modelsDb.replaceSyncedAvailableModelsForConnection("openai", "conn-a", [
{ id: "shared/model-c", name: "Model C", source: "imported" },
]);
const removed = await modelsDb.deleteSyncedAvailableModelsForProvider("openrouter");
assert.equal(removed, 2);
assert.deepEqual(await modelsDb.getSyncedAvailableModels("openrouter"), []);
assert.deepEqual(await modelsDb.getSyncedAvailableModels("openai"), [
{ id: "shared/model-c", name: "Model C", source: "imported" },
]);
});
test("OpenAI import excludes deprecated and shutdown models from new selections", async () => {
const result = await importManagedModels({
providerId: "openai",
connectionId: "openai-conn",
mode: "sync",
fetchedModels: [
{ id: "gpt-5.6-sol", name: "GPT-5.6 Sol" },
{ id: "gpt-5.2-codex", name: "GPT-5.2 Codex" },
{ id: "gpt-5.3-chat-latest", name: "GPT-5.3 Chat" },
],
});
assert.deepEqual(
result.discoveredModels.map((model) => model.id),
["gpt-5.6-sol"]
);
assert.deepEqual(
(await modelsDb.getSyncedAvailableModels("openai")).map((model) => model.id),
["gpt-5.6-sol"]
);
});
test("OpenAI import excludes image and video generation models from chat selections", async () => {
const result = await importManagedModels({
providerId: "openai",
connectionId: "openai-media-conn",
mode: "sync",
fetchedModels: [
{ id: "gpt-5.6-sol", name: "GPT-5.6 Sol" },
{ id: "gpt-image-2", name: "GPT Image 2" },
{ id: "sora-2-pro", name: "Sora 2 Pro" },
{
id: "vendor-image-model",
name: "Vendor Image Model",
supportedEndpoints: ["/v1/images/generations"],
},
],
});
assert.deepEqual(
result.discoveredModels.map((model) => model.id),
["gpt-5.6-sol"]
);
assert.deepEqual(
(await modelsDb.getSyncedAvailableModels("openai")).map((model) => model.id),
["gpt-5.6-sol"]
);
});
test("pruning stale connection available models during import", async () => {
const db = core.getDbInstance();
// Insert connections
db.prepare(
"INSERT INTO provider_connections (id, provider, auth_type, name, is_active, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
).run("conn-active", "openrouter", "apikey", "Active Connection", 1, "2026-05-29", "2026-05-29");
db.prepare(
"INSERT INTO provider_connections (id, provider, auth_type, name, is_active, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
).run("conn-stale", "openrouter", "apikey", "Stale Connection", 0, "2026-05-29", "2026-05-29");
// Create synced available models for both
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", "conn-active", [
{ id: "shared/model-active", name: "Model Active", source: "imported" },
]);
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", "conn-stale", [
{ id: "shared/model-stale", name: "Model Stale", source: "imported" },
]);
// Import on conn-new
await importManagedModels({
providerId: "openrouter",
connectionId: "conn-new",
mode: "sync",
fetchedModels: [{ id: "shared/model-new", name: "Model New" }],
});
// Check models for "openrouter"
const allSyncedModels = await modelsDb.getSyncedAvailableModels("openrouter");
// Stale connection should be pruned. Active connection and the new syncing connection should be kept.
const ids = allSyncedModels.map((m) => m.id);
assert.ok(ids.includes("shared/model-active"));
assert.ok(ids.includes("shared/model-new"));
assert.ok(!ids.includes("shared/model-stale"));
});
test("antigravity sync dynamically builds and saves mitmAlias mappings", async () => {
const db = core.getDbInstance();
// Create an antigravity connection
db.prepare(
"INSERT INTO provider_connections (id, provider, auth_type, name, is_active, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
).run("antigravity-conn", "antigravity", "oauth", "Antigravity", 1, "2026-05-29", "2026-05-29");
await importManagedModels({
providerId: "antigravity",
connectionId: "antigravity-conn",
mode: "sync",
fetchedModels: [
{ id: "gemini-3.5-flash", name: "Gemini 3.5 Flash" },
{ id: "custom-antigravity-model", name: "Custom Antigravity Model" },
],
});
const models = await modelsDb.getSyncedAvailableModels("antigravity");
console.log("SYNCED MODELS IN TEST:", models);
const mitmMappings = await modelsDb.getMitmAlias("antigravity");
console.log("MITM MAPPINGS IN TEST:", mitmMappings);
// Should contain standard mapping
assert.equal(mitmMappings["gemini-3.5-flash"], "antigravity/gemini-3.5-flash");
assert.equal(mitmMappings["custom-antigravity-model"], "antigravity/custom-antigravity-model");
// Removed Antigravity 2.0 preview/agent aliases must not be reintroduced.
assert.equal(mitmMappings["gemini-3.5-flash-preview"], undefined);
assert.equal(mitmMappings["gemini-3-flash-agent"], undefined);
});