Files
OmniRoute/tests/unit/combo-auto-candidate-expansion.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

322 lines
11 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";
// Regression coverage for the #3322 auto-combo candidate expansion: an auto-combo
// without an explicit candidatePool broadens its eligible targets to every model
// of every active provider connection (so the router has the full pool to score).
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-combo-expand-"));
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const modelsDb = await import("../../src/lib/db/models.ts");
const combo = await import("../../open-sse/services/combo.ts");
const providerModels = await import("../../open-sse/config/providerModels.ts");
function resetStorage() {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
test.beforeEach(() => resetStorage());
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
if (ORIGINAL_DATA_DIR === undefined) delete process.env.DATA_DIR;
else process.env.DATA_DIR = ORIGINAL_DATA_DIR;
});
test("expandAutoComboCandidatePool adds every model of an active provider when no candidatePool is set", async () => {
await providersDb.createProviderConnection({
provider: "openai",
authType: "apikey",
name: "OpenAI",
apiKey: "sk-test-openai",
defaultModel: "gpt-4o-mini",
});
const expanded = await combo.expandAutoComboCandidatePool([], { config: {} });
// It should surface at least one openai/<model> target, all well-formed.
assert.ok(expanded.length > 0, "expected the active provider's models to be expanded in");
const openaiTargets = expanded.filter((t) => t.provider === "openai");
assert.ok(openaiTargets.length > 0, "expected openai targets");
for (const t of openaiTargets) {
assert.equal(t.kind, "model");
assert.equal(t.modelStr, `openai/${t.modelStr.split("/").slice(1).join("/")}`);
assert.equal(t.connectionId, null);
}
// Every catalog model for openai should be represented.
const catalogIds = providerModels.getProviderModels("openai").map((m) => `openai/${m.id}`);
assert.ok(catalogIds.length > 0);
for (const id of catalogIds) {
assert.ok(
expanded.some((t) => t.modelStr === id),
`expected expanded targets to include ${id}`
);
}
});
test("expandAutoComboCandidatePool excludes retired Qwen rows with synced models", async () => {
const db = core.getDbInstance();
db.exec(`
DROP TRIGGER provider_connections_retire_qwen_web_insert;
DROP TRIGGER provider_connections_retire_qwen_web_update;
`);
const qwenWeb = await providersDb.createProviderConnection({
provider: "qwen-web",
authType: "apikey",
name: "Retired Qwen Web",
apiKey: "retired-qwen-web-key",
});
const legacyAlias = await providersDb.createProviderConnection({
provider: "qw",
authType: "apikey",
name: "Retired Qwen Web Alias",
apiKey: "retired-qw-key",
});
const qwenCloud = await providersDb.createProviderConnection({
provider: "qwen-cloud",
authType: "apikey",
name: "Qwen Cloud Control",
apiKey: "qwen-cloud-key",
});
await modelsDb.replaceSyncedAvailableModelsForConnection("qwen-web", qwenWeb.id, [
{ id: "retired-web-model", name: "Retired Web Model" },
]);
await modelsDb.replaceSyncedAvailableModelsForConnection("qw", legacyAlias.id, [
{ id: "retired-alias-model", name: "Retired Alias Model" },
]);
await modelsDb.replaceSyncedAvailableModelsForConnection("qwen-cloud", qwenCloud.id, [
{ id: "qwen3.8-max", name: "Qwen3.8 Max" },
]);
const expanded = await combo.expandAutoComboCandidatePool([], { config: {} });
assert.equal(
expanded.some((target) => target.provider === "qwen-web"),
false
);
assert.equal(
expanded.some((target) => target.provider === "qw"),
false
);
assert.ok(expanded.some((target) => target.modelStr === "qwen-cloud/qwen3.8-max"));
});
test("expandAutoComboCandidatePool excludes restored retired ChatGPT Web connections", async () => {
const db = core.getDbInstance();
db.exec(`
DROP TRIGGER IF EXISTS provider_connections_retire_chatgpt_web_insert;
DROP TRIGGER IF EXISTS provider_connections_retire_chatgpt_web_update;
`);
for (const provider of ["chatgpt-web", "cgpt-web"]) {
db.prepare(
"INSERT INTO provider_connections " +
"(id, provider, auth_type, name, api_key, is_active, test_status, created_at, updated_at) " +
"VALUES (?, ?, 'apikey', ?, ?, 1, 'active', datetime('now'), datetime('now'))"
).run(
`${provider}-restored-expansion`,
provider,
`${provider} restored expansion`,
`sk-${provider}-restored-expansion`
);
await modelsDb.addCustomModel(provider, "gpt-5.5", "Retired model fixture");
}
const expanded = await combo.expandAutoComboCandidatePool([], { config: {} });
assert.equal(
expanded.some((target) => ["chatgpt-web", "cgpt-web"].includes(target.provider)),
false
);
});
test("expandAutoComboCandidatePool is a no-op when an explicit candidatePool exists", async () => {
await providersDb.createProviderConnection({
provider: "openai",
authType: "apikey",
name: "OpenAI",
apiKey: "sk-test-openai",
defaultModel: "gpt-4o-mini",
});
const seed = [
{
kind: "model" as const,
stepId: "openai/gpt-4o",
executionKey: "openai/gpt-4o",
modelStr: "openai/gpt-4o",
provider: "openai",
providerId: "openai",
connectionId: null,
weight: 1,
label: null,
},
];
const result = await combo.expandAutoComboCandidatePool(seed, {
config: { auto: { candidatePool: ["openai"] } },
});
assert.equal(result.length, 1, "candidatePool present → no expansion");
assert.equal(result[0].modelStr, "openai/gpt-4o");
});
test("expandAutoComboCandidatePool falls through to active connections when candidatePool is an empty array", async () => {
await providersDb.createProviderConnection({
provider: "openai",
authType: "apikey",
name: "OpenAI",
apiKey: "sk-test-openai",
defaultModel: "gpt-4o-mini",
});
const expanded = await combo.expandAutoComboCandidatePool([], {
config: { auto: { candidatePool: [] } },
});
// An empty candidatePool should NOT trigger early return — the function
// should fall through and expand from active connections instead.
assert.ok(
expanded.length > 0,
"expected expansion from active connections despite empty candidatePool"
);
const openaiTargets = expanded.filter((t) => t.provider === "openai");
assert.ok(openaiTargets.length > 0, "expected openai targets to be expanded");
});
test('expandAutoComboCandidatePool is a no-op when the combo references other combos via kind:"combo-ref" entries', async () => {
await providersDb.createProviderConnection({
provider: "openai",
authType: "apikey",
name: "OpenAI",
apiKey: "sk-test-openai",
defaultModel: "gpt-4o-mini",
});
const seed = [
{
kind: "model" as const,
stepId: "anthropic/claude-3-5-sonnet",
executionKey: "anthropic/claude-3-5-sonnet",
modelStr: "anthropic/claude-3-5-sonnet",
provider: "anthropic",
providerId: "anthropic",
connectionId: null,
weight: 1,
label: null,
},
];
// An "auto" combo delegating to a "priority" sub-combo via a combo-ref entry:
// expanding to every model of every active provider (openai included) would
// defeat the point of the combo-ref constraint, so the resolved
// eligibleTargets must be returned unchanged (#COMBO-REF).
const result = await combo.expandAutoComboCandidatePool(seed, {
config: {},
models: [{ kind: "combo-ref", ref: "priority-subcombo" }],
});
assert.equal(result.length, 1, "combo-ref guard must prevent provider-wide expansion");
assert.equal(result[0].modelStr, "anthropic/claude-3-5-sonnet");
assert.ok(
!result.some((t) => t.provider === "openai"),
"no openai targets should have been pulled in despite an active openai connection"
);
});
test("expandAutoComboCandidatePool is a no-op when the operator has populated models[] (no candidatePool)", async () => {
await providersDb.createProviderConnection({
provider: "openai",
authType: "apikey",
name: "OpenAI",
apiKey: "sk-test-openai",
defaultModel: "gpt-4o-mini",
});
const result = await combo.expandAutoComboCandidatePool([], {
config: {},
models: ["openai/gpt-4o-mini"],
});
// When the operator has populated models[] with explicit entries
// (whether plain strings or records), the function must return the
// seed unchanged rather than expanding to every model of every
// active provider (which would silently inject unapproved models).
assert.equal(result.length, 0, "populated models[] must short-circuit provider-wide expansion");
});
test("expandAutoComboCandidatePool does not duplicate an already-present modelStr", async () => {
await providersDb.createProviderConnection({
provider: "openai",
authType: "apikey",
name: "OpenAI",
apiKey: "sk-test-openai",
defaultModel: "gpt-4o-mini",
});
const firstCatalogId = providerModels.getProviderModels("openai")[0]?.id;
assert.ok(firstCatalogId, "expected at least one openai catalog model");
const existing = `openai/${firstCatalogId}`;
const seed = [
{
kind: "model" as const,
stepId: existing,
executionKey: existing,
modelStr: existing,
provider: "openai",
providerId: "openai",
connectionId: "conn-1",
weight: 5,
label: "pinned",
},
];
const result = await combo.expandAutoComboCandidatePool(seed, { config: {} });
const matches = result.filter((t) => t.modelStr === existing);
assert.equal(matches.length, 1, "the pre-existing target must not be duplicated");
// …and the original pinned entry (weight 5 / conn-1) is preserved, not overwritten.
assert.equal(matches[0].connectionId, "conn-1");
assert.equal(matches[0].weight, 5);
});
test("expandAutoComboCandidatePool excludes trigger-bypassed Microsoft Designer providers", async () => {
await core.ensureDbInitialized();
const db = core.getDbInstance();
db.exec("DROP TRIGGER IF EXISTS trg_retire_microsoft_designer_web_provider_insert");
db.exec("DROP TRIGGER IF EXISTS trg_retire_microsoft_designer_web_provider_update");
for (const provider of ["microsoft-designer-web", "msdesigner", "openai"]) {
await providersDb.createProviderConnection({
provider,
authType: "apikey",
name: `${provider}-trigger-bypass`,
apiKey: `sk-${provider}-test`,
defaultModel: provider === "openai" ? "gpt-4o-mini" : "dall-e-3",
isActive: true,
});
if (provider !== "openai") {
await modelsDb.addCustomModel(provider, "designer-chat-bypass", "Bypass model");
}
}
const expanded = await combo.expandAutoComboCandidatePool([], { config: {} });
assert.equal(
expanded.some((target) => ["microsoft-designer-web", "msdesigner"].includes(target.provider)),
false
);
assert.equal(
expanded.some((target) => target.provider === "openai"),
true,
"supported active providers must remain expandable"
);
});