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.2 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";

const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-combo-builder-options-"));
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 combosDb = await import("../../src/lib/db/combos.ts");
const modelsDevSync = await import("../../src/lib/modelsDevSync.ts");
const route = await import("../../src/app/api/combos/builder/options/route.ts");

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

async function seedConnection(provider, overrides = {}) {
  const payload = {
    provider,
    authType: overrides.authType || "apikey",
    displayName: overrides.displayName,
    email: overrides.email,
    apiKey: overrides.apiKey === undefined ? "sk-test" : overrides.apiKey,
    accessToken: overrides.accessToken,
    isActive: overrides.isActive ?? true,
    priority: overrides.priority || 1,
    testStatus: overrides.testStatus || "active",
    rateLimitedUntil: overrides.rateLimitedUntil,
    defaultModel: overrides.defaultModel,
    providerSpecificData: overrides.providerSpecificData || {},
  };

  if (overrides.name !== undefined) {
    payload.name = overrides.name;
  } else if ((overrides.authType || "apikey") !== "oauth") {
    payload.name = `${provider}-${Math.random().toString(16).slice(2, 8)}`;
  }

  return providersDb.createProviderConnection(payload);
}

test.beforeEach(async () => {
  await resetStorage();
});

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

test("combo builder options route aggregates providers, connections, models and combo refs", async () => {
  const nowPlusMinute = Date.now() + 60_000;
  // gpt-4o was removed from the openai registry; use gpt-4.1 (confirmed at providerRegistry.ts:1156)
  modelsDevSync.saveModelsDevCapabilities({
    openai: {
      "gpt-4.1": {
        tool_call: true,
        reasoning: false,
        attachment: true,
        structured_output: true,
        temperature: true,
        modalities_input: JSON.stringify(["text", "image"]),
        modalities_output: JSON.stringify(["text"]),
        knowledge_cutoff: "2024-10",
        release_date: "2024-04-14",
        last_updated: "2024-10-01",
        status: "stable",
        family: "gpt-4",
        open_weights: false,
        limit_context: 1047576,
        limit_input: 1047576,
        limit_output: 32768,
        interleaved_field: null,
      },
    },
  });

  await seedConnection("openai", {
    name: "OpenAI Primary",
    priority: 2,
    defaultModel: "gpt-4.1",
  });
  await seedConnection("openai", {
    authType: "oauth",
    email: "[email protected]",
    accessToken: "oauth-token",
    isActive: false,
    priority: 1,
    testStatus: "failed",
  });
  await seedConnection("codex", {
    authType: "oauth",
    email: "[email protected]",
    accessToken: "codex-token",
    rateLimitedUntil: nowPlusMinute,
    defaultModel: "gpt-5.3-codex",
  });

  await modelsDb.addCustomModel("openai", "custom-ops", "Custom Ops");
  await modelsDb.addCustomModel("openai", "gpt-4.1", "Operator GPT");
  // #6975: embeddings-only models (supportedEndpoints without "chat") are no longer
  // dropped from the combo builder — they must appear like any other model.
  await modelsDb.addCustomModel(
    "openai",
    "text-embedding-visible",
    "Text Embedding",
    "manual",
    "chat-completions",
    ["embeddings"]
  );
  modelsDb.mergeModelCompatOverride("openai", "gpt-4o-mini", { isHidden: true });

  const visibleCombo = await combosDb.createCombo({
    name: "team-router",
    strategy: "priority",
    models: ["openai/gpt-4.1"],
  });
  await combosDb.createCombo({
    name: "hidden-router",
    strategy: "priority",
    models: ["openai/gpt-4.1"],
    isHidden: true,
  });

  const response = await route.GET();
  const body = (await response.json()) as any;

  assert.equal(response.status, 200);
  assert.equal(body.schemaVersion, 2);
  assert.ok(Array.isArray(body.providers));
  assert.ok(Array.isArray(body.comboRefs));

  const openai = body.providers.find((provider) => provider.providerId === "openai");
  const codex = body.providers.find((provider) => provider.providerId === "codex");

  assert.ok(openai);
  assert.equal(openai.displayName, "OpenAI");
  assert.equal(openai.connectionCount, 2);
  assert.equal(openai.activeConnectionCount, 1);
  const sharedModel = openai.models.find((model) => model.id === "gpt-4.1");
  assert.ok(sharedModel);
  assert.equal(sharedModel.name, "Operator GPT");
  assert.equal(sharedModel.contextLength, 1047576);
  assert.equal(sharedModel.outputTokenLimit, 32768);
  assert.equal(sharedModel.supportsThinking, false);
  assert.equal(
    openai.models.some((model) => model.id === "gpt-4o-mini"),
    false
  );
  assert.ok(openai.models.some((model) => model.id === "custom-ops"));
  // #6975: embeddings-only models must now appear in the combo builder output.
  assert.equal(
    openai.models.some((model) => model.id === "text-embedding-visible"),
    true
  );
  assert.deepEqual(
    openai.connections.map((connection) => ({
      label: connection.label,
      status: connection.status,
      isActive: connection.isActive,
    })),
    [
      {
        label: "OpenAI Primary",
        status: "active",
        isActive: true,
      },
      {
        label: "[email protected]",
        status: "inactive",
        isActive: false,
      },
    ]
  );
  assert.equal(
    openai.connections.some((connection) =>
      Object.prototype.hasOwnProperty.call(connection, "apiKey")
    ),
    false
  );
  assert.equal(
    openai.connections.some((connection) =>
      Object.prototype.hasOwnProperty.call(connection, "accessToken")
    ),
    false
  );

  assert.ok(codex);
  assert.equal(codex.connections[0].status, "rate-limited");
  assert.equal(codex.connections[0].defaultModel, "gpt-5.3-codex");

  assert.deepEqual(
    body.comboRefs.map((combo) => combo.name),
    [visibleCombo.name]
  );
});

