import { describe, it, expect, vi, afterEach } from 'vitest';
import { AgentRouter } from '../agents/router.js';
import { AgentConfigRegistry } from '../agents/registry.js';
import { HookEngine } from '../hooks/index.js';
import type { ModelTier } from '../models/router.js';
import { createMessageRouter } from './routing.js';
import { AgentOrchestrator } from '../backends/index.js';
import { CommandRegistry, registerBuiltinCommands } from '../commands/index.js';
import { ComponentRegistry } from '../intents/index.js';
import { RoutingPolicy } from '../routing/index.js';
import type { OutboundMessage } from '../channels/index.js';
import { initAuditLogger } from '../audit/index.js';

type MessageRouterDeps = Parameters<typeof createMessageRouter>[0];
type MessageRouterInput = Parameters<ReturnType<typeof createMessageRouter>['handler']>[0];

describe('daemon agent routing integration', () => {
  it('resolves agent config for channel messages', () => {
    const registry = new AgentConfigRegistry();
    registry.loadFromConfig({
      assistant: { system_prompt: 'Be helpful.', model_tier: 'default', tool_profile: 'messaging', sandbox: false },
      coder: { system_prompt: 'Write code.', model_tier: 'complex', tool_profile: 'coding', sandbox: true },
    });

    const router = new AgentRouter({
      default_agent: 'assistant',
      channels: { discord: 'coder' },
      senders: { 'telegram:admin': 'coder' },
    });

    // Discord user gets coder
    const discordAgent = router.resolve('discord', 'user123');
    expect(discordAgent).toBe('coder');
    expect(discordAgent).toBeDefined();
    const discordAgentConfig = discordAgent ? registry.get(discordAgent) : undefined;
    expect(discordAgentConfig?.systemPrompt).toBe('Write code.');

    // Telegram admin gets coder
    const telegramAdmin = router.resolve('telegram', 'admin');
    expect(telegramAdmin).toBe('coder');

    // Random telegram user gets assistant
    const telegramUser = router.resolve('telegram', 'random');
    expect(telegramUser).toBe('assistant');
    expect(telegramUser).toBeDefined();
    const telegramUserConfig = telegramUser ? registry.get(telegramUser) : undefined;
    expect(telegramUserConfig?.systemPrompt).toBe('Be helpful.');
  });

  it('uses default agent when no routing configured', () => {
    const router = new AgentRouter({ channels: {}, senders: {} });
    expect(router.resolve('telegram', '123')).toBeUndefined();
  });

  it('model tier precedence: metadata > metadata modelFor > agent config > global default', () => {
    // This test documents the tier resolution precedence used by createMessageRouter.
    // The actual resolution logic:
    // tierFromMetadata ?? tierFromMetadataModelFor ?? agentConfig?.modelTier ?? primary_tier ?? 'default'
    function resolveTier(
      metadataTier: ModelTier | undefined,
      metadataForTier: ModelTier | undefined,
      agentTier: ModelTier | undefined,
      globalTier: ModelTier | undefined,
    ): ModelTier {
      return metadataTier ?? metadataForTier ?? agentTier ?? globalTier ?? 'default';
    }

    // With all three set, metadata wins
    expect(resolveTier('fast', 'default', 'complex', 'default')).toBe('fast');

    // Without explicit metadata tier, modelFor-resolved tier wins
    expect(resolveTier(undefined, 'complex', 'default', 'fast')).toBe('complex');

    // Without metadata, agent config wins
    expect(resolveTier(undefined, undefined, 'complex', 'default')).toBe('complex');

    // Without metadata or agent config, global wins
    expect(resolveTier(undefined, undefined, undefined, 'default')).toBe('default');

    // Without anything, falls back to 'default'
    expect(resolveTier(undefined, undefined, undefined, undefined)).toBe('default');
  });

  it('uses metadata.modelFor tags to select tier', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');
    const session = {
      id: 'telegram:model-for',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: {
          default: { provider: 'anthropic', model: 'claude', for: ['chat'] },
          fast: { provider: 'anthropic', model: 'haiku', for: ['search'] },
        },
      } as unknown as MessageRouterDeps['config'],
    });

    await router.handler({
      id: 'm-model-for',
      channel: 'telegram',
      senderId: 'model-for',
      text: 'find this quickly',
      timestamp: Date.now(),
      metadata: { modelFor: 'search' },
    } as MessageRouterInput, vi.fn(async () => {}));

    const keys = Array.from(router.agents.keys());
    expect(keys.some((key) => key.endsWith(':fast'))).toBe(true);
    expect(processSpy).toHaveBeenCalled();
  });
});

