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

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 });
}
});