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

9.7 KB
Raw Blame
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";
import { providerUsesAuthoritativeLiveCatalog } from "../../open-sse/config/providerRegistry.ts";

const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-live-catalog-8926-"));

process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "live-catalog-8926-test-secret";

const core = await import("../../src/lib/db/core.ts");
const { addCustomModel, replaceSyncedAvailableModelsForConnection } =
  await import("../../src/lib/db/models.ts");
const { getActiveSyncedCatalog, getAllActiveSyncedModels } =
  await import("../../src/lib/db/models/activeSyncedCatalog.ts");
const { isRegisteredProviderEffortVariant } =
  await import("../../open-sse/utils/registeredEffortVariants.ts");

const { getModelInfo } = await import("../../src/sse/services/model.ts");
const { getProviderModels } = await import("../../open-sse/config/providerModels.ts");

const PROVIDER = "github";
const CONNECTION_ID = "github-live-catalog-8926";
const LIVE_MODEL = "gpt-5.6-sol";
// Present in both Copilot static catalogs, but absent from this connection's live discovery.
const PHANTOM_MODEL = "claude-opus-4.8-fast";

function seedProviderCatalog(
  providerId: string,
  connectionId: string,
  modelIds: string[],
  isActive = true
) {
  const db = core.getDbInstance();
  const now = new Date().toISOString();

  db.prepare(
    `INSERT OR REPLACE INTO provider_connections
       (id, provider, is_active, created_at, updated_at)
     VALUES (?, ?, ?, ?, ?)`
  ).run(connectionId, providerId, isActive ? 1 : 0, now, now);

  return replaceSyncedAvailableModelsForConnection(
    providerId,
    connectionId,
    modelIds.map((id) => ({
      id,
      name: id,
      source: "imported" as const,
    }))
  );
}

function seedActiveLiveCatalog() {
  return seedProviderCatalog(PROVIDER, CONNECTION_ID, [LIVE_MODEL]);
}

test.beforeEach(async () => {
  core.resetDbInstance();
  fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
  fs.mkdirSync(TEST_DATA_DIR, { recursive: true });

  assert.ok(
    getProviderModels(PROVIDER).some((model: { id?: string }) => model.id === PHANTOM_MODEL),
    `precondition: ${PHANTOM_MODEL} must exist in the static GitHub catalog`
  );

  await seedActiveLiveCatalog();
});

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

test("#8926: bare inference excludes a stale static model absent from the active live catalog", async () => {
  const resolved = await getModelInfo(PHANTOM_MODEL);

  assert.equal(resolved.provider, null);
  assert.equal(resolved.errorType, "model_not_found");
  assert.match(
    resolved.errorMessage,
    /active live catalog/i,
    "the error should explain that live availability rejected the stale entry"
  );
});

test("#8926: explicit provider/model rejects a stale static model before upstream dispatch", async () => {
  const resolved = await getModelInfo(`${PROVIDER}/${PHANTOM_MODEL}`);

  assert.equal(resolved.provider, null);
  assert.equal(resolved.errorType, "model_not_found");
  assert.match(
    resolved.errorMessage,
    /active live catalog/i,
    "the explicit route should report live-catalog unavailability"
  );
});

test("#8926: active alternative provider remains eligible", async () => {
  await seedProviderCatalog("ghe-copilot", "ghe-copilot-active-8926", []);

  const resolved = await getModelInfo(PHANTOM_MODEL);

  assert.equal(resolved.provider, "ghe-copilot");
  assert.equal(resolved.model, PHANTOM_MODEL);
});

test("#8926: explicit custom model overrides live-catalog exclusion", async () => {
  await addCustomModel(PROVIDER, PHANTOM_MODEL, "Operator custom override");

  const resolved = await getModelInfo(`${PROVIDER}/${PHANTOM_MODEL}`);

  assert.equal(resolved.provider, PROVIDER);
  assert.equal(resolved.model, PHANTOM_MODEL);
});

test("#8926: effort helper identifies only explicitly registered variants", () => {
  assert.equal(isRegisteredProviderEffortVariant("cursor", "claude-fable-5-1-thinking-high"), true);

  assert.equal(
    isRegisteredProviderEffortVariant("cursor", "claude-fable-5-1-thinking-ultra"),
    false,
    "an invented suffix must not bypass live-catalog authority"
  );
});

test("#8926: registered effort route survives while invented effort route is rejected", async () => {
  await seedProviderCatalog("cursor", "cursor-live-8926", ["claude-fable-5-1"]);

  const registered = await getModelInfo("cursor/claude-fable-5-1-thinking-high");

  assert.equal(registered.provider, "cursor");
  assert.equal(registered.model, "claude-fable-5-1-thinking-high");

  const invented = await getModelInfo("cursor/claude-fable-5-1-thinking-ultra");

  assert.equal(invented.provider, null);
  assert.equal(invented.errorType, "model_not_found");
});

