import { describe, expect, it } from 'vitest'; import { defineProvider, defineTranscriber } from '@moxxy/sdk'; import { RequirementRegistry } from './requirements.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 { silentLogger } from './logger.js'; const makeRequirements = () => { 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, }); return { requirements, tools, providers, modes, compactors, transcribers }; }; describe('RequirementRegistry', () => { it('reports missing registered requirements', () => { const { requirements } = makeRequirements(); const check = requirements.check([{ kind: 'tool', name: 'web_fetch' }]); expect(check).toMatchObject({ ready: false, issues: [{ code: 'missing', message: 'Required tool is not registered: web_fetch' }], }); }); it('reports inactive providers when active state is required', () => { const { requirements, providers } = makeRequirements(); providers.register( defineProvider({ name: 'codex-agent', models: [], createClient: () => ({ name: 'codex-agent', models: [], stream: async function* () {}, countTokens: async () => 0 }), }), ); const check = requirements.check([{ kind: 'provider', name: 'codex-agent', state: 'active' }]); expect(check.ready).toBe(false); expect(check.issues[0]).toMatchObject({ code: 'inactive', message: 'Required provider is not active: codex-agent', }); }); it('reports runtime requirements until they are marked ready', () => { const { requirements } = makeRequirements(); const req = { kind: 'runtime' as const, name: 'auth:provider:codex-agent', state: 'ready' as const }; expect(requirements.check([req])).toMatchObject({ ready: false, issues: [{ code: 'not_ready' }], }); requirements.setRuntime('auth:provider:codex-agent', 'ready'); expect(requirements.check([req])).toEqual({ ready: true, issues: [] }); }); it('keeps optional requirements as diagnostics without blocking readiness', () => { const { requirements } = makeRequirements(); const check = requirements.check([{ kind: 'runtime', name: 'browser:installed', state: 'ready', optional: true }]); expect(check.ready).toBe(true); expect(check.issues[0]).toMatchObject({ code: 'not_ready' }); }); it('does not report version_mismatch for a non-plugin kind carrying a version', () => { // `version` is only resolvable for the plugin kind. A version on any other // kind used to compare against an always-undefined target.version and emit a // permanent spurious version_mismatch ("(unknown)") even though the target // was present and active. const { requirements, providers } = makeRequirements(); providers.register( defineProvider({ name: 'codex-agent', models: [], createClient: () => ({ name: 'codex-agent', models: [], stream: async function* () {}, countTokens: async () => 0 }), }), ); providers.setActive('codex-agent'); const check = requirements.check([ { kind: 'provider', name: 'codex-agent', state: 'active', version: '1.0.0' }, ]); expect(check.ready).toBe(true); expect(check.issues).toEqual([]); }); it('still honors a version mismatch for the plugin kind', () => { const { requirements } = makeRequirements(); requirements.registerPlugin('@moxxy/plugin-example', '2.0.0'); const check = requirements.check([ { kind: 'plugin', name: '@moxxy/plugin-example', version: '1.0.0' }, ]); expect(check.ready).toBe(false); expect(check.issues[0]).toMatchObject({ code: 'version_mismatch' }); }); it('isReady checks a single named target without composing per-def requirements', () => { const { requirements, transcribers } = makeRequirements(); transcribers.register( defineTranscriber({ name: 'openai-gpt-transcribe', createClient: () => ({ name: 'openai-gpt-transcribe', transcribe: async () => ({ text: 'ok' }) }), }), ); expect(requirements.isReady('transcriber', 'openai-gpt-transcribe')).toMatchObject({ ready: false, issues: [{ code: 'inactive' }], }); transcribers.setActive('openai-gpt-transcribe'); expect(requirements.isReady('transcriber', 'openai-gpt-transcribe')).toEqual({ ready: true, issues: [], }); }); });