Files
OmniRoute/tests/unit/combo-auto-config-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

80 lines
2.6 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { parseAutoConfig } from "@omniroute/open-sse/services/combo/autoConfig.ts";
import { DEFAULT_WEIGHTS } from "@omniroute/open-sse/services/autoCombo/scoring.ts";
// Split guard for Block J Task 2: parseAutoConfig was extracted verbatim from
// handleComboChat's inline auto-strategy config block. These assertions pin the
// pure derivation so the extraction stays behavior-identical.
const target = (provider: string, modelStr: string) =>
({ provider, modelStr, executionKey: `${provider}>${modelStr}` }) as never;
test("defaults: rules strategy, provider-derived pool, default weights", () => {
const cfg = parseAutoConfig({ name: "c", config: {} } as never, [
target("openai", "gpt-4o"),
target("anthropic", "claude-3"),
target("openai", "gpt-4o-mini"),
]);
assert.equal(cfg.routingStrategy, "rules");
assert.deepEqual(cfg.candidatePool, ["openai", "anthropic"]);
assert.equal(cfg.weights, DEFAULT_WEIGHTS);
assert.equal(cfg.explorationRate, 0.05);
assert.equal(cfg.budgetCap, undefined);
assert.equal(cfg.modePack, undefined);
});
test("routerStrategy takes precedence over routingStrategy/strategyName", () => {
const cfg = parseAutoConfig(
{
name: "c",
autoConfig: {
routerStrategy: "lkgp",
routingStrategy: "cost",
strategyName: "p2c",
},
} as never,
[]
);
assert.equal(cfg.routingStrategy, "lkgp");
});
test("explicit candidatePool, weights, exploration and budget are honored", () => {
const customWeights = { latency: 1 } as never;
const cfg = parseAutoConfig(
{
name: "c",
autoConfig: {
candidatePool: ["glm", "openai"],
weights: customWeights,
explorationRate: 0.3,
budgetCap: 5,
modePack: "coding",
},
} as never,
[target("ignored", "x")]
);
assert.deepEqual(cfg.candidatePool, ["glm", "openai"]);
assert.equal(cfg.weights, customWeights);
assert.equal(cfg.explorationRate, 0.3);
assert.equal(cfg.budgetCap, 5);
assert.equal(cfg.modePack, "coding");
});
test("config.auto is preferred over top-level config", () => {
const cfg = parseAutoConfig(
{ name: "c", config: { auto: { routerStrategy: "cost" }, routerStrategy: "rules" } } as never,
[]
);
assert.equal(cfg.routingStrategy, "cost");
});
test("non-finite explorationRate falls back to 0.05", () => {
const cfg = parseAutoConfig(
{ name: "c", autoConfig: { explorationRate: "not-a-number" } } as never,
[]
);
assert.equal(cfg.explorationRate, 0.05);
});