describe('daemon command fast-path integration', () => {
  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('handles known reset command without calling agent.process', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
    const session = {
      id: 'telegram:user-1',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'm1',
      channel: 'telegram',
      senderId: 'user-1',
      text: '/reset',
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'reset' },
    } as MessageRouterInput, reply);

    expect(processSpy).not.toHaveBeenCalled();
    expect(session.deleteConfig).toHaveBeenCalledWith('modelTier');
  });

  it('handles /transfer command via command fast-path and copies session to TUI', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
    const transferSpy = vi.fn();
    const session = {
      id: 'telegram:user-1',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
        transferSession: transferSpy,
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'm-transfer',
      channel: 'telegram',
      senderId: 'user-1',
      text: '/transfer tui',
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'transfer', commandArgs: 'tui' },
    } as MessageRouterInput, reply);

    expect(processSpy).not.toHaveBeenCalled();
    expect(transferSpy).toHaveBeenCalledWith('telegram', 'user-1', 'tui', 'local');
    expect(reply).toHaveBeenCalledWith({
      text: 'Session transferred to TUI (local)',
      replyTo: 'm-transfer',
    });
  });

  it('emits user.action audit events for channel messages', async () => {
    const mockAuditLogger = {
      userAction: vi.fn(),
    };
    initAuditLogger(mockAuditLogger as any);

    const session = {
      id: 'telegram:user-audit',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
    });

    await router.handler({
      id: 'm-audit',
      channel: 'telegram',
      senderId: 'user-audit',
      text: '/reset',
      metadata: { isCommand: true, command: 'reset' },
    } as unknown as MessageRouterInput, vi.fn(async (_: OutboundMessage) => {}));

    expect(mockAuditLogger.userAction).toHaveBeenCalledWith(
      expect.objectContaining({
        source: 'channel',
        action_type: 'command',
        channel: 'telegram',
      }),
    );
  });

  it('handles model command via fast-path and persists tier override', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
    const setModelTierSpy = vi.spyOn(AgentOrchestrator.prototype, 'setModelTier');
    const session = {
      id: 'telegram:user-4',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'm4',
      channel: 'telegram',
      senderId: 'user-4',
      text: '/model fast',
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'model', commandArgs: 'fast' },
    } as MessageRouterInput, reply);

    expect(processSpy).not.toHaveBeenCalled();
    expect(setModelTierSpy).toHaveBeenCalledWith('fast');
    expect(session.setConfig).toHaveBeenCalledWith('modelTier', 'fast');
  });

  it('handles queue command via fast-path and persists queue override', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
    const session = {
      id: 'telegram:user-queue',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        server: {
          queue: {
            mode: 'collect',
            cap: 50,
            overflow: 'drop_old',
            debounce_ms: 0,
            summarize_overflow: true,
          },
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'q1',
      channel: 'telegram',
      senderId: 'user-queue',
      text: '/queue set mode followup',
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'queue', commandArgs: 'set mode followup' },
    } as MessageRouterInput, reply);

    expect(processSpy).not.toHaveBeenCalled();
    expect(session.setConfig).toHaveBeenCalledWith('queue.mode', 'followup');
  });

  it('uses intent match to override agent target', async () => {
    const session = {
      id: 'telegram:user-2',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const intentRegistry = new ComponentRegistry({ matchThreshold: 0.5 });
    intentRegistry.register({
      name: 'deploy-route',
      patterns: ['deploy *'],
      target: { type: 'agent', name: 'coder' },
      priority: 10,
      enabled: true,
    });

    const agentConfigRegistry = new AgentConfigRegistry();
    agentConfigRegistry.loadFromConfig({
      assistant: { model_tier: 'default', sandbox: false },
      coder: { model_tier: 'complex', sandbox: false },
    });

    const agentRouter = new AgentRouter({
      default_agent: 'assistant',
      channels: {},
      senders: {},
    });

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        intents: { enabled: true },
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
      intentRegistry,
      agentConfigRegistry,
      agentRouter,
    });

    await router.handler({
      id: 'm2',
      channel: 'telegram',
      senderId: 'user-2',
      text: 'deploy backend now',
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'reset' },
    } as MessageRouterInput, vi.fn(async () => {}));

    const keys = Array.from(router.agents.keys());
    expect(keys.some(key => key.includes(':coder'))).toBe(true);
  });

  it('auto-routes research-prefixed messages to research agent when configured', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');
    const session = {
      id: 'telegram:user-research',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const agentConfigRegistry = new AgentConfigRegistry();
    agentConfigRegistry.loadFromConfig({
      assistant: { model_tier: 'default', sandbox: false },
      research: { model_tier: 'complex', sandbox: false },
    });

    const agentRouter = new AgentRouter({
      default_agent: 'assistant',
      channels: {},
      senders: {},
    });

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        intents: { enabled: false },
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
      agentConfigRegistry,
      agentRouter,
    });

    await router.handler({
      id: 'm-research',
      channel: 'telegram',
      senderId: 'user-research',
      text: 'research compare k0s vs k3s for a homelab',
      timestamp: Date.now(),
    } as MessageRouterInput, vi.fn(async () => {}));

    const keys = Array.from(router.agents.keys());
    expect(keys.some(key => key.includes(':research'))).toBe(true);
    expect(processSpy).toHaveBeenCalledWith('compare k0s vs k3s for a homelab', undefined, undefined);
  });

  it('falls back to llm path when confidence is below fast threshold', async () => {
    const session = {
      id: 'telegram:user-3',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const intentRegistry = new ComponentRegistry({ matchThreshold: 0.5 });
    intentRegistry.register({
      name: 'deploy-route',
      patterns: ['deploy *'],
      target: { type: 'agent', name: 'coder' },
      priority: 10,
      enabled: true,
    });

    const routingPolicy = new RoutingPolicy({
      enabled: true,
      fastPathThreshold: 0.99,
      llmThreshold: 0.2,
      defaultPath: 'llm',
    });

    const agentConfigRegistry = new AgentConfigRegistry();
    agentConfigRegistry.loadFromConfig({
      assistant: { model_tier: 'default', sandbox: false },
      coder: { model_tier: 'complex', sandbox: false },
    });

    const agentRouter = new AgentRouter({
      default_agent: 'assistant',
      channels: {},
      senders: {},
    });

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        intents: { enabled: true },
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
      intentRegistry,
      routingPolicy,
      agentConfigRegistry,
      agentRouter,
    });

    await router.handler({
      id: 'm3',
      channel: 'telegram',
      senderId: 'user-3',
      text: 'deploy backend now',
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'reset' },
    } as MessageRouterInput, vi.fn(async () => {}));

    const keys = Array.from(router.agents.keys());
    expect(keys.some(key => key.includes(':assistant'))).toBe(true);
  });

  it('includes selected backend in status output', async () => {
    const session = {
      id: 'telegram:user-status',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
      externalBackends: { codex: { name: 'codex', process: vi.fn(async () => 'unused') } } as unknown as MessageRouterDeps['externalBackends'],
      defaultName: 'codex',
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'm-status',
      channel: 'telegram',
      senderId: 'user-status',
      text: '/status',
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'status' },
    } as MessageRouterInput, reply);

    const outbound = reply.mock.calls[0]?.[0] as OutboundMessage | undefined;
    expect(outbound?.text).toContain('Backend: codex');
  });

  it('lists and resolves pending approvals for the current session via /approvals + /approve', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
    const session = {
      id: 'telegram:approvals-user',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const hookEngine = new HookEngine({ confirm: [], log: [], silent: [] });
    const pendingPromise = hookEngine.requestConfirmation(
      'shell.exec',
      { command: 'rm -rf /tmp/example' },
      { sessionId: session.id, channel: 'telegram', sender: 'approvals-user' },
    );
    const pending = hookEngine.getPendingConfirmations({ sessionId: session.id });
    const pendingId = pending[0]?.id;
    if (!pendingId) {
      throw new Error('Expected pending approval id');
    }

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
      hookEngine,
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});

    await router.handler({
      id: 'approvals-1',
      channel: 'telegram',
      senderId: 'approvals-user',
      text: '/approvals',
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'approvals' },
    } as MessageRouterInput, reply);

    const listReply = reply.mock.calls[0]?.[0] as OutboundMessage | undefined;
    expect(String(listReply?.text)).toContain('Pending approvals:');
    expect(String(listReply?.text)).toContain(pendingId);

    await router.handler({
      id: 'approvals-2',
      channel: 'telegram',
      senderId: 'approvals-user',
      text: `/approve ${pendingId}`,
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'approve', commandArgs: pendingId },
    } as MessageRouterInput, reply);

    const approveReply = reply.mock.calls[1]?.[0] as OutboundMessage | undefined;
    expect(String(approveReply?.text)).toContain('Approved: shell.exec');
    await expect(pendingPromise).resolves.toEqual({ approved: true });
    expect(processSpy).not.toHaveBeenCalled();
  });

  it('handles /skill list via command fast-path', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
    const session = {
      id: 'telegram:skill-user',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
      skillRegistry: {
        listAvailable: () => ([
          { manifest: { name: 'calendar', tier: 'managed', version: '1.2.0' } },
          { manifest: { name: 'todoist', tier: 'workspace', version: '0.4.1' } },
        ]),
      } as unknown as MessageRouterDeps['skillRegistry'],
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'skill-1',
      channel: 'telegram',
      senderId: 'skill-user',
      text: '/skill list',
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'skill', commandArgs: 'list' },
    } as MessageRouterInput, reply);

    expect(processSpy).not.toHaveBeenCalled();
    const outbound = reply.mock.calls[0]?.[0] as OutboundMessage | undefined;
    expect(String(outbound?.text)).toContain('Available skills (2):');
    expect(String(outbound?.text)).toContain('calendar');
    expect(String(outbound?.text)).toContain('todoist');
  });
});

