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

3.3 KB
Raw Blame
import { test, after } from "node:test";
import assert from "node:assert/strict";

import { applyStrategyOrdering } from "@omniroute/open-sse/services/combo/applyStrategyOrdering.ts";
import { resetDbInstance } from "@/lib/db/core.ts";

// Split guard for Block J Task 3: the non-`auto` strategy-ordering chain
// (lkgp / strict-random / random / fill-first / p2c / ... / quota-share) was
// extracted verbatim into applyStrategyOrdering. These tests pin the exits that
// need no DB/deck state (random / fill-first / unknown); the DB-backed branches
// (lkgp, reset-*, quota-share) are covered end-to-end by the 47 consumer tests
// (router-strategies / combo-strategy-fallbacks / rr-session-stickiness).

after(() => {
  // some branches (lkgp/quota-share) may touch the DB singleton; release handles.
  resetDbInstance();
});

const noopLog = { info() {}, warn() {}, error() {}, debug() {} } as never;

const target = (provider: string, modelStr: string): never =>
  ({
    kind: "model",
    stepId: "s1",
    executionKey: `${provider}>${modelStr}`,
    modelStr,
    provider,
    providerId: null,
    connectionId: null,
    weight: 1,
    label: null,
  }) as never;

const deps = () =>
  ({
    combo: { id: "c1", name: "c1", config: {} },
    config: {},
    body: { messages: [] },
    log: noopLog,
    apiKeyAllowedConnections: null,
  }) as never;

const keys = (arr: Array<{ executionKey: string }>) => arr.map((t) => t.executionKey).sort();

test("exports applyStrategyOrdering", () => {
  assert.equal(typeof applyStrategyOrdering, "function");
});

test("unknown strategy -> input order unchanged (same reference contents)", async () => {
  const input = [target("openai", "gpt-4o"), target("anthropic", "claude-3")];
  const out = await applyStrategyOrdering("no-such-strategy", input, deps());
  assert.deepEqual(
    out.orderedTargets.map((t: { executionKey: string }) => t.executionKey),
    ["openai>gpt-4o", "anthropic>claude-3"]
  );
});

test("fill-first -> preserves priority order", async () => {
  const input = [target("a", "m1"), target("b", "m2"), target("c", "m3")];
  const out = await applyStrategyOrdering("fill-first", input, deps());
  assert.deepEqual(
    out.orderedTargets.map((t: { executionKey: string }) => t.executionKey),
    ["a>m1", "b>m2", "c>m3"]
  );
});

test("random -> same multiset of targets (a permutation)", async () => {
  const input = [target("a", "m1"), target("b", "m2"), target("c", "m3")];
  const out = await applyStrategyOrdering("random", input, deps());
  assert.equal(out.orderedTargets.length, 3);
  assert.deepEqual(keys(out.orderedTargets), keys(input));
});

test("cost-optimized manifest routing logs through the canonical strategy path", async () => {
  const debugCalls: unknown[][] = [];
  const log = {
    info() {},
    warn() {},
    error() {},
    debug(...args: unknown[]) {
      debugCalls.push(args);
    },
  } as never;
  const input = [target("openai", "gpt-4o"), target("anthropic", "claude-3")];

  const out = await applyStrategyOrdering("cost-optimized", input, {
    ...deps(),
    config: { manifestRouting: true },
    body: { messages: [{ role: "user", content: "hello" }] },
    log,
  } as never);

  assert.equal(out.orderedTargets.length, 2);
  assert.equal(
    debugCalls.some((args) => args[1] === "manifest routing applied"),
    true,
    "manifest routing must log from applyStrategyOrdering"
  );
});