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

12.9 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";

// Regression coverage for the #3322 auto-combo candidate expansion: an auto-combo
// without an explicit candidatePool broadens its eligible targets to every model
// of every active provider connection (so the router has the full pool to score).

const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-combo-expand-"));
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
process.env.DATA_DIR = TEST_DATA_DIR;

const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const modelsDb = await import("../../src/lib/db/models.ts");
const combo = await import("../../open-sse/services/combo.ts");
const providerModels = await import("../../open-sse/config/providerModels.ts");

function resetStorage() {
  core.resetDbInstance();
  fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
  fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}

test.beforeEach(() => resetStorage());

test.after(() => {
  core.resetDbInstance();
  fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
  if (ORIGINAL_DATA_DIR === undefined) delete process.env.DATA_DIR;
  else process.env.DATA_DIR = ORIGINAL_DATA_DIR;
});

test("expandAutoComboCandidatePool adds every model of an active provider when no candidatePool is set", async () => {
  await providersDb.createProviderConnection({
    provider: "openai",
    authType: "apikey",
    name: "OpenAI",
    apiKey: "sk-test-openai",
    defaultModel: "gpt-4o-mini",
  });

  const expanded = await combo.expandAutoComboCandidatePool([], { config: {} });

  // It should surface at least one openai/<model> target, all well-formed.
  assert.ok(expanded.length > 0, "expected the active provider's models to be expanded in");
  const openaiTargets = expanded.filter((t) => t.provider === "openai");
  assert.ok(openaiTargets.length > 0, "expected openai targets");
  for (const t of openaiTargets) {
    assert.equal(t.kind, "model");
    assert.equal(t.modelStr, `openai/${t.modelStr.split("/").slice(1).join("/")}`);
    assert.equal(t.connectionId, null);
  }
  // Every catalog model for openai should be represented.
  const catalogIds = providerModels.getProviderModels("openai").map((m) => `openai/${m.id}`);
  assert.ok(catalogIds.length > 0);
  for (const id of catalogIds) {
    assert.ok(
      expanded.some((t) => t.modelStr === id),
      `expected expanded targets to include ${id}`
    );
  }
});

test("expandAutoComboCandidatePool ignores malformed custom rows while keeping chat models", async () => {
  await providersDb.createProviderConnection({
    provider: "openai",
    authType: "apikey",
    name: "OpenAI",
    apiKey: "sk-test-openai",
  });

  // customModels is persisted as operator-writable JSON. A malformed row must not
  // abort the whole expansion or cause the static catalogue to replace the
  // explicitly visible custom chat model.
  core
    .getDbInstance()
    .prepare("INSERT OR REPLACE INTO key_value (namespace, key, value) VALUES (?, ?, ?)")
    .run(
      "customModels",
      "openai",
      JSON.stringify([
        null,
        "not-an-object",
        { name: "Missing Id" },
        { id: "gpt-custom", supportedEndpoints: ["chat"] },
      ])
    );

  const expanded = await combo.expandAutoComboCandidatePool([], { config: {} });

  assert.deepEqual(
    expanded.map((target) => target.modelStr),
    ["openai/gpt-custom"],
    "only the well-formed visible custom chat model should be expanded"
  );
});

test("expandAutoComboCandidatePool excludes retired Qwen rows with synced models", async () => {
  const db = core.getDbInstance();
  db.exec(`
    DROP TRIGGER provider_connections_retire_qwen_web_insert;
    DROP TRIGGER provider_connections_retire_qwen_web_update;
  `);

  const qwenWeb = await providersDb.createProviderConnection({
    provider: "qwen-web",
    authType: "apikey",
    name: "Retired Qwen Web",
    apiKey: "retired-qwen-web-key",
  });
  const legacyAlias = await providersDb.createProviderConnection({
    provider: "qw",
    authType: "apikey",
    name: "Retired Qwen Web Alias",
    apiKey: "retired-qw-key",
  });
  const qwenCloud = await providersDb.createProviderConnection({
    provider: "qwen-cloud",
    authType: "apikey",
    name: "Qwen Cloud Control",
    apiKey: "qwen-cloud-key",
  });

  await modelsDb.replaceSyncedAvailableModelsForConnection("qwen-web", qwenWeb.id, [
    { id: "retired-web-model", name: "Retired Web Model" },
  ]);
  await modelsDb.replaceSyncedAvailableModelsForConnection("qw", legacyAlias.id, [
    { id: "retired-alias-model", name: "Retired Alias Model" },
  ]);
  await modelsDb.replaceSyncedAvailableModelsForConnection("qwen-cloud", qwenCloud.id, [
    { id: "qwen3.8-max", name: "Qwen3.8 Max" },
  ]);

  const expanded = await combo.expandAutoComboCandidatePool([], { config: {} });

  assert.equal(
    expanded.some((target) => target.provider === "qwen-web"),
    false
  );
  assert.equal(
    expanded.some((target) => target.provider === "qw"),
    false
  );
  assert.ok(expanded.some((target) => target.modelStr === "qwen-cloud/qwen3.8-max"));
});

