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

135 lines
5.8 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync, writeFileSync, mkdirSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
let tmpDir: string;
let origDataDir: string | undefined;
test.before(() => {
tmpDir = mkdtempSync(join(tmpdir(), "omniroute-runtime-test-"));
origDataDir = process.env.DATA_DIR;
process.env.DATA_DIR = tmpDir;
});
test.after(() => {
if (origDataDir === undefined) delete process.env.DATA_DIR;
else process.env.DATA_DIR = origDataDir;
try {
rmSync(tmpDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
} catch {}
});
test("nativeDeps.mjs pode ser importado sem erro", async () => {
const mod = await import("../../bin/cli/runtime/nativeDeps.mjs");
assert.equal(typeof mod.ensureRuntimeDir, "function");
assert.equal(typeof mod.getRuntimeNodeModules, "function");
assert.equal(typeof mod.hasModule, "function");
assert.equal(typeof mod.isBetterSqliteBinaryValid, "function");
assert.equal(typeof mod.npmInstallRuntime, "function");
assert.equal(typeof mod.ensureBetterSqliteRuntime, "function");
assert.equal(typeof mod.buildEnvWithRuntime, "function");
});
test("ensureRuntimeDir cria diretório e package.json", async () => {
const { ensureRuntimeDir } = await import("../../bin/cli/runtime/nativeDeps.mjs");
const dir = ensureRuntimeDir();
const { existsSync } = await import("node:fs");
assert.ok(existsSync(dir), "runtime dir deve existir");
assert.ok(existsSync(join(dir, "package.json")), "package.json deve existir");
});
test("getRuntimeNodeModules retorna caminho dentro do DATA_DIR", async () => {
const { getRuntimeNodeModules } = await import("../../bin/cli/runtime/nativeDeps.mjs");
const nm = getRuntimeNodeModules();
assert.ok(nm.startsWith(tmpDir), "node_modules deve estar dentro do tmpDir");
assert.ok(nm.endsWith("node_modules"), "path deve terminar com node_modules");
});
test("hasModule retorna false para módulo inexistente", async () => {
const { hasModule } = await import("../../bin/cli/runtime/nativeDeps.mjs");
assert.equal(hasModule("definitely-not-installed-xyz"), false);
});
test("isBetterSqliteBinaryValid retorna false quando binário não existe", async () => {
const { isBetterSqliteBinaryValid } = await import("../../bin/cli/runtime/nativeDeps.mjs");
assert.equal(isBetterSqliteBinaryValid(), false);
});
test("buildEnvWithRuntime extende NODE_PATH com runtime node_modules", async () => {
const { buildEnvWithRuntime, getRuntimeNodeModules } =
await import("../../bin/cli/runtime/nativeDeps.mjs");
const nm = getRuntimeNodeModules();
const env = buildEnvWithRuntime({});
assert.ok(env.NODE_PATH.includes(nm), "NODE_PATH deve conter runtime node_modules");
});
test("buildEnvWithRuntime preserva NODE_PATH existente", async () => {
const { buildEnvWithRuntime } = await import("../../bin/cli/runtime/nativeDeps.mjs");
const env = buildEnvWithRuntime({ NODE_PATH: "/existing/path" });
assert.ok(env.NODE_PATH.includes("/existing/path"), "NODE_PATH original deve ser preservado");
});
test("isBetterSqliteBinaryValid rejeita binário com magic bytes válidos mas ABI incompatível (regressão #2493)", async () => {
// Regression for upstream 9router#2493: a binary that only "looks" native (correct ELF/Mach-O/PE
// header) but was built for a different Node ABI (NODE_MODULE_VERSION) must NOT be reported as
// valid — loading it crashes the process (segfault) instead of triggering a rebuild.
const { getRuntimeNodeModules, isBetterSqliteBinaryValid } =
await import("../../bin/cli/runtime/nativeDeps.mjs");
const nm = getRuntimeNodeModules();
const buildDir = join(nm, "better-sqlite3", "build", "Release");
mkdirSync(buildDir, { recursive: true });
const binary = join(buildDir, "better_sqlite3.node");
const { platform } = await import("node:os");
const os = platform();
// Correct file-format magic bytes for the current OS, but not a real, loadable native addon —
// this is exactly what the old magic-bytes-only check let through.
const magicByPlatform = {
linux: [0x7f, 0x45, 0x4c, 0x46],
darwin: [0xcf, 0xfa, 0xed, 0xfe],
win32: [0x4d, 0x5a],
};
const magic = magicByPlatform[os] ?? magicByPlatform.linux;
const buf = Buffer.concat([Buffer.from(magic), Buffer.alloc(64, 0)]);
writeFileSync(binary, buf);
const result = isBetterSqliteBinaryValid();
assert.equal(
result,
false,
"binário com header válido mas ABI/conteúdo incompatível deve ser inválido"
);
rmSync(join(nm, "better-sqlite3"), { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("isBetterSqliteBinaryValid aceita um binário nativo real e carregável", async () => {
const { getRuntimeNodeModules, isBetterSqliteBinaryValid } =
await import("../../bin/cli/runtime/nativeDeps.mjs");
const { existsSync, copyFileSync } = await import("node:fs");
const realBinary = join(
process.cwd(),
"node_modules",
"better-sqlite3",
"build",
"Release",
"better_sqlite3.node"
);
if (!existsSync(realBinary)) {
// Ambient runtime without a compiled better-sqlite3 binary — nothing to assert here.
return;
}
const nm = getRuntimeNodeModules();
const buildDir = join(nm, "better-sqlite3", "build", "Release");
mkdirSync(buildDir, { recursive: true });
const binary = join(buildDir, "better_sqlite3.node");
copyFileSync(realBinary, binary);
const result = isBetterSqliteBinaryValid();
assert.equal(result, true, "um binário real, compatível com o Node atual, deve ser válido");
rmSync(join(nm, "better-sqlite3"), { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("commands/runtime.mjs pode ser importado sem erro", async () => {
const mod = await import("../../bin/cli/commands/runtime.mjs");
assert.equal(typeof mod.registerRuntime, "function");
});