Files
OmniRoute/tests/unit/codex-connection-defaults.test.ts
Diego Rodrigues de Sa e Souza 93265eede3 test(infra): retry recursive temp-dir removal on main (main twin of #11968) (#12246)
* test(infra): retry recursive temp-dir removal on main (main twin of #11968)

`main` has been red since b342c1a361 on the vitest and integration gates:

  ✖ tests/unit/autoCombo/provider-family-combos.test.ts > auto/<family>
  ✖ chat pipeline applies Codex OAuth fingerprint and priority tier inside combos

Both call resetStorage() from beforeEach, which does an fs.rmSync(TEST_DATA_DIR,
{recursive: true, force: true}) with no retry, and intermittently loses the race
with a not-yet-released SQLite handle (ENOTEMPTY).

release/v3.8.51 fixed this in #11968 with a mechanical codemod adding
maxRetries/retryDelay to every recursive rm/rmSync/rmdirSync under tests/, but
that PR landed only on the release branch. Because main only receives work at
the release squash, it stayed broken for the whole cycle — and repo-wide gates
then turn every open PR into main red on checks unrelated to their diff.

This is the --base main twin: re-runs the same codemod that already shipped on
the release branch (scripts/ad-hoc/codemod-rm-maxretries.mjs), so the two
branches converge on identical test-teardown semantics. Test-only; no product
logic is touched.

The remaining three failures reported on #12133 (unit full suite exceeding its
4800s ceiling, package-artifact exceeding 1200s, and the boot-smoke that is
skipped as a consequence) are runner-contention timeouts, not code defects —
validate-release-green.mjs runs those heavy gates concurrently on one shared
hosted runner. There is no fix to port for those.

* chore(scripts): carry the rm-maxretries codemod onto main alongside its output

The codemod that generated the previous commit lives in the repo on
release/v3.8.51 (added by #11968) but was never on main. Bringing it over keeps
the tool next to the change it produced, so the transformation stays
reproducible and auditable from either branch.
2026-09-01 01:48:00 -03:00

159 lines
5.7 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-codex-defaults-"));
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 settingsDb = await import("../../src/lib/db/settings.ts");
const { migrateCodexConnectionDefaultsFromLegacySettings } =
await import("../../src/lib/providers/codexConnectionDefaults.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(async () => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("migration backfills Codex request defaults, preserves existing providerSpecificData, and is idempotent", async () => {
const first = await providersDb.createProviderConnection({
provider: "codex",
authType: "oauth",
email: "first@example.com",
providerSpecificData: {
workspaceId: "ws-first",
tag: "team-a",
codexLimitPolicy: { use5h: false, useWeekly: true },
},
});
const second = await providersDb.createProviderConnection({
provider: "codex",
authType: "oauth",
email: "second@example.com",
providerSpecificData: {
workspaceId: "ws-second",
tag: "team-b",
requestDefaults: { reasoningEffort: "high" },
},
});
const untouched = await providersDb.createProviderConnection({
provider: "codex",
authType: "oauth",
email: "third@example.com",
providerSpecificData: {
workspaceId: "ws-third",
tag: "team-c",
requestDefaults: { reasoningEffort: "low", serviceTier: "priority" },
},
});
await settingsDb.updateSettings({ codexServiceTier: { enabled: true } });
const firstRun = await migrateCodexConnectionDefaultsFromLegacySettings();
const rows = await providersDb.getProviderConnections({ provider: "codex" });
const byId = new Map(rows.map((row) => [row.id, row]));
const settings = await settingsDb.getSettings();
assert.equal(firstRun.migrated, true);
assert.deepEqual(firstRun.updatedConnectionIds.sort(), [first.id, second.id].sort());
assert.deepEqual((byId.get(first.id).providerSpecificData as any).requestDefaults, {
reasoningEffort: "medium",
serviceTier: "priority",
});
assert.deepEqual((byId.get(second.id) as any).providerSpecificData.requestDefaults, {
reasoningEffort: "high",
serviceTier: "priority",
});
assert.deepEqual((byId.get(untouched.id) as any).providerSpecificData.requestDefaults, {
reasoningEffort: "low",
serviceTier: "priority",
});
assert.equal((byId.get((first as any).id).providerSpecificData as any).tag, "team-a");
assert.deepEqual((byId as any).get(first.id).providerSpecificData.codexLimitPolicy, {
use5h: false,
useWeekly: true,
});
assert.ok(settings.codexConnectionDefaultsMigrationV1);
const secondRun = await migrateCodexConnectionDefaultsFromLegacySettings();
assert.equal(secondRun.migrated, false);
assert.deepEqual(secondRun.updatedConnectionIds, []);
});
test("provider connection persistence normalizes request defaults without dropping unrelated keys", async () => {
const created = await providersDb.createProviderConnection({
provider: "codex",
authType: "oauth",
email: "normalize@example.com",
providerSpecificData: {
openaiStoreEnabled: true,
workspaceId: "ws-normalize",
tag: "team-z",
requestDefaults: {
reasoningEffort: "HIGH",
serviceTier: "fast",
customFlag: "keep-me",
},
},
});
(assert as any).deepEqual((created.providerSpecificData as any).requestDefaults, {
reasoningEffort: "high",
serviceTier: "priority",
customFlag: "keep-me",
});
assert.equal((created.providerSpecificData as any).openaiStoreEnabled, true);
assert.equal((created.providerSpecificData as any).workspaceId, "ws-normalize");
assert.equal((created.providerSpecificData as any).tag, "team-z");
const updated = await providersDb.updateProviderConnection((created as any).id, {
providerSpecificData: {
...created.providerSpecificData,
requestDefaults: { reasoningEffort: "medium" },
},
});
assert.deepEqual((updated.providerSpecificData as any).requestDefaults, {
reasoningEffort: "medium",
});
assert.equal((updated.providerSpecificData as any).openaiStoreEnabled, true);
assert.equal((updated.providerSpecificData as any).workspaceId, "ws-normalize");
assert.equal((updated.providerSpecificData as any).tag, "team-z");
});
test("migration does not treat explicit default global tier as legacy fast", async () => {
const created = await providersDb.createProviderConnection({
provider: "codex",
authType: "oauth",
email: "default-tier@example.com",
providerSpecificData: { workspaceId: "ws-default" },
});
await settingsDb.updateSettings({ codexServiceTier: { enabled: true, tier: "default" } });
const firstRun = await migrateCodexConnectionDefaultsFromLegacySettings();
const rows = await providersDb.getProviderConnections({ provider: "codex" });
const byId = new Map(rows.map((row) => [row.id, row]));
assert.equal(firstRun.legacyFastEnabled, false);
const providerSpecificData = byId.get(created.id)?.providerSpecificData as
| { requestDefaults?: unknown }
| undefined;
assert.deepEqual(providerSpecificData?.requestDefaults, {
reasoningEffort: "medium",
});
});