test("#8926: active-only catalog excludes inactive siblings and resolves aliases", async () => {
  await seedProviderCatalog(PROVIDER, "github-inactive-sibling-8926", [PHANTOM_MODEL], false);

  const allActive = await getAllActiveSyncedModels();

  assert.deepEqual(
    allActive[PROVIDER]?.map((model) => model.id),
    [LIVE_MODEL]
  );

  const aliasCatalog = await getActiveSyncedCatalog("gh");

  assert.equal(aliasCatalog.authoritative, true);

  assert.deepEqual(
    aliasCatalog.models.map((model) => model.id),
    [LIVE_MODEL]
  );
});

test("#8926: providers without an authoritative live catalog retain static fallback", async () => {
  const resolved = await getModelInfo("openai/gpt-4o-mini");

  assert.equal(resolved.provider, "openai");
  assert.equal(resolved.model, "gpt-4o-mini");
});

test("#8926: registered effort variant is rejected when its live base is absent", async () => {
  await seedProviderCatalog("cursor", "cursor-live-without-base-8926", ["cursor-live-only-8926"]);

  const explicit = await getModelInfo("cursor/claude-fable-5-1-thinking-high");

  assert.equal(explicit.provider, null);
  assert.equal(explicit.errorType, "model_not_found");
  assert.match(explicit.errorMessage, /active live catalog/i);

  const bare = await getModelInfo("claude-fable-5-1-thinking-high");

  assert.equal(bare.provider, null);
  assert.equal(bare.errorType, "model_not_found");
  assert.match(bare.errorMessage, /active live catalog/i);
});

test("#8926: live authority defaults to strict and honors explicit partial-discovery opt-outs", () => {
  assert.equal(providerUsesAuthoritativeLiveCatalog("github"), true);
  assert.equal(providerUsesAuthoritativeLiveCatalog("cursor"), true);
  assert.equal(providerUsesAuthoritativeLiveCatalog("unknown-provider-8926"), true);
  assert.equal(providerUsesAuthoritativeLiveCatalog("command-code"), false);
});

test("#8926: partial passthrough discovery remains non-authoritative", async () => {
  await seedProviderCatalog("command-code", "command-code-partial-live-8926", ["gpt-5.6-luna"]);

  const catalog = await getActiveSyncedCatalog("command-code");

  assert.equal(catalog.authoritative, false);
  assert.deepEqual(
    catalog.models.map((model) => model.id),
    ["gpt-5.6-luna"]
  );
});

test("#12866: agy CLI catalog is visible after parseModel folds the prefix to antigravity", async () => {
  // Production shape: combo steps are `agy/gemini-3.8-flash-high` on CLI-card
  // rows. parseModel canonicalizes `agy/` → `antigravity` (#8013), then live
  // authority looks up the IDE catalog keyed `antigravity:<id>`. Those two
  // catalogs are distinct stored ids — CLI has flash-high, IDE does not —
  // so the request 400s even though the pinned agy connection serves the model.
  await seedProviderCatalog("agy", "agy-cli-catalog-12866", ["gemini-3.8-flash-high"]);
  await seedProviderCatalog("antigravity", "antigravity-ide-catalog-12866", [
    "gemini-3.8-flash-tiered",
  ]);

  const resolved = await getModelInfo("agy/gemini-3.8-flash-high");

  assert.equal(resolved.errorType, undefined, resolved.errorMessage);
  assert.equal(resolved.model, "gemini-3.8-flash-high");
  assert.ok(
    resolved.provider === "agy" || resolved.provider === "antigravity",
    `expected agy/antigravity, got ${resolved.provider}`
  );

  const antigravityCatalog = await getActiveSyncedCatalog("antigravity");
  assert.equal(
    antigravityCatalog.models.some((model) => model.id === "gemini-3.8-flash-high"),
    true,
    "antigravity live lookup must union the sibling agy CLI catalog"
  );
});

test("#16929: batched /v1/models source honors same-id operator endpoints", async () => {
  const [original] = (await getAllActiveSyncedModels())[PROVIDER] || [];
  assert.ok(original, "synced row must be visible before operator override");

  const { updateCustomModel } = await import("../../src/lib/db/models.ts");
  const savedOverride = await updateCustomModel(
    PROVIDER,
    LIVE_MODEL,
    {
      supportedEndpoints: ["embeddings"],
    },
    { createIfMissing: true }
  );
  assert.equal(
    savedOverride?.source,
    "endpoint-override",
    "a synced-only model needs an explicit virtual endpoint override"
  );
  const [withOverride] = (await getAllActiveSyncedModels())[PROVIDER] || [];
  assert.deepEqual(withOverride.supportedEndpoints, ["embeddings"]);
  assert.equal(withOverride.endpointCapabilitySource, "operator");

  // A fresh discovery does not wipe the operator's choice.
  await replaceSyncedAvailableModelsForConnection(PROVIDER, CONNECTION_ID, [
    {
      id: LIVE_MODEL,
      name: LIVE_MODEL,
      source: "imported",
      supportedEndpoints: ["chat"],
      endpointCapabilitySource: "declared",
    },
  ]);
  const [afterSync] = (await getAllActiveSyncedModels())[PROVIDER] || [];
  assert.deepEqual(afterSync.supportedEndpoints, ["embeddings"]);
});