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

138 lines
4.9 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);
});
});
});