Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

3.8 KB
Raw Blame
/**
 * #4558 — Auto-combo must respect model visibility (isHidden).
 *
 * When an operator hides a model with the EYE/visibility toggle
 * (`mergeModelCompatOverride(provider, model, { isHidden: true })`, written to
 * the `modelCompatOverrides` key_value namespace), that model must be excluded
 * from the AUTO-combo candidate pool. Both auto paths consume the same seam:
 *   - `open-sse/services/combo.ts::buildAutoCandidates` (combo.ts:322,520-521)
 *   - `open-sse/services/autoCombo/virtualFactory.ts` (virtualFactory.ts:239,256-257)
 * via the new bulk `getHiddenModelsByProvider()` map (single query, not N+1).
 *
 * This guards that seam: a hidden model is present in the map's per-provider set
 * while a visible sibling is not, and that toggling visibility back off
 * (`isHidden: null`) removes it from the map again. Without the fix the map is
 * empty and the auto pool would keep serving hidden models.
 */
import test, { before, 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";

// Hermetic DB: this test writes overrides into the `modelCompatOverrides`
// key_value namespace. Without an isolated DATA_DIR it would leak that state
// into the shared dev/CI database. Point DATA_DIR at a throwaway dir before any
// import that opens the SQLite handle (CLAUDE.md "Database Handles in Tests").
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-test-hidden-4558-"));
process.env.DATA_DIR = tmpDir;

const {
  addCustomModel,
  getHiddenModelsByProvider,
  getModelIsHidden,
  mergeModelCompatOverride,
  updateCustomModel,
} = await import("@/lib/db/models");
const { resetDbInstance } = await import("../../src/lib/db/core.ts");

before(() => {
  resetDbInstance();
});

after(() => {
  resetDbInstance();
  fs.rmSync(tmpDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});

const PROVIDER = "openai";
const HIDDEN_MODEL = "gpt-hidden-preview";
const VISIBLE_MODEL = "gpt-visible-4o";

test("getHiddenModelsByProvider: empty before any model is hidden", () => {
  const map = getHiddenModelsByProvider();
  assert.equal(map.get(PROVIDER)?.has(HIDDEN_MODEL) ?? false, false);
});

test("a hidden model lands in the provider's hidden set; a visible sibling does not", () => {
  // Hide one model, leave a sibling visible (overridden for an unrelated reason).
  mergeModelCompatOverride(PROVIDER, HIDDEN_MODEL, { isHidden: true });
  mergeModelCompatOverride(PROVIDER, VISIBLE_MODEL, { normalizeToolCallId: true });

  // Sanity: the per-model read agrees.
  assert.equal(getModelIsHidden(PROVIDER, HIDDEN_MODEL), true);
  assert.equal(getModelIsHidden(PROVIDER, VISIBLE_MODEL), false);

  const map = getHiddenModelsByProvider();
  const hiddenForProvider = map.get(PROVIDER);
  assert.ok(hiddenForProvider, "expected an entry for the provider");
  assert.equal(hiddenForProvider.has(HIDDEN_MODEL), true, "hidden model must be in the set");
  assert.equal(
    hiddenForProvider.has(VISIBLE_MODEL),
    false,
    "visible model must NOT be in the hidden set"
  );
});

test("un-hiding a model (isHidden: null) removes it from the map", () => {
  mergeModelCompatOverride(PROVIDER, HIDDEN_MODEL, { isHidden: null });
  const map = getHiddenModelsByProvider();
  assert.equal(
    map.get(PROVIDER)?.has(HIDDEN_MODEL) ?? false,
    false,
    "un-hidden model must drop out of the hidden set"
  );
});

test("an explicit visible custom-model setting overrides a hidden compat fallback", async () => {
  const modelId = "gpt-custom-visible-override";
  mergeModelCompatOverride(PROVIDER, modelId, { isHidden: true });
  await addCustomModel(PROVIDER, modelId);
  await updateCustomModel(PROVIDER, modelId, { isHidden: false });

  assert.equal(getModelIsHidden(PROVIDER, modelId), false);
  assert.equal(getHiddenModelsByProvider().get(PROVIDER)?.has(modelId) ?? false, false);
});