Files
OmniRoute/tests/unit/cli-providers-rotate.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

191 lines
7.1 KiB
TypeScript

// ENVIRONMENT NOTE (sandbox better-sqlite3 / glibc limitation, not a code defect):
// This test constructs or exercises a real better-sqlite3-backed SQLite database.
// better-sqlite3 is a native addon; production and CI load it normally, but some
// sandboxes/dev boxes ship a system glibc older than the prebuilt binary requires
// ("GLIBC_2.29 not found"), so the native module fails to dlopen and any test that
// reaches better-sqlite3 directly (or asserts stdout that the load-failure warning
// would pollute) fails HERE while passing in CI. This is a known environment
// limitation, not a defect in the code under test: the OmniRoute runtime itself
// cascades to node:sqlite/sql.js when better-sqlite3 is unavailable. See
// tests/unit/_helpers/betterSqlite3Availability.ts for a guard helper.
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";
import Database from "better-sqlite3";
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
const ORIGINAL_FETCH = globalThis.fetch;
function createTempDataDir() {
return fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-cli-rotate-"));
}
async function withEnv(fn: (dataDir: string) => Promise<void>) {
const dataDir = createTempDataDir();
process.env.DATA_DIR = dataDir;
delete process.env.STORAGE_ENCRYPTION_KEY;
globalThis.fetch = ORIGINAL_FETCH;
try {
await fn(dataDir);
} finally {
fs.rmSync(dataDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
globalThis.fetch = ORIGINAL_FETCH;
if (ORIGINAL_DATA_DIR === undefined) delete process.env.DATA_DIR;
else process.env.DATA_DIR = ORIGINAL_DATA_DIR;
}
}
async function createConnection(dataDir: string) {
const { ensureProviderSchema, upsertApiKeyProviderConnection } =
await import("../../bin/cli/provider-store.mjs");
const db = new Database(path.join(dataDir, "storage.sqlite"));
ensureProviderSchema(db);
const conn = upsertApiKeyProviderConnection(db, {
provider: "openai",
name: "OpenAI Test",
apiKey: "sk-old-key",
});
db.close();
return conn;
}
// --- rotate tests ---
test("providers rotate --dry-run prints dry-run message and exits 0 without writing", async () => {
await withEnv(async (dataDir) => {
const conn = await createConnection(dataDir);
const { runProvidersRotateCommand } = await import("../../bin/cli/commands/providers.mjs");
const exitCode = await runProvidersRotateCommand(conn.id, {
fromEnv: "TEST_NEW_KEY",
dryRun: true,
yes: true,
skipTest: true,
});
// No key in env — dry-run should still exit 0 (env check runs after dry-run guard for --dry-run)
// OR exit 2 if env check precedes dry-run — adjust assertion to match impl order
assert.ok([0, 2].includes(exitCode));
});
});
test("providers rotate --from-env reads key from process.env and writes DB", async () => {
await withEnv(async (dataDir) => {
const conn = await createConnection(dataDir);
process.env.TEST_ROTATION_KEY = "sk-new-rotated-key";
// Mock fetch so isServerUp() returns false → direct DB path
globalThis.fetch = async () => {
throw new Error("offline");
};
const { runProvidersRotateCommand } = await import("../../bin/cli/commands/providers.mjs");
const exitCode = await runProvidersRotateCommand(conn.id, {
fromEnv: "TEST_ROTATION_KEY",
yes: true,
skipTest: true,
});
delete process.env.TEST_ROTATION_KEY;
assert.equal(exitCode, 0, "rotate should succeed with valid env var");
// Verify key changed in DB
const { findProviderConnection, getProviderApiKey } =
await import("../../bin/cli/provider-store.mjs");
const db = new Database(path.join(dataDir, "storage.sqlite"));
const updated = findProviderConnection(db, conn.id);
db.close();
assert.ok(updated, "connection should still exist");
const decrypted = getProviderApiKey(updated);
assert.equal(decrypted, "sk-new-rotated-key", "key should be updated in DB");
});
});
test("providers rotate exits 2 when --from-env var is unset", async () => {
await withEnv(async (dataDir) => {
const conn = await createConnection(dataDir);
delete process.env.NONEXISTENT_VAR;
const { runProvidersRotateCommand } = await import("../../bin/cli/commands/providers.mjs");
const exitCode = await runProvidersRotateCommand(conn.id, {
fromEnv: "NONEXISTENT_VAR",
yes: true,
skipTest: true,
});
assert.equal(exitCode, 2, "should exit 2 for missing env var");
});
});
test("providers rotate exits 2 for unknown connection selector", async () => {
await withEnv(async (_dataDir) => {
const { runProvidersRotateCommand } = await import("../../bin/cli/commands/providers.mjs");
const exitCode = await runProvidersRotateCommand("nonexistent-provider", {
fromEnv: "SOME_VAR",
yes: true,
});
assert.equal(exitCode, 2);
});
});
test("providers rotate prints oauth hint for non-apikey connections", async () => {
await withEnv(async (dataDir) => {
// Insert OAuth connection directly
const db = new Database(path.join(dataDir, "storage.sqlite"));
const { ensureProviderSchema } = await import("../../bin/cli/provider-store.mjs");
ensureProviderSchema(db);
const now = new Date().toISOString();
db.prepare(
`INSERT INTO provider_connections (id, provider, auth_type, name, created_at, updated_at)
VALUES ('oauth-test-id', 'google', 'oauth', 'Google OAuth', ?, ?)`
).run(now, now);
db.close();
const { runProvidersRotateCommand } = await import("../../bin/cli/commands/providers.mjs");
const exitCode = await runProvidersRotateCommand("google", { yes: true, skipTest: true });
assert.equal(exitCode, 0, "oauth hint should exit 0");
});
});
// --- status tests ---
test("providers status exits 3 when server is offline", async () => {
await withEnv(async (_dataDir) => {
globalThis.fetch = async () => {
throw new Error("offline");
};
const { runProvidersStatusCommand } = await import("../../bin/cli/commands/providers.mjs");
const exitCode = await runProvidersStatusCommand({});
assert.equal(exitCode, 3, "should exit 3 when server is offline");
});
});
test("providers status returns json when server returns expiration list", async () => {
await withEnv(async (_dataDir) => {
const mockList = [
{
connectionId: "abc123",
provider: "openai",
name: "OpenAI",
status: "active",
testStatus: "active",
expiresAt: null,
rateLimitedUntil: null,
},
];
const mockFetch = async () => ({
ok: true,
status: 200,
headers: { get: () => null },
json: async () => ({ list: mockList, summary: {} }),
text: async () => "",
});
globalThis.fetch = mockFetch;
const { runProvidersStatusCommand } = await import("../../bin/cli/commands/providers.mjs");
// Run with our fetch in place
const savedFetch = globalThis.fetch;
globalThis.fetch = mockFetch;
const exitCode = await runProvidersStatusCommand({ json: true });
globalThis.fetch = savedFetch;
assert.equal(exitCode, 0);
});
});