test("expandAutoComboCandidatePool includes clean-room ChatGPT Web and excludes its legacy alias", async () => {
  const db = core.getDbInstance();
  db.exec(`
    DROP TRIGGER IF EXISTS provider_connections_retire_chatgpt_web_insert;
    DROP TRIGGER IF EXISTS provider_connections_retire_chatgpt_web_update;
  `);
  for (const provider of ["chatgpt-web", "cgpt-web"]) {
    const model = provider === "chatgpt-web" ? "gpt-5-5-thinking" : "gpt-5.5";
    const credential =
      provider === "chatgpt-web"
        ? JSON.stringify({
            cookies: [
              {
                name: "session",
                value: "fixture",
                domain: ".chatgpt.com",
                path: "/",
                expires: -1,
                httpOnly: true,
                secure: true,
                sameSite: "Lax",
              },
            ],
            origins: [],
          })
        : `sk-${provider}-restored-expansion`;
    db.prepare(
      "INSERT INTO provider_connections " +
        "(id, provider, auth_type, name, api_key, is_active, test_status, created_at, updated_at) " +
        "VALUES (?, ?, 'apikey', ?, ?, 1, 'active', datetime('now'), datetime('now'))"
    ).run(`${provider}-restored-expansion`, provider, `${provider} restored expansion`, credential);
    await modelsDb.addCustomModel(provider, model, `${provider} model fixture`);
  }

  const expanded = await combo.expandAutoComboCandidatePool([], { config: {} });
  assert.ok(expanded.some((target) => target.modelStr === "chatgpt-web/gpt-5-5-thinking"));
  assert.equal(
    expanded.some((target) => target.provider === "cgpt-web"),
    false
  );
});

test("expandAutoComboCandidatePool is a no-op when an explicit candidatePool exists", async () => {
  await providersDb.createProviderConnection({
    provider: "openai",
    authType: "apikey",
    name: "OpenAI",
    apiKey: "sk-test-openai",
    defaultModel: "gpt-4o-mini",
  });

  const seed = [
    {
      kind: "model" as const,
      stepId: "openai/gpt-4o",
      executionKey: "openai/gpt-4o",
      modelStr: "openai/gpt-4o",
      provider: "openai",
      providerId: "openai",
      connectionId: null,
      weight: 1,
      label: null,
    },
  ];
  const result = await combo.expandAutoComboCandidatePool(seed, {
    config: { auto: { candidatePool: ["openai"] } },
  });
  assert.equal(result.length, 1, "candidatePool present → no expansion");
  assert.equal(result[0].modelStr, "openai/gpt-4o");
});

test("expandAutoComboCandidatePool falls through to active connections when candidatePool is an empty array", async () => {
  await providersDb.createProviderConnection({
    provider: "openai",
    authType: "apikey",
    name: "OpenAI",
    apiKey: "sk-test-openai",
    defaultModel: "gpt-4o-mini",
  });

  const expanded = await combo.expandAutoComboCandidatePool([], {
    config: { auto: { candidatePool: [] } },
  });

  // An empty candidatePool should NOT trigger early return — the function
  // should fall through and expand from active connections instead.
  assert.ok(
    expanded.length > 0,
    "expected expansion from active connections despite empty candidatePool"
  );
  const openaiTargets = expanded.filter((t) => t.provider === "openai");
  assert.ok(openaiTargets.length > 0, "expected openai targets to be expanded");
});

