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

5.9 KB
Raw Blame
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import test from "node:test";

const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-codex-synced-routing-"));
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 providersDb = await import("../../src/lib/db/providers.ts");
const { getModelInfoCore } = await import("../../open-sse/services/model.ts");

type TestProvider = "anthropic" | "codex" | "openai";

const GPT_56_CODEX_MODEL = "gpt-5.6-sol";
const FUTURE_CODEX_MODEL = "codex-next-preview";
const FUTURE_NON_GPT_MODEL = "orion-preview-2027";

async function seedConnection(provider: TestProvider, isActive = true) {
  return providersDb.createProviderConnection({
    provider,
    authType: provider === "codex" ? "oauth" : "apikey",
    name: `${provider}-routing-test`,
    email: provider === "codex" ? `${provider}@example.com` : undefined,
    apiKey: provider !== "codex" ? `sk-${provider}-routing-test` : undefined,
    isActive,
    providerSpecificData: provider === "codex" ? { workspaceId: "ws-routing-test" } : undefined,
  });
}

async function seedSyncedModel(provider: TestProvider, modelId: string, isActive = true) {
  const connection = await seedConnection(provider, isActive);
  assert.ok(connection?.id, `${provider} connection must be created`);
  await modelsDb.replaceSyncedAvailableModelsForConnection(provider, String(connection.id), [
    {
      id: modelId,
      name: modelId,
      apiFormat: "openai-responses",
      supportedEndpoints: ["chat"],
    },
  ]);
  return connection;
}

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

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

test("bare GPT-5.6 model routes through Codex when it is the only active provider", async () => {
  await seedSyncedModel("codex", GPT_56_CODEX_MODEL);

  const info = await getModelInfoCore(GPT_56_CODEX_MODEL, null);

  assert.equal(info.provider, "codex");
  assert.equal(info.model, GPT_56_CODEX_MODEL);
});

// #9275 put the whole gpt-5.6-sol tier set into CODEX_NATIVE_UNPREFIXED_MODELS, so an
// active Codex connection now claims the bare id ahead of OpenAI. Before, OpenAI won the
// overlap; the escape hatch is the explicit prefix, covered by the last test in this file.
test("Codex claims the bare model when both providers advertise it", async () => {
  await seedSyncedModel("codex", GPT_56_CODEX_MODEL);
  await seedSyncedModel("openai", GPT_56_CODEX_MODEL);

  const info = await getModelInfoCore(GPT_56_CODEX_MODEL, null);

  assert.equal(info.provider, "codex");
  assert.equal(info.model, GPT_56_CODEX_MODEL);
});

test("Codex is inferred when only its active catalog advertises the model", async () => {
  await seedSyncedModel("codex", FUTURE_CODEX_MODEL);
  await seedConnection("openai");

  const info = await getModelInfoCore(FUTURE_CODEX_MODEL, null);

  assert.equal(info.provider, "codex");
  assert.equal(info.model, FUTURE_CODEX_MODEL);
});

test("non-GPT models use the same active synchronized-catalog inference", async () => {
  await seedSyncedModel("anthropic", FUTURE_NON_GPT_MODEL);

  const info = await getModelInfoCore(FUTURE_NON_GPT_MODEL, null);

  assert.equal(info.provider, "anthropic");
  assert.equal(info.model, FUTURE_NON_GPT_MODEL);
});

test("OpenAI remains selected when it is the only active provider advertising the model", async () => {
  await seedSyncedModel("openai", GPT_56_CODEX_MODEL);

  const info = await getModelInfoCore(GPT_56_CODEX_MODEL, null);

  assert.equal(info.provider, "openai");
  assert.equal(info.model, GPT_56_CODEX_MODEL);
});

test("inactive Codex synchronized models do not influence bare-model routing", async () => {
  await seedSyncedModel("codex", GPT_56_CODEX_MODEL, false);
  await seedSyncedModel("openai", GPT_56_CODEX_MODEL);

  const info = await getModelInfoCore(GPT_56_CODEX_MODEL, null);

  assert.equal(info.provider, "openai");
  assert.equal(info.model, GPT_56_CODEX_MODEL);
});

test("Codex claims an overlapping static model when both connections are active", async () => {
  await seedConnection("codex");
  await seedConnection("openai");

  const info = await getModelInfoCore("gpt-5.5", null);

  assert.equal(info.provider, "codex");
  assert.equal(info.model, "gpt-5.5");
});

// The regression #9275 introduced and this file now guards: the Codex-native set must
// never claim a bare id when no codex connection is active — an OpenAI-only install
// would get "no active credentials for provider: codex" for a model OpenAI serves.
test("a Codex-native bare id stays on OpenAI when no codex connection exists", async () => {
  await seedConnection("openai");

  const info = await getModelInfoCore("gpt-5.5", null);

  assert.equal(info.provider, "openai");
  assert.equal(info.model, "gpt-5.5");
});

test("OpenAI remains selected for an overlapping static model when Codex is inactive", async () => {
  await seedConnection("codex", false);
  await seedConnection("openai");

  const info = await getModelInfoCore("gpt-5.5", null);

  assert.equal(info.provider, "openai");
  assert.equal(info.model, "gpt-5.5");
});

test("explicit Codex and OpenAI prefixes remain authoritative", async () => {
  await seedSyncedModel("codex", GPT_56_CODEX_MODEL);
  await seedSyncedModel("openai", GPT_56_CODEX_MODEL);

  const codexAlias = await getModelInfoCore(`cx/${GPT_56_CODEX_MODEL}`, null);
  const codexCanonical = await getModelInfoCore(`codex/${GPT_56_CODEX_MODEL}`, null);
  const openai = await getModelInfoCore(`openai/${GPT_56_CODEX_MODEL}`, null);

  assert.equal(codexAlias.provider, "codex");
  assert.equal(codexCanonical.provider, "codex");
  assert.equal(openai.provider, "openai");
});