Files
OmniRoute/tests/unit/reasoning-routing.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966)

Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.

One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.

Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.

* fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff

The gate shells out to `git diff` through execFileSync with Node's default 1 MB
maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to
overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing
anything. 64 MB is far above any real PR and costs nothing when unused.
2026-08-29 01:17:40 -03:00

240 lines
8.5 KiB
TypeScript

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-reasoning-routing-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "test-reasoning-routing-secret";
const core = await import("../../src/lib/db/core.ts");
const apiKeysDb = await import("../../src/lib/db/apiKeys.ts");
const combosDb = await import("../../src/lib/db/combos.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const rulesDb = await import("../../src/lib/db/reasoningRoutingRules.ts");
const policy = await import("../../src/lib/reasoningRouting/policy.ts");
const schemas = await import("../../src/shared/validation/schemas/reasoningRouting.ts");
async function resetStorage() {
apiKeysDb.resetApiKeyState();
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
rulesDb.invalidateReasoningRoutingRuleCache();
}
function ruleInput(
patch: Partial<rulesDb.ReasoningRoutingRuleInput> = {}
): rulesDb.ReasoningRoutingRuleInput {
return {
name: "Test rule",
description: "",
scope: "global",
apiKeyId: null,
comboId: null,
connectionId: null,
modelPattern: null,
sourceEffort: "any",
requestTags: [],
tagMatchMode: "any",
effortMode: "inherit",
targetEffort: null,
targetKind: "keep",
targetModel: null,
targetComboId: null,
budgetAction: "preserve",
budgetTokens: null,
priority: 0,
enabled: true,
...patch,
};
}
test.beforeEach(resetStorage);
test.after(async () => {
await resetStorage();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("reasoning intent distinguishes missing, discrete effort, toggle, and budget-only signals", () => {
assert.deepEqual(policy.extractReasoningIntent("codex/gpt-5.6-sol-high", {}), {
model: "codex/gpt-5.6-sol",
effort: "high",
sourceEffort: "high",
hasReasoningSignal: true,
hasThinkingBudget: false,
});
const missing = policy.extractReasoningIntent("openai/gpt-4o", {});
assert.equal(missing.sourceEffort, "missing");
assert.equal(missing.hasReasoningSignal, false);
const budgetOnly = policy.extractReasoningIntent("anthropic/claude-opus-4-8", {
thinking: { type: "enabled", budget_tokens: 4096 },
});
assert.equal(budgetOnly.sourceEffort, "signal");
assert.equal(budgetOnly.hasThinkingBudget, true);
const disabled = policy.extractReasoningIntent("anthropic/claude-opus-4-8", {
thinking: { type: "disabled" },
});
assert.equal(disabled.sourceEffort, "none");
assert.equal(disabled.effort, "none");
const ordinarySuffixedModel = policy.extractReasoningIntent("custom/my-model-high", {});
assert.equal(ordinarySuffixedModel.model, "custom/my-model-high");
assert.equal(ordinarySuffixedModel.sourceEffort, "missing");
});
test("glob and tag matching use deterministic scope, priority, and exact-model precedence", async () => {
const key = await apiKeysDb.createApiKey("Scoped key", "reasoning-test-machine");
const keyId = String((key as Record<string, unknown>).id);
const global = await rulesDb.createReasoningRoutingRule(
ruleInput({ name: "global", priority: 999, targetKind: "model", targetModel: "openai/global" })
);
const wildcard = await rulesDb.createReasoningRoutingRule(
ruleInput({
name: "wildcard",
scope: "apiKey",
apiKeyId: keyId,
modelPattern: "codex/gpt-*",
priority: 10,
requestTags: ["coding", "internal"],
tagMatchMode: "all",
targetKind: "model",
targetModel: "codex/gpt-5.6-terra",
})
);
const exact = await rulesDb.createReasoningRoutingRule(
ruleInput({
name: "exact",
scope: "apiKey",
apiKeyId: keyId,
modelPattern: "codex/gpt-5.6-sol",
priority: 10,
requestTags: ["coding"],
targetKind: "model",
targetModel: "codex/gpt-5.6-luna",
})
);
const decision = await policy.resolveReasoningRoutingRule({
sourceModel: "codex/gpt-5.6-sol",
sourceEffort: "missing",
hasReasoningSignal: false,
apiKeyId: keyId,
requestTags: ["INTERNAL", "coding"],
});
assert.equal(decision?.rule.id, exact.id);
assert.notEqual(decision?.rule.id, wildcard.id);
assert.notEqual(decision?.rule.id, global.id);
assert.equal(policy.globMatches("codex/gpt-5.?", "codex/gpt-5.6"), true);
});
test("default does not override a budget-only signal and force replaces discrete effort", async () => {
await rulesDb.createReasoningRoutingRule(
ruleInput({ effortMode: "default", targetEffort: "high" })
);
const budgetOnly = policy.extractReasoningIntent("custom/unknown", {
thinking: { type: "enabled", budget_tokens: 2048 },
});
const defaultDecision = await policy.resolveReasoningRoutingRule({
sourceModel: budgetOnly.model,
sourceEffort: budgetOnly.sourceEffort,
hasReasoningSignal: budgetOnly.hasReasoningSignal,
});
assert.equal(defaultDecision?.targetEffort, null);
const forced = policy.applyReasoningRuleDirective({
model: "codex/gpt-5.6-sol",
effort: "low",
reasoning_effort: "low",
reasoning: { effort: "low", summary: "auto" },
thinking: { type: "enabled", budget_tokens: 2048 },
_omnirouteReasoningRule: {
id: "force-high",
effortMode: "force",
targetEffort: "high",
budgetAction: "preserve",
budgetTokens: null,
},
}) as Record<string, unknown>;
assert.equal(forced.reasoning_effort, "high");
assert.equal(forced.effort, undefined);
assert.deepEqual(forced.reasoning, { summary: "auto", effort: "high" });
assert.deepEqual(forced.thinking, { type: "enabled", budget_tokens: 2048 });
assert.equal(forced._omnirouteReasoningRule, undefined);
const untouched = { model: "openai/gpt-4o", messages: [] };
assert.equal(policy.applyReasoningRuleDirective(untouched), untouched);
});
test("CRUD validates references, invalidates cache, and cascades deleted owners", async () => {
const key = await apiKeysDb.createApiKey("Owner", "reasoning-owner-machine");
const combo = await combosDb.createCombo({
name: "reasoning-target",
models: ["openai/gpt-4o-mini"],
strategy: "priority",
});
const connection = await providersDb.createProviderConnection({
provider: "openai",
authType: "apikey",
name: "Reasoning connection",
apiKey: "sk-reasoning-test",
});
await assert.rejects(
rulesDb.createReasoningRoutingRule(ruleInput({ scope: "apiKey", apiKeyId: "missing-key" })),
/API key does not exist/
);
const apiRule = await rulesDb.createReasoningRoutingRule(
ruleInput({ scope: "apiKey", apiKeyId: String((key as Record<string, unknown>).id) })
);
const comboRule = await rulesDb.createReasoningRoutingRule(
ruleInput({ scope: "combo", comboId: String((combo as Record<string, unknown>).id) })
);
const connectionRule = await rulesDb.createReasoningRoutingRule(
ruleInput({
scope: "connection",
connectionId: String((connection as Record<string, unknown>).id),
})
);
assert.equal((await rulesDb.getReasoningRoutingRules()).length, 3);
await rulesDb.updateReasoningRoutingRule(apiRule.id, { priority: 42 });
assert.equal((await rulesDb.getReasoningRoutingRuleById(apiRule.id))?.priority, 42);
await apiKeysDb.deleteApiKey(String((key as Record<string, unknown>).id));
await combosDb.deleteCombo(String((combo as Record<string, unknown>).id));
await providersDb.deleteProviderConnection(String((connection as Record<string, unknown>).id));
assert.equal(await rulesDb.getReasoningRoutingRuleById(apiRule.id), null);
assert.equal(await rulesDb.getReasoningRoutingRuleById(comboRule.id), null);
assert.equal(await rulesDb.getReasoningRoutingRuleById(connectionRule.id), null);
assert.equal((await rulesDb.getReasoningRoutingRules()).length, 0);
});
test("schema rejects connection reroutes and none with a fixed budget", () => {
const connectionReroute = schemas.createReasoningRoutingRuleSchema.safeParse({
...ruleInput(),
scope: "connection",
connectionId: "connection-id",
targetKind: "model",
targetModel: "openai/gpt-4o",
});
assert.equal(connectionReroute.success, false);
const noneWithBudget = schemas.createReasoningRoutingRuleSchema.safeParse({
...ruleInput(),
effortMode: "force",
targetEffort: "none",
budgetAction: "set",
budgetTokens: 1024,
});
assert.equal(noneWithBudget.success, false);
});