test('expandAutoComboCandidatePool is a no-op when the combo references other combos via kind:"combo-ref" entries', async () => {
  await providersDb.createProviderConnection({
    provider: "openai",
    authType: "apikey",
    name: "OpenAI",
    apiKey: "sk-test-openai",
    defaultModel: "gpt-4o-mini",
  });

  const seed = [
    {
      kind: "model" as const,
      stepId: "anthropic/claude-3-5-sonnet",
      executionKey: "anthropic/claude-3-5-sonnet",
      modelStr: "anthropic/claude-3-5-sonnet",
      provider: "anthropic",
      providerId: "anthropic",
      connectionId: null,
      weight: 1,
      label: null,
    },
  ];

  // An "auto" combo delegating to a "priority" sub-combo via a combo-ref entry:
  // expanding to every model of every active provider (openai included) would
  // defeat the point of the combo-ref constraint, so the resolved
  // eligibleTargets must be returned unchanged (#COMBO-REF).
  const result = await combo.expandAutoComboCandidatePool(seed, {
    config: {},
    models: [{ kind: "combo-ref", ref: "priority-subcombo" }],
  });

  assert.equal(result.length, 1, "combo-ref guard must prevent provider-wide expansion");
  assert.equal(result[0].modelStr, "anthropic/claude-3-5-sonnet");
  assert.ok(
    !result.some((t) => t.provider === "openai"),
    "no openai targets should have been pulled in despite an active openai connection"
  );
});

test("expandAutoComboCandidatePool is a no-op when the operator has populated models[] (no candidatePool)", async () => {
  await providersDb.createProviderConnection({
    provider: "openai",
    authType: "apikey",
    name: "OpenAI",
    apiKey: "sk-test-openai",
    defaultModel: "gpt-4o-mini",
  });

  const result = await combo.expandAutoComboCandidatePool([], {
    config: {},
    models: ["openai/gpt-4o-mini"],
  });

  // When the operator has populated models[] with explicit entries
  // (whether plain strings or records), the function must return the
  // seed unchanged rather than expanding to every model of every
  // active provider (which would silently inject unapproved models).
  assert.equal(result.length, 0, "populated models[] must short-circuit provider-wide expansion");
});

test("expandAutoComboCandidatePool does not duplicate an already-present modelStr", async () => {
  await providersDb.createProviderConnection({
    provider: "openai",
    authType: "apikey",
    name: "OpenAI",
    apiKey: "sk-test-openai",
    defaultModel: "gpt-4o-mini",
  });

  const firstCatalogId = providerModels.getProviderModels("openai")[0]?.id;
  assert.ok(firstCatalogId, "expected at least one openai catalog model");
  const existing = `openai/${firstCatalogId}`;
  const seed = [
    {
      kind: "model" as const,
      stepId: existing,
      executionKey: existing,
      modelStr: existing,
      provider: "openai",
      providerId: "openai",
      connectionId: "conn-1",
      weight: 5,
      label: "pinned",
    },
  ];

  const result = await combo.expandAutoComboCandidatePool(seed, { config: {} });
  const matches = result.filter((t) => t.modelStr === existing);
  assert.equal(matches.length, 1, "the pre-existing target must not be duplicated");
  // …and the original pinned entry (weight 5 / conn-1) is preserved, not overwritten.
  assert.equal(matches[0].connectionId, "conn-1");
  assert.equal(matches[0].weight, 5);
});

test("expandAutoComboCandidatePool excludes trigger-bypassed Microsoft Designer providers", async () => {
  await core.ensureDbInitialized();
  const db = core.getDbInstance();
  db.exec("DROP TRIGGER IF EXISTS trg_retire_microsoft_designer_web_provider_insert");
  db.exec("DROP TRIGGER IF EXISTS trg_retire_microsoft_designer_web_provider_update");

  for (const provider of ["microsoft-designer-web", "msdesigner", "openai"]) {
    await providersDb.createProviderConnection({
      provider,
      authType: "apikey",
      name: `${provider}-trigger-bypass`,
      apiKey: `sk-${provider}-test`,
      defaultModel: provider === "openai" ? "gpt-4o-mini" : "dall-e-3",
      isActive: true,
    });
    if (provider !== "openai") {
      await modelsDb.addCustomModel(provider, "designer-chat-bypass", "Bypass model");
    }
  }

  const expanded = await combo.expandAutoComboCandidatePool([], { config: {} });

  assert.equal(
    expanded.some((target) => ["microsoft-designer-web", "msdesigner"].includes(target.provider)),
    false
  );
  assert.equal(
    expanded.some((target) => target.provider === "openai"),
    true,
    "supported active providers must remain expandable"
  );
});