Files
OmniRoute/tests/unit/cli-storage-key-bootstrap.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

85 lines
3.3 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
const BIN = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"..",
"..",
"bin",
"omniroute.mjs"
);
function runCli(dataDir: string): { code: number | null; stderr: string } {
const cleanEnv = { ...process.env };
delete cleanEnv.STORAGE_ENCRYPTION_KEY;
// Isolate from the development repo's .env so local runs match CI where the
// working tree has no .env at checkout time (gitignored). Without this,
// bin/omniroute.mjs picks up STORAGE_ENCRYPTION_KEY from the repo .env and
// the bootstrap skips writing DATA_DIR/.env (the behaviour the test exercises).
const isolatedHome = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-key-home-"));
try {
// Use a real (non-informational) command so the STORAGE_ENCRYPTION_KEY
// bootstrap runs. `--version`/`--help` are intentionally skipped now (#3129),
// so the #1622 provisioning path must be exercised by an actual command.
// `config list --json` is fast and offline (no server, no network).
const res = spawnSync("node", [BIN, "config", "list", "--json"], {
cwd: dataDir,
env: {
...cleanEnv,
DATA_DIR: dataDir,
HOME: isolatedHome,
NO_UPDATE_NOTIFIER: "1",
OMNIROUTE_CLI_SKIP_REPO_ENV: "1",
},
timeout: 60_000,
encoding: "utf-8",
});
return { code: res.status, stderr: res.stderr ?? "" };
} finally {
fs.rmSync(isolatedHome, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
}
// #1622 follow-up (reported by Daniel Nach; original persistence by @Chewji9875):
// the CLI must persist the key into DATA_DIR (not just ~/.omniroute) so Docker/custom-DATA_DIR
// users keep it across restarts, and must NEVER auto-generate a fresh key when a database
// already exists (a new key can't decrypt prior data → user locked out).
test("CLI generates STORAGE_ENCRYPTION_KEY into DATA_DIR on first run (#1622)", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-key-a-"));
try {
runCli(dir);
const envPath = path.join(dir, ".env");
assert.ok(fs.existsSync(envPath), "DATA_DIR/.env must be created");
const content = fs.readFileSync(envPath, "utf-8");
assert.match(
content,
/STORAGE_ENCRYPTION_KEY=[0-9a-f]{64}/,
"key persisted into DATA_DIR/.env"
);
} finally {
fs.rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("CLI refuses to auto-generate a key when a database already exists (#1622)", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-key-b-"));
try {
fs.writeFileSync(path.join(dir, "storage.sqlite"), "fake-db");
const { stderr } = runCli(dir);
const envPath = path.join(dir, ".env");
const hasKey =
fs.existsSync(envPath) &&
fs.readFileSync(envPath, "utf-8").includes("STORAGE_ENCRYPTION_KEY=");
assert.equal(hasKey, false, "must NOT generate a key when a DB already exists");
assert.match(stderr, /already exists/i, "must warn that a database already exists");
} finally {
fs.rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});