import { describe, test, expect, vi } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import { ChatToolFullViewDialog } from './chat-tool-full-view-dialog'

describe('ChatToolFullViewDialog', () => {
  const defaultProps = {
    open: true,
    onClose: vi.fn(),
    title: 'Tool executed: Output',
    content: '{"status": 200,\n"data": "test"}',
  }

  test('renders dialog with title when open', () => {
    render(<ChatToolFullViewDialog {...defaultProps} />)
    expect(screen.getByText('Tool executed: Output')).toBeTruthy()
  })

  test('renders content with line numbers', () => {
    render(<ChatToolFullViewDialog {...defaultProps} />)
    // NOTE: need to query outside of component to reach dialog portal
    const pre = document.querySelector('pre')
    expect(pre).toBeTruthy()
    // NOTE: json text is tokenized and split into various tokens so we cannot reach it fully with `queryByText`
    expect(pre!.textContent).toContain('{"status": 200,')
    expect(pre!.textContent).toContain('"data": "test"}')
  })

  test('does not render content when closed', () => {
    render(<ChatToolFullViewDialog {...defaultProps} open={false} />)
    expect(screen.queryByText('Tool executed: Output')).toBeNull()
  })

  test('calls onClose when close button is clicked', () => {
    const onClose = vi.fn()
    render(<ChatToolFullViewDialog {...defaultProps} onClose={onClose} />)
    fireEvent.click(screen.getByLabelText('close'))
    expect(onClose).toHaveBeenCalledOnce()
  })
})