describe('daemon external backend integration', () => {
  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('uses configured external backend for non-command messages', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
    const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
    const session = {
      id: 'telegram:external-user',
      addMessage: vi.fn((msg: { role: 'user' | 'assistant'; content: string }) => {
        history.push(msg);
        return msg;
      }),
      getHistory: vi.fn(() => [...history]),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const externalBackend = {
      name: 'codex',
      process: vi.fn(async () => 'external backend response'),
    };

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      externalBackends: { codex: externalBackend } as unknown as MessageRouterDeps['externalBackends'],
      defaultName: 'codex',
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'm-external',
      channel: 'telegram',
      senderId: 'external-user',
      text: 'hello from external path',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(externalBackend.process).toHaveBeenCalled();
    expect(processSpy).not.toHaveBeenCalled();
    expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'external backend response' }));
  });

  it('forces native processing for capability/tool inventory queries', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
      .mockResolvedValue('Available tools (authoritative):\n- file.read');
    const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
    const session = {
      id: 'telegram:external-tools-query',
      addMessage: vi.fn((msg: { role: 'user' | 'assistant'; content: string }) => {
        history.push(msg);
        return msg;
      }),
      getHistory: vi.fn(() => [...history]),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const externalBackend = {
      name: 'codex',
      process: vi.fn(async () => 'external backend response'),
    };

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      externalBackends: { codex: externalBackend } as unknown as MessageRouterDeps['externalBackends'],
      defaultName: 'codex',
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'm-external-tools-query',
      channel: 'telegram',
      senderId: 'external-tools-query',
      text: 'check your available tools',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(externalBackend.process).not.toHaveBeenCalled();
    expect(processSpy).toHaveBeenCalled();
    expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'Available tools (authoritative):\n- file.read' }));
  });

  it('does not force native processing for incidental tool wording', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
      .mockResolvedValue('native response');
    const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
    const session = {
      id: 'telegram:external-incidental-tool',
      addMessage: vi.fn((msg: { role: 'user' | 'assistant'; content: string }) => {
        history.push(msg);
        return msg;
      }),
      getHistory: vi.fn(() => [...history]),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const externalBackend = {
      name: 'codex',
      process: vi.fn(async () => 'external backend response'),
    };

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      externalBackends: { codex: externalBackend } as unknown as MessageRouterDeps['externalBackends'],
      defaultName: 'codex',
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'm-external-incidental-tool',
      channel: 'telegram',
      senderId: 'external-incidental-tool',
      text: 'The json by default is up to you, same as for gemini, codex is your tool, so decide what format is best for you to deal with.',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(externalBackend.process).toHaveBeenCalled();
    expect(processSpy).not.toHaveBeenCalled();
    expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'external backend response' }));
  });

  it('falls back to native processing when external backend fails', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
      .mockResolvedValue('native fallback response');
    const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
    const session = {
      id: 'telegram:external-fail',
      addMessage: vi.fn((msg: { role: 'user' | 'assistant'; content: string }) => {
        history.push(msg);
        return msg;
      }),
      getHistory: vi.fn(() => [...history]),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const externalBackend = {
      name: 'codex',
      process: vi.fn(async () => {
        throw new Error('external failed');
      }),
    };

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      externalBackends: { codex: externalBackend } as unknown as MessageRouterDeps['externalBackends'],
      defaultName: 'codex',
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'm-external-fail',
      channel: 'telegram',
      senderId: 'external-fail',
      text: 'hello fallback',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(externalBackend.process).toHaveBeenCalled();
    expect(processSpy).toHaveBeenCalled();
    expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'native fallback response' }));
  });

  it('uses pi_embedded backend for plain text canary turns', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
    const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
    const session = {
      id: 'telegram:pi-canary',
      addMessage: vi.fn((msg: { role: 'user' | 'assistant'; content: string }) => {
        history.push(msg);
        return msg;
      }),
      getHistory: vi.fn(() => [...history]),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const piBackend = {
      name: 'pi_embedded',
      process: vi.fn(async () => 'pi embedded response'),
    };

    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        backends: {
          pi_embedded: { no_tools_mode: false },
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      externalBackends: { pi_embedded: piBackend } as unknown as MessageRouterDeps['externalBackends'],
      defaultName: 'pi_embedded',
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'm-pi-canary',
      channel: 'telegram',
      senderId: 'pi-canary',
      text: 'just chat with me',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(piBackend.process).toHaveBeenCalled();
    expect(piBackend.process).toHaveBeenCalledWith(expect.objectContaining({
      systemPrompt: 'test prompt',
    }));
    expect(processSpy).not.toHaveBeenCalled();
    expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'pi embedded response' }));
  });

  it('forces native processing for pi_embedded no-tools mode when prompt appears tool-oriented', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
      .mockResolvedValue('native guarded response');
    const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
    const session = {
      id: 'telegram:pi-no-tools',
      addMessage: vi.fn((msg: { role: 'user' | 'assistant'; content: string }) => {
        history.push(msg);
        return msg;
      }),
      getHistory: vi.fn(() => [...history]),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const piBackend = {
      name: 'pi_embedded',
      process: vi.fn(async () => 'pi embedded response'),
    };

    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        backends: {
          pi_embedded: { no_tools_mode: true },
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      externalBackends: { pi_embedded: piBackend } as unknown as MessageRouterDeps['externalBackends'],
      defaultName: 'pi_embedded',
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'm-pi-no-tools',
      channel: 'telegram',
      senderId: 'pi-no-tools',
      text: 'please read the file and run a shell command',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(piBackend.process).not.toHaveBeenCalled();
    expect(processSpy).toHaveBeenCalled();
    expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'native guarded response' }));
  });

  it('forces native processing for pi_embedded no-tools mode on quick-check execution intent', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
      .mockResolvedValue('native guarded response');
    const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
    const session = {
      id: 'telegram:pi-no-tools-quick-check',
      addMessage: vi.fn((msg: { role: 'user' | 'assistant'; content: string }) => {
        history.push(msg);
        return msg;
      }),
      getHistory: vi.fn(() => [...history]),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const piBackend = {
      name: 'pi_embedded',
      process: vi.fn(async () => 'pi embedded response'),
    };

    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        backends: {
          pi_embedded: { no_tools_mode: true },
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      externalBackends: { pi_embedded: piBackend } as unknown as MessageRouterDeps['externalBackends'],
      defaultName: 'pi_embedded',
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'm-pi-no-tools-quick-check',
      channel: 'telegram',
      senderId: 'pi-no-tools-quick-check',
      text: 'run a quick check',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(piBackend.process).not.toHaveBeenCalled();
    expect(processSpy).toHaveBeenCalled();
    expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'native guarded response' }));
  });

  it('supports manual global pi deactivation and re-activation via /runtime alias', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
      .mockResolvedValue('native fallback response');
    const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
    const session = {
      id: 'telegram:pi-manual-toggle',
      addMessage: vi.fn((msg: { role: 'user' | 'assistant'; content: string }) => {
        history.push(msg);
        return msg;
      }),
      getHistory: vi.fn(() => [...history]),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const piBackend = {
      name: 'pi_embedded',
      process: vi.fn(async () => 'pi embedded response'),
    };

    let backendMode: 'config_default' | 'force_native' | 'force_pi_embedded' = 'force_pi_embedded';
    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        backends: {
          pi_embedded: { no_tools_mode: false },
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
      externalBackends: { pi_embedded: piBackend } as unknown as MessageRouterDeps['externalBackends'],
      defaultName: 'pi_embedded',
      getBackendMode: () => backendMode,
      setBackendMode: (mode) => {
        backendMode = mode;
      },
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});

    await router.handler({
      id: 'm-backend-deactivate',
      channel: 'telegram',
      senderId: 'pi-manual-toggle',
      text: '/runtime deactivate pi',
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'runtime', commandArgs: 'deactivate pi' },
    } as MessageRouterInput, reply);

    await router.handler({
      id: 'm-after-deactivate',
      channel: 'telegram',
      senderId: 'pi-manual-toggle',
      text: 'hello after deactivate',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(backendMode).toBe('force_native');
    expect(piBackend.process).not.toHaveBeenCalled();
    expect(processSpy).toHaveBeenCalled();

    await router.handler({
      id: 'm-backend-activate',
      channel: 'telegram',
      senderId: 'pi-manual-toggle',
      text: '/backend activate pi',
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'backend', commandArgs: 'activate pi' },
    } as MessageRouterInput, reply);

    await router.handler({
      id: 'm-after-activate',
      channel: 'telegram',
      senderId: 'pi-manual-toggle',
      text: 'hello after activate',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(backendMode).toBe('force_pi_embedded');
    expect(piBackend.process).toHaveBeenCalledTimes(1);
  });

  it('accepts full-command backend subcommand input and still returns status', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
      .mockResolvedValue('native fallback response');
    const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
    const session = {
      id: 'telegram:pi-runtime-status-normalization',
      addMessage: vi.fn((msg: { role: 'user' | 'assistant'; content: string }) => {
        history.push(msg);
        return msg;
      }),
      getHistory: vi.fn(() => [...history]),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        backends: {
          pi_embedded: { no_tools_mode: false },
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
      externalBackends: {} as unknown as MessageRouterDeps['externalBackends'],
      getBackendMode: () => 'config_default',
      setBackendMode: vi.fn(),
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});

    await router.handler({
      id: 'm-runtime-status-normalized',
      channel: 'telegram',
      senderId: 'pi-runtime-status-normalization',
      text: '/runtime status',
      timestamp: Date.now(),
      metadata: { isCommand: true, command: 'runtime', commandArgs: '/runtime status' },
    } as MessageRouterInput, reply);

    expect(processSpy).not.toHaveBeenCalled();
    expect(reply).toHaveBeenCalledWith(expect.objectContaining({
      text: expect.stringContaining('Backend mode:'),
    }));
  });
});

