import { describe, it, expect } from 'vitest';
import { selectPieceForMessage } from './skill-router';

describe('selectPieceForMessage', () => {
  it('returns null when effectiveSkills is empty', () => {
    const result = selectPieceForMessage({
      messageText: 'test',
      effectiveSkills: [],
      effectiveSpaceIds: ['space1'],
    });
    expect(result).toBeNull();
  });

  it('returns null when effectiveSpaceIds is empty', () => {
    const result = selectPieceForMessage({
      messageText: 'test',
      effectiveSkills: ['chat'],
      effectiveSpaceIds: [],
    });
    expect(result).toBeNull();
  });

  it('uses requestedSkillId if it is in effectiveSkills', () => {
    const result = selectPieceForMessage({
      messageText: 'test',
      effectiveSkills: ['chat', 'research'],
      effectiveSpaceIds: ['space1'],
      requestedSkillId: 'research',
    });
    expect(result).toEqual({ pieceName: 'research', spaceId: 'space1' });
  });

  it('returns null if requestedSkillId is NOT in effectiveSkills (fail-closed)', () => {
    const result = selectPieceForMessage({
      messageText: 'test',
      effectiveSkills: ['chat'],
      effectiveSpaceIds: ['space1'],
      requestedSkillId: 'research',
    });
    expect(result).toBeNull();
  });

  it('uses effectiveSkills[0] when requestedSkillId is absent', () => {
    const result = selectPieceForMessage({
      messageText: 'test',
      effectiveSkills: ['chat', 'research'],
      effectiveSpaceIds: ['space1', 'space2'],
    });
    expect(result).toEqual({ pieceName: 'chat', spaceId: 'space1' });
  });

  it('uses effectiveSpaceIds[0] for spaceId', () => {
    const result = selectPieceForMessage({
      messageText: 'test',
      effectiveSkills: ['research'],
      effectiveSpaceIds: ['space2', 'space3'],
    });
    expect(result).toEqual({ pieceName: 'research', spaceId: 'space2' });
  });

  it('handles single skill and space', () => {
    const result = selectPieceForMessage({
      messageText: 'help me',
      effectiveSkills: ['chat'],
      effectiveSpaceIds: ['personal'],
    });
    expect(result).toEqual({ pieceName: 'chat', spaceId: 'personal' });
  });
});
