import { describe, it, expect } from 'vitest';
import { purgeErrors } from '../strategies/purge-errors.js';
import type { DcpMessage } from '../messages.js';

describe('purgeErrors', () => {
  it('removes tool results where content starts with Error:', () => {
    const messages: DcpMessage[] = [
      { role: 'tool', content: 'Error: file not found', tool_call_id: '1' },
      { role: 'tool', content: '{"files":[]}', tool_call_id: '2' },
    ];
    const { messages: result, stats } = purgeErrors(messages);
    expect(result).toHaveLength(1);
    expect(stats.removedCount).toBe(1);
  });

  it('removes empty tool results', () => {
    const messages: DcpMessage[] = [
      { role: 'tool', content: '', tool_call_id: '1' },
    ];
    const { messages: result, stats } = purgeErrors(messages);
    expect(result).toHaveLength(0);
    expect(stats.removedCount).toBe(1);
  });

  it('preserves valid tool results', () => {
    const messages: DcpMessage[] = [
      { role: 'tool', content: '{"files":["a.ts"]}', tool_call_id: '1' },
    ];
    const { messages: result, stats } = purgeErrors(messages);
    expect(result).toHaveLength(1);
    expect(stats.removedCount).toBe(0);
  });
});