describe('daemon audio routing integration', () => {
  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('fast-path replies for voice attachments when transcription is not configured and model does not support audio', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');

    const session = {
      id: 'telegram:user-voice-1',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['default'],
        getAllLabels: () => ({ default: 'default' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: { clone() { return this; }, register: vi.fn() } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'default',
            memory_extraction: 'default',
            classification: 'default',
            tool_summarisation: 'default',
            complex_reasoning: 'default',
          },
          max_delegation_depth: 1,
          max_iterations: 3,
        },
        compaction: { enabled: false },
        // Anthropic doesn't support native audio; ensures routing hits the non-audio path.
        models: { default: { provider: 'anthropic', model: 'claude' } },
        audio: { enabled: false },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'v1',
      channel: 'telegram',
      senderId: 'user-voice-1',
      text: '',
      attachments: [{ mimeType: 'audio/ogg', data: 'ZGF0YQ==', filename: 'voice.ogg' }],
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(processSpy).not.toHaveBeenCalled();
    expect(reply).toHaveBeenCalledTimes(1);
    const firstReply = reply.mock.calls[0]?.[0] as OutboundMessage | undefined;
    const msg = firstReply as { text?: string };
    expect(String(msg.text)).toContain('audio transcription is not configured');
  });

  it('transcribes voice attachments when transcription is configured and preserves audio for anthropic tool fallback', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');

    // Mock transcription endpoint call.
    const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
      ok: true,
      status: 200,
      statusText: 'OK',
      json: async () => ({ text: 'hello world' }),
    } as Response);

    const session = {
      id: 'telegram:user-voice-2',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['default'],
        getAllLabels: () => ({ default: 'default' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: { clone() { return this; }, register: vi.fn() } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'default',
            memory_extraction: 'default',
            classification: 'default',
            tool_summarisation: 'default',
            complex_reasoning: 'default',
          },
          max_delegation_depth: 1,
          max_iterations: 3,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
        audio: {
          enabled: true,
          provider: { type: 'openai', endpoint: 'https://example.com/v1/audio/transcriptions', api_key: 'sk-test', model: 'whisper-1' },
        },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'v2',
      channel: 'telegram',
      senderId: 'user-voice-2',
      text: 'caption',
      attachments: [
        { mimeType: 'audio/ogg', data: 'ZGF0YQ==', filename: 'voice.ogg' },
        { mimeType: 'image/jpeg', data: 'aW1n', filename: 'img.jpg' },
      ],
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(fetchSpy).toHaveBeenCalled();
    expect(processSpy).toHaveBeenCalledTimes(1);
    const [calledText, calledAttachments] = processSpy.mock.calls[0] ?? [];
    expect(String(calledText)).toContain('[Voice message]: hello world');
    expect(String(calledText)).toContain('caption');
    const atts = calledAttachments as Array<{ mimeType: string }> | undefined;
    expect(atts?.some(a => a.mimeType === 'audio/ogg')).toBe(true);
    expect(atts?.some(a => a.mimeType === 'image/jpeg')).toBe(true);
    expect(session.setConfig).toHaveBeenCalledWith(
      'lastAudioAttachment',
      expect.stringContaining('"mimeType":"audio/ogg"'),
    );
  });

  it('transcribes voice attachments when transcription is configured and strips audio for openai-compatible providers', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');

    const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
      ok: true,
      status: 200,
      statusText: 'OK',
      json: async () => ({ text: 'hello world' }),
    } as Response);

    const session = {
      id: 'telegram:user-voice-3',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const commandRegistry = new CommandRegistry();
    registerBuiltinCommands(commandRegistry);

    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['default'],
        getAllLabels: () => ({ default: 'default' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: { clone() { return this; }, register: vi.fn() } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'default',
            memory_extraction: 'default',
            classification: 'default',
            tool_summarisation: 'default',
            complex_reasoning: 'default',
          },
          max_delegation_depth: 1,
          max_iterations: 3,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'openai', model: 'gpt-4.1', supports_audio: false } },
        audio: {
          enabled: true,
          provider: { type: 'openai', endpoint: 'https://example.com/v1/audio/transcriptions', api_key: 'sk-test', model: 'whisper-1' },
        },
      } as unknown as MessageRouterDeps['config'],
      commandRegistry,
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'v3',
      channel: 'telegram',
      senderId: 'user-voice-3',
      text: 'caption',
      attachments: [
        { mimeType: 'audio/ogg', data: 'ZGF0YQ==', filename: 'voice.ogg' },
        { mimeType: 'image/jpeg', data: 'aW1n', filename: 'img.jpg' },
      ],
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(fetchSpy).toHaveBeenCalled();
    expect(processSpy).toHaveBeenCalledTimes(1);
    const [calledText, calledAttachments] = processSpy.mock.calls[0] ?? [];
    expect(String(calledText)).toContain('[Voice message]: hello world');
    expect(String(calledText)).toContain('caption');
    const atts = calledAttachments as Array<{ mimeType: string }> | undefined;
    expect(atts?.some(a => a.mimeType === 'audio/ogg')).toBe(false);
    expect(atts?.some(a => a.mimeType === 'image/jpeg')).toBe(true);
    expect(session.setConfig).toHaveBeenCalledWith(
      'lastAudioAttachment',
      expect.stringContaining('"mimeType":"audio/ogg"'),
    );
  });
});

