mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-21 14:22:14 +03:00
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.
306 lines
11 KiB
TypeScript
306 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";
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-managed-model-import-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const modelsDb = await import("../../src/lib/db/models.ts");
|
|
const localDb = await import("../../src/lib/localDb.ts");
|
|
const { importManagedModels } = await import("../../src/lib/providerModels/managedModelImport.ts");
|
|
const { mergeProviderModelListing } =
|
|
await import("../../src/lib/providers/mergeProviderModelListing.ts");
|
|
|
|
async 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(async () => {
|
|
await resetStorage();
|
|
});
|
|
|
|
test.after(() => {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
});
|
|
|
|
test("sync mode builds aliases from provider-level synced available models", async () => {
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", "conn-a", [
|
|
{ id: "shared/model-a", name: "Model A", source: "imported" },
|
|
]);
|
|
|
|
await importManagedModels({
|
|
providerId: "openrouter",
|
|
connectionId: "conn-b",
|
|
mode: "sync",
|
|
fetchedModels: [{ id: "shared/model-b", name: "Model B" }],
|
|
});
|
|
|
|
const aliases = await localDb.getModelAliases();
|
|
|
|
assert.equal(aliases["model-a"], "openrouter/shared/model-a");
|
|
assert.equal(aliases["model-b"], "openrouter/shared/model-b");
|
|
});
|
|
test("Crof managed import persists boolean reasoning effort metadata", async () => {
|
|
const result = await importManagedModels({
|
|
providerId: "crof",
|
|
connectionId: "conn-crof",
|
|
mode: "sync",
|
|
fetchedModels: [{ id: "crof-managed-model", reasoning_effort: true }],
|
|
});
|
|
|
|
const synced = await modelsDb.getSyncedAvailableModelsForConnection("crof", "conn-crof");
|
|
assert.deepEqual(synced[0]?.supportedThinkingEfforts, ["none", "low", "medium", "high", "max"]);
|
|
assert.equal(synced[0]?.supportsThinking, true);
|
|
|
|
assert.deepEqual(result.importedModels[0]?.supportedThinkingEfforts, [
|
|
"none",
|
|
"low",
|
|
"medium",
|
|
"high",
|
|
"max",
|
|
]);
|
|
assert.equal(result.importedModels[0]?.supportsThinking, true);
|
|
});
|
|
|
|
test("merge mode builds aliases from discovered models without pruning missing provider aliases", async () => {
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", "conn-a", [
|
|
{ id: "shared/model-a", name: "Model A", source: "imported" },
|
|
]);
|
|
await localDb.setModelAlias("existing", "openrouter/shared/existing");
|
|
|
|
await importManagedModels({
|
|
providerId: "openrouter",
|
|
connectionId: "conn-b",
|
|
mode: "merge",
|
|
fetchedModels: [{ id: "shared/model-b", name: "Model B" }],
|
|
});
|
|
|
|
const aliases = await localDb.getModelAliases();
|
|
|
|
assert.equal(aliases.existing, "openrouter/shared/existing");
|
|
assert.equal(aliases["model-a"], undefined);
|
|
assert.equal(aliases["model-b"], "openrouter/shared/model-b");
|
|
});
|
|
|
|
test("sync keeps a same-id manual model as the user-configurable metadata override", async () => {
|
|
await modelsDb.addCustomModel(
|
|
"openrouter",
|
|
"shared-model",
|
|
"Operator configuration",
|
|
"manual",
|
|
"responses",
|
|
["responses"],
|
|
"claude",
|
|
{},
|
|
true
|
|
);
|
|
|
|
await importManagedModels({
|
|
providerId: "openrouter",
|
|
connectionId: "openrouter-connection",
|
|
mode: "sync",
|
|
fetchedModels: [
|
|
{
|
|
id: "shared-model",
|
|
name: "Upstream name",
|
|
apiFormat: "chat-completions",
|
|
supportedEndpoints: ["chat"],
|
|
description: "Upstream description",
|
|
},
|
|
],
|
|
});
|
|
|
|
const customModels = (await modelsDb.getCustomModels("openrouter")) as Array<{
|
|
id: string;
|
|
apiFormat?: string;
|
|
targetFormat?: string;
|
|
supportedEndpoints?: string[];
|
|
supportsVision?: boolean;
|
|
}>;
|
|
assert.deepEqual(customModels, [
|
|
{
|
|
id: "shared-model",
|
|
name: "Operator configuration",
|
|
source: "manual",
|
|
apiFormat: "responses",
|
|
supportedEndpoints: ["responses"],
|
|
targetFormat: "claude",
|
|
supportsVision: true,
|
|
},
|
|
]);
|
|
|
|
const syncedModels = await modelsDb.getSyncedAvailableModels("openrouter");
|
|
const effectiveModel = mergeProviderModelListing({
|
|
providerId: "openrouter",
|
|
registryModels: [],
|
|
syncedModels,
|
|
customModels,
|
|
}).find((model) => model.id === "shared-model");
|
|
assert.equal(effectiveModel?.apiFormat, "responses");
|
|
assert.deepEqual(effectiveModel?.supportedEndpoints, ["responses"]);
|
|
assert.equal(effectiveModel?.targetFormat, "claude");
|
|
assert.equal(effectiveModel?.supportsVision, true);
|
|
assert.equal(effectiveModel?.description, "Upstream description");
|
|
|
|
assert.equal(await modelsDb.removeCustomModel("openrouter", "shared-model"), true);
|
|
const resetModel = mergeProviderModelListing({
|
|
providerId: "openrouter",
|
|
registryModels: [],
|
|
syncedModels,
|
|
customModels: await modelsDb.getCustomModels("openrouter"),
|
|
}).find((model) => model.id === "shared-model");
|
|
assert.equal(resetModel?.apiFormat, "chat-completions");
|
|
assert.deepEqual(resetModel?.supportedEndpoints, ["chat"]);
|
|
assert.equal(resetModel?.description, "Upstream description");
|
|
});
|
|
|
|
test("provider-level synced model deletion removes only that provider", async () => {
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", "conn-a", [
|
|
{ id: "shared/model-a", name: "Model A", source: "imported" },
|
|
]);
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", "conn-b", [
|
|
{ id: "shared/model-b", name: "Model B", source: "imported" },
|
|
]);
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection("openai", "conn-a", [
|
|
{ id: "shared/model-c", name: "Model C", source: "imported" },
|
|
]);
|
|
|
|
const removed = await modelsDb.deleteSyncedAvailableModelsForProvider("openrouter");
|
|
|
|
assert.equal(removed, 2);
|
|
assert.deepEqual(await modelsDb.getSyncedAvailableModels("openrouter"), []);
|
|
assert.deepEqual(await modelsDb.getSyncedAvailableModels("openai"), [
|
|
{ id: "shared/model-c", name: "Model C", source: "imported" },
|
|
]);
|
|
});
|
|
|
|
test("OpenAI import excludes deprecated and shutdown models from new selections", async () => {
|
|
const result = await importManagedModels({
|
|
providerId: "openai",
|
|
connectionId: "openai-conn",
|
|
mode: "sync",
|
|
fetchedModels: [
|
|
{ id: "gpt-5.6-sol", name: "GPT-5.6 Sol" },
|
|
{ id: "gpt-5.2-codex", name: "GPT-5.2 Codex" },
|
|
{ id: "gpt-5.3-chat-latest", name: "GPT-5.3 Chat" },
|
|
],
|
|
});
|
|
|
|
assert.deepEqual(
|
|
result.discoveredModels.map((model) => model.id),
|
|
["gpt-5.6-sol"]
|
|
);
|
|
assert.deepEqual(
|
|
(await modelsDb.getSyncedAvailableModels("openai")).map((model) => model.id),
|
|
["gpt-5.6-sol"]
|
|
);
|
|
});
|
|
|
|
test("OpenAI import excludes image and video generation models from chat selections", async () => {
|
|
const result = await importManagedModels({
|
|
providerId: "openai",
|
|
connectionId: "openai-media-conn",
|
|
mode: "sync",
|
|
fetchedModels: [
|
|
{ id: "gpt-5.6-sol", name: "GPT-5.6 Sol" },
|
|
{ id: "gpt-image-2", name: "GPT Image 2" },
|
|
{ id: "sora-2-pro", name: "Sora 2 Pro" },
|
|
{
|
|
id: "vendor-image-model",
|
|
name: "Vendor Image Model",
|
|
supportedEndpoints: ["/v1/images/generations"],
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.deepEqual(
|
|
result.discoveredModels.map((model) => model.id),
|
|
["gpt-5.6-sol"]
|
|
);
|
|
assert.deepEqual(
|
|
(await modelsDb.getSyncedAvailableModels("openai")).map((model) => model.id),
|
|
["gpt-5.6-sol"]
|
|
);
|
|
});
|
|
|
|
test("pruning stale connection available models during import", async () => {
|
|
const db = core.getDbInstance();
|
|
// Insert connections
|
|
db.prepare(
|
|
"INSERT INTO provider_connections (id, provider, auth_type, name, is_active, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
|
|
).run("conn-active", "openrouter", "apikey", "Active Connection", 1, "2026-05-29", "2026-05-29");
|
|
|
|
db.prepare(
|
|
"INSERT INTO provider_connections (id, provider, auth_type, name, is_active, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
|
|
).run("conn-stale", "openrouter", "apikey", "Stale Connection", 0, "2026-05-29", "2026-05-29");
|
|
|
|
// Create synced available models for both
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", "conn-active", [
|
|
{ id: "shared/model-active", name: "Model Active", source: "imported" },
|
|
]);
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection("openrouter", "conn-stale", [
|
|
{ id: "shared/model-stale", name: "Model Stale", source: "imported" },
|
|
]);
|
|
|
|
// Import on conn-new
|
|
await importManagedModels({
|
|
providerId: "openrouter",
|
|
connectionId: "conn-new",
|
|
mode: "sync",
|
|
fetchedModels: [{ id: "shared/model-new", name: "Model New" }],
|
|
});
|
|
|
|
// Check models for "openrouter"
|
|
const allSyncedModels = await modelsDb.getSyncedAvailableModels("openrouter");
|
|
|
|
// Stale connection should be pruned. Active connection and the new syncing connection should be kept.
|
|
const ids = allSyncedModels.map((m) => m.id);
|
|
assert.ok(ids.includes("shared/model-active"));
|
|
assert.ok(ids.includes("shared/model-new"));
|
|
assert.ok(!ids.includes("shared/model-stale"));
|
|
});
|
|
|
|
test("antigravity sync dynamically builds and saves mitmAlias mappings", async () => {
|
|
const db = core.getDbInstance();
|
|
// Create an antigravity connection
|
|
db.prepare(
|
|
"INSERT INTO provider_connections (id, provider, auth_type, name, is_active, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
|
|
).run("antigravity-conn", "antigravity", "oauth", "Antigravity", 1, "2026-05-29", "2026-05-29");
|
|
|
|
await importManagedModels({
|
|
providerId: "antigravity",
|
|
connectionId: "antigravity-conn",
|
|
mode: "sync",
|
|
fetchedModels: [
|
|
{ id: "gemini-3.5-flash", name: "Gemini 3.5 Flash" },
|
|
{ id: "gemini-3.7-flash-high", name: "Gemini 3.7 Flash High" },
|
|
{ id: "custom-antigravity-model", name: "Custom Antigravity Model" },
|
|
],
|
|
});
|
|
|
|
const models = await modelsDb.getSyncedAvailableModels("antigravity");
|
|
console.log("SYNCED MODELS IN TEST:", models);
|
|
|
|
const mitmMappings = await modelsDb.getMitmAlias("antigravity");
|
|
console.log("MITM MAPPINGS IN TEST:", mitmMappings);
|
|
|
|
// Retired models reported by upstream must not be imported or mapped.
|
|
assert.equal(mitmMappings["gemini-3.5-flash"], undefined);
|
|
assert.equal(
|
|
models.some((model) => model.id === "gemini-3.5-flash"),
|
|
false
|
|
);
|
|
assert.equal(mitmMappings["gemini-3.7-flash-high"], "antigravity/gemini-3.7-flash-high");
|
|
assert.equal(mitmMappings["custom-antigravity-model"], "antigravity/custom-antigravity-model");
|
|
|
|
// Removed Antigravity 2.0 preview/agent aliases must not be reintroduced.
|
|
assert.equal(mitmMappings["gemini-3.5-flash-preview"], undefined);
|
|
assert.equal(mitmMappings["gemini-3-flash-agent"], undefined);
|
|
});
|