import { describe, expect, it } from 'vitest'
import {
  findExploreToolStartLineIndices,
  findLastExploreMarkerLineIndex,
  getExploreCollapsedPreviewLines,
  isExploreMarkerLine,
  isExploreToolStartLine,
  parseExploreNamedMarkerLine,
  parseExploreToolLine,
  safeParseExploreArgs,
  summarizeExploreToolArgs,
} from './explore-rendering'

describe('explore rendering helpers', () => {
  it('detects marker and tool lines', () => {
    expect(isExploreMarkerLine('[result] read-file')).toBe(true)
    expect(isExploreToolStartLine('[tool] read-file {"path":"/tmp/demo.ts"}')).toBe(true)
    expect(findLastExploreMarkerLineIndex(['', '[tool] one', '[result] one'])).toBe(2)
    expect(findExploreToolStartLineIndices(['[tool] one', 'x', '[tool] two'])).toEqual([0, 2])
  })

  it('parses tool and named marker lines', () => {
    expect(
      parseExploreToolLine('[tool] read-file {"path":"/tmp/demo.ts","_call_id":"call-1"}'),
    ).toEqual({
      argsText: '{"path":"/tmp/demo.ts","_call_id":"call-1"}',
      callId: 'call-1',
      toolName: 'read-file',
    })
    expect(parseExploreNamedMarkerLine('[stderr] shell call-2')).toEqual({
      callId: 'call-2',
      kind: 'stderr',
      toolName: 'shell',
    })
    expect(safeParseExploreArgs('{"path":"/tmp/demo.ts"}')).toEqual({ path: '/tmp/demo.ts' })
  })

  it('summarizes args and builds collapsed previews', () => {
    expect(
      summarizeExploreToolArgs('read-file', '{"path":"/tmp/demo.ts","offset":10,"limit":5}'),
    ).toContain('/tmp/demo.ts')
    expect(
      summarizeExploreToolArgs('websearch', '{"query":"needle"}'),
    ).toContain('needle')

    const previews = getExploreCollapsedPreviewLines({
      output: '[tool] websearch {"query":"needle"}\n[result] websearch\nfound needle',
    })

    expect(previews[0]).toContain('[tool]')
    expect(previews[1]).toContain('found needle')
  })
})
