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

84 lines
3.0 KiB
TypeScript

import { describe, it } 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/scanner.ts");
function makePluginDir(tmpDir: string, name: string, manifest: Record<string, unknown>) {
const pluginDir = join(tmpDir, name);
mkdirSync(pluginDir, { recursive: true });
writeFileSync(join(pluginDir, "plugin.json"), JSON.stringify(manifest, null, 2));
writeFileSync(join(pluginDir, "index.js"), "module.exports = {};");
return pluginDir;
}
const validManifest = { name: "scan-test", version: "1.0.0" };
describe("plugin scanner", () => {
describe("getDefaultPluginDir", () => {
it("returns a string path", () => {
const dir = mod.getDefaultPluginDir();
assert.equal(typeof dir, "string");
assert.ok(dir.includes("plugins") || dir.includes("omniroute"));
});
});
describe("scanPluginDir", () => {
it("discovers valid plugins", async () => {
const tmp = mkdtempSync(join(tmpdir(), "scan-test-"));
try {
makePluginDir(tmp, "my-plugin", validManifest);
const result = await mod.scanPluginDir(tmp);
assert.equal(result.plugins.length, 1);
assert.equal(result.plugins[0].name, "scan-test");
assert.ok(result.plugins[0].manifest);
assert.ok(result.plugins[0].pluginDir);
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});
it("skips directories without plugin.json", async () => {
const tmp = mkdtempSync(join(tmpdir(), "scan-test-"));
try {
mkdirSync(join(tmp, "not-a-plugin"));
writeFileSync(join(tmp, "not-a-plugin", "index.js"), "");
const result = await mod.scanPluginDir(tmp);
assert.equal(result.plugins.length, 0);
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});
it("skips plugins with invalid manifest", async () => {
const tmp = mkdtempSync(join(tmpdir(), "scan-test-"));
try {
makePluginDir(tmp, "bad-plugin", { name: "INVALID NAME!", version: "nope" });
const result = await mod.scanPluginDir(tmp);
assert.equal(result.plugins.length, 0);
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});
it("handles non-existent directory", async () => {
const result = await mod.scanPluginDir("/nonexistent/path");
assert.equal(result.plugins.length, 0);
});
it("discovers multiple plugins", async () => {
const tmp = mkdtempSync(join(tmpdir(), "scan-test-"));
try {
makePluginDir(tmp, "plugin-a", { name: "plugin-a", version: "1.0.0" });
makePluginDir(tmp, "plugin-b", { name: "plugin-b", version: "2.0.0" });
const result = await mod.scanPluginDir(tmp);
assert.equal(result.plugins.length, 2);
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});
});
});