import { describe, expect, it } from 'vitest';
import { extractDeckEventsFromChunk } from './deck-events';
import { stripGoaFromChunk } from './goa-events';
import { createThinkTagState, stripThinkTagsFromChunk } from './think-tags';
import { extractV4RuntimeEvents } from './v4-runtime-events';

describe('terminal stream parsers', () => {
  it('extracts deck events split across chunks', () => {
    const events: Array<{ tool?: string }> = [];
    const first = extractDeckEventsFromChunk(
      '@vigthoria-deck:{"type":"tool_',
      '',
      (event) => events.push(event),
    );
    const second = extractDeckEventsFromChunk(
      'start","ts":1,"tool":"read"}\nvisible',
      first.carry,
      (event) => events.push(event),
    );
    expect(events).toHaveLength(1);
    expect(events[0].tool).toBe('read');
    expect(second.visible).toContain('visible');
  });

  it('strips GOA control records while preserving normal output', () => {
    const result = stripGoaFromChunk('@vigthoria-goa:{"executed":true}\nhello\n', '');
    expect(result.visible).toBe('hello\n');
  });

  it('removes think blocks spanning chunks', () => {
    const state = createThinkTagState();
    expect(stripThinkTagsFromChunk('before<think>secret', state)).toBe('before');
    expect(stripThinkTagsFromChunk(' reasoning</think>after', state)).toBe('after');
  });

  it('extracts V4 sub-agent runtime events', () => {
    const result = extractV4RuntimeEvents(
      '{"event":"sub_agent_spawn","agent_id":"a1","task":"inspect","ts":1}\n',
      '',
    );
    expect(result.events[0]).toMatchObject({ event: 'sub_agent_spawn', agent_id: 'a1' });
  });
});
