import { describe, expect, it } from 'vitest';
import { buildClassificationPrompt, parseClassificationResponse } from './piece-classifier.js';

describe('buildClassificationPrompt', () => {
  it('includes task text and piece descriptions', () => {
    const prompt = buildClassificationPrompt(
      'Rustの最新動向を調査して',
      [
        { name: 'research', description: '調査・分析タスク' },
        { name: 'general', description: '汎用タスク' },
      ],
      [],
    );
    expect(prompt).toContain('Rustの最新動向を調査して');
    expect(prompt).toContain('research');
    expect(prompt).toContain('general');
  });

  it('includes file names when provided', () => {
    const prompt = buildClassificationPrompt(
      '集計して',
      [{ name: 'office-process', description: 'Office処理' }],
      ['sales.xlsx'],
    );
    expect(prompt).toContain('sales.xlsx');
  });

  it('biases the classifier toward chat as the default piece', () => {
    const prompt = buildClassificationPrompt(
      '何かの質問',
      [
        { name: 'chat', description: '汎用デフォルト' },
        { name: 'slide', description: 'スライド作成' },
      ],
      [],
    );
    expect(prompt).toContain('デフォルトは "chat"');
    expect(prompt).toContain('迷ったら "chat" を選ぶ');
  });
});

describe('parseClassificationResponse', () => {
  const validPieces = ['research', 'general', 'data-process'];

  it('extracts valid piece name from response', () => {
    expect(parseClassificationResponse('research', validPieces)).toBe('research');
  });

  it('extracts piece name from noisy response', () => {
    expect(parseClassificationResponse('最適なピースは research です', validPieces)).toBe('research');
  });

  it('prefers longer match over shorter (data-process over general)', () => {
    expect(parseClassificationResponse('data-process が最適', validPieces)).toBe('data-process');
  });

  it('returns null for invalid piece', () => {
    expect(parseClassificationResponse('unknown-piece', validPieces)).toBeNull();
  });

  it('returns null for empty response', () => {
    expect(parseClassificationResponse('', validPieces)).toBeNull();
  });
});
