import { describe, expect, test, vi } from "vitest"; import { bootstrapPluginHookFanout, isPluginHookFanoutRequired, PluginHookFanoutRequiredBootstrapError, } from "./plugin-hook-fanout-bootstrap.js"; import { PLUGIN_HOOK_FANOUT_OUTBOX_QUEUE } from "./plugin-hook-fanout.js"; type WorkerProcessor = (job: { data: unknown }) => Promise; type WorkerOptions = { concurrency?: number }; function validManifest(hooks: Array<{ name: string; export: string }>) { return { schemaVersion: 1 as const, id: "com.acme.audit", version: "1.0.0", name: "Audit", entry: "plugin.wasm", wasmSha256: "a".repeat(64), signature: "b".repeat(128), signedBy: "c".repeat(64), allowedHosts: [], capabilities: [], hooks, }; } function fakeWorker() { const close = vi.fn(async () => undefined); const handle = { name: "fake-plugin-hook-fanout", close }; const calls: Array<[string, WorkerProcessor, WorkerOptions]> = []; const createWorkerImpl = vi.fn((queueName: string, processor: WorkerProcessor, options: WorkerOptions) => { calls.push([queueName, processor, options]); return handle; }); return { close, handle, createWorkerImpl, calls }; } describe("bootstrapPluginHookFanout", () => { test("disables on non-general worker groups", () => { const { createWorkerImpl } = fakeWorker(); const result = bootstrapPluginHookFanout({ WORKER_QUEUE_GROUP: "plugin" }, { createWorkerImpl }); expect(result.status).toBe("disabled"); expect(createWorkerImpl).not.toHaveBeenCalled(); }); test("disables when explicitly turned off", () => { const { createWorkerImpl } = fakeWorker(); const result = bootstrapPluginHookFanout({ WORKER_QUEUE_GROUP: "general", PLUGIN_HOOK_FANOUT_ENABLED: "false" }, { createWorkerImpl }); expect(result.status).toBe("disabled"); }); test("treats production plugin runtime as required even when the optional toggle is disabled", () => { expect( isPluginHookFanoutRequired( { NODE_ENV: "production", WORKER_QUEUE_GROUP: "general", BULLMQ_REDIS_URL: "redis://localhost:6379/0", PLUGIN_RUNTIME_PRODUCTION_ENABLED: "true", PLUGIN_HOOK_FANOUT_ENABLED: "false", }, "general" ) ).toBe(true); }); test("fails closed when required fanout is explicitly disabled", () => { const { createWorkerImpl } = fakeWorker(); expect(() => bootstrapPluginHookFanout({ WORKER_QUEUE_GROUP: "general", PLUGIN_HOOK_FANOUT_REQUIRED: "true", PLUGIN_HOOK_FANOUT_ENABLED: "false" }, { createWorkerImpl }) ).toThrow(PluginHookFanoutRequiredBootstrapError); expect(createWorkerImpl).not.toHaveBeenCalled(); }); test("disables in production until the plugin runtime rollout gate is enabled", () => { const { createWorkerImpl } = fakeWorker(); const result = bootstrapPluginHookFanout({ NODE_ENV: "production", WORKER_QUEUE_GROUP: "general" }, { createWorkerImpl }); expect(result).toMatchObject({ status: "disabled", reason: expect.stringContaining("PLUGIN_RUNTIME_PRODUCTION_ENABLED=true"), }); expect(createWorkerImpl).not.toHaveBeenCalled(); }); test("fails closed when required dependencies are not wired", () => { const { createWorkerImpl } = fakeWorker(); const env = { WORKER_QUEUE_GROUP: "general", PLUGIN_HOOK_FANOUT_REQUIRED: "true" }; expect(() => bootstrapPluginHookFanout(env, { createWorkerImpl })).toThrow("no prisma surface wired"); expect(() => bootstrapPluginHookFanout(env, { createWorkerImpl, prisma: { plugin: { findMany: vi.fn(async () => []) } }, }) ).toThrow("no plugin invoke queue wired"); expect(createWorkerImpl).not.toHaveBeenCalled(); }); test("starts the outbox plugin-hook subscriber and targets plugin invoke queue", async () => { const { createWorkerImpl, handle, calls } = fakeWorker(); const addCalls: Array<[string, unknown, Record]> = []; const add = vi.fn(async (jobName: string, payload: unknown, options: Record) => { addCalls.push([jobName, payload, options]); return undefined; }); const prisma = { plugin: { findMany: vi.fn(async () => [ { pluginId: "com.acme.audit", version: "1.0.0", manifest: validManifest([{ name: "tenant.created", export: "onTenantCreated" }]), capabilityGrants: [{ capabilityName: "hook:tenant.created" }], }, ]), }, }; const result = bootstrapPluginHookFanout( { WORKER_QUEUE_GROUP: "general", PLUGIN_HOOK_FANOUT_CONCURRENCY: "3" }, { createWorkerImpl, prisma, idempotency: { claim: vi.fn(async () => true), complete: vi.fn(async () => undefined), get: vi.fn(async () => null), }, pluginInvokeQueue: { add } as never, } ); expect(result.status).toBe("started"); expect(result.status === "started" ? result.handle : null).toBe(handle); expect(createWorkerImpl).toHaveBeenCalledWith( PLUGIN_HOOK_FANOUT_OUTBOX_QUEUE, expect.any(Function), expect.objectContaining({ concurrency: 3 }) ); const processor = calls[0]?.[1]; expect(processor).toBeDefined(); if (!processor) throw new Error("missing processor"); await processor({ data: { outboxEventId: "evt_1", tenantId: "tenant_1", eventType: "plugin.hook.tenant.created", payload: { tenantId: "tenant_1", slug: "acme", ownerEmail: "owner@example.test", createdAt: "2026-05-26T20:00:00.000Z", }, }, }); expect(add).toHaveBeenCalledWith( "invoke-plugin", expect.objectContaining({ kind: "plugin.invoke", pluginId: "com.acme.audit" }), expect.objectContaining({ jobId: "plugin-hook:evt_1:com.acme.audit:1.0.0" }) ); expect(prisma.plugin.findMany).toHaveBeenCalledWith({ where: { tenantId: "tenant_1", enabled: true }, select: { pluginId: true, version: true, manifest: true, capabilityGrants: { select: { capabilityName: true } } }, }); expect(addCalls[0]?.[1]).toMatchObject({ kind: "plugin.invoke" }); expect(addCalls[0]?.[2]).toMatchObject({ attempts: 3 }); expect(addCalls[0]?.[1]).not.toMatchObject({ queueName: expect.any(String) }); }); test("loads per-user plugin config when the hook payload has userId", async () => { const { createWorkerImpl, calls } = fakeWorker(); const add = vi.fn(async () => undefined); const prisma = { plugin: { findMany: vi.fn(async () => [ { pluginId: "com.acme.audit", version: "1.0.0", manifest: validManifest([{ name: "user.created", export: "onUserCreated" }]), capabilityGrants: [{ capabilityName: "hook:user.created" }], userConfigs: [{ enabled: false }], }, ]), }, }; bootstrapPluginHookFanout( { WORKER_QUEUE_GROUP: "general" }, { createWorkerImpl, prisma, pluginInvokeQueue: { add } as never, } ); const processor = calls[0]?.[1]; expect(processor).toBeDefined(); if (!processor) throw new Error("missing processor"); await processor({ data: { outboxEventId: "evt_1", tenantId: "tenant_1", eventType: "plugin.hook.user.created", payload: { userId: "user_1", tenantId: "tenant_1", email: "user@example.test", locale: "en", createdAt: "2026-05-26T20:00:00.000Z", }, }, }); expect(prisma.plugin.findMany).toHaveBeenCalledWith({ where: { tenantId: "tenant_1", enabled: true }, select: { pluginId: true, version: true, manifest: true, capabilityGrants: { select: { capabilityName: true } }, userConfigs: { where: { userId: "user_1" }, select: { enabled: true } }, }, }); expect(add).not.toHaveBeenCalled(); }); });