mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 19:02:17 +03:00
* 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.
103 lines
4.7 KiB
TypeScript
103 lines
4.7 KiB
TypeScript
/**
|
|
* Regression guard for #5006 — `/api/system/env/repair` 500s on packaged install.
|
|
*
|
|
* Root cause: `route.ts` statically imports `scripts/dev/sync-env.mjs`, so webpack
|
|
* inlines it into the standalone route bundle. `sync-env.mjs` then ran
|
|
* `createRequire(import.meta.url)` at TOP-LEVEL module scope — when bundled,
|
|
* `import.meta.url` is frozen to the build-machine path
|
|
* (`file:///home/runner/.../sync-env.mjs`), which `createRequire` rejects at runtime.
|
|
* Because the throw happens during module evaluation (reached via the static import),
|
|
* the whole route module fails to load → every hit returns HTTP 500 → onboarding breaks.
|
|
*
|
|
* The fix moves `createRequire` out of top-level scope into the single function that
|
|
* needs `better-sqlite3` (guarded by the existing try/catch), and makes the root-dir
|
|
* resolution tolerant of an unusable `import.meta.url` (falls back to `process.cwd()`),
|
|
* so importing/evaluating the module — and calling its exports — never throws.
|
|
*/
|
|
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 { fileURLToPath } from "node:url";
|
|
|
|
const SYNC_ENV_PATH = fileURLToPath(new URL("../../scripts/dev/sync-env.mjs", import.meta.url));
|
|
|
|
const mod = (await import("../../scripts/dev/sync-env.mjs")) as {
|
|
getEnvSyncPlan: (opts?: { rootDir?: string; scope?: string }) => {
|
|
available: boolean;
|
|
created: boolean;
|
|
added: number;
|
|
missingEntries: Array<{ key: string; value: string }>;
|
|
};
|
|
syncEnv: (opts?: { rootDir?: string; quiet?: boolean; scope?: string }) => {
|
|
created: boolean;
|
|
added: number;
|
|
};
|
|
};
|
|
|
|
function writeOauthEnvExample(rootDir: string) {
|
|
fs.writeFileSync(
|
|
path.join(rootDir, ".env.example"),
|
|
[
|
|
"# ═══════════════════════════════════════════════════",
|
|
"# OAUTH PROVIDER CREDENTIALS",
|
|
"# ═══════════════════════════════════════════════════",
|
|
"CLAUDE_OAUTH_CLIENT_ID=claude-default",
|
|
"CODEX_OAUTH_CLIENT_ID=codex-default",
|
|
"# ─────────────────────────────────────────────────────────────────────────────",
|
|
"# Provider User-Agent Overrides (optional)",
|
|
"# ─────────────────────────────────────────────────────────────────────────────",
|
|
"",
|
|
].join("\n"),
|
|
"utf8"
|
|
);
|
|
}
|
|
|
|
test("#5006: sync-env.mjs does not call createRequire at module top-level scope", () => {
|
|
const source = fs.readFileSync(SYNC_ENV_PATH, "utf8");
|
|
|
|
// A top-level `const require = createRequire(import.meta.url)` (column 0, no
|
|
// leading indentation) is the exact crash vector: it runs during module
|
|
// evaluation with a webpack-frozen `import.meta.url`. It must live inside a
|
|
// function body / try-catch instead.
|
|
assert.doesNotMatch(
|
|
source,
|
|
/^const\s+require\s*=\s*createRequire\s*\(/m,
|
|
"createRequire(import.meta.url) must not run at top-level module scope (bundled import.meta.url is frozen → throws on module load → route 500)"
|
|
);
|
|
|
|
// `createRequire` is still used lazily where better-sqlite3 is needed — but only
|
|
// from inside a function (every occurrence is indented, never column 0).
|
|
for (const line of source.split(/\r?\n/)) {
|
|
if (/createRequire\s*\(/.test(line)) {
|
|
assert.match(
|
|
line,
|
|
/^\s+/,
|
|
`createRequire must be called from inside a function (indented), found top-level: ${line}`
|
|
);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("#5006: getEnvSyncPlan(oauth) works with explicit rootDir and never throws on bad DATA_DIR", () => {
|
|
const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-5006-"));
|
|
const origDataDir = process.env.DATA_DIR;
|
|
try {
|
|
writeOauthEnvExample(rootDir);
|
|
// Point DATA_DIR at a dir with no real SQLite DB — exercises the
|
|
// better-sqlite3 / createRequire path that used to crash at module load.
|
|
process.env.DATA_DIR = rootDir;
|
|
|
|
const plan = mod.getEnvSyncPlan({ rootDir, scope: "oauth" });
|
|
|
|
assert.equal(plan.available, true);
|
|
assert.equal(plan.added, 2);
|
|
const keys = plan.missingEntries.map((e) => e.key);
|
|
assert.deepEqual(keys.sort(), ["CLAUDE_OAUTH_CLIENT_ID", "CODEX_OAUTH_CLIENT_ID"]);
|
|
} finally {
|
|
process.env.DATA_DIR = origDataDir;
|
|
fs.rmSync(rootDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
}
|
|
});
|