Files
OmniRoute/tests/unit/combo-resolve-auto-strategy-split.test.ts
Diego Rodrigues de Sa e Souza d6dc869c9c refactor(sse): decompose handleComboChat auto-strategy region (Block J Task 2 — parseAutoConfig + resolveAutoStrategyOrder) (#6049)
* refactor(sse): extract pure parseAutoConfig leaf from handleComboChat

Block J Task 2 (safe slice): the auto-strategy config-resolution block in
handleComboChat is a pure function of (combo, eligibleTargets) with no side
effects, no early returns and no mutation. Extract it verbatim into
open-sse/services/combo/autoConfig.ts::parseAutoConfig so the god-function
shrinks and the derivation is independently unit-testable.

Behavior is byte-identical (verbatim-audited); combo.ts 3309->3280 LOC.
Adds tests/unit/combo-auto-config-split.test.ts (5 cases) pinning the
strategy-precedence, candidate-pool, weights and fallback derivations.

* refactor(sse): extract resolveAutoStrategyOrder leaf from handleComboChat

Block J Task 2 (coupled slice): the ~215-line `if (strategy === "auto")`
branch of handleComboChat is extracted into
open-sse/services/combo/resolveAutoStrategy.ts::resolveAutoStrategyOrder.

The branch is a control-flow region (mutates orderedTargets +
autoUsedExplicitRouter, early-returns 429, side-effect _registerExecutionCandidates),
so it is not a pure byte-identical move: the two `return unavailableResponse(...)`
exits become `{ earlyResponse }` and the mutated locals are returned instead of
closed over. Every other logic line is verbatim (semantic diff = only those
wrappers + the deeper getLKGP import path). `buildAutoCandidates` lives in
combo.ts, so it is injected via deps to keep the leaf acyclic (same DI pattern as
buildTargetTimeoutRunner) — which also makes the branch independently testable.

combo.ts 3280->3065 LOC. typecheck:core + check:cycles clean; dead host imports
removed. 60/60 consumer tests (router-strategies / auto-combo-engine /
combo-strategy-fallbacks / scoring-clamp / candidate-expansion / hidden-models)
cover the routable path end-to-end; new tests/unit/combo-resolve-auto-strategy-split.test.ts
pins the DI contract + the early-429 and default-ordering exits.

* test(sse): point quota-bypass source scan at resolveAutoStrategy leaf

The 'auto combo disables hard provider quota cutoffs when relay requests bypass'
source scan asserted combo.ts contains the bypass logic
(relayOptions?.bypassProviderQuotaPolicy === true + quotaPreflight enabled:false).
That block was extracted verbatim into combo/resolveAutoStrategy.ts (Block J
Task 2), so the scan now reads the leaf. Behavior unchanged.
2026-07-03 04:00:03 -03:00

89 lines
2.9 KiB
TypeScript

import { test, after } from "node:test";
import assert from "node:assert/strict";
import { resolveAutoStrategyOrder } from "@omniroute/open-sse/services/combo/resolveAutoStrategy.ts";
import { resetDbInstance } from "@/lib/db/core.ts";
// resolveAutoStrategyOrder loads the LKGP via the DB singleton (dynamic import);
// release the handle so the node:test runner does not hang on teardown (learning #3).
after(() => {
resetDbInstance();
});
// Split guard for Block J Task 2 (coupled slice): the `if (strategy === "auto")`
// branch of handleComboChat was extracted verbatim into resolveAutoStrategyOrder,
// with `buildAutoCandidates` injected (it lives in combo.ts, so a direct import
// would cycle). These tests pin the DI contract and the two control-flow exits
// that the host now forwards: an early 429 Response, and the default-ordering
// pass-through. The routable-selection path is covered end-to-end by the 60
// consumer tests (router-strategies / auto-combo-engine / combo-strategy-fallbacks).
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 baseDeps = (buildAutoCandidates: never) =>
({
orderedTargets: [target("openai", "gpt-4o"), target("anthropic", "claude-3")],
body: { messages: [{ role: "user", content: "hi" }] },
combo: { id: "c1", name: "autoc", config: {} },
settings: null,
config: {},
relayOptions: null,
resilienceSettings: { quotaPreflight: { enabled: false } },
log: noopLog,
buildAutoCandidates,
}) as never;
test("exports resolveAutoStrategyOrder", () => {
assert.equal(typeof resolveAutoStrategyOrder, "function");
});
test("no candidates -> keeps default ordering, no explicit router", async () => {
const build = (async () => []) as never;
const result = await resolveAutoStrategyOrder(baseDeps(build));
assert.ok(!("earlyResponse" in result));
if ("orderedTargets" in result) {
assert.equal(result.autoUsedExplicitRouter, false);
// default ordering preserved (both original targets survive)
assert.equal(result.orderedTargets.length, 2);
assert.equal(result.orderedTargets[0].provider, "openai");
}
});
test("all candidates quota-cutoff-blocked -> early 429 Response", async () => {
const build = (async () => [
{
kind: "model",
stepId: "s1",
executionKey: "openai>gpt-4o",
modelStr: "gpt-4o",
provider: "openai",
model: "gpt-4o",
quotaCutoffBlocked: true,
},
]) as never;
const result = await resolveAutoStrategyOrder(baseDeps(build));
assert.ok("earlyResponse" in result);
if ("earlyResponse" in result) {
assert.ok(result.earlyResponse instanceof Response);
assert.equal(result.earlyResponse.status, 429);
}
});