import { describe, it, expect } from 'vitest';
import { summarizeTraceEvent } from './traceEvent';

describe('summarizeTraceEvent', () => {
  it('tool_call: includes tool name and file_path arg', () => {
    const result = summarizeTraceEvent({
      kind: 'tool_call',
      payload: { tool: 'Read', args: { file_path: '/output/report.md' } },
    });
    expect(result).toBe('Read /output/report.md');
  });

  it('tool_call: falls back to path arg when file_path is absent', () => {
    const result = summarizeTraceEvent({
      kind: 'tool_call',
      payload: { tool: 'Glob', args: { path: '**/*.ts' } },
    });
    expect(result).toBe('Glob **/*.ts');
  });

  it('tool_result: shows ok with duration', () => {
    const result = summarizeTraceEvent({
      kind: 'tool_result',
      payload: { tool: 'Read', isError: false, durationMs: 250 },
    });
    expect(result).toBe('Read ok 250ms');
  });

  it('tool_result: shows error flag', () => {
    const result = summarizeTraceEvent({
      kind: 'tool_result',
      payload: { tool: 'Bash', isError: true, durationMs: 50 },
    });
    expect(result).toContain('⚠ error');
  });

  it('tool_result: shows cached flag when cacheHit is true', () => {
    const result = summarizeTraceEvent({
      kind: 'tool_result',
      payload: { tool: 'WebFetch', isError: false, cacheHit: true, durationMs: 10 },
    });
    expect(result).toContain('(cached)');
  });

  it('transition: shows next step', () => {
    const result = summarizeTraceEvent({
      kind: 'transition',
      payload: { nextStep: 'verify' },
    });
    expect(result).toBe('→ verify');
  });

  it('complete: shows status string', () => {
    const result = summarizeTraceEvent({
      kind: 'complete',
      payload: { status: 'success' },
    });
    expect(result).toBe('success');
  });

  it('movement_start: shows visit counts', () => {
    const result = summarizeTraceEvent({
      kind: 'movement_start',
      payload: { visitCount: 2, maxVisits: 5, movement: 'execute' },
    });
    expect(result).toBe('visit 2/5');
  });

  it('movement_complete: shows next step from payload.next', () => {
    const result = summarizeTraceEvent({
      kind: 'movement_complete',
      payload: { next: 'verify', movement: 'execute' },
    });
    expect(result).toBe('→ verify');
  });

  it('unknown kind: returns empty string', () => {
    const result = summarizeTraceEvent({
      kind: 'some_future_event',
      payload: { data: 123 },
    });
    expect(result).toBe('');
  });

  it('null payload: returns empty string', () => {
    const result = summarizeTraceEvent({ kind: 'tool_call', payload: null });
    expect(result).toBe('');
  });

  it('llm_call_start: shows iteration and message count', () => {
    const result = summarizeTraceEvent({
      kind: 'llm_call_start',
      payload: { iteration: 3, messageCount: 12 },
    });
    expect(result).toBe('iter=3 msgs=12');
  });

  it('run_start: shows piece name', () => {
    const result = summarizeTraceEvent({
      kind: 'run_start',
      payload: { pieceName: 'chat' },
    });
    expect(result).toBe('piece=chat');
  });
});
