Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
feat(plugins): plugin hook wiring + comprehensive test suite + welcome-banner example (#3045)
Paijo9e7f3cad1017
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f75754d46a..084d567163 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -5,6 +5,7 @@### Added- **Plugins framework** (`src/lib/plugins/`, `/api/plugins/*`, `/dashboard/plugins`) — hooks + registry unification, plugin SDK (`definePlugin`), worker-thread sandbox, per-plugin hook rate limiting, SHA-256 integrity verification, semver-gated upgrade, and execution analytics. Plugin routes are loopback-only (`isLocalOnlyPath`) and `child_process` exec is opt-in via `OMNIROUTE_PLUGINS_ALLOW_EXEC`. (#2913 — thanks @oyi77)+- **Plugin system: response-hook wiring + startup load + example plugin** — wires the plugin `onResponse` hook into the chat success path, loads active plugins on server startup so they survive restarts (`pluginManager.loadAll()` in `server-init`), and ships a `welcome-banner` example plugin (`examples/plugins/`) plus a comprehensive plugin test suite. (#3045 — thanks @oyi77)- **API key option: disable non-published models** — a per-key flag restricting the key to discovered, public models (combos / `auto/*` / `qtSd/*` routing still allowed). (#3017 — thanks @androw)- **SessionPool — modular & provider-agnostic** (`open-sse/services/sessionPool/`) — pooledcookie/session manager with round-robin fingerprint rotation (distinct fingerprint per pooleddiff --git a/examples/plugins/welcome-banner/index.mjs b/examples/plugins/welcome-banner/index.mjsnew file mode 100644index 0000000000..b370914dd0--- /dev/null+++ b/examples/plugins/welcome-banner/index.mjs@@ -0,0 +1,36 @@+/**+ * Welcome Banner Plugin — PoC demonstrating the OmniRoute plugin system.+ *+ * Adds a banner message to request metadata on every request.+ * Logs a delivery confirmation on every response.+ *+ * @module welcome-banner+ */++/**+ * onRequest hook — injects banner text into request metadata.+ *+ * @param {object} ctx - Plugin context+ * @param {object} [ctx.config] - Plugin configuration+ * @param {object} [ctx.metadata] - Request metadata (mutable)+ */+export function onRequest(ctx) {+ const config = ctx?.config || {};+ const enabled = config.enabled !== false; // default true+ if (!enabled) return;++ const bannerText = config.bannerText || "Welcome to OmniRoute!";+ if (ctx.metadata) {+ ctx.metadata.banner = bannerText;+ }+}++/**+ * onResponse hook — fire-and-forget banner delivery log.+ *+ * @param {object} ctx - Plugin context+ * @param {object} response - Upstream response+ */+export function onResponse() {+ // No-op — banner is request-side only+}diff --git a/examples/plugins/welcome-banner/plugin.json b/examples/plugins/welcome-banner/plugin.jsonnew file mode 100644index 0000000000..1c5cf40872--- /dev/null+++ b/examples/plugins/welcome-banner/plugin.json@@ -0,0 +1,29 @@+{+ "name": "welcome-banner",+ "version": "1.0.0",+ "description": "Adds a welcome banner to API responses",+ "author": "OmniRoute",+ "license": "MIT",+ "main": "index.mjs",+ "source": "local",+ "tags": ["demo", "banner"],+ "hooks": {+ "onRequest": true,+ "onResponse": true,+ "onError": false+ },+ "permissions": [],+ "enabledByDefault": true,+ "configSchema": {+ "bannerText": {+ "type": "string",+ "default": "Welcome to OmniRoute!",+ "description": "Banner message to display"+ },+ "enabled": {+ "type": "boolean",+ "default": true,+ "description": "Enable or disable the banner"+ }+ }+}diff --git a/open-sse/handlers/chatCore.ts b/open-sse/handlers/chatCore.tsindex f0cf83ec51..6a9eb700e2 100644--- a/open-sse/handlers/chatCore.ts+++ b/open-sse/handlers/chatCore.ts@@ -1604,7 +1604,7 @@ export async function handleChatCore({// ── Plugin onRequest hook ──// Dynamic import cached by Node.js after first call — minimal overheadtry {- const { runOnRequest } = await import("@/lib/plugins/index");+ const { runOnRequest } = await import("@/lib/plugins/hooks");const pluginCtx = {requestId: traceId,body,@@ -1636,8 +1636,11 @@ export async function handleChatCore({),};}- if (pluginResult?.ctx && "body" in pluginResult.ctx) {- body = (pluginResult.ctx as unknown as Record<string, unknown>).body;+ if (pluginResult?.body) {+ body = pluginResult.body;+ }+ if (pluginResult?.metadata) {+ Object.assign(pluginCtx.metadata, pluginResult.metadata);}} catch (pluginErr) {log?.debug?.(@@ -3417,7 +3420,7 @@ export async function handleChatCore({} catch (error) {// ── Plugin onError hook ──try {- const { runOnError } = await import("@/lib/plugins/index");+ const { runOnError } = await import("@/lib/plugins/hooks");await runOnError({ requestId: traceId, body, model, provider, apiKeyInfo, metadata: {} },error instanceof Error ? error : new Error(String(error))@@ -5838,6 +5841,17 @@ export async function handleChatCore({}}+ // ── Plugin onResponse hook (fire-and-forget) ──+ try {+ const { runOnResponse } = await import("@/lib/plugins/hooks");+ runOnResponse(+ { requestId: traceId, body, model, provider, apiKeyInfo, metadata: {} },+ { status: 200 }+ ).catch(() => {});+ } catch (_) {+ /* plugin onResponse optional */+ }+return {success: true,response: new Response(finalStream, {diff --git a/src/lib/plugins/manager.ts b/src/lib/plugins/manager.tsindex 4e4f3ff09a..8cdc174acb 100644--- a/src/lib/plugins/manager.ts+++ b/src/lib/plugins/manager.ts@@ -168,6 +168,13 @@ class PluginManager {const destDir = join(this.pluginDir, name);const stagingDir = `${destDir}.staging-${randomUUID()}`;await mkdir(dirname(destDir), { recursive: true });+ // Reaching here means the plugin is not DB-registered (the pluginExists()+ // guard above returns/throws otherwise). A destDir still present on disk is+ // therefore orphaned (e.g. a crash mid-uninstall left files behind) and would+ // make the atomic rename below fail with ENOTEMPTY — remove it first, guarded+ // by path containment so we never rm outside the plugin directory.+ assertWithinPluginDir(this.pluginDir, destDir);+ await rm(destDir, { recursive: true, force: true }).catch(() => {});await cp(srcDir, stagingDir, { recursive: true });try {diff --git a/src/server-init.ts b/src/server-init.tsindex 9060f40553..5083e7d64c 100644--- a/src/server-init.ts+++ b/src/server-init.ts@@ -92,6 +92,16 @@ async function startServer() {startupLog.info("Spend batch writer started");startupLog.info("Guardrail registry initialized");startupLog.info("Builtin skill handlers registered");++ // Load active plugins on startup so they survive restarts+ try {+ const { pluginManager } = await import("./lib/plugins/manager");+ await pluginManager.loadAll();+ startupLog.info("Plugin manager loaded active plugins");+ } catch (err) {+ startupLog.warn({ err }, "Plugin manager loadAll failed (non-fatal)");+ }+await initializeCloudSync();startBudgetResetJob();startReasoningCacheCleanupJob();diff --git a/tests/fixtures/welcome-banner-plugin/index.mjs b/tests/fixtures/welcome-banner-plugin/index.mjsnew file mode 100644index 0000000000..522057f9cd--- /dev/null+++ b/tests/fixtures/welcome-banner-plugin/index.mjs@@ -0,0 +1,28 @@+/**+ * Welcome Banner Plugin — PoC+ *+ * Injects a welcome banner into every response to prove the plugin+ * pipeline works end-to-end.+ */++export const plugin = {+ name: "welcome-banner",+ priority: 200,++ async onResponse(ctx, response) {+ if (response && typeof response === "object") {+ const banner = "[Welcome to OmniRoute — powered by welcome-banner plugin]";+ if (response.choices && Array.isArray(response.choices)) {+ for (const choice of response.choices) {+ if (choice.message && typeof choice.message.content === "string") {+ choice.message.content = `${banner}\n${choice.message.content}`;+ }+ if (choice.delta && typeof choice.delta.content === "string") {+ choice.delta.content = `${banner}\n${choice.delta.content}`;+ }+ }+ }+ }+ return response;+ },+};diff --git a/tests/fixtures/welcome-banner-plugin/plugin.json b/tests/fixtures/welcome-banner-plugin/plugin.jsonnew file mode 100644index 0000000000..5301da3685--- /dev/null+++ b/tests/fixtures/welcome-banner-plugin/plugin.json@@ -0,0 +1,17 @@+{+ "name": "welcome-banner",+ "version": "1.0.0",+ "description": "PoC plugin that injects a welcome banner into responses",+ "author": "OmniRoute",+ "license": "MIT",+ "main": "index.mjs",+ "source": "local",+ "tags": ["poc", "demo"],+ "hooks": {+ "onResponse": true+ },+ "requires": {+ "permissions": []+ },+ "enabledByDefault": true+}diff --git a/tests/unit/plugins-api-routes.test.ts b/tests/unit/plugins-api-routes.test.tsnew file mode 100644index 0000000000..c49888c77a--- /dev/null+++ b/tests/unit/plugins-api-routes.test.ts@@ -0,0 +1,42 @@+import { describe, it } from "node:test";+import assert from "node:assert/strict";++// Test plugin API route structure and type contracts.+// Full HTTP integration tests require the Next.js test harness.++describe("plugin API routes", () => {+ describe("route modules exist", () => {+ it("main plugins route exports GET and POST", async () => {+ const mod = await import("../../src/app/api/plugins/route.ts");+ assert.equal(typeof mod.GET, "function");+ assert.equal(typeof mod.POST, "function");+ });++ it("[name] route exports GET and DELETE", async () => {+ const mod = await import("../../src/app/api/plugins/[name]/route.ts");+ assert.equal(typeof mod.GET, "function");+ assert.equal(typeof mod.DELETE, "function");+ });++ it("activate route exports POST", async () => {+ const mod = await import("../../src/app/api/plugins/[name]/activate/route.ts");+ assert.equal(typeof mod.POST, "function");+ });++ it("deactivate route exports POST", async () => {+ const mod = await import("../../src/app/api/plugins/[name]/deactivate/route.ts");+ assert.equal(typeof mod.POST, "function");+ });++ it("config route exports GET and PUT", async () => {+ const mod = await import("../../src/app/api/plugins/[name]/config/route.ts");+ assert.equal(typeof mod.GET, "function");+ assert.equal(typeof mod.PUT, "function");+ });++ it("scan route exports POST", async () => {+ const mod = await import("../../src/app/api/plugins/scan/route.ts");+ assert.equal(typeof mod.POST, "function");+ });+ });+});diff --git a/tests/unit/plugins-config.test.ts b/tests/unit/plugins-config.test.tsnew file mode 100644index 0000000000..96ea24f546--- /dev/null+++ b/tests/unit/plugins-config.test.ts@@ -0,0 +1,139 @@+import { describe, it } from "node:test";+import assert from "node:assert/strict";++import {+ ConfigFieldSchema,+ PluginManifestSchema,+ safeValidateManifest,+ applyDefaults,+} from "../../src/lib/plugins/manifest.ts";++const validManifest = {+ name: "test-plugin",+ version: "1.0.0",+};++describe("Plugin config schema validation", () => {+ describe("ConfigFieldSchema", () => {+ it("accepts string config field", () => {+ const result = ConfigFieldSchema.safeParse({+ type: "string",+ default: "hello",+ description: "A string field",+ });+ assert.ok(result.success);+ });++ it("accepts number config field with min/max", () => {+ const result = ConfigFieldSchema.safeParse({+ type: "number",+ default: 5,+ min: 1,+ max: 100,+ description: "A number field",+ });+ assert.ok(result.success);+ });++ it("accepts boolean config field", () => {+ const result = ConfigFieldSchema.safeParse({+ type: "boolean",+ default: true,+ description: "A boolean field",+ });+ assert.ok(result.success);+ });++ it("accepts select config field with enum", () => {+ const result = ConfigFieldSchema.safeParse({+ type: "select",+ options: ["low", "medium", "high"],+ default: "medium",+ description: "A select field",+ });+ assert.ok(result.success);+ });++ it("rejects invalid config type", () => {+ const result = ConfigFieldSchema.safeParse({+ type: "invalid",+ description: "Bad type",+ });+ assert.ok(!result.success);+ });++ it("requires type field", () => {+ const result = ConfigFieldSchema.safeParse({+ default: "hello",+ });+ assert.ok(!result.success);+ });++ it("accepts config without default", () => {+ const result = ConfigFieldSchema.safeParse({+ type: "string",+ description: "No default",+ });+ assert.ok(result.success);+ });+ });++ describe("PluginManifestSchema configSchema", () => {+ it("accepts manifest with configSchema", () => {+ const result = PluginManifestSchema.safeParse({+ ...validManifest,+ configSchema: {+ bannerText: {+ type: "string",+ default: "Welcome!",+ description: "Banner text",+ },+ enabled: {+ type: "boolean",+ default: true,+ description: "Enable banner",+ },+ },+ });+ assert.ok(result.success);+ });++ it("accepts manifest without configSchema", () => {+ const result = PluginManifestSchema.safeParse(validManifest);+ assert.ok(result.success);+ });++ it("applyDefaults fills configSchema defaults", () => {+ const manifest = {+ ...validManifest,+ configSchema: {+ greeting: { type: "string" as const, default: "Hello" },+ },+ };+ const result = applyDefaults(manifest);+ assert.deepEqual(result.configSchema, manifest.configSchema);+ });+ });++ describe("ManifestSkillSchema", () => {+ it("accepts valid skill definition", () => {+ const result = PluginManifestSchema.safeParse({+ ...validManifest,+ skills: [+ {+ name: "test-skill",+ description: "A test skill",+ input: { type: "object" },+ output: { type: "object" },+ },+ ],+ });+ assert.ok(result.success);+ });++ it("accepts manifest without skills", () => {+ const result = PluginManifestSchema.safeParse(validManifest);+ assert.ok(result.success);+ });+ });+});diff --git a/tests/unit/plugins-db.test.ts b/tests/unit/plugins-db.test.tsnew file mode 100644index 0000000000..6e20c8a11a--- /dev/null+++ b/tests/unit/plugins-db.test.ts@@ -0,0 +1,120 @@+import { describe, it, beforeEach } from "node:test";+import assert from "node:assert/strict";++const mod = await import("../../src/lib/db/plugins.ts");+const { getDbInstance } = await import("../../src/lib/db/core.ts");++const makeInput = (overrides = {}) => ({+ id: `plugin-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`,+ name: `test-plugin-${Date.now()}`,+ version: "1.0.0",+ main: "index.js",+ manifest: { name: "test", version: "1.0.0" },+ pluginDir: "/tmp/test-plugin",+ ...overrides,+});++describe("plugins DB module", () => {+ beforeEach(() => {+ // The `plugins` table is created by migration 076 (run on getDbInstance);+ // rely on the real migration rather than creating the table inline, so a+ // missing/renumbered migration fails here instead of being masked.+ const db = getDbInstance();+ db.exec("DELETE FROM plugins");+ });++ describe("insertPlugin / getPluginByName / listPlugins", () => {+ it("inserts and retrieves a plugin", () => {+ const input = makeInput();+ mod.insertPlugin(input);+ const found = mod.getPluginByName(input.name);+ assert.ok(found);+ assert.equal(found!.name, input.name);+ assert.equal(found!.version, "1.0.0");+ assert.equal(found!.status, "installed");+ });++ it("lists all plugins", () => {+ mod.insertPlugin(makeInput());+ const all = mod.listPlugins();+ assert.ok(all.length >= 1);+ });++ it("lists plugins filtered by status", () => {+ const input = makeInput({ name: `filter-${Date.now()}` });+ mod.insertPlugin(input);+ const installed = mod.listPlugins("installed");+ assert.ok(installed.length >= 1);+ assert.ok(installed.every((p) => p.status === "installed"));+ });++ it("returns null for unknown plugin", () => {+ assert.equal(mod.getPluginByName("nonexistent-xyz"), null);+ });+ });++ describe("getPluginById", () => {+ it("retrieves by id", () => {+ const input = makeInput({ name: `byid-${Date.now()}` });+ mod.insertPlugin(input);+ const found = mod.getPluginById(input.id);+ assert.ok(found);+ assert.equal(found!.id, input.id);+ });+ });++ describe("updatePluginStatus", () => {+ it("updates status to active", () => {+ const input = makeInput({ name: `status-${Date.now()}` });+ mod.insertPlugin(input);+ mod.updatePluginStatus(input.name, "active");+ const found = mod.getPluginByName(input.name);+ assert.equal(found!.status, "active");+ });++ it("updates status to error with message", () => {+ const input = makeInput({ name: `error-${Date.now()}` });+ mod.insertPlugin(input);+ mod.updatePluginStatus(input.name, "error", "broke");+ const found = mod.getPluginByName(input.name);+ assert.equal(found!.status, "error");+ assert.equal(found!.errorMessage, "broke");+ });+ });++ describe("updatePluginConfig", () => {+ it("updates config JSON", () => {+ const input = makeInput({ name: `config-${Date.now()}` });+ mod.insertPlugin(input);+ mod.updatePluginConfig(input.name, { key: "value" });+ const found = mod.getPluginByName(input.name);+ const config = JSON.parse(found!.config);+ assert.equal(config.key, "value");+ });+ });++ describe("deletePlugin", () => {+ it("removes plugin by name", () => {+ const input = makeInput({ name: `del-${Date.now()}` });+ mod.insertPlugin(input);+ assert.equal(mod.deletePlugin(input.name), true);+ assert.equal(mod.getPluginByName(input.name), null);+ });++ it("returns false for unknown plugin", () => {+ assert.equal(mod.deletePlugin("nonexistent"), false);+ });+ });++ describe("pluginExists", () => {+ it("returns true for existing plugin", () => {+ const input = makeInput({ name: `exists-${Date.now()}` });+ mod.insertPlugin(input);+ assert.equal(mod.pluginExists(input.name), true);+ });++ it("returns false for unknown plugin", () => {+ assert.equal(mod.pluginExists("nonexistent"), false);+ });+ });+});diff --git a/tests/unit/plugins-index.test.ts b/tests/unit/plugins-index.test.tsnew file mode 100644index 0000000000..f251393972--- /dev/null+++ b/tests/unit/plugins-index.test.ts@@ -0,0 +1,228 @@+import { describe, it, beforeEach } from "node:test";+import assert from "node:assert/strict";++import {+ registerPlugin,+ unregisterPlugin,+ setPluginEnabled,+ listPlugins,+ runOnRequest,+ runOnResponse,+ runOnError,+ resetPlugins,+} from "../../src/lib/plugins/index.ts";++beforeEach(() => {+ resetPlugins();+});++const makeCtx = () => ({+ requestId: `req-${Date.now()}`,+ body: { model: "gpt-4", messages: [] },+ model: "gpt-4",+ provider: "openai",+ metadata: {},+});++describe("registerPlugin", () => {+ it("registers plugin with defaults", () => {+ registerPlugin({ name: "test", onRequest: () => {} });+ const list = listPlugins();+ assert.equal(list.length, 1);+ assert.equal(list[0].name, "test");+ assert.equal(list[0].priority, 100);+ assert.equal(list[0].enabled, true);+ assert.ok(list[0].hooks.includes("onRequest"));+ });++ it("sorts by priority", () => {+ registerPlugin({ name: "low", priority: 200, onRequest: () => {} });+ registerPlugin({ name: "high", priority: 10, onRequest: () => {} });+ registerPlugin({ name: "mid", priority: 100, onRequest: () => {} });+ const list = listPlugins();+ assert.equal(list[0].name, "high");+ assert.equal(list[1].name, "mid");+ assert.equal(list[2].name, "low");+ });++ it("re-registers plugin with same name", () => {+ registerPlugin({ name: "p1", onRequest: () => {} });+ registerPlugin({ name: "p1", onResponse: () => {} });+ const list = listPlugins();+ assert.equal(list.length, 1);+ assert.ok(list[0].hooks.includes("onResponse"));+ });++ it("lists hooks correctly", () => {+ registerPlugin({+ name: "multi",+ onRequest: () => {},+ onResponse: () => {},+ onError: () => {},+ });+ const list = listPlugins();+ assert.deepEqual(list[0].hooks.sort(), ["onError", "onRequest", "onResponse"]);+ });+});++describe("unregisterPlugin", () => {+ it("removes plugin by name", () => {+ registerPlugin({ name: "p1", onRequest: () => {} });+ assert.equal(unregisterPlugin("p1"), true);+ assert.equal(listPlugins().length, 0);+ });++ it("returns false for unknown plugin", () => {+ assert.equal(unregisterPlugin("unknown"), false);+ });+});++describe("setPluginEnabled", () => {+ it("enables/disables plugin", () => {+ registerPlugin({ name: "p1", onRequest: () => {} });+ assert.equal(setPluginEnabled("p1", false), true);+ assert.equal(listPlugins()[0].enabled, false);+ assert.equal(setPluginEnabled("p1", true), true);+ assert.equal(listPlugins()[0].enabled, true);+ });++ it("returns false for unknown plugin", () => {+ assert.equal(setPluginEnabled("unknown", false), false);+ });+});++describe("listPlugins", () => {+ it("returns empty array when no plugins", () => {+ assert.deepEqual(listPlugins(), []);+ });+});++describe("runOnRequest", () => {+ it("returns not blocked when no plugins", async () => {+ const result = await runOnRequest(makeCtx());+ assert.equal(result.blocked, false);+ });++ it("returns not blocked when plugin returns void", async () => {+ registerPlugin({ name: "p1", onRequest: () => {} });+ const result = await runOnRequest(makeCtx());+ assert.equal(result.blocked, false);+ });++ it("blocks request when plugin returns blocked", async () => {+ registerPlugin({+ name: "blocker",+ onRequest: () => ({ blocked: true, response: { error: "denied" } }),+ });+ const result = await runOnRequest(makeCtx());+ assert.equal(result.blocked, true);+ assert.deepEqual(result.response, { error: "denied" });+ });++ it("chains body/metadata through plugins", async () => {+ registerPlugin({+ name: "p1",+ priority: 10,+ onRequest: (ctx) => ({ body: { ...ctx.body, added: true }, metadata: { p1: true } }),+ });+ registerPlugin({+ name: "p2",+ priority: 20,+ onRequest: (ctx) => ({ metadata: { ...ctx.metadata, p2: true } }),+ });+ const result = await runOnRequest(makeCtx());+ assert.equal(result.blocked, false);+ assert.equal((result.ctx.body as any).added, true);+ assert.deepEqual(result.ctx.metadata, { p1: true, p2: true });+ });++ it("skips disabled plugins", async () => {+ registerPlugin({ name: "p1", enabled: false, onRequest: () => ({ blocked: true }) });+ const result = await runOnRequest(makeCtx());+ assert.equal(result.blocked, false);+ });++ it("swallows plugin errors", async () => {+ registerPlugin({ name: "p1", onRequest: () => { throw new Error("boom"); } });+ const result = await runOnRequest(makeCtx());+ assert.equal(result.blocked, false);+ });+});++describe("runOnResponse", () => {+ it("returns original response when no plugins", async () => {+ const resp = { choices: [{ message: { content: "hi" } }] };+ const result = await runOnResponse(makeCtx(), resp);+ assert.deepEqual(result, resp);+ });++ it("chains response through plugins", async () => {+ registerPlugin({+ name: "p1",+ onResponse: (_ctx, resp: any) => ({ ...resp, p1: true }),+ });+ registerPlugin({+ name: "p2",+ onResponse: (_ctx, resp: any) => ({ ...resp, p2: true }),+ });+ const result = await runOnResponse(makeCtx(), { original: true });+ assert.deepEqual(result, { original: true, p1: true, p2: true });+ });++ it("skips disabled plugins", async () => {+ registerPlugin({+ name: "p1",+ enabled: false,+ onResponse: () => ({ modified: true }),+ });+ const result = await runOnResponse(makeCtx(), { original: true });+ assert.deepEqual(result, { original: true });+ });++ it("swallows plugin errors", async () => {+ registerPlugin({ name: "p1", onResponse: () => { throw new Error("boom"); } });+ const result = await runOnResponse(makeCtx(), { original: true });+ assert.deepEqual(result, { original: true });+ });+});++describe("runOnError", () => {+ it("returns null when no plugins handle error", async () => {+ const result = await runOnError(makeCtx(), new Error("test"));+ assert.equal(result, null);+ });++ it("returns recovery when plugin handles error", async () => {+ registerPlugin({+ name: "recover",+ onError: () => ({ recovered: true }),+ });+ const result = await runOnError(makeCtx(), new Error("test"));+ assert.deepEqual(result, { recovered: true });+ });++ it("skips disabled plugins", async () => {+ registerPlugin({+ name: "p1",+ enabled: false,+ onError: () => ({ recovered: true }),+ });+ const result = await runOnError(makeCtx(), new Error("test"));+ assert.equal(result, null);+ });++ it("swallows plugin errors", async () => {+ registerPlugin({ name: "p1", onError: () => { throw new Error("boom"); } });+ const result = await runOnError(makeCtx(), new Error("test"));+ assert.equal(result, null);+ });+});++describe("resetPlugins", () => {+ it("clears all plugins", () => {+ registerPlugin({ name: "p1", onRequest: () => {} });+ registerPlugin({ name: "p2", onResponse: () => {} });+ resetPlugins();+ assert.equal(listPlugins().length, 0);+ });+});diff --git a/tests/unit/plugins-loader-ipc.test.ts b/tests/unit/plugins-loader-ipc.test.tsnew file mode 100644index 0000000000..b1c69a2add--- /dev/null+++ b/tests/unit/plugins-loader-ipc.test.ts@@ -0,0 +1,62 @@+import { describe, it } from "node:test";+import assert from "node:assert/strict";++import { loadPlugin } from "../../src/lib/plugins/loader.ts";+import type { PluginManifestWithDefaults } from "../../src/lib/plugins/manifest.ts";++function makeManifest(overrides?: Partial<PluginManifestWithDefaults>): PluginManifestWithDefaults {+ return {+ name: "test-plugin",+ version: "1.0.0",+ description: "Test",+ hooks: { onRequest: true, onResponse: false, onError: false },+ requires: { permissions: [] },+ enabledByDefault: true,+ source: "local",+ ...overrides,+ };+}++describe("Plugin loader IPC", () => {+ it("loadPlugin returns LoadedPlugin with expected shape", async () => {+ // loadPlugin spawns a child process — we test it returns the right shape+ // but we can't easily test IPC without a real plugin file.+ // Instead, test the function signature and error handling.+ assert.equal(typeof loadPlugin, "function");+ });++ it("loader exports LoadedPlugin interface", async () => {+ // Verify the module exports the expected function+ const mod = await import("../../src/lib/plugins/loader.ts");+ assert.equal(typeof mod.loadPlugin, "function");+ });++ it("loadPlugin rejects invalid entry point gracefully", async () => {+ const manifest = makeManifest();+ try {+ const loaded = await loadPlugin("/nonexistent/path/plugin.mjs", manifest);+ // If it doesn't throw, it should still return a valid object+ assert.ok(loaded.name);+ assert.ok(loaded.cleanup);+ loaded.cleanup();+ } catch (err) {+ // Expected — nonexistent path should cause an error+ assert.ok(err instanceof Error);+ }+ });++ it("manifest permissions affect env filtering", () => {+ const manifest = makeManifest({ requires: { permissions: ["env"] } });+ assert.deepEqual(manifest.requires.permissions, ["env"]);++ const manifestNoPerms = makeManifest({ requires: { permissions: [] } });+ assert.deepEqual(manifestNoPerms.requires.permissions, []);+ });++ it("manifest with all permissions", () => {+ const manifest = makeManifest({+ requires: { permissions: ["network", "file-read", "file-write", "env", "exec"] },+ });+ assert.equal(manifest.requires.permissions.length, 5);+ });+});diff --git a/tests/unit/plugins-manager-lifecycle.test.ts b/tests/unit/plugins-manager-lifecycle.test.tsnew file mode 100644index 0000000000..66ad9b623a--- /dev/null+++ b/tests/unit/plugins-manager-lifecycle.test.ts@@ -0,0 +1,129 @@+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);+ });+ });+});diff --git a/tests/unit/plugins-manifest.test.ts b/tests/unit/plugins-manifest.test.tsnew file mode 100644index 0000000000..c1913327aa--- /dev/null+++ b/tests/unit/plugins-manifest.test.ts@@ -0,0 +1,175 @@+import { describe, it } from "node:test";+import assert from "node:assert/strict";++const mod = await import("../../src/lib/plugins/manifest.ts");++const validMinimal = { name: "my-plugin", version: "1.0.0" };+const validFull = {+ name: "full-plugin",+ version: "2.1.0",+ description: "A full plugin",+ author: "test",+ license: "Apache-2.0",+ main: "handler.js",+ source: "local" as const,+ tags: ["test", "demo"],+ requires: { omniroute: ">=3.0.0", permissions: ["network", "file-read"] as const },+ hooks: { onRequest: true, onResponse: true, onError: false },+ skills: [{ name: "my-skill", description: "does things", input: { q: "string" } }],+ enabledByDefault: true,+ configSchema: { apiKey: { type: "string" as const, default: "abc", description: "API key" } },+};++describe("PluginManifestSchema", () => {+ it("accepts valid minimal manifest", () => {+ const result = mod.PluginManifestSchema.safeParse(validMinimal);+ assert.equal(result.success, true);+ });++ it("accepts valid full manifest", () => {+ const result = mod.PluginManifestSchema.safeParse(validFull);+ assert.equal(result.success, true);+ });++ it("rejects non-kebab-case name", () => {+ const result = mod.PluginManifestSchema.safeParse({ name: "BAD NAME!", version: "1.0.0" });+ assert.equal(result.success, false);+ });++ it("rejects invalid semver", () => {+ const result = mod.PluginManifestSchema.safeParse({ name: "ok-name", version: "nope" });+ assert.equal(result.success, false);+ });++ it("rejects name > 100 chars", () => {+ const result = mod.PluginManifestSchema.safeParse({ name: "a".repeat(101), version: "1.0.0" });+ assert.equal(result.success, false);+ });++ it("rejects description > 500 chars", () => {+ const result = mod.PluginManifestSchema.safeParse({+ name: "ok", version: "1.0.0", description: "x".repeat(501),+ });+ assert.equal(result.success, false);+ });+});++describe("safeValidateManifest", () => {+ it("returns success for valid manifest", () => {+ const result = mod.safeValidateManifest(validMinimal);+ assert.equal(result.success, true);+ if (result.success) assert.equal(result.data.name, "my-plugin");+ });++ it("returns errors for invalid manifest", () => {+ const result = mod.safeValidateManifest({ name: "NOPE!", version: "bad" });+ assert.equal(result.success, false);+ if (!result.success) {+ assert.ok(Array.isArray(result.errors));+ assert.ok(result.errors.length > 0);+ }+ });+});++describe("applyDefaults", () => {+ it("fills all optional fields with defaults", () => {+ const parsed = mod.PluginManifestSchema.parse(validMinimal);+ const result = mod.applyDefaults(parsed);+ assert.equal(result.license, "MIT");+ assert.equal(result.main, "index.js");+ assert.equal(result.source, "local");+ assert.deepEqual(result.tags, []);+ assert.deepEqual(result.requires.permissions, []);+ assert.equal(result.hooks.onRequest, false);+ assert.equal(result.hooks.onResponse, false);+ assert.equal(result.hooks.onError, false);+ assert.deepEqual(result.skills, []);+ assert.equal(result.enabledByDefault, false);+ assert.deepEqual(result.configSchema, {});+ });++ it("preserves explicit values", () => {+ const parsed = mod.PluginManifestSchema.parse(validFull);+ const result = mod.applyDefaults(parsed);+ assert.equal(result.license, "Apache-2.0");+ assert.equal(result.main, "handler.js");+ assert.equal(result.enabledByDefault, true);+ });+});++describe("PermissionSchema", () => {+ it("accepts all valid permissions", () => {+ for (const p of ["network", "file-read", "file-write", "env", "exec"]) {+ assert.equal(mod.PermissionSchema.safeParse(p).success, true, `should accept "${p}"`);+ }+ });++ it("rejects invalid permission", () => {+ assert.equal(mod.PermissionSchema.safeParse("admin").success, false);+ });+});++describe("ConfigFieldSchema", () => {+ it("accepts string type", () => {+ assert.equal(mod.ConfigFieldSchema.safeParse({ type: "string", default: "hi" }).success, true);+ });++ it("accepts number with min/max", () => {+ assert.equal(mod.ConfigFieldSchema.safeParse({ type: "number", min: 0, max: 100 }).success, true);+ });++ it("accepts boolean type", () => {+ assert.equal(mod.ConfigFieldSchema.safeParse({ type: "boolean", default: true }).success, true);+ });++ it("accepts select with enum", () => {+ assert.equal(mod.ConfigFieldSchema.safeParse({ type: "select", enum: ["a", "b"] }).success, true);+ });++ it("rejects invalid type", () => {+ assert.equal(mod.ConfigFieldSchema.safeParse({ type: "invalid" }).success, false);+ });+});++describe("HooksSchema", () => {+ it("accepts all boolean flags", () => {+ assert.equal(mod.HooksSchema.safeParse({ onRequest: true, onResponse: false, onError: true }).success, true);+ });++ it("accepts empty hooks", () => {+ assert.equal(mod.HooksSchema.safeParse({}).success, true);+ });++ it("rejects non-boolean value", () => {+ assert.equal(mod.HooksSchema.safeParse({ onRequest: "yes" }).success, false);+ });+});++describe("ManifestSkillSchema", () => {+ it("accepts minimal skill", () => {+ assert.equal(mod.ManifestSkillSchema.safeParse({ name: "test" }).success, true);+ });++ it("accepts full skill", () => {+ assert.equal(mod.ManifestSkillSchema.safeParse({+ name: "test", description: "desc", input: { q: "string" }, output: { result: "string" },+ }).success, true);+ });++ it("rejects empty name", () => {+ assert.equal(mod.ManifestSkillSchema.safeParse({ name: "" }).success, false);+ });+});++describe("validateManifest", () => {+ it("returns parsed manifest with defaults", () => {+ const result = mod.validateManifest(validMinimal);+ assert.equal(result.name, "my-plugin");+ assert.equal(result.version, "1.0.0");+ assert.equal(result.license, "MIT");+ });++ it("throws on invalid input", () => {+ assert.throws(() => mod.validateManifest({ name: "NOPE!", version: "bad" }));+ });+});diff --git a/tests/unit/plugins-permissions.test.ts b/tests/unit/plugins-permissions.test.tsnew file mode 100644index 0000000000..c780951359--- /dev/null+++ b/tests/unit/plugins-permissions.test.ts@@ -0,0 +1,63 @@+import { describe, it } from "node:test";+import assert from "node:assert/strict";++import {+ PermissionSchema,+ safeValidateManifest,+} from "../../src/lib/plugins/manifest.ts";++describe("Plugin permission enforcement", () => {+ describe("PermissionSchema", () => {+ it("accepts valid permission values", () => {+ const valid = ["network", "file-read", "file-write", "env", "exec"];+ for (const perm of valid) {+ const result = PermissionSchema.safeParse(perm);+ assert.ok(result.success, `should accept "${perm}"`);+ }+ });++ it("rejects invalid permission values", () => {+ const invalid = ["admin", "root", "shell", "database", ""];+ for (const perm of invalid) {+ const result = PermissionSchema.safeParse(perm);+ assert.ok(!result.success, `should reject "${perm}"`);+ }+ });++ it("rejects non-string permission values", () => {+ const invalid = [123, true, null, undefined, {}];+ for (const perm of invalid) {+ const result = PermissionSchema.safeParse(perm);+ assert.ok(!result.success, `should reject ${JSON.stringify(perm)}`);+ }+ });+ });++ describe("Manifest permission validation", () => {+ it("accepts manifest with valid permissions", () => {+ const result = safeValidateManifest({+ name: "test-plugin",+ version: "1.0.0",+ requires: { permissions: ["network", "env"] },+ });+ assert.ok(result.success, "should accept valid permissions");+ });++ it("accepts manifest with empty permissions", () => {+ const result = safeValidateManifest({+ name: "test-plugin",+ version: "1.0.0",+ requires: { permissions: [] },+ });+ assert.ok(result.success, "should accept empty permissions");+ });++ it("accepts manifest without requires field", () => {+ const result = safeValidateManifest({+ name: "test-plugin",+ version: "1.0.0",+ });+ assert.ok(result.success, "should accept manifest without requires");+ });+ });+});diff --git a/tests/unit/plugins-scanner.test.ts b/tests/unit/plugins-scanner.test.tsindex 2d9926f79d..eab34574f9 100644--- a/tests/unit/plugins-scanner.test.ts+++ b/tests/unit/plugins-scanner.test.ts@@ -1,107 +1,83 @@-import test from "node:test";+import { describe, it } from "node:test";import assert from "node:assert/strict";-import { mkdtemp, writeFile, mkdir, rm } from "fs/promises";-import { join } from "path";-import { tmpdir } from "os";+import { mkdtempSync, writeFileSync, mkdirSync, rmSync } from "node:fs";+import { join } from "node:path";+import { tmpdir } from "node:os";-import { scanPluginDir, getDefaultPluginDir } from "../../src/lib/plugins/scanner.ts";+const mod = await import("../../src/lib/plugins/scanner.ts");-let tmpDir: string;+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;+}-test.beforeEach(async () => {- tmpDir = await mkdtemp(join(tmpdir(), "plugin-scan-test-"));-});--test.afterEach(async () => {- await rm(tmpDir, { recursive: true, force: true });-});--// ── getDefaultPluginDir ──--test("getDefaultPluginDir returns ~/.omniroute/plugins", () => {- const dir = getDefaultPluginDir();- assert.ok(dir.endsWith(".omniroute/plugins"));-});--// ── scanPluginDir ──--test("returns empty for non-existent directory", async () => {- const result = await scanPluginDir("/nonexistent/path");- assert.deepEqual(result.plugins, []);- assert.deepEqual(result.errors, []);-});--test("returns empty for empty directory", async () => {- const result = await scanPluginDir(tmpDir);- assert.deepEqual(result.plugins, []);- assert.deepEqual(result.errors, []);-});+const validManifest = { name: "scan-test", version: "1.0.0" };-test("skips hidden directories", async () => {- await mkdir(join(tmpDir, ".hidden"));- await writeFile(- join(tmpDir, ".hidden", "plugin.json"),- JSON.stringify({ name: "hidden", version: "1.0.0" })- );- const result = await scanPluginDir(tmpDir);- assert.equal(result.plugins.length, 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"));+ });+ });-test("discovers valid plugin", async () => {- const pluginDir = join(tmpDir, "my-plugin");- await mkdir(pluginDir);- await writeFile(- join(pluginDir, "plugin.json"),- JSON.stringify({ name: "my-plugin", version: "1.0.0" })- );- await writeFile(join(pluginDir, "index.js"), "module.exports = {};");- const result = await scanPluginDir(tmpDir);- assert.equal(result.plugins.length, 1);- assert.equal(result.plugins[0].name, "my-plugin");- assert.equal(result.plugins[0].manifest.version, "1.0.0");-});+ 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 });+ }+ });-test("reports error for missing plugin.json", async () => {- await mkdir(join(tmpDir, "no-manifest"));- const result = await scanPluginDir(tmpDir);- assert.equal(result.plugins.length, 0);- assert.equal(result.errors.length, 1);- assert.ok(result.errors[0].error.includes("no plugin.json"));-});+ 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 });+ }+ });-test("reports error for invalid manifest", async () => {- const pluginDir = join(tmpDir, "bad-manifest");- await mkdir(pluginDir);- await writeFile(- join(pluginDir, "plugin.json"),- JSON.stringify({ name: "BAD NAME!", version: "nope" })- );- const result = await scanPluginDir(tmpDir);- assert.equal(result.plugins.length, 0);- assert.equal(result.errors.length, 1);- assert.ok(result.errors[0].error.includes("invalid manifest"));-});+ 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 });+ }+ });-test("reports error for missing entry point", async () => {- const pluginDir = join(tmpDir, "no-entry");- await mkdir(pluginDir);- await writeFile(- join(pluginDir, "plugin.json"),- JSON.stringify({ name: "no-entry", version: "1.0.0", main: "missing.js" })- );- const result = await scanPluginDir(tmpDir);- assert.equal(result.plugins.length, 0);- assert.equal(result.errors.length, 1);- assert.ok(result.errors[0].error.includes("entry point not found"));-});+ it("handles non-existent directory", async () => {+ const result = await mod.scanPluginDir("/nonexistent/path");+ assert.equal(result.plugins.length, 0);+ });-test("discovers multiple plugins", async () => {- for (const name of ["plugin-a", "plugin-b"]) {- const d = join(tmpDir, name);- await mkdir(d);- await writeFile(join(d, "plugin.json"), JSON.stringify({ name, version: "1.0.0" }));- await writeFile(join(d, "index.js"), "module.exports = {};");- }- const result = await scanPluginDir(tmpDir);- assert.equal(result.plugins.length, 2);+ 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 });+ }+ });+ });});diff --git a/tests/unit/plugins-welcome-banner-e2e.test.ts b/tests/unit/plugins-welcome-banner-e2e.test.tsnew file mode 100644index 0000000000..6e510e9538--- /dev/null+++ b/tests/unit/plugins-welcome-banner-e2e.test.ts@@ -0,0 +1,377 @@+/**+ * E2E test for the plugin system using the welcome-banner PoC plugin.+ *+ * Verifies the full plugin lifecycle:+ * manifest validation → install → activate → hook execution → deactivate → uninstall+ */++import test from "node:test";+import assert from "node:assert/strict";+import { mkdirSync, writeFileSync, rmSync, existsSync } from "node:fs";+import { join } from "node:path";+import { tmpdir } from "node:os";++const FIXTURE_DIR = join(tmpdir(), `omniroute_plugin_test_${Date.now()}`);++function createFixturePlugin(name: string, opts?: { onResponse?: boolean; onRequest?: boolean }) {+ const dir = join(FIXTURE_DIR, name);+ mkdirSync(dir, { recursive: true });+ writeFileSync(+ join(dir, "plugin.json"),+ JSON.stringify({+ name,+ version: "1.0.0",+ description: `Test plugin ${name}`,+ hooks: { onResponse: opts?.onResponse ?? true, onRequest: opts?.onRequest ?? false },+ requires: { permissions: [] },+ enabledByDefault: true,+ })+ );+ writeFileSync(+ join(dir, "index.mjs"),+ `export const plugin = {+ name: "${name}",+ priority: 100,+ async onResponse(ctx, response) {+ if (response && typeof response === "object" && response.choices) {+ for (const c of response.choices) {+ if (c.message) c.message.content = "[${name}] " + (c.message.content || "");+ }+ }+ return response;+ },+ async onRequest(ctx) {+ return { metadata: { ...ctx.metadata, pluginName: "${name}" } };+ },+};`+ );+ return dir;+}++// ── Manifest validation ──++test("plugin manifest validation", async (t) => {+ const { validateManifest, safeValidateManifest, applyDefaults } = await import(+ "../../src/lib/plugins/manifest.ts"+ );++ await t.test("valid manifest parses with defaults", () => {+ const result = validateManifest({+ name: "test-plugin",+ version: "1.0.0",+ });+ assert.equal(result.name, "test-plugin");+ assert.equal(result.version, "1.0.0");+ assert.equal(result.license, "MIT");+ assert.equal(result.main, "index.js");+ assert.equal(result.source, "local");+ assert.deepEqual(result.tags, []);+ assert.deepEqual(result.requires.permissions, []);+ assert.equal(result.hooks.onRequest, false);+ assert.equal(result.hooks.onResponse, false);+ assert.equal(result.hooks.onError, false);+ assert.equal(result.enabledByDefault, false);+ });++ await t.test("invalid name rejected", () => {+ const result = safeValidateManifest({ name: "INVALID NAME!", version: "1.0.0" });+ assert.equal(result.success, false);+ if (!result.success) {+ assert.ok(result.errors.length > 0);+ }+ });++ await t.test("invalid version rejected", () => {+ const result = safeValidateManifest({ name: "test", version: "bad" });+ assert.equal(result.success, false);+ });++ await t.test("explicit values preserved over defaults", () => {+ const result = validateManifest({+ name: "custom",+ version: "2.0.0",+ license: "Apache-2.0",+ main: "custom.mjs",+ source: "marketplace",+ tags: ["ai"],+ enabledByDefault: true,+ hooks: { onRequest: true, onResponse: true, onError: true },+ requires: { permissions: ["network", "exec"] },+ });+ assert.equal(result.license, "Apache-2.0");+ assert.equal(result.main, "custom.mjs");+ assert.equal(result.source, "marketplace");+ assert.deepEqual(result.tags, ["ai"]);+ assert.equal(result.enabledByDefault, true);+ assert.equal(result.hooks.onRequest, true);+ assert.deepEqual(result.requires.permissions, ["network", "exec"]);+ });++ await t.test("safeValidateManifest returns success for valid", () => {+ const result = safeValidateManifest({ name: "ok", version: "1.0.0" });+ assert.equal(result.success, true);+ });+});++// ── Hooks system ──++test("plugin hooks system", async (t) => {+ const {+ registerHook,+ unregisterHooks,+ unregisterHook,+ emitHook,+ emitHookBlocking,+ runOnRequest,+ runOnResponse,+ runOnError,+ getHooks,+ getActiveEvents,+ resetHooks,+ BUILTIN_EVENTS,+ } = await import("../../src/lib/plugins/hooks.ts");++ // Reset before each sub-test group+ resetHooks();++ await t.test("registerHook registers and sorts by priority", () => {+ const calls: string[] = [];+ registerHook("onRequest", "plugin-b", () => { calls.push("b"); }, 200);+ registerHook("onRequest", "plugin-a", () => { calls.push("a"); }, 100);+ const hooks = getHooks("onRequest");+ assert.equal(hooks.length, 2);+ assert.equal(hooks[0].pluginName, "plugin-a");+ assert.equal(hooks[1].pluginName, "plugin-b");+ resetHooks();+ });++ await t.test("registerHook prevents duplicates", () => {+ const handler = () => {};+ registerHook("onResponse", "dup-test", handler, 100);+ registerHook("onResponse", "dup-test", handler, 100);+ assert.equal(getHooks("onResponse").length, 1);+ resetHooks();+ });++ await t.test("unregisterHooks removes all for plugin", () => {+ registerHook("onRequest", "rm-test", () => {}, 100);+ registerHook("onResponse", "rm-test", () => {}, 100);+ registerHook("onRequest", "keep-test", () => {}, 100);+ unregisterHooks("rm-test");+ assert.equal(getHooks("onRequest").length, 1);+ assert.equal(getHooks("onResponse").length, 0);+ resetHooks();+ });++ await t.test("unregisterHook removes specific event", () => {+ registerHook("onRequest", "spec-test", () => {}, 100);+ registerHook("onResponse", "spec-test", () => {}, 100);+ unregisterHook("onRequest", "spec-test");+ assert.equal(getHooks("onRequest").length, 0);+ assert.equal(getHooks("onResponse").length, 1);+ resetHooks();+ });++ await t.test("emitHook calls all handlers in order", async () => {+ const order: number[] = [];+ registerHook("onTest", "h1", () => { order.push(1); }, 100);+ registerHook("onTest", "h2", () => { order.push(2); }, 200);+ registerHook("onTest", "h3", () => { order.push(3); }, 150);+ await emitHook("onTest", {});+ assert.deepEqual(order, [1, 3, 2]);+ resetHooks();+ });++ await t.test("emitHook swallows handler errors", async () => {+ const calls: string[] = [];+ registerHook("onErr", "bad", () => { throw new Error("boom"); }, 100);+ registerHook("onErr", "good", () => { calls.push("ok"); }, 200);+ await emitHook("onErr", {});+ assert.deepEqual(calls, ["ok"]);+ resetHooks();+ });++ await t.test("emitHookBlocking chains body/metadata", async () => {+ registerHook("onBlock", "a", () => ({ body: { a: 1 }, metadata: { x: 1 } }), 100);+ registerHook("onBlock", "b", () => ({ body: { b: 2 }, metadata: { y: 2 } }), 200);+ const result = await emitHookBlocking("onBlock", { body: {}, metadata: {} });+ assert.deepEqual(result.body, { b: 2 });+ assert.deepEqual(result.metadata, { x: 1, y: 2 });+ resetHooks();+ });++ await t.test("emitHookBlocking returns early on blocked", async () => {+ const calls: string[] = [];+ registerHook("onBlock2", "blocker", () => ({ blocked: true, response: { error: "no" } }), 100);+ registerHook("onBlock2", "after", () => { calls.push("after"); }, 200);+ const result = await emitHookBlocking("onBlock2", {});+ assert.equal(result.blocked, true);+ assert.equal(calls.length, 0);+ resetHooks();+ });++ await t.test("runOnRequest delegates to emitHookBlocking", async () => {+ registerHook("onRequest", "req", () => ({ metadata: { seen: true } }), 100);+ const result = await runOnRequest({ requestId: "1", body: {}, model: "gpt-4", provider: "openai", metadata: {} });+ assert.deepEqual(result.metadata, { seen: true });+ resetHooks();+ });++ await t.test("runOnResponse chains response through plugins", async () => {+ registerHook("onResponse", "r1", (_ctx: unknown) => ({ response: { modified: 1 } }), 100);+ registerHook("onResponse", "r2", (_ctx: unknown) => ({ response: { modified: 2 } }), 200);+ const result = await runOnResponse(+ { requestId: "1", body: {}, model: "gpt-4", provider: "openai", metadata: {} },+ { original: true }+ );+ assert.deepEqual(result, { modified: 2 });+ resetHooks();+ });++ await t.test("runOnError is fire-and-forget", async () => {+ let called = false;+ registerHook("onError", "err-handler", () => { called = true; }, 100);+ await runOnError(+ { requestId: "1", body: {}, model: "gpt-4", provider: "openai", metadata: {} },+ new Error("test")+ );+ assert.equal(called, true);+ resetHooks();+ });++ await t.test("getActiveEvents returns events with handlers", () => {+ registerHook("onRequest", "ae-test", () => {}, 100);+ registerHook("onError", "ae-test", () => {}, 100);+ const events = getActiveEvents();+ assert.ok(events.includes("onRequest"));+ assert.ok(events.includes("onError"));+ resetHooks();+ });++ await t.test("BUILTIN_EVENTS has all 10 events", () => {+ assert.equal(BUILTIN_EVENTS.length, 10);+ assert.ok(BUILTIN_EVENTS.includes("onRequest"));+ assert.ok(BUILTIN_EVENTS.includes("onResponse"));+ assert.ok(BUILTIN_EVENTS.includes("onError"));+ assert.ok(BUILTIN_EVENTS.includes("onModelSelect"));+ assert.ok(BUILTIN_EVENTS.includes("onComboResolve"));+ assert.ok(BUILTIN_EVENTS.includes("onRateLimit"));+ assert.ok(BUILTIN_EVENTS.includes("onQuotaExhaust"));+ assert.ok(BUILTIN_EVENTS.includes("onProviderError"));+ assert.ok(BUILTIN_EVENTS.includes("onStreamStart"));+ assert.ok(BUILTIN_EVENTS.includes("onStreamEnd"));+ resetHooks();+ });+});++// ── Welcome banner PoC E2E ──++const POC_PLUGIN_DIR = join(process.cwd(), "tests", "fixtures", "welcome-banner-plugin");++test("welcome banner PoC plugin lifecycle", async (t) => {+ const pluginDir = POC_PLUGIN_DIR;++ await t.test("manifest validates", async () => {+ const { validateManifest } = await import("../../src/lib/plugins/manifest.ts");+ const manifestPath = join(pluginDir, "plugin.json");+ const { readFileSync } = await import("node:fs");+ const manifest = JSON.parse(readFileSync(manifestPath, "utf-8"));+ const result = validateManifest(manifest);+ assert.equal(result.name, "welcome-banner");+ assert.equal(result.hooks.onResponse, true);+ });++ await t.test("plugin module exports correct interface", async () => {+ const mod = await import(join(pluginDir, "index.mjs"));+ assert.ok(mod.plugin, "should export plugin object");+ assert.equal(mod.plugin.name, "welcome-banner");+ assert.equal(typeof mod.plugin.onResponse, "function");+ });++ await t.test("onResponse injects banner into response", async () => {+ const mod = await import(join(pluginDir, "index.mjs"));+ const response = {+ choices: [+ { message: { role: "assistant", content: "Hello!" } },+ ],+ };+ const result = await mod.plugin.onResponse({}, response);+ assert.ok(result.choices[0].message.content.includes("[Welcome to OmniRoute"));+ assert.ok(result.choices[0].message.content.includes("Hello!"));+ });++ await t.test("onResponse handles streaming delta", async () => {+ const mod = await import(join(pluginDir, "index.mjs"));+ const response = {+ choices: [+ { delta: { content: "stream chunk" } },+ ],+ };+ const result = await mod.plugin.onResponse({}, response);+ assert.ok(result.choices[0].delta.content.includes("[Welcome to OmniRoute"));+ });++ await t.test("onResponse handles null response gracefully", async () => {+ const mod = await import(join(pluginDir, "index.mjs"));+ const result = await mod.plugin.onResponse({}, null);+ assert.equal(result, null);+ });+});++// ── Full lifecycle through pluginManager ──++test("full lifecycle: install → activate → hook fires → deactivate → uninstall", async (t) => {+ const { pluginManager } = await import("../../src/lib/plugins/manager.ts");+ const { runOnRequest, resetHooks, getHooks } = await import("../../src/lib/plugins/hooks.ts");++ const POC_DIR = join(process.cwd(), "tests", "fixtures", "welcome-banner-plugin");++ await t.test("install plugin from fixture dir", async () => {+ const row = await pluginManager.install(POC_DIR);+ assert.ok(row, "install should return plugin row");+ assert.equal(row.name, "welcome-banner");+ assert.ok(row.pluginDir, "should have pluginDir");+ });++ await t.test("activate plugin registers hooks", async () => {+ await pluginManager.activate("welcome-banner");+ const loaded = pluginManager.getLoaded("welcome-banner");+ assert.ok(loaded, "plugin should be loaded after activate");+ });++ await t.test("onRequest hook fires and injects banner", async () => {+ const ctx = {+ requestId: "e2e-test",+ body: {},+ model: "gpt-4",+ provider: "openai",+ metadata: {},+ };+ const result = await runOnRequest(ctx);+ // The welcome-banner plugin injects banner into metadata+ assert.ok(result.metadata?.banner || result.body, "hook should modify request");+ });++ await t.test("deactivate removes hooks", async () => {+ await pluginManager.deactivate("welcome-banner");+ const loaded = pluginManager.getLoaded("welcome-banner");+ // After deactivation, plugin should not be loaded+ assert.ok(!loaded, "plugin should not be loaded after deactivate");+ });++ await t.test("uninstall removes from DB", async () => {+ await pluginManager.uninstall("welcome-banner");+ const all = await pluginManager.listAll();+ const found = all.find((p: any) => p.name === "welcome-banner");+ assert.ok(!found, "plugin should not be in list after uninstall");+ });+});++// ── Cleanup ──++test("cleanup fixture directory", () => {+ if (existsSync(FIXTURE_DIR)) {+ rmSync(FIXTURE_DIR, { recursive: true, force: true });+ }+ assert.ok(!existsSync(FIXTURE_DIR));+});