import { describe, it, expect } from 'vitest';
import { classifyPrompt } from '../classifier.js';

describe('classifyPrompt', () => {
  it('classifies code prompts', () => {
    const result = classifyPrompt([
      { role: 'user', content: 'Write a typescript function to sort an array' },
    ]);
    expect(result.category).toBe('code');
    expect(result.estimatedTokens).toBeGreaterThan(0);
  });

  it('classifies code with keywords like refactor and debug', () => {
    const result = classifyPrompt([
      { role: 'user', content: 'Debug this error in my React component and refactor the handler' },
    ]);
    expect(result.category).toBe('code');
  });

  it('classifies math prompts', () => {
    const result = classifyPrompt([
      { role: 'user', content: 'Calculate the integral of x^2 from 0 to 5' },
    ]);
    expect(result.category).toBe('math');
  });

  it('classifies reasoning prompts', () => {
    const result = classifyPrompt([
      {
        role: 'user',
        content:
          'Explain step by step why this approach has trade-offs and analyze the implications',
      },
    ]);
    expect(result.category).toBe('reasoning');
  });

  it('classifies creative prompts', () => {
    const result = classifyPrompt([
      { role: 'user', content: 'Write a short story about a robot who learns to paint' },
    ]);
    expect(result.category).toBe('creative');
  });

  it('defaults to general for ambiguous prompts', () => {
    const result = classifyPrompt([{ role: 'user', content: 'Hello, how are you?' }]);
    expect(result.category).toBe('general');
  });

  it('estimates tokens roughly correctly', () => {
    const text = 'a'.repeat(400); // ~100 tokens
    const result = classifyPrompt([{ role: 'user', content: text }]);
    expect(result.estimatedTokens).toBe(100);
  });

  it('handles multi-message conversations', () => {
    const result = classifyPrompt([
      { role: 'system', content: 'You are a coding assistant' },
      { role: 'user', content: 'Fix the bug in my python function' },
    ]);
    expect(result.category).toBe('code');
  });

  it('detects code blocks in backticks', () => {
    const result = classifyPrompt([
      {
        role: 'user',
        content: 'What is wrong with this?\n```\nconst x = 1;\nconsole.log(x);\n```',
      },
    ]);
    expect(result.category).toBe('code');
  });
});