test("combo builder options route includes no-auth provider (uncloseai) even without provider_connections rows", async () => {
  // No connections seeded — uncloseai has noAuth: true and never gets a provider_connections row.
  const response = await route.GET();
  const body = (await response.json()) as any;

  assert.equal(response.status, 200);
  assert.ok(Array.isArray(body.providers), "providers should be an array");

  const noAuth = body.providers.find((p: any) => p.providerId === "uncloseai");
  assert.ok(noAuth, "uncloseai should appear in combo builder options even without connections");
  assert.equal(noAuth.connectionCount, 0, "uncloseai should have 0 connections");
  assert.equal(noAuth.activeConnectionCount, 0, "uncloseai should have 0 active connections");
  assert.ok(noAuth.models.length > 0, "uncloseai should expose its built-in models");
  // Spot-check a known built-in model from providerRegistry.ts
  assert.ok(
    noAuth.models.some((m: any) => m.id === "turboderp/Qwen3.8-27B-exl3"),
    "the built-in model should be among uncloseai models"
  );
  // #2901: no-auth providers route under their alias ("unc/"), not their id.
  assert.equal(noAuth.models[0].qualifiedModel.startsWith("unc/"), true);
  assert.equal(noAuth.source, "system", "uncloseai should have source=system");
});

test("combo builder options route exposes compatible provider nodes with node metadata", async () => {
  await providersDb.createProviderNode({
    id: "openai-compatible-demo",
    type: "openai-compatible",
    name: "Gateway Demo",
    prefix: "gd",
    baseUrl: "https://proxy.example.com",
    chatPath: "/v1/chat/completions",
    modelsPath: "/v1/models",
  });
  await seedConnection("openai-compatible-demo", {
    name: "Gateway Account",
    providerSpecificData: {
      baseUrl: "https://proxy.example.com",
    },
  });
  await modelsDb.addCustomModel("openai-compatible-demo", "gpt-custom", "GPT Custom");

  const response = await route.GET();
  const body = (await response.json()) as any;
  const provider = body.providers.find((entry) => entry.providerId === "openai-compatible-demo");

  assert.equal(response.status, 200);
  assert.ok(provider);
  assert.equal(provider.displayName, "Gateway Demo");
  assert.equal(provider.providerType, "openai-compatible");
  assert.equal(provider.alias, "gd");
  assert.equal(provider.prefix, "gd");
  assert.equal(provider.source, "provider-node");
  assert.equal(provider.acceptsArbitraryModel, true);
  assert.ok(provider.models.some((model) => model.id === "gpt-custom"));
  // #14135/#14143: when a provider node declares a routing prefix, qualifiedModel must
  // carry that prefix, not the internal node id — `parseModel()` only resolves the alias.
  // Asserted against the node's own prefix so the pair cannot drift apart again.
  assert.equal(provider.models[0].qualifiedModel, `${provider.prefix}/gpt-custom`);
  assert.equal(provider.models[0].qualifiedModel, "gd/gpt-custom");
});