Files
OmniRoute/tests/unit/plugins-manager-lifecycle.test.ts
Paijo 9e7f3cad10 feat(plugins): plugin hook wiring + comprehensive test suite + welcome-banner example (#3045)
Follow-up to the plugins framework (#3041): wires the plugin hooks end-to-end and adds full test coverage.

- Wires `onRequest` / `onResponse` / `onError` hooks into the chat pipeline (`chatCore` now imports from the unified `hooks` registry).
- Loads active plugins on server startup (`pluginManager.loadAll()` in `server-init`) so they survive restarts.
- Ships a `welcome-banner` example plugin (`examples/plugins/`) + test fixture.
- Adds a comprehensive plugin test suite (manifest, db, config, hooks, manager lifecycle, loader IPC, permissions, scanner, welcome-banner e2e).

Integration fixes applied during review:
- `manager.install` now removes an orphaned `destDir` (DB row gone but files left on disk) before the atomic rename, guarded by path containment — it previously failed with `ENOTEMPTY` (a regression surfaced by the new lifecycle test).
- `plugins-db` / `plugins-manager-lifecycle` tests now initialize the DB via the real migration `076` (`getDbInstance`) rather than relying on ambient state, so a missing/renumbered migration fails loudly instead of being masked.

348/348 plugin tests pass; typecheck / cycles clean.

Co-authored-by: oyi77 <14921983+oyi77@users.noreply.github.com>
2026-06-01 16:20:11 -03:00

130 lines
4.4 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 });
}
});
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 {
rmSync(dir.split("/").slice(0, -1).join("/"), { recursive: true, force: true });
}
});
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 });
}
});
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 });
}
});
});
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);
});
});
});