describe('daemon tts routing integration', () => {
  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('attaches synthesized audio reply when tts is enabled for the channel', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('voice-enabled response');
    const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
      ok: true,
      status: 200,
      statusText: 'OK',
      arrayBuffer: async () => Uint8Array.from([7, 8, 9]).buffer,
    } as Response);

    const session = {
      id: 'telegram:tts-user-1',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['default'],
        getAllLabels: () => ({ default: 'default' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: { clone() { return this; }, register: vi.fn() } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'default',
            memory_extraction: 'default',
            classification: 'default',
            tool_summarisation: 'default',
            complex_reasoning: 'default',
          },
          max_delegation_depth: 1,
          max_iterations: 3,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
        tts: {
          enabled: true,
          enabled_channels: ['telegram'],
          provider: {
            type: 'custom',
            endpoint: 'https://example.com/v1/audio/speech',
            api_key: 'sk-test',
            model: 'gpt-4o-mini-tts',
            voice: 'alloy',
            format: 'mp3',
          },
        },
      } as unknown as MessageRouterDeps['config'],
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'tts-1',
      channel: 'telegram',
      senderId: 'tts-user-1',
      text: 'say hello',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(processSpy).toHaveBeenCalledTimes(1);
    expect(fetchSpy).toHaveBeenCalledTimes(1);
    const outbound = reply.mock.calls[0]?.[0] as OutboundMessage | undefined;
    expect(outbound?.attachments).toBeDefined();
    expect(outbound?.attachments?.[0]).toMatchObject({
      mimeType: 'audio/mpeg',
      data: 'BwgJ',
    });
  });

  it('does not synthesize tts when channel is not enabled', async () => {
    vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('text-only response');
    const fetchSpy = vi.spyOn(globalThis, 'fetch');

    const session = {
      id: 'discord:tts-user-2',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['default'],
        getAllLabels: () => ({ default: 'default' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: { clone() { return this; }, register: vi.fn() } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'default',
            memory_extraction: 'default',
            classification: 'default',
            tool_summarisation: 'default',
            complex_reasoning: 'default',
          },
          max_delegation_depth: 1,
          max_iterations: 3,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
        tts: {
          enabled: true,
          enabled_channels: ['telegram'],
          provider: {
            type: 'custom',
            endpoint: 'https://example.com/v1/audio/speech',
          },
        },
      } as unknown as MessageRouterDeps['config'],
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'tts-2',
      channel: 'discord',
      senderId: 'tts-user-2',
      text: 'respond as text',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(fetchSpy).not.toHaveBeenCalled();
    const outbound = reply.mock.calls[0]?.[0] as OutboundMessage | undefined;
    expect(outbound?.attachments).toBeUndefined();
  });
});

