import { afterEach, describe, expect, it, vi } from 'vitest'; import { promises as fs } from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; import { z } from 'zod/v3'; import type { Plugin, ResolvedPluginManifest } from '@moxxy/sdk'; import { defineCompactor, defineMode, definePlugin, defineProvider, defineTool, defineTranscriber, } from '@moxxy/sdk'; import { silentLogger } from '../logger.js'; import { ToolRegistryImpl } from '../registries/tools.js'; import { ProviderRegistry } from '../registries/providers.js'; import { ModeRegistry } from '../registries/modes.js'; import { CompactorRegistry } from '../registries/compactors.js'; import { ChannelRegistryImpl } from '../registries/channels.js'; import { AgentRegistry } from '../registries/agents.js'; import { CommandRegistry } from '../registries/commands.js'; import { TranscriberRegistry } from '../registries/transcribers.js'; import { HookDispatcherImpl } from './lifecycle.js'; import { PluginHost } from './host.js'; import { RequirementRegistry } from '../requirements.js'; import { removeDir } from '@moxxy/vitest-preset/fs'; const makeHost = () => { const tools = new ToolRegistryImpl({ logger: silentLogger, cwd: '/tmp' }); const providers = new ProviderRegistry(); const modes = new ModeRegistry(); const compactors = new CompactorRegistry(); const channels = new ChannelRegistryImpl(); const agents = new AgentRegistry(); const commands = new CommandRegistry(); const transcribers = new TranscriberRegistry(); const requirements = new RequirementRegistry({ tools, providers, modes, compactors, channels, agents, commands, transcribers, }); const dispatcher = new HookDispatcherImpl({ logger: silentLogger }); const host = new PluginHost({ cwd: '/tmp', logger: silentLogger, tools, providers, modes, compactors, channels, agents, commands, transcribers, requirements, dispatcher, }); return { host, tools, providers, modes, compactors, channels, agents, commands, transcribers, requirements, dispatcher }; }; describe('PluginHost', () => { it('registerStatic wires up tools, providers, modes, compactors', () => { const { host, tools, providers, modes, compactors } = makeHost(); const tool = defineTool({ name: 'echo', description: '', inputSchema: z.any(), handler: () => null, }); const provider = defineProvider({ name: 'fake', models: [], createClient: () => ({ name: 'fake', models: [], stream: async function* () {}, countTokens: async () => 0 }), }); const strategy = defineMode({ name: 'fake-loop', run: async function* () {} }); const compactor = defineCompactor({ name: 'fake-compact', shouldCompact: () => false, compact: async () => ({ type: 'compaction', compactor: 'fake-compact', replacedRange: [0, 0], summary: '', tokensSaved: 0, sessionId: '' as never, turnId: '' as never, source: 'compactor' }), }); const plugin = definePlugin({ name: 'demo', tools: [tool], providers: [provider], modes: [strategy], compactors: [compactor], }); host.registerStatic(plugin); expect(tools.has('echo')).toBe(true); expect(providers.list()).toHaveLength(1); expect(modes.list()).toHaveLength(1); expect(compactors.list()).toHaveLength(1); expect(host.list()).toEqual([ { name: 'demo', version: '0.0.0', loaded: true, installed: false, kinds: ['provider', 'mode', 'compactor', 'tool'], }, ]); }); it('rejects double registration', () => { const { host } = makeHost(); const p = definePlugin({ name: 'x' }); host.registerStatic(p); expect(() => host.registerStatic(p)).toThrow(/already registered/); }); it('exposes bounded, sanitized client-chrome slots without renderer code', () => { const { host } = makeHost(); host.registerStatic( definePlugin({ name: 'review-plugin', clientChrome: [ { id: 'review', slot: 'status.trailing', label: ' Review\u001b[31m\u009b31m ready ', tone: 'positive', priority: 999, }, ], }), ); expect(host.listClientChrome()).toEqual([ { id: 'review-plugin:review', source: 'review-plugin', slot: 'status.trailing', label: 'Review[31m31m ready', tone: 'positive', priority: 100, }, ]); expect(host.list()[0]?.kinds).toContain('clientChrome'); }); it('rejects plugins whose required plugin is missing without partial registration', () => { const { host, tools } = makeHost(); const plugin = definePlugin({ name: 'needs-codex', tools: [ defineTool({ name: 'should-not-register', description: '', inputSchema: z.any(), handler: () => null, }), ], }); expect(() => host.registerStatic(plugin, { requirements: [{ kind: 'plugin', name: '@moxxy/plugin-provider-codex-agent' }], }), ).toThrow(/Required plugin is not registered: @moxxy\/plugin-provider-codex-agent/); expect(tools.has('should-not-register')).toBe(false); expect(host.list()).toEqual([]); expect(host.listSkipped()).toMatchObject([ { pluginName: 'needs-codex', source: 'static', reason: 'unmet_requirements', message: 'Required plugin is not registered: @moxxy/plugin-provider-codex-agent', }, ]); }); it('keeps optional plugin requirements as diagnostics without skipping registration', () => { const { host, tools } = makeHost(); host.registerStatic( definePlugin({ name: 'optional-codex', tools: [ defineTool({ name: 'optional-tool', description: '', inputSchema: z.any(), handler: () => null, }), ], }), { requirements: [ { kind: 'plugin', name: '@moxxy/plugin-provider-codex-agent', optional: true, hint: 'Enable Codex for richer behavior.', }, ], }, ); expect(tools.has('optional-tool')).toBe(true); expect(host.listSkipped()).toEqual([]); }); it('clears a previous skip once the same plugin registers successfully', () => { const { host } = makeHost(); const plugin = definePlugin({ name: 'needs-codex' }); const opts = { requirements: [{ kind: 'plugin' as const, name: '@moxxy/plugin-provider-codex-agent' }], }; expect(() => host.registerStatic(plugin, opts)).toThrow(/Required plugin is not registered/); expect(host.listSkipped()).toHaveLength(1); host.registerStatic(definePlugin({ name: '@moxxy/plugin-provider-codex-agent' })); host.registerStatic(plugin, opts); expect(host.listSkipped()).toEqual([]); }); it('records discovered plugin skips with discovered source metadata', () => { const { host } = makeHost(); const plugin = definePlugin({ name: '@demo/discovered' }); expect(() => host.registerDiscovered(plugin, { entry: './dist/index.js', packageName: '@demo/discovered', packageVersion: '1.0.0', packagePath: '/tmp/discovered', requirements: [{ kind: 'plugin', name: '@demo/base' }], }), ).toThrow(/Required plugin is not registered/); expect(host.listSkipped()).toMatchObject([ { pluginName: '@demo/discovered', source: 'discovered', packageName: '@demo/discovered', }, ]); }); it('allows plugin registration after required plugin is loaded', () => { const { host, tools } = makeHost(); host.registerStatic(definePlugin({ name: '@moxxy/plugin-provider-codex-agent' })); host.registerStatic( definePlugin({ name: 'needs-codex', tools: [ defineTool({ name: 'registers-after-requirements', description: '', inputSchema: z.any(), handler: () => null, }), ], }), { requirements: [{ kind: 'plugin', name: '@moxxy/plugin-provider-codex-agent' }], }, ); expect(tools.has('registers-after-requirements')).toBe(true); }); it('unload removes contributions', async () => { const { host, tools } = makeHost(); const tool = defineTool({ name: 'e', description: '', inputSchema: z.any(), handler: () => null, }); const plugin = definePlugin({ name: 'demo', tools: [tool] }); host.registerStatic(plugin); expect(tools.has('e')).toBe(true); await host.unload('demo'); expect(tools.has('e')).toBe(false); }); it('keys a discovered plugin by package name, so unload(packageName) works even when it differs from the declared name', async () => { const { host, tools } = makeHost(); const tool = defineTool({ name: 'dt', description: '', inputSchema: z.any(), handler: () => null, }); // Declared plugin name intentionally differs from the package name. const plugin = definePlugin({ name: 'weird-internal-name', tools: [tool] }); host.registerDiscovered(plugin, { entry: './dist/index.js', packageName: '@scope/pkg', packageVersion: '1.0.0', packagePath: '/tmp/pkg', }); expect(tools.has('dt')).toBe(true); // Callers (self-update/config/plugins-admin) unload by PACKAGE name. await host.unload('@scope/pkg'); expect(tools.has('dt')).toBe(false); }); it('satisfies a kind:plugin requirement by PACKAGE name even when the dep\'s declared name differs', () => { const { host, tools, requirements } = makeHost(); // The dependency: declared plugin.name intentionally differs from its // package name. toposort resolves kind:'plugin' deps by packageName, so the // readiness gate must register/match by packageName too — otherwise the // dependent's requirement (keyed on the dep's packageName) silently fails. host.registerDiscovered(definePlugin({ name: 'weird-internal-name' }), { entry: './dist/index.js', packageName: '@scope/base-pkg', packageVersion: '1.0.0', packagePath: '/tmp/base', }); // The readiness gate is keyed by the PACKAGE name. expect(requirements.isReady('plugin', '@scope/base-pkg').ready).toBe(true); // A dependent that requires the base by its PACKAGE name must load cleanly. const dependent = definePlugin({ name: 'dependent', tools: [ defineTool({ name: 'dep-tool', description: '', inputSchema: z.any(), handler: () => null, }), ], }); expect(() => host.registerDiscovered(dependent, { entry: './dist/index.js', packageName: '@scope/dependent-pkg', packageVersion: '1.0.0', packagePath: '/tmp/dependent', requirements: [{ kind: 'plugin', name: '@scope/base-pkg' }], }), ).not.toThrow(); expect(tools.has('dep-tool')).toBe(true); }); it('discoverAndLoad without loader warns and returns nothing', async () => { const { host } = makeHost(); const warn = vi.spyOn(silentLogger, 'warn'); const result = await host.discoverAndLoad(); expect(result).toEqual([]); warn.mockRestore(); }); describe('reload with userPaths', () => { const tempDirs: string[] = []; afterEach(async () => { await Promise.all(tempDirs.splice(0).map((d) => removeDir(d))); }); const makeHostWithUserPaths = ( userPaths: ReadonlyArray, isDisabled?: (packageName: string) => boolean, ) => { const base = makeHost(); const loader = { load: async (m: ResolvedPluginManifest): Promise => definePlugin({ name: m.packageName, tools: [ defineTool({ name: `${m.packageName}_tool`, description: '', inputSchema: z.any(), handler: () => null, }), ], }), }; const host = new PluginHost({ cwd: '/tmp', logger: silentLogger, tools: base.tools, providers: base.providers, modes: base.modes, compactors: base.compactors, channels: base.channels, agents: base.agents, commands: base.commands, transcribers: base.transcribers, requirements: base.requirements, dispatcher: base.dispatcher, loader, userPaths, ...(isDisabled ? { isDisabled } : {}), }); return { host, tools: base.tools }; }; const writeUserPlugin = async (root: string, name: string): Promise => { const dir = path.join(root, name); await fs.mkdir(dir, { recursive: true }); await fs.writeFile( path.join(dir, 'package.json'), JSON.stringify({ name, version: '0.0.0', moxxy: { plugin: { entry: './index.mjs' } } }), 'utf8', ); }; it('preserves static builtins and (re)discovers user-path plugins', async () => { const root = await fs.mkdtemp(path.join(os.tmpdir(), 'moxxy-host-')); tempDirs.push(root); const { host, tools } = makeHostWithUserPaths([root]); // A statically-registered builtin has no manifest — it must survive reload. host.registerStatic( definePlugin({ name: 'builtin', tools: [defineTool({ name: 'builtin_tool', description: '', inputSchema: z.any(), handler: () => null })], }), ); await writeUserPlugin(root, 'userplug'); await host.reload(); expect(tools.has('builtin_tool')).toBe(true); // static preserved expect(tools.has('userplug_tool')).toBe(true); // user-path discovered // Remove the user plugin from disk → reload unloads it, keeps the builtin. await removeDir(path.join(root, 'userplug')); await host.reload(); expect(tools.has('userplug_tool')).toBe(false); expect(tools.has('builtin_tool')).toBe(true); }); it('never loads a disabled discovered plugin, and a runtime toggle takes effect on reload', async () => { const root = await fs.mkdtemp(path.join(os.tmpdir(), 'moxxy-host-')); tempDirs.push(root); const disabled = new Set(['userplug']); const { host, tools } = makeHostWithUserPaths([root], (pkg) => disabled.has(pkg)); await writeUserPlugin(root, 'userplug'); // Disabled on disk → reload must NOT load it. await host.reload(); expect(tools.has('userplug_tool')).toBe(false); // Runtime enable (predicate flips) → next reload discovers + loads it. disabled.delete('userplug'); await host.reload(); expect(tools.has('userplug_tool')).toBe(true); // Runtime disable of a currently-loaded plugin → reload unloads it and // does not resurrect it (the bug this predicate fixes). disabled.add('userplug'); await host.reload(); expect(tools.has('userplug_tool')).toBe(false); }); }); it('rolls back ALL contributions when one collides mid-registration (no orphans)', () => { const { host, tools, providers } = makeHost(); // First plugin claims provider "dup". host.registerStatic( definePlugin({ name: 'first', providers: [ defineProvider({ name: 'dup', models: [], createClient: () => ({ name: 'dup', models: [], stream: async function* () {}, countTokens: async () => 0 }), }), ], }), ); // Second plugin registers a tool FIRST (succeeds), then a provider named // "dup" which collides → applyPlugin throws part way through. The already- // registered tool must be rolled back, and no LoadedRecord must exist. const colliding = definePlugin({ name: 'second', tools: [ defineTool({ name: 'orphan-tool', description: '', inputSchema: z.any(), handler: () => null }), ], providers: [ defineProvider({ name: 'dup', models: [], createClient: () => ({ name: 'dup', models: [], stream: async function* () {}, countTokens: async () => 0 }), }), ], }); expect(() => host.registerStatic(colliding)).toThrow(/already registered/); // The tool registered before the collision must NOT be stranded. expect(tools.has('orphan-tool')).toBe(false); // The first plugin's provider survives; no duplicate from the failed load. expect(providers.list().map((p) => p.name)).toEqual(['dup']); // No half-loaded record for "second". expect(host.list().map((p) => p.name)).toEqual(['first']); }); it('registerStatic + unload roundtrip transcribers', async () => { const { host, transcribers } = makeHost(); const t = defineTranscriber({ name: 'fake-stt', createClient: () => ({ name: 'fake-stt', transcribe: async () => ({ text: '' }) }), }); host.registerStatic(definePlugin({ name: 'stt-demo', transcribers: [t] })); expect(transcribers.has('fake-stt')).toBe(true); expect(transcribers.list()).toHaveLength(1); await host.unload('stt-demo'); expect(transcribers.has('fake-stt')).toBe(false); }); }); describe('PluginHost skill directories', () => { it('names the skills folder each loaded plugin ships, from its package.json or its skillsDir', () => { const { host } = makeHost(); host.registerDiscovered(definePlugin({ name: 'computer' }), { entry: './dist/index.js', skills: './skills', packageName: '@moxxy/plugin-computer-control', packageVersion: '1.0.0', packagePath: path.join(os.tmpdir(), 'computer'), }); host.registerDiscovered(definePlugin({ name: 'plain' }), { entry: './dist/index.js', packageName: '@scope/plain', packageVersion: '1.0.0', packagePath: path.join(os.tmpdir(), 'plain'), }); host.registerStatic(definePlugin({ name: 'bundled', skillsDir: path.join(os.tmpdir(), 'bundled-skills') })); expect(host.skillDirs()).toEqual([ path.join(os.tmpdir(), 'computer', 'skills'), path.join(os.tmpdir(), 'bundled-skills'), ]); }); it('forgets the folder of a plugin that was unloaded', async () => { const { host } = makeHost(); host.registerDiscovered(definePlugin({ name: 'computer' }), { entry: './dist/index.js', skills: './skills', packageName: '@moxxy/plugin-computer-control', packageVersion: '1.0.0', packagePath: path.join(os.tmpdir(), 'computer'), }); await host.unload('@moxxy/plugin-computer-control'); expect(host.skillDirs()).toEqual([]); }); });