Files
OmniRoute/tests/unit/services/installers/bifrost.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966)

Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.

One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.

Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.

* fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff

The gate shells out to `git diff` through execFileSync with Node's default 1 MB
maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to
overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing
anything. 64 MB is far above any real PR and costs nothing when unused.
2026-08-29 01:17:40 -03:00

161 lines
5.6 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";
import { execSync } from "node:child_process";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-bifrost-installer-"));
const FAKE_BIN_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-bifrost-fake-bin-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.NODE_ENV = "test";
process.env.DISABLE_SQLITE_AUTO_BACKUP = "true";
const originalPath = process.env.PATH ?? "";
process.env.PATH = `${FAKE_BIN_DIR}:${originalPath}`;
const INSTALL_DIR = path.join(TEST_DATA_DIR, "services", "bifrost");
const fakeNpmScript = `#!/bin/sh
set -e
CMD="$1"
shift
if [ "$CMD" = "install" ]; then
PREFIX=""
while [ $# -gt 0 ]; do
if [ "$1" = "--prefix" ]; then PREFIX="$2"; shift 2; else shift; fi
done
if [ -z "$PREFIX" ]; then PREFIX="$npm_config_prefix"; fi
PKG_DIR="$PREFIX/node_modules/@maximhq/bifrost"
mkdir -p "$PKG_DIR"
echo '{"name":"@maximhq/bifrost","version":"1.6.3"}' > "$PKG_DIR/package.json"
touch "$PKG_DIR/bin.js"
exit 0
fi
if [ "$CMD" = "view" ]; then
echo "1.6.3"
exit 0
fi
exit 0
`;
const fakeNpmPath = path.join(FAKE_BIN_DIR, "npm");
fs.writeFileSync(fakeNpmPath, fakeNpmScript, { mode: 0o755 });
execSync("which npm", { env: process.env });
// DB bootstrap (must be before bifrost import due to db/core eager init)
const core = await import("../../../../src/lib/db/core.ts");
const db = core.getDbInstance();
db.prepare(
`INSERT OR IGNORE INTO version_manager (tool, status, port, auto_start, auto_update, provider_expose)
VALUES ('bifrost', 'not_installed', 8080, 0, 1, 1)`
).run();
const {
install,
update,
getInstalledVersion,
getLatestVersion,
resolveSpawnArgs,
BIFROST_DEFAULT_PORT,
BIFROST_INSTALL_DIR,
} = await import("../../../../src/lib/services/installers/bifrost.ts");
test.after(() => {
process.env.PATH = originalPath;
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.rmSync(FAKE_BIN_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("BIFROST_DEFAULT_PORT is 8080", () => {
assert.equal(BIFROST_DEFAULT_PORT, 8080);
});
test("install creates host package.json structure", async () => {
const result = await install("1.6.3");
const hostPkg = path.join(BIFROST_INSTALL_DIR, "package.json");
assert.ok(fs.existsSync(hostPkg), "host package.json should exist");
const parsedHost = JSON.parse(fs.readFileSync(hostPkg, "utf8")) as {
name: string;
private: boolean;
};
assert.equal(parsedHost.name, "omniroute-bifrost-host");
assert.ok(parsedHost.private);
assert.equal(result.installedVersion, "1.6.3");
assert.equal(result.installPath, BIFROST_INSTALL_DIR);
assert.ok(result.durationMs >= 0);
});
test("getInstalledVersion reads from node_modules/@maximhq/bifrost/package.json", async () => {
const ver = await getInstalledVersion();
assert.equal(ver, "1.6.3", "should read version from installed package");
});
test("update calls npm install with latest (idempotent)", async () => {
const result = await update();
assert.equal(result.installedVersion, "1.6.3");
});
test("getLatestVersion returns version string from npm view", async () => {
const ver = await getLatestVersion();
assert.equal(ver, "1.6.3");
});
test("resolveSpawnArgs shape: command is node, bin.js path, Go single-dash flags", () => {
const args = resolveSpawnArgs(8080);
assert.equal(args.command, process.execPath, "command must be current node binary");
assert.ok(args.args[0]?.includes("bin.js"), "args[0] should point to bin.js");
// Go-style single-dash flags
const portIdx = args.args.indexOf("-port");
assert.ok(portIdx !== -1, "must have -port flag");
assert.equal(args.args[portIdx + 1], "8080");
const hostIdx = args.args.indexOf("-host");
assert.ok(hostIdx !== -1, "must have -host flag");
assert.equal(args.args[hostIdx + 1], "127.0.0.1");
const appDirIdx = args.args.indexOf("-app-dir");
assert.ok(appDirIdx !== -1, "must have -app-dir flag");
assert.ok(args.args[appDirIdx + 1]?.includes("bifrost"), "-app-dir must point into bifrost dir");
const logLevelIdx = args.args.indexOf("-log-level");
assert.ok(logLevelIdx !== -1, "must have -log-level flag");
assert.equal(args.args[logLevelIdx + 1], "warn");
// BIFROST_TRANSPORT_VERSION must be set in env AND match the format
// bifrost's own bin.js requires: /^v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/ or "latest".
// (regression check for the "Invalid transport version format" startup crash -
// the fake npm helper above reports version "1.6.3", bare semver with no "v")
assert.ok(
typeof args.env.BIFROST_TRANSPORT_VERSION === "string" &&
args.env.BIFROST_TRANSPORT_VERSION.length > 0,
"BIFROST_TRANSPORT_VERSION must be set in env"
);
assert.match(
args.env.BIFROST_TRANSPORT_VERSION,
/^(latest|v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)$/,
`BIFROST_TRANSPORT_VERSION must be "latest" or v-prefixed semver, got: ${args.env.BIFROST_TRANSPORT_VERSION}`
);
});
test("resolveSpawnArgs with different port passes correct -port value", () => {
const args = resolveSpawnArgs(9090);
const portIdx = args.args.indexOf("-port");
assert.ok(portIdx !== -1);
assert.equal(args.args[portIdx + 1], "9090");
});
test("INSTALL_DIR constant points into DATA_DIR/services/bifrost", () => {
assert.ok(BIFROST_INSTALL_DIR.includes("bifrost"), "install dir must include 'bifrost'");
assert.ok(
BIFROST_INSTALL_DIR.startsWith(TEST_DATA_DIR),
"install dir must be under TEST_DATA_DIR"
);
assert.equal(INSTALL_DIR, BIFROST_INSTALL_DIR);
});