Files
OmniRoute/tests/unit/json-migration-combos.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

175 lines
5.4 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-json-migration-"));
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 { runJsonMigration } = await import("../../src/lib/db/jsonMigration.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const usageHistory = await import("../../src/lib/usage/usageHistory.ts");
const exportRoute = await import("../../src/app/api/settings/export-json/route.ts");
test.beforeEach(() => {
const db = core.getDbInstance();
db.prepare("DELETE FROM usage_history").run();
db.prepare("DELETE FROM provider_connections").run();
db.prepare("DELETE FROM combos").run();
});
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("runJsonMigration preserves exported snapshots and camelCase connection attribution", () => {
const db = core.getDbInstance();
runJsonMigration(db, {
providerConnections: [
{
id: "codex-connection",
provider: "codex",
authType: "oauth",
email: "current@example.com",
providerSpecificData: { workspaceId: "team", chatgptUserId: "user-a" },
},
],
usageHistory: [
{
id: 1,
provider: "codex",
model: "gpt-5.5",
connectionId: "codex-connection",
accountKey: "stable-exported-account",
accountLabel: "Historical label",
accountLabelPriority: 4,
tokens_input: 10,
tokens_output: 5,
timestamp: "2026-01-01T00:00:00.000Z",
},
],
});
const row = db
.prepare(
`SELECT connection_id, account_key, account_label, account_label_priority,
tokens_input + tokens_output total_tokens
FROM usage_history WHERE id = 1`
)
.get();
assert.deepEqual(row, {
connection_id: "codex-connection",
account_key: "stable-exported-account",
account_label: "Historical label",
account_label_priority: 4,
total_tokens: 15,
});
});
test("usage snapshots survive an export, connection deletion, and import round trip", async () => {
const db = core.getDbInstance();
const connection = await providersDb.createProviderConnection({
provider: "codex",
authType: "oauth",
email: "member@example.com",
displayName: "Production Codex",
providerSpecificData: { workspaceId: "team", chatgptUserId: "user-a" },
});
await usageHistory.saveRequestUsage({
provider: "codex",
model: "gpt-5.5",
connectionId: connection.id as string,
tokens: { input: 10, output: 5 },
timestamp: "2026-02-01T00:00:00.000Z",
});
const response = await exportRoute.GET(
new Request("http://localhost/api/settings/export-json?includeHistory=true")
);
assert.equal(response.status, 200);
const exported = await response.json();
await providersDb.deleteProviderConnection(connection.id as string);
db.prepare("DELETE FROM usage_history").run();
db.prepare("DELETE FROM provider_connections").run();
runJsonMigration(db, exported);
const restored = db
.prepare(
`SELECT COUNT(*) requests, SUM(tokens_input + tokens_output) tokens,
MAX(account_label) label
FROM usage_history WHERE connection_id = ?`
)
.get(connection.id);
assert.deepEqual(restored, { requests: 1, tokens: 15, label: "Production Codex" });
});
test("runJsonMigration normalizes legacy combo strategy names at the import boundary", () => {
const db = core.getDbInstance();
runJsonMigration(db, {
combos: [
{
id: "combo-usage",
name: "combo-usage",
strategy: "usage",
models: ["openai/gpt-4o-mini"],
config: { strategy: "context" },
},
{
id: "combo-unknown",
name: "combo-unknown",
strategy: "not-a-real-strategy",
models: ["openai/gpt-4o-mini"],
},
],
});
const rows = db.prepare("SELECT id, data FROM combos ORDER BY id ASC").all() as Array<{
id: string;
data: string;
}>;
const byId = new Map(rows.map((row) => [row.id, JSON.parse(row.data)]));
assert.equal(byId.get("combo-usage").strategy, "least-used");
assert.equal(byId.get("combo-usage").config.strategy, "context-optimized");
assert.equal(byId.get("combo-unknown").strategy, "priority");
});
test("runJsonMigration rejects invalid combo invariants atomically", () => {
const db = core.getDbInstance();
assert.throws(
() =>
runJsonMigration(db, {
settings: { importedBeforeFailure: true },
combos: [
{
id: "invalid-import-combo",
name: "invalid-import-combo",
allowedProviders: ["github"],
allowedModelFamilies: ["gpt"],
models: [{ provider: "zai", model: "zai/glm-5" }],
},
],
}),
/target 1 \(zai\/glm-5\) violates its invariant/
);
assert.equal(db.prepare("SELECT COUNT(*) count FROM combos").get().count, 0);
assert.equal(
db
.prepare("SELECT value FROM key_value WHERE namespace = ? AND key = ?")
.get("settings", "importedBeforeFailure"),
undefined
);
});