import { describe, it, expect } from 'vitest';
import { AgentRouter } from './router.js';
import type { RoutingConfig } from '../config/schema.js';

describe('AgentRouter', () => {
  describe('resolve()', () => {
    it('returns default_agent when no specific match', () => {
      const router = new AgentRouter({
        default_agent: 'assistant',
        channels: {},
        senders: {},
      });
      expect(router.resolve('telegram', '12345')).toBe('assistant');
    });

    it('returns undefined when no default and no match', () => {
      const router = new AgentRouter({
        channels: {},
        senders: {},
      });
      expect(router.resolve('telegram', '12345')).toBeUndefined();
    });

    it('matches exact sender', () => {
      const router = new AgentRouter({
        default_agent: 'assistant',
        channels: {},
        senders: { 'telegram:12345': 'coder' },
      });
      expect(router.resolve('telegram', '12345')).toBe('coder');
    });

    it('matches sender with glob pattern', () => {
      const router = new AgentRouter({
        default_agent: 'assistant',
        channels: {},
        senders: { 'slack:U0*': 'coder' },
      });
      expect(router.resolve('slack', 'U0ABC')).toBe('coder');
    });

    it('matches channel when no sender match', () => {
      const router = new AgentRouter({
        default_agent: 'assistant',
        channels: { discord: 'coder' },
        senders: {},
      });
      expect(router.resolve('discord', 'any-user')).toBe('coder');
    });

    it('sender match takes priority over channel match', () => {
      const router = new AgentRouter({
        default_agent: 'assistant',
        channels: { discord: 'coder' },
        senders: { 'discord:special-user': 'vip' },
      });
      expect(router.resolve('discord', 'special-user')).toBe('vip');
      expect(router.resolve('discord', 'normal-user')).toBe('coder');
    });

    it('falls through: sender -> channel -> default', () => {
      const router = new AgentRouter({
        default_agent: 'fallback',
        channels: { discord: 'guild-agent' },
        senders: { 'discord:admin': 'admin-agent' },
      });
      expect(router.resolve('discord', 'admin')).toBe('admin-agent');
      expect(router.resolve('discord', 'regular')).toBe('guild-agent');
      expect(router.resolve('telegram', 'someone')).toBe('fallback');
    });
  });
});
