import { describe, it, expect } from 'vitest';
import { summarizeActivityLog } from './activity-summarizer.js';

describe('summarizeActivityLog', () => {
  it('keeps errors verbatim', () => {
    const events = [
      { type: 'tool_call', tool: 'Bash', args: { cmd: 'ls' } },
      { type: 'tool_error', tool: 'Bash', error: 'permission denied: /etc/shadow' },
    ];
    const out = summarizeActivityLog(events, 4096);
    expect(out).toContain('permission denied: /etc/shadow');
  });

  it('respects the byte cap', () => {
    const events = Array.from({ length: 10000 }, (_, i) => ({
      type: 'tool_call', tool: 'Read', args: { path: `f${i}` },
    }));
    const out = summarizeActivityLog(events, 4096);
    expect(Buffer.byteLength(out, 'utf8')).toBeLessThanOrEqual(4096);
  });

  it('keeps the complete() payload', () => {
    const events = [
      { type: 'tool_call', tool: 'Read', args: { path: 'x' } },
      { type: 'tool_call', tool: 'complete', args: { status: 'success', result: 'done' } },
    ];
    const out = summarizeActivityLog(events, 4096);
    expect(out).toContain('complete');
    expect(out).toContain('done');
  });

  it('is deterministic for identical input', () => {
    const events = [{ type: 'transition', from: 'a', to: 'b' }];
    expect(summarizeActivityLog(events, 4096)).toBe(summarizeActivityLog(events, 4096));
  });
});
