Files
OmniRoute/tests/unit/plugins-manager-lifecycle.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

158 lines
5.1 KiB
TypeScript

import { describe, it, beforeEach } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, writeFileSync, mkdirSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
const mod = await import("../../src/lib/plugins/manager.ts");
const db = await import("../../src/lib/db/plugins.ts");
const { getDbInstance } = await import("../../src/lib/db/core.ts");
function makeTmpPlugin(name: string, manifest: Record<string, unknown> = {}) {
const tmp = mkdtempSync(join(tmpdir(), "mgr-test-"));
const pluginDir = join(tmp, name);
mkdirSync(pluginDir, { recursive: true });
writeFileSync(
join(pluginDir, "plugin.json"),
JSON.stringify({ name, version: "1.0.0", ...manifest })
);
writeFileSync(
join(pluginDir, "index.js"),
`module.exports = { onRequest: async (ctx) => ({ metadata: { banner: "hello" } }) };`
);
return pluginDir;
}
function cleanup(name: string) {
try {
db.deletePlugin(name);
} catch {}
}
describe("pluginManager lifecycle", () => {
const testPlugins: string[] = [];
beforeEach(() => {
// Ensure migrations ran (creates the `plugins` table via migration 076)
// before any lifecycle call touches it — uses the real migration, not an
// inline CREATE TABLE, so a missing/renumbered migration fails loudly.
getDbInstance();
// Clean up test plugins
for (const name of testPlugins) {
try {
db.deletePlugin(name);
} catch {}
}
testPlugins.length = 0;
});
describe("install", () => {
it("installs a valid plugin from directory", async () => {
const dir = makeTmpPlugin("install-test");
testPlugins.push("install-test");
try {
const result = await mod.pluginManager.install(dir);
assert.ok(result);
assert.equal(result.name, "install-test");
const dbRow = db.getPluginByName("install-test");
assert.ok(dbRow);
assert.equal(dbRow!.status, "installed");
} finally {
rmSync(dir.split("/").slice(0, -1).join("/"), {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 100,
});
}
});
it("rejects invalid directory", async () => {
await assert.rejects(() => mod.pluginManager.install("/nonexistent/path"));
});
});
describe("activate / deactivate", () => {
it("activates an installed plugin", async () => {
const dir = makeTmpPlugin("activate-test");
testPlugins.push("activate-test");
try {
await mod.pluginManager.install(dir);
await mod.pluginManager.activate("activate-test");
const dbRow = db.getPluginByName("activate-test");
assert.equal(dbRow!.status, "active");
} finally {
// deactivate() is the only path that reaches the loader's cleanup() and kills
// the plugin's child process — without it the child outlives the test and its
// IPC channel keeps this process's event loop alive after the suite finishes.
await mod.pluginManager.deactivate("activate-test").catch(() => {});
rmSync(dir.split("/").slice(0, -1).join("/"), {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 100,
});
}
});
it("deactivates an active plugin", async () => {
const dir = makeTmpPlugin("deactivate-test");
testPlugins.push("deactivate-test");
try {
await mod.pluginManager.install(dir);
await mod.pluginManager.activate("deactivate-test");
await mod.pluginManager.deactivate("deactivate-test");
const dbRow = db.getPluginByName("deactivate-test");
assert.equal(dbRow!.status, "inactive");
} finally {
rmSync(dir.split("/").slice(0, -1).join("/"), {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 100,
});
}
});
it("throws for unknown plugin", async () => {
await assert.rejects(() => mod.pluginManager.activate("nonexistent"));
});
});
describe("uninstall", () => {
it("removes plugin completely", async () => {
const dir = makeTmpPlugin("uninstall-test");
testPlugins.push("uninstall-test");
try {
await mod.pluginManager.install(dir);
await mod.pluginManager.uninstall("uninstall-test");
const dbRow = db.getPluginByName("uninstall-test");
assert.equal(dbRow, null);
} finally {
rmSync(dir.split("/").slice(0, -1).join("/"), {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 100,
});
}
});
});
describe("getLoaded / listAll / getPlugin", () => {
it("getLoaded returns undefined for unloaded plugin", () => {
assert.equal(mod.pluginManager.getLoaded("not-loaded"), undefined);
});
it("listAll returns array", async () => {
const list = await mod.pluginManager.listAll();
assert.ok(Array.isArray(list));
});
it("getPlugin returns null for unknown", async () => {
const result = await mod.pluginManager.getPlugin("nonexistent");
assert.equal(result, null);
});
});
});