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

10.5 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-chat-combo-live-"));
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 settingsDb = await import("../../src/lib/db/settings.ts");
const chatRoute = await import("../../src/app/api/v1/chat/completions/route.ts");
const { generateSignature, invalidateBySignature, setCachedResponse, outputContractOf } =
  await import("../../src/lib/semanticCache.ts");
const { getCircuitBreaker, resetAllCircuitBreakers, STATE } =
  await import("../../src/shared/utils/circuitBreaker.ts");

const originalFetch = globalThis.fetch;

async function flushBackgroundWork() {
  await new Promise((resolve) => setTimeout(resolve, 50));
  await new Promise((resolve) => setImmediate(resolve));
}

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 });
  resetAllCircuitBreakers();
}

async function seedSuppressedConnection() {
  return providersDb.createProviderConnection({
    provider: "openai",
    authType: "apikey",
    name: "openai-live-test",
    apiKey: "sk-live-test",
    isActive: true,
    testStatus: "credits_exhausted",
    rateLimitedUntil: new Date(Date.now() + 60_000).toISOString(),
  });
}

async function seedHealthyConnection() {
  return providersDb.createProviderConnection({
    provider: "openai",
    authType: "apikey",
    name: "openai-cache-test",
    apiKey: "sk-cache-test",
    isActive: true,
    testStatus: "active",
  });
}

const DEFAULT_REQUEST_BODY = {
  model: "openai/gpt-4.1",
  messages: [{ role: "user", content: "Reply with OK only." }],
  max_tokens: 16,
  stream: false,
  temperature: 0,
};

function makeRequest(extraHeaders = {}) {
  return new Request("http://localhost/v1/chat/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      ...extraHeaders,
    },
    body: JSON.stringify(DEFAULT_REQUEST_BODY),
  });
}

function makeStreamingRequest(extraHeaders = {}) {
  return new Request("http://localhost/v1/chat/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Accept: "text/event-stream",
      ...extraHeaders,
    },
    body: JSON.stringify({
      model: "openai/gpt-4.1",
      messages: [{ role: "user", content: "Stream OK only." }],
      max_tokens: 16,
      stream: true,
      temperature: 0,
    }),
  });
}

function makeRequestWithoutStreamFlag(extraHeaders = {}) {
  return new Request("http://localhost/v1/chat/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      ...extraHeaders,
    },
    body: JSON.stringify({
      model: "openai/gpt-4.1",
      messages: [{ role: "user", content: "Reply with OMITTED STREAM OK only." }],
      max_tokens: 16,
      temperature: 0,
    }),
  });
}

async function readAll(response: Response): Promise<string> {
  const reader = response.body!.getReader();
  const decoder = new TextDecoder();
  let out = "";
  while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    if (value) out += decoder.decode(value, { stream: true });
  }
  return out;
}

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

test.afterEach(async () => {
  await flushBackgroundWork();
  globalThis.fetch = originalFetch;
  resetAllCircuitBreakers();
});

test.after(async () => {
  await flushBackgroundWork();
  globalThis.fetch = originalFetch;
  resetAllCircuitBreakers();
  core.resetDbInstance();
  fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});

test("combo live test bypasses connection cooldown and breaker state to perform a real upstream request", async () => {
  const created = await seedSuppressedConnection();

  const fetchCalls = [];
  globalThis.fetch = async (url, init = {}) => {
    fetchCalls.push({ url: String(url), init });
    return Response.json({
      id: "chatcmpl-live-test",
      choices: [
        {
          message: {
            role: "assistant",
            content: "OK",
          },
        },
      ],
    });
  };

  const blockedByCooldown = await chatRoute.POST(makeRequest());
  assert.equal(blockedByCooldown.status, 503);
  assert.equal(fetchCalls.length, 0);

  const breaker = getCircuitBreaker("openai");
  breaker.state = STATE.OPEN;
  breaker.lastFailureTime = Date.now();
  breaker.resetTimeout = 60_000;

  const blockedByBreaker = await chatRoute.POST(makeRequest());
  assert.equal(blockedByBreaker.status, 503);
  assert.equal(fetchCalls.length, 0);

  const liveResponse = await chatRoute.POST(
    makeRequest({ "X-Internal-Test": "combo-health-check" })
  );
  const liveBody = (await liveResponse.json()) as any;

  assert.equal(liveResponse.status, 200);
  assert.equal(fetchCalls.length, 1);
  assert.match(fetchCalls[0].url, /\/chat\/completions$/);
  assert.equal(fetchCalls[0].init.headers.Authorization, "Bearer sk-live-test");
  assert.equal(liveBody.choices[0].message.content, "OK");

  const updated = await providersDb.getProviderConnectionById((created as any).id);
  assert.equal(updated.testStatus, "active");
});

