import { describe, it, expect } from 'vitest';
import { transformMessages } from '../transform.js';
import type { DcpMessage } from '../messages.js';

describe('transformMessages', () => {
  it('applies dedup then purge in order', () => {
    const input: DcpMessage[] = [
      { role: 'user', content: 'hello' },
      { role: 'assistant', content: '', tool_calls: [{ id: '1', name: 'grep', arguments: '{}' }] },
      { role: 'tool', content: 'result', tool_call_id: '1' },
      { role: 'assistant', content: '', tool_calls: [{ id: '2', name: 'grep', arguments: '{}' }] },
      { role: 'tool', content: 'result', tool_call_id: '2' }, // Dup
    ];

    const { messages, stats } = transformMessages('test-chat', input);
    expect(stats.removedCount).toBeGreaterThan(0);
    expect(messages.length).toBeLessThan(input.length);
  });

  it('handles empty input', () => {
    const { messages, stats } = transformMessages('empty', []);
    expect(messages).toHaveLength(0);
    expect(stats.removedCount).toBe(0);
  });
});
