Files
OmniRoute/tests/unit/materialize-bundled-symlinks.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

226 lines
8.3 KiB
TypeScript

import assert from "node:assert/strict";
import {
existsSync,
lstatSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
symlinkSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
// @ts-expect-error - JS build helper without type declarations
import {
materializeBundledSymlinks,
syncRebuiltNativeModuleIntoHashedEntries,
} from "../../scripts/build/assembleStandalone.mjs";
function makePkg(dir: string, name: string, marker: string) {
const pkgDir = join(dir, name);
mkdirSync(pkgDir, { recursive: true });
writeFileSync(join(pkgDir, "package.json"), JSON.stringify({ name, marker }));
return pkgDir;
}
test("materializeBundledSymlinks dereferences a live symlink into a real directory", () => {
const root = mkdtempSync(join(tmpdir(), "mbs-live-"));
try {
const realPkgHome = join(root, "external");
mkdirSync(realPkgHome, { recursive: true });
makePkg(realPkgHome, "ws", "real-ws");
const nm = join(root, "bundle", "node_modules");
mkdirSync(nm, { recursive: true });
symlinkSync(join(realPkgHome, "ws"), join(nm, "ws-a972e7ffa40ff725"), "dir");
const summary = materializeBundledSymlinks(nm);
assert.equal(summary.materialized, 1);
const target = join(nm, "ws-a972e7ffa40ff725");
assert.equal(lstatSync(target).isSymbolicLink(), false);
assert.equal(lstatSync(target).isDirectory(), true);
assert.equal(JSON.parse(readFileSync(join(target, "package.json"), "utf8")).marker, "real-ws");
} finally {
rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("materializeBundledSymlinks relinks a dangling hashed symlink to its sibling real package", () => {
const root = mkdtempSync(join(tmpdir(), "mbs-dangle-"));
try {
const nm = join(root, "node_modules");
mkdirSync(nm, { recursive: true });
// Sibling real package (as copied by copyNativeAssetsAndExtraModules).
makePkg(nm, "better-sqlite3", "real-bsq");
// Dangling absolute link into a build machine that does not exist here.
symlinkSync(
"/Users/runner/work/OmniRoute/OmniRoute/.build/next/standalone/node_modules/better-sqlite3",
join(nm, "better-sqlite3-90e2652d1716b047"),
"dir"
);
const summary = materializeBundledSymlinks(nm);
assert.equal(summary.relinked, 1);
const target = join(nm, "better-sqlite3-90e2652d1716b047");
assert.equal(lstatSync(target).isSymbolicLink(), false);
assert.equal(JSON.parse(readFileSync(join(target, "package.json"), "utf8")).marker, "real-bsq");
} finally {
rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("materializeBundledSymlinks drops a dangling link with no resolvable sibling", () => {
const root = mkdtempSync(join(tmpdir(), "mbs-drop-"));
try {
const nm = join(root, "node_modules");
mkdirSync(nm, { recursive: true });
symlinkSync(
"/nonexistent/build/machine/path/mystery",
join(nm, "mystery-deadbeefcafe0001"),
"dir"
);
const summary = materializeBundledSymlinks(nm);
assert.equal(summary.removed, 1);
assert.equal(existsSync(join(nm, "mystery-deadbeefcafe0001")), false);
} finally {
rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("materializeBundledSymlinks handles scoped-package symlinks", () => {
const root = mkdtempSync(join(tmpdir(), "mbs-scope-"));
try {
const realPkgHome = join(root, "external");
mkdirSync(join(realPkgHome, "@huggingface"), { recursive: true });
makePkg(join(realPkgHome, "@huggingface"), "transformers", "real-hf");
const nm = join(root, "node_modules");
mkdirSync(join(nm, "@huggingface"), { recursive: true });
symlinkSync(
join(realPkgHome, "@huggingface", "transformers"),
join(nm, "@huggingface", "transformers-abc1234567890def"),
"dir"
);
const summary = materializeBundledSymlinks(nm);
assert.equal(summary.materialized, 1);
const target = join(nm, "@huggingface", "transformers-abc1234567890def");
assert.equal(lstatSync(target).isSymbolicLink(), false);
assert.equal(JSON.parse(readFileSync(join(target, "package.json"), "utf8")).marker, "real-hf");
} finally {
rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("materializeBundledSymlinks leaves real directories untouched and no-ops on missing dir", () => {
const root = mkdtempSync(join(tmpdir(), "mbs-noop-"));
try {
const nm = join(root, "node_modules");
mkdirSync(nm, { recursive: true });
makePkg(nm, "pino", "real-pino");
const summary = materializeBundledSymlinks(nm);
assert.deepEqual(summary, { materialized: 0, relinked: 0, removed: 0 });
assert.equal(existsSync(join(nm, "pino", "package.json")), true);
const missing = materializeBundledSymlinks(join(root, "does-not-exist"));
assert.deepEqual(missing, { materialized: 0, relinked: 0, removed: 0 });
} finally {
rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("syncRebuiltNativeModuleIntoHashedEntries overwrites a hashed entry with the rebuilt root module", () => {
const root = mkdtempSync(join(tmpdir(), "sync-hashed-"));
try {
const rootModule = makePkg(root, "better-sqlite3", "electron-abi-rebuilt");
const nm = join(root, "nested", "node_modules");
makePkg(nm, "better-sqlite3-90e2652d1716b047", "stale-node-abi");
const summary = syncRebuiltNativeModuleIntoHashedEntries(rootModule, nm);
assert.equal(summary.synced, 1);
const target = join(nm, "better-sqlite3-90e2652d1716b047");
assert.equal(
JSON.parse(readFileSync(join(target, "package.json"), "utf8")).marker,
"electron-abi-rebuilt"
);
} finally {
rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("syncRebuiltNativeModuleIntoHashedEntries overwrites a plain-named entry too", () => {
const root = mkdtempSync(join(tmpdir(), "sync-plain-"));
try {
const rootModule = makePkg(root, "better-sqlite3", "electron-abi-rebuilt");
const nm = join(root, "nested", "node_modules");
makePkg(nm, "better-sqlite3", "stale-node-abi");
const summary = syncRebuiltNativeModuleIntoHashedEntries(rootModule, nm);
assert.equal(summary.synced, 1);
assert.equal(
JSON.parse(readFileSync(join(nm, "better-sqlite3", "package.json"), "utf8")).marker,
"electron-abi-rebuilt"
);
} finally {
rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("syncRebuiltNativeModuleIntoHashedEntries no-ops when root module or nested node_modules is missing", () => {
const root = mkdtempSync(join(tmpdir(), "sync-noop-"));
try {
const rootModule = join(root, "does-not-exist", "better-sqlite3");
const nm = join(root, "nested", "node_modules");
makePkg(nm, "better-sqlite3", "stale-node-abi");
const missingRoot = syncRebuiltNativeModuleIntoHashedEntries(rootModule, nm);
assert.deepEqual(missingRoot, { synced: 0 });
const realRootModule = makePkg(root, "better-sqlite3", "electron-abi-rebuilt");
const missingNm = syncRebuiltNativeModuleIntoHashedEntries(
realRootModule,
join(root, "does-not-exist-nm")
);
assert.deepEqual(missingNm, { synced: 0 });
} finally {
rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("syncRebuiltNativeModuleIntoHashedEntries leaves unrelated entries untouched", () => {
const root = mkdtempSync(join(tmpdir(), "sync-unrelated-"));
try {
const rootModule = makePkg(root, "better-sqlite3", "electron-abi-rebuilt");
const nm = join(root, "nested", "node_modules");
makePkg(nm, "pino", "real-pino");
makePkg(nm, "better-sqlite3-helper", "unrelated-package");
const summary = syncRebuiltNativeModuleIntoHashedEntries(rootModule, nm);
assert.deepEqual(summary, { synced: 0 });
assert.equal(
JSON.parse(readFileSync(join(nm, "pino", "package.json"), "utf8")).marker,
"real-pino"
);
assert.equal(
JSON.parse(readFileSync(join(nm, "better-sqlite3-helper", "package.json"), "utf8")).marker,
"unrelated-package"
);
} finally {
rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});