test("combo live test bypasses semantic cache and forces a fresh upstream request", async () => {
  await seedHealthyConnection();

  // Seed under the signature the production read/store paths compute: the request
  // carries generation params (max_tokens — #15149) that outputContractOf folds into
  // the digest, so a bare legacy signature would miss this entry entirely.
  const signature = generateSignature(
    "gpt-4.1",
    DEFAULT_REQUEST_BODY.messages,
    0,
    1,
    undefined,
    outputContractOf(DEFAULT_REQUEST_BODY)
  );

  setCachedResponse(signature, "gpt-4.1", {
    id: "chatcmpl-cached",
    choices: [
      {
        message: {
          role: "assistant",
          content: "CACHED",
        },
      },
    ],
  });

  const fetchCalls = [];
  globalThis.fetch = async (url, init = {}) => {
    fetchCalls.push({ url: String(url), init });
    return Response.json({
      id: "chatcmpl-live",
      choices: [
        {
          message: {
            role: "assistant",
            content: "LIVE",
          },
        },
      ],
    });
  };

  try {
    const cachedResponse = await chatRoute.POST(makeRequest());
    const cachedBody = (await cachedResponse.json()) as any;

    assert.equal(cachedResponse.status, 200);
    assert.equal(fetchCalls.length, 0);
    assert.equal(cachedBody.choices[0].message.content, "CACHED");

    const liveResponse = await chatRoute.POST(
      makeRequest({
        "X-Internal-Test": "combo-health-check",
        "X-OmniRoute-No-Cache": "true",
        "X-Request-Id": "combo-test-cache-bypass",
      })
    );
    const liveBody = (await liveResponse.json()) as any;

    assert.equal(liveResponse.status, 200);
    assert.equal(fetchCalls.length, 1);
    assert.match(fetchCalls[0].url, /\/chat\/completions$/);
    assert.equal(liveBody.choices[0].message.content, "LIVE");
  } finally {
    invalidateBySignature(signature);
  }
});

test("chat completions route emits early keepalive while waiting for stream readiness", async () => {
  await seedHealthyConnection();

  globalThis.fetch = async () => {
    // Must exceed resolveKeepaliveThreshold()'s DEFAULT_THRESHOLD_MS (2000ms) for
    // openai/* — otherwise withEarlyStreamKeepalive takes the FAST path, forwards
    // the handler response as-is and no keepalive frame is ever emitted. The old
    // 100ms only worked while unrelated handler latency happened to push the
    // total past the threshold, which made this assertion incidental rather than
    // deterministic; it stopped holding once the handler got faster.
    await new Promise((resolve) => setTimeout(resolve, 2_400));
    return new Response(
      [
        `data: ${JSON.stringify({
          id: "chatcmpl-slow-stream",
          object: "chat.completion.chunk",
          choices: [{ index: 0, delta: { role: "assistant", content: "OK" } }],
        })}`,
        "",
        "data: [DONE]",
        "",
      ].join("\n"),
      {
        status: 200,
        headers: { "Content-Type": "text/event-stream" },
      }
    );
  };

  const response = await chatRoute.POST(makeStreamingRequest());
  assert.equal(response.status, 200);
  assert.match(response.headers.get("content-type") || "", /text\/event-stream/);

  const body = await readAll(response);
  assert.match(body, /data: \{"id":"chatcmpl-keepalive","object":"chat\.completion\.chunk"/);
  assert.match(body, /OK/);
  assert.match(body, /\[DONE\]/);
});

test("chat completions route returns JSON without early SSE framing when stream is omitted and Accept is application/json", async () => {
  await seedHealthyConnection();

  globalThis.fetch = async () => {
    await new Promise((resolve) => setTimeout(resolve, 100));
    return Response.json({
      id: "chatcmpl-slow-json",
      choices: [
        {
          message: {
            role: "assistant",
            content: "OK",
          },
        },
      ],
    });
  };

  const response = await chatRoute.POST(
    makeRequestWithoutStreamFlag({
      Accept: "application/json",
      "X-OmniRoute-No-Cache": "true",
      "X-Request-Id": "chat-route-omitted-stream-json",
    })
  );
  assert.equal(response.status, 200);
  assert.doesNotMatch(response.headers.get("content-type") || "", /text\/event-stream/);

  const body = (await response.json()) as any;
  assert.equal(body.choices[0].message.content, "OK");
});

test("combo live test does not use cooldown-aware request retry on upstream failures", async () => {
  await seedHealthyConnection();
  await settingsDb.updateSettings({
    requestRetry: 3,
    maxRetryIntervalSec: 5,
  });

  let fetchCalls = 0;
  globalThis.fetch = async () => {
    fetchCalls += 1;
    return Response.json(
      {
        error: {
          message: "upstream unavailable",
        },
      },
      { status: 503 }
    );
  };

  const liveResponse = await chatRoute.POST(
    makeRequest({ "X-Internal-Test": "combo-health-check" })
  );
  const liveBody = (await liveResponse.json()) as any;

  assert.equal(liveResponse.status, 503);
  assert.ok(
    fetchCalls >= 1 && fetchCalls <= 3,
    `live combo test should not storm retries, got ${fetchCalls} fetches`
  );
  assert.match(liveBody.error.message, /upstream unavailable/i);
});