Files
OmniRoute/tests/unit/sync-env.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

188 lines
7.0 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 { syncEnv } = (await import("../../scripts/dev/sync-env.mjs")) as {
syncEnv: (opts?: { rootDir?: string; quiet?: boolean; scope?: string }) => {
created: boolean;
added: number;
};
};
function createTempRoot(): string {
return fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-sync-env-"));
}
function writeEnvExample(rootDir: string) {
fs.writeFileSync(
path.join(rootDir, ".env.example"),
[
"JWT_SECRET=",
"API_KEY_SECRET=",
"STORAGE_ENCRYPTION_KEY=",
"MACHINE_ID_SALT=",
"CLAUDE_OAUTH_CLIENT_ID=claude-default",
"CODEX_OAUTH_CLIENT_ID=codex-default",
'CLAUDE_USER_AGENT="claude-cli/2.1.219 (external, cli)"',
"# COMMENTED_KEY=skip-me",
"",
].join("\n"),
"utf8"
);
}
function writeOauthEnvExample(rootDir: string) {
fs.writeFileSync(
path.join(rootDir, ".env.example"),
[
"# ═══════════════════════════════════════════════════",
"# OAUTH PROVIDER CREDENTIALS",
"# ═══════════════════════════════════════════════════",
"CLAUDE_OAUTH_CLIENT_ID=claude-default",
"CODEX_OAUTH_CLIENT_ID=codex-default",
"# ─────────────────────────────────────────────────────────────────────────────",
"# Provider User-Agent Overrides (optional — customize per-provider UA headers)",
"# ─────────────────────────────────────────────────────────────────────────────",
"JWT_SECRET=should-not-be-copied",
"",
].join("\n"),
"utf8"
);
}
test("syncEnv creates .env from .env.example and generates install-time secrets", () => {
const rootDir = createTempRoot();
// Temporarily override DATA_DIR so the encrypted-credentials guard doesn't
// find the user's real DB at ~/.omniroute/ during tests
const origDataDir = process.env.DATA_DIR;
try {
writeEnvExample(rootDir);
process.env.DATA_DIR = rootDir;
const result = syncEnv({ rootDir, quiet: true });
const envContent = fs.readFileSync(path.join(rootDir, ".env"), "utf8");
assert.deepEqual(result, { created: true, added: 7 });
assert.match(envContent, /^JWT_SECRET=.{32,}$/m);
assert.match(envContent, /^API_KEY_SECRET=.{32,}$/m);
assert.match(envContent, /^STORAGE_ENCRYPTION_KEY=$/m);
assert.match(envContent, /^MACHINE_ID_SALT=omniroute-/m);
assert.match(envContent, /^CLAUDE_OAUTH_CLIENT_ID=claude-default$/m);
assert.match(envContent, /^CODEX_OAUTH_CLIENT_ID=codex-default$/m);
assert.match(envContent, /^CLAUDE_USER_AGENT="claude-cli\/2\.1\.219 \(external, cli\)"$/m);
assert.doesNotMatch(envContent, /^COMMENTED_KEY=/m);
} finally {
process.env.DATA_DIR = origDataDir;
fs.rmSync(rootDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("syncEnv appends only missing keys and preserves existing values", () => {
const rootDir = createTempRoot();
const origDataDir = process.env.DATA_DIR;
try {
writeEnvExample(rootDir);
fs.writeFileSync(
path.join(rootDir, ".env"),
[
"JWT_SECRET=my-custom-secret-that-should-stay",
"CLAUDE_OAUTH_CLIENT_ID=custom-claude",
"",
].join("\n"),
"utf8"
);
process.env.DATA_DIR = rootDir;
const result = syncEnv({ rootDir, quiet: true });
const envContent = fs.readFileSync(path.join(rootDir, ".env"), "utf8");
assert.deepEqual(result, { created: false, added: 5 });
assert.match(envContent, /^JWT_SECRET=my-custom-secret-that-should-stay$/m);
assert.match(envContent, /^CLAUDE_OAUTH_CLIENT_ID=custom-claude$/m);
assert.match(envContent, /^API_KEY_SECRET=.{32,}$/m);
assert.match(envContent, /^STORAGE_ENCRYPTION_KEY=$/m);
assert.match(envContent, /^MACHINE_ID_SALT=omniroute-/m);
assert.match(envContent, /^CODEX_OAUTH_CLIENT_ID=codex-default$/m);
assert.match(envContent, /^CLAUDE_USER_AGENT=claude-cli\/2\.1\.219 \(external, cli\)$/m);
assert.match(envContent, /Auto-added by sync-env/);
} finally {
process.env.DATA_DIR = origDataDir;
fs.rmSync(rootDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("syncEnv treats quoted and unquoted values as equivalent", () => {
const rootDir = createTempRoot();
const origDataDir = process.env.DATA_DIR;
try {
writeEnvExample(rootDir);
fs.writeFileSync(
path.join(rootDir, ".env"),
[
"JWT_SECRET=jwt-secret",
"API_KEY_SECRET=api-secret",
"STORAGE_ENCRYPTION_KEY=storage-secret",
"MACHINE_ID_SALT=machine-salt",
"CLAUDE_OAUTH_CLIENT_ID=claude-default",
"CODEX_OAUTH_CLIENT_ID=codex-default",
'CLAUDE_USER_AGENT="claude-cli/2.1.219 (external, cli)"',
"",
].join("\n"),
"utf8"
);
process.env.DATA_DIR = rootDir;
const result = syncEnv({ rootDir, quiet: true });
assert.deepEqual(result, { created: false, added: 0 });
} finally {
process.env.DATA_DIR = origDataDir;
fs.rmSync(rootDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("syncEnv is idempotent when .env is already complete", () => {
const rootDir = createTempRoot();
const origDataDir = process.env.DATA_DIR;
try {
writeEnvExample(rootDir);
process.env.DATA_DIR = rootDir;
syncEnv({ rootDir, quiet: true });
const before = fs.readFileSync(path.join(rootDir, ".env"), "utf8");
const result = syncEnv({ rootDir, quiet: true });
const after = fs.readFileSync(path.join(rootDir, ".env"), "utf8");
assert.deepEqual(result, { created: false, added: 0 });
assert.equal(after, before);
} finally {
process.env.DATA_DIR = origDataDir;
fs.rmSync(rootDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("syncEnv oauth scope only copies oauth defaults", () => {
const rootDir = createTempRoot();
try {
writeOauthEnvExample(rootDir);
const result = syncEnv({ rootDir, quiet: true, scope: "oauth" });
const envContent = fs.readFileSync(path.join(rootDir, ".env"), "utf8");
assert.deepEqual(result, { created: true, added: 2 });
assert.match(envContent, /^CLAUDE_OAUTH_CLIENT_ID=claude-default$/m);
assert.match(envContent, /^CODEX_OAUTH_CLIENT_ID=codex-default$/m);
assert.doesNotMatch(envContent, /^JWT_SECRET=/m);
assert.doesNotMatch(envContent, /^Provider User-Agent Overrides/m);
} finally {
fs.rmSync(rootDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});