import { describe, it, expect } from 'vitest';
import { deduplicate } from '../strategies/deduplication.js';
import type { DcpMessage } from '../messages.js';

describe('deduplicate', () => {
  it('removes consecutive identical tool_call+tool_result pairs', () => {
    const messages: DcpMessage[] = [
      { role: 'user', content: 'search for x' },
      { role: 'assistant', content: '', tool_calls: [{ id: '1', name: 'grep', arguments: '{}' }] },
      { role: 'tool', content: 'result1', tool_call_id: '1' },
      // Duplicate pair
      { role: 'assistant', content: '', tool_calls: [{ id: '2', name: 'grep', arguments: '{}' }] },
      { role: 'tool', content: 'result1', tool_call_id: '2' },
    ];

    const { messages: result, stats } = deduplicate(messages);
    expect(result).toHaveLength(3); // user + first pair
    expect(stats.removedCount).toBe(2);
  });

  it('preserves non-duplicate content', () => {
    const messages: DcpMessage[] = [
      { role: 'assistant', content: '', tool_calls: [{ id: '1', name: 'grep', arguments: '{}' }] },
      { role: 'tool', content: 'result1', tool_call_id: '1' },
      { role: 'assistant', content: '', tool_calls: [{ id: '2', name: 'grep', arguments: '{}' }] },
      { role: 'tool', content: 'result2', tool_call_id: '2' }, // Different result
    ];

    const { messages: result, stats } = deduplicate(messages);
    expect(result).toHaveLength(4);
    expect(stats.removedCount).toBe(0);
  });
});
