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

6.8 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-noauth-models-"));
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 modelsRoute = await import("../../src/app/api/providers/[id]/models/route.ts");

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

// #3047 — a no-auth provider (originally OpenCode Free, now exercised through
// uncloseai) has no connection row, so the
// "Import from /models" button used to hit a 404 and silently no-op. The models
// route must serve a non-empty model list when called with a no-auth provider id.
// #3611 — the source may now be "upstream" (live fetch succeeded) or
// "local_catalog" (live fetch failed/unavailable); both are acceptable here.
test("models route serves models for a no-auth provider id (#3047)", async () => {
  const response = await modelsRoute.GET(
    new Request("http://localhost/api/providers/uncloseai/models?refresh=true"),
    { params: { id: "uncloseai" } }
  );
  assert.equal(response.status, 200);
  const body = await response.json();
  assert.equal(body.provider, "uncloseai");
  assert.ok(
    body.source === "local_catalog" || body.source === "upstream",
    `source must be 'local_catalog' or 'upstream', got '${body.source}'`
  );
  assert.ok(Array.isArray(body.models) && body.models.length > 0, "should return catalog models");
  assert.ok(
    body.models.every((m: { id?: unknown }) => typeof m.id === "string" && m.id.length > 0),
    "every model must have a non-empty id"
  );
});

test("models route still 404s for an unknown provider/connection id", async () => {
  const response = await modelsRoute.GET(
    new Request("http://localhost/api/providers/does-not-exist-xyz/models"),
    { params: { id: "does-not-exist-xyz" } }
  );
  assert.equal(response.status, 404);
});

// #3611 — a noAuth provider with a modelsUrl (uncloseai) must fetch live models from the
// provider's modelsUrl instead of always returning the stale local_catalog.

const LIVE_MODEL_LIST = [
  { id: "live-model-alpha", object: "model" },
  { id: "live-model-beta", object: "model" },
];

test("models route fetches live models from modelsUrl for noAuth provider with modelsUrl (#3611)", async () => {
  const originalFetch = globalThis.fetch;
  globalThis.fetch = async (url: string | URL, _init?: RequestInit) => {
    const urlStr = String(url);
    if (urlStr === "https://hermes.ai.unturf.com/v1/models") {
      return Response.json({ data: LIVE_MODEL_LIST });
    }
    return new Response("unexpected fetch: " + urlStr, { status: 500 });
  };

  try {
    const response = await modelsRoute.GET(
      new Request("http://localhost/api/providers/uncloseai/models"),
      { params: { id: "uncloseai" } }
    );
    assert.equal(response.status, 200);
    const body = await response.json();
    assert.equal(body.provider, "uncloseai");
    assert.equal(
      body.source,
      "upstream",
      "should report source as 'upstream' when live fetch succeeds"
    );
    assert.ok(Array.isArray(body.models), "models should be an array");
    const ids = body.models.map((m: { id: string }) => m.id);
    assert.ok(ids.includes("live-model-alpha"), "should include live model alpha");
    assert.ok(ids.includes("live-model-beta"), "should include live model beta");
  } finally {
    globalThis.fetch = originalFetch;
  }
});

test("metadata-only no-auth connection row still uses public model discovery", async () => {
  const connection = await providersDb.createProviderConnection({
    provider: "uncloseai",
    authType: "apikey",
    name: "uncloseai-metadata",
    isActive: true,
    testStatus: "unknown",
    providerSpecificData: {
      fingerprints: [{ id: "fingerprint-1" }],
      accountProxies: [],
    },
  });
  const originalFetch = globalThis.fetch;
  globalThis.fetch = async (url: string | URL) => {
    if (String(url) === "https://hermes.ai.unturf.com/v1/models") {
      return Response.json({ data: LIVE_MODEL_LIST });
    }
    return new Response("unexpected", { status: 500 });
  };

  try {
    const response = await modelsRoute.GET(
      new Request(`http://localhost/api/providers/${connection.id}/models`),
      { params: { id: connection.id } }
    );
    assert.equal(response.status, 200);
    const body = await response.json();
    assert.equal(body.provider, "uncloseai");
    assert.equal(body.connectionId, connection.id);
    assert.equal(body.source, "upstream");
    assert.ok(body.models.some((model: { id: string }) => model.id === "live-model-alpha"));
  } finally {
    globalThis.fetch = originalFetch;
  }
});

test("models route falls back to local_catalog when live modelsUrl fetch throws (#3611 fallback on error)", async () => {
  const originalFetch = globalThis.fetch;
  globalThis.fetch = async (url: string | URL, _init?: RequestInit) => {
    if (String(url) === "https://hermes.ai.unturf.com/v1/models") {
      throw new Error("network failure");
    }
    return new Response("unexpected", { status: 500 });
  };

  try {
    const response = await modelsRoute.GET(
      new Request("http://localhost/api/providers/uncloseai/models"),
      { params: { id: "uncloseai" } }
    );
    assert.equal(response.status, 200);
    const body = await response.json();
    assert.equal(body.provider, "uncloseai");
    assert.equal(body.source, "local_catalog", "should fall back to local_catalog on fetch error");
    assert.ok(Array.isArray(body.models) && body.models.length > 0, "should have catalog models");
  } finally {
    globalThis.fetch = originalFetch;
  }
});

test("models route falls back to local_catalog when live modelsUrl fetch returns non-OK (#3611 fallback on non-OK)", async () => {
  const originalFetch = globalThis.fetch;
  globalThis.fetch = async (url: string | URL, _init?: RequestInit) => {
    if (String(url) === "https://hermes.ai.unturf.com/v1/models") {
      return new Response("Service Unavailable", { status: 503 });
    }
    return new Response("unexpected", { status: 500 });
  };

  try {
    const response = await modelsRoute.GET(
      new Request("http://localhost/api/providers/uncloseai/models"),
      { params: { id: "uncloseai" } }
    );
    assert.equal(response.status, 200);
    const body = await response.json();
    assert.equal(body.provider, "uncloseai");
    assert.equal(
      body.source,
      "local_catalog",
      "should fall back to local_catalog when upstream returns non-OK"
    );
    assert.ok(Array.isArray(body.models) && body.models.length > 0, "should have catalog models");
  } finally {
    globalThis.fetch = originalFetch;
  }
});