Files
OmniRoute/tests/unit/cli-doctor-prebuilt-native-binary-10083.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

92 lines
3.7 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";
// #10083 — `doctor` only looked for the node-gyp layout
// (build/Release/better_sqlite3.node), so every install that resolves a
// prebuilt binary (`npm i -g omniroute`) warned "better-sqlite3 native binary
// was not found" even though the binary was present and loading fine.
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
interface DoctorCheck {
name: string;
status: string;
message: string;
details: Record<string, unknown>;
}
function nativeBinaryCheck(result: { checks: DoctorCheck[] }) {
const check = result.checks.find((c) => c.name === "Native binary");
assert.ok(check, "expected a 'Native binary' check");
return check;
}
async function withTempRoot(fn: (rootDir: string) => Promise<void>) {
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-prebuild-data-"));
const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-prebuild-root-"));
process.env.DATA_DIR = dataDir;
try {
await fn(rootDir);
} finally {
fs.rmSync(dataDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.rmSync(rootDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
if (ORIGINAL_DATA_DIR === undefined) delete process.env.DATA_DIR;
else process.env.DATA_DIR = ORIGINAL_DATA_DIR;
}
}
test("prebuiltBinaryName maps platform/arch the way better-sqlite3 ships them", async () => {
const { prebuiltBinaryName } = await import("../../bin/cli/commands/doctor.mjs");
assert.equal(prebuiltBinaryName("darwin", "arm64"), "darwin-arm64.node");
assert.equal(prebuiltBinaryName("win32", "x64"), "win32-x64.node");
// glibc Linux keeps the plain `linux-` prefix …
const glibcReport = { getReport: () => ({ header: { glibcVersionRuntime: "2.39" } }) };
assert.equal(prebuiltBinaryName("linux", "x64", glibcReport), "linux-x64.node");
// … while musl builds (no glibcVersionRuntime) use `linuxmusl-`.
const muslReport = { getReport: () => ({ header: {} }) };
assert.equal(prebuiltBinaryName("linux", "arm64", muslReport), "linuxmusl-arm64.node");
});
test("doctor finds a prebuilt better-sqlite3 binary instead of warning 'not found'", async () => {
await withTempRoot(async (rootDir) => {
const { collectDoctorChecks, prebuiltBinaryName } =
await import("../../bin/cli/commands/doctor.mjs");
const prebuildDir = path.join(rootDir, "node_modules", "better-sqlite3", "prebuilds");
fs.mkdirSync(prebuildDir, { recursive: true });
const binaryPath = path.join(prebuildDir, prebuiltBinaryName());
fs.writeFileSync(binaryPath, Buffer.alloc(64));
const result = await collectDoctorChecks({ rootDir }, { skipLiveness: true });
const check = nativeBinaryCheck(result);
assert.ok(
!/was not found/.test(check.message),
`expected the prebuild to be discovered, got: ${check.message}`
);
assert.equal(check.details.binaryPath, binaryPath);
});
});
test("doctor still warns when neither layout has a binary", async () => {
await withTempRoot(async (rootDir) => {
const { collectDoctorChecks } = await import("../../bin/cli/commands/doctor.mjs");
const result = await collectDoctorChecks({ rootDir }, { skipLiveness: true });
const check = nativeBinaryCheck(result);
assert.equal(check.status, "warn");
assert.match(check.message, /was not found/);
// Both layouts should be reported so the warning is actionable.
const candidates = check.details.candidates as string[];
assert.ok(candidates.some((c) => c.includes(path.join("build", "Release"))));
assert.ok(candidates.some((c) => c.includes("prebuilds")));
});
});