Files
OmniRoute/tests/unit/cli-runtime-known-path-shortcircuit-7774.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

68 lines
2.8 KiB
TypeScript

import { describe, it, before, after } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
// HOME must be overridden BEFORE importing cliRuntime.ts — the module computes
// EXPECTED_PARENT_PATHS (the known-path realpath containment check) once at
// import time from os.homedir().
const fakeHome = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-7774-home-"));
const realBinDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-7774-realbin-"));
const savedEnv: Record<string, string | undefined> = {
HOME: process.env.HOME,
USERPROFILE: process.env.USERPROFILE,
PATH: process.env.PATH,
CLI_CLAUDE_BIN: process.env.CLI_CLAUDE_BIN,
CLI_EXTRA_PATHS: process.env.CLI_EXTRA_PATHS,
npm_config_prefix: process.env.npm_config_prefix,
};
process.env.HOME = fakeHome;
process.env.USERPROFILE = fakeHome;
delete process.env.CLI_CLAUDE_BIN;
delete process.env.CLI_EXTRA_PATHS;
process.env.npm_config_prefix = path.join(fakeHome, "npm-prefix-unused");
const { getCliRuntimeStatus, getKnownToolPaths } = await import(
"../../src/shared/services/cliRuntime.ts"
);
function makeExecutable(filePath: string, content: string) {
fs.writeFileSync(filePath, content);
if (process.platform !== "win32") fs.chmodSync(filePath, 0o755);
}
describe("#7774 — known-path short-circuit hides a genuinely runnable Claude binary", () => {
before(() => {
const poisonedCandidate = path.join(fakeHome, ".local", "bin", "claude");
fs.mkdirSync(poisonedCandidate, { recursive: true });
const known = getKnownToolPaths("claude");
assert.ok(known.includes(poisonedCandidate));
const realClaude = path.join(realBinDir, "claude");
makeExecutable(realClaude, "#!/bin/sh\necho '2.1.215 (Claude Code)'\n");
// Prepend realBinDir rather than replacing PATH outright — locateCommand()
// spawns `sh`/`where.exe` itself using this same PATH, so the standard
// system bin dirs (containing `sh`) must stay resolvable too.
process.env.PATH = [realBinDir, savedEnv.PATH].filter(Boolean).join(path.delimiter);
});
after(() => {
for (const [key, value] of Object.entries(savedEnv)) {
if (value === undefined) delete (process.env as Record<string, string | undefined>)[key];
else process.env[key] = value;
}
fs.rmSync(fakeHome, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.rmSync(realBinDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
it("should still find and report Claude as installed+runnable via PATH fallback", async () => {
const result = await getCliRuntimeStatus("claude");
assert.equal(result.installed, true, `expected installed=true, got reason=${result.reason}`);
assert.equal(result.runnable, true, `expected runnable=true, got reason=${result.reason}`);
});
});