describe('daemon reactions routing integration', () => {
  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('rewrites automation event prompts when a reaction rule matches', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');

    const session = {
      id: 'gmail:reaction-user-1',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['default'],
        getAllLabels: () => ({ default: 'default' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: { clone() { return this; }, register: vi.fn() } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'default',
            memory_extraction: 'default',
            classification: 'default',
            tool_summarisation: 'default',
            complex_reasoning: 'default',
          },
          max_delegation_depth: 1,
          max_iterations: 3,
        },
        automation: {
          reactions: [{
            name: 'boss-email',
            enabled: true,
            on: ['gmail'],
            filter: { contains: 'boss@company.com' },
            run: 'Summarize and suggest next steps:\n\n{{text}}',
          }],
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
    });

    await router.handler({
      id: 'r1',
      channel: 'gmail',
      senderId: 'reaction-user-1',
      text: 'New email from boss@company.com: Please share timeline',
      timestamp: Date.now(),
    } as MessageRouterInput, vi.fn(async (_message: OutboundMessage) => {}));

    expect(processSpy).toHaveBeenCalledTimes(1);
    const [prompt] = processSpy.mock.calls[0] ?? [];
    expect(prompt).toBe(
      'Summarize and suggest next steps:\n\nNew email from boss@company.com: Please share timeline',
    );
  });

  it('keeps original prompt when no reaction rule matches', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');

    const session = {
      id: 'gmail:reaction-user-2',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['default'],
        getAllLabels: () => ({ default: 'default' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: { clone() { return this; }, register: vi.fn() } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: {
            compaction: 'default',
            memory_extraction: 'default',
            classification: 'default',
            tool_summarisation: 'default',
            complex_reasoning: 'default',
          },
          max_delegation_depth: 1,
          max_iterations: 3,
        },
        automation: {
          reactions: [{
            name: 'boss-email',
            enabled: true,
            on: ['gmail'],
            filter: { contains: 'boss@company.com' },
            run: 'Summarize: {{text}}',
          }],
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
    });

    await router.handler({
      id: 'r2',
      channel: 'gmail',
      senderId: 'reaction-user-2',
      text: 'New email from teammate@company.com: FYI',
      timestamp: Date.now(),
    } as MessageRouterInput, vi.fn(async (_message: OutboundMessage) => {}));

    expect(processSpy).toHaveBeenCalledTimes(1);
    const [prompt] = processSpy.mock.calls[0] ?? [];
    expect(prompt).toBe('New email from teammate@company.com: FYI');
  });
});

