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

2.7 KB
Raw Blame
// tests/integration/combo-matrix/weighted.test.ts
import test from "node:test";
import assert from "node:assert/strict";
import { createComboRoutingHarness } from "../_comboRoutingHarness.ts";

const h = await createComboRoutingHarness("combo-weighted");
const { BaseExecutor, combosDb, handleChat, buildRequest, seedConnection, resetStorage } = h;

function body(model: string) {
  return { model, stream: false, messages: [{ role: "user", content: "w" }] };
}

test.beforeEach(async () => {
  BaseExecutor.RETRY_CONFIG.delayMs = 0;
  await resetStorage();
});
test.afterEach(async () => {
  BaseExecutor.RETRY_CONFIG.delayMs = h.originalRetryDelayMs;
  await resetStorage();
});
test.after(async () => {
  await h.cleanup();
});

test("weighted: 70/30 weights produce roughly proportional distribution", async () => {
  const N = 200;
  // This measures selection, not throughput. The default 60 RPM reservoir can
  // exhaust mid-sample on fast CI runners and make one target unavailable.
  // Keep protection active, but budget for all samples choosing either target.
  await h.settingsDb.updateSettings({
    resilienceSettings: {
      requestQueue: { requestsPerMinute: N, minTimeBetweenRequestsMs: 0 },
    },
  });
  const { resolveResilienceSettings } = await import("../../../src/lib/resilience/settings.ts");
  const queue = resolveResilienceSettings(await h.settingsDb.getSettings()).requestQueue;
  assert.equal(
    queue.autoEnableApiKeyProviders,
    true,
    "fixture keeps rate-limit protection enabled"
  );
  assert.ok(queue.requestsPerMinute >= N, "fixture budget must fit the entire distribution sample");
  await seedConnection("openai", { apiKey: "sk-openai-w" });
  await seedConnection("claude", { apiKey: "sk-claude-w" });
  await combosDb.createCombo({
    name: "m-weighted",
    strategy: "weighted",
    config: { maxRetries: 0, retryDelayMs: 0, stickyWeightedLimit: 1 },
    models: [
      { id: "w-openai", kind: "model", providerId: "openai", model: "gpt-4o-mini", weight: 70 },
      {
        id: "w-claude",
        kind: "model",
        providerId: "claude",
        model: "claude-sonnet-4-6",
        weight: 30,
      },
    ],
  });
  h.installRecordingFetch();

  for (let i = 0; i < N; i++) {
    const r = await handleChat(buildRequest({ body: body("m-weighted") }));
    assert.equal(r.status, 200);
    assert.equal((await r.json()).choices[0].message.content, "ok");
  }
  const seen = h.providersSeen();
  const openaiShare = seen.filter((p) => p === "openai").length / N;
  // Tolerance ±0.12 absorbs sampling noise at N=200 while still proving the split.
  assert.ok(openaiShare > 0.58 && openaiShare < 0.82, `openai share ${openaiShare} not ~0.70`);
  assert.ok(seen.includes("claude"), "weighted must still reach the 30% target");
});