describe('daemon auto-escalate integration', () => {
  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('retries on complex tier when auto_escalate is enabled', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
      .mockRejectedValueOnce(new Error('primary tier failed'))
      .mockResolvedValueOnce('complex-tier response');
    const setModelTierSpy = vi.spyOn(AgentOrchestrator.prototype, 'setModelTier');

    const session = {
      id: 'telegram:auto-escalate',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const router = createMessageRouter({
      sessionManager: {
        getSession: vi.fn(() => session),
      } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: {
        clone() { return this; },
        register: vi.fn(),
      } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          auto_escalate: true,
          delegation: {
            compaction: 'fast',
            memory_extraction: 'fast',
            classification: 'fast',
            tool_summarisation: 'fast',
            complex_reasoning: 'complex',
          },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
      } as unknown as MessageRouterDeps['config'],
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});
    await router.handler({
      id: 'm-auto-escalate',
      channel: 'telegram',
      senderId: 'auto-escalate',
      text: 'do something hard',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);

    expect(processSpy).toHaveBeenCalledTimes(2);
    expect(setModelTierSpy).toHaveBeenCalledWith('complex');
    expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'complex-tier response' }));
  });
});

describe('daemon talk mode (voice wake) integration', () => {
  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('ignores messages until wake phrase is used', async () => {
    const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');
    const session = {
      id: 'telegram:user-talk-1',
      addMessage: vi.fn(),
      getHistory: vi.fn(() => []),
      clear: vi.fn(),
      replaceHistory: vi.fn(),
      getConfig: vi.fn(() => undefined),
      setConfig: vi.fn(),
      deleteConfig: vi.fn(),
    };

    const router = createMessageRouter({
      sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
      modelRouter: {
        getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
        getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
        getLabel: (tier: string) => tier,
      } as unknown as MessageRouterDeps['modelRouter'],
      systemPrompt: 'test prompt',
      toolRegistry: { clone() { return this; }, register: vi.fn() } as unknown as MessageRouterDeps['toolRegistry'],
      toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
      config: {
        agents: {
          primary_tier: 'default',
          delegation: { compaction: 'fast', memory_extraction: 'fast', classification: 'fast', tool_summarisation: 'fast', complex_reasoning: 'complex' },
          max_delegation_depth: 3,
          max_iterations: 10,
        },
        compaction: { enabled: false },
        models: { default: { provider: 'anthropic', model: 'claude' } },
        audio: { talk_mode: { enabled: true, wake_phrase: 'hey flynn', timeout_ms: 120000, allow_manual_toggle: true } },
      } as unknown as MessageRouterDeps['config'],
    });

    const reply = vi.fn(async (_message: OutboundMessage) => {});

    await router.handler({
      id: 'm-talk-1',
      channel: 'telegram',
      senderId: 'user-talk-1',
      text: 'hello there',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);
    expect(processSpy).not.toHaveBeenCalled();

    await router.handler({
      id: 'm-talk-2',
      channel: 'telegram',
      senderId: 'user-talk-1',
      text: 'hey flynn what time is it?',
      timestamp: Date.now(),
    } as MessageRouterInput, reply);
    expect(processSpy).toHaveBeenCalledOnce();
    expect(processSpy).toHaveBeenCalledWith('what time is it?', undefined, undefined);
  });
});
