import { render, screen, fireEvent } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import { ErrorScreen } from './ErrorScreen';

describe('ErrorScreen', () => {
  const mockError = new Error('Test error message');
  (mockError as any).digest = 'test-digest';
  (mockError as any).stack = 'test-stack-trace';

  const mockReset = vi.fn();
  const mockOnHome = vi.fn();

  it('renders error message and system malfunction title', () => {
    render(<ErrorScreen error={mockError} reset={mockReset} onHome={mockOnHome} />);
    
    expect(screen.getByText('System Malfunction')).toBeDefined();
    expect(screen.getByText('Test error message')).toBeDefined();
  });

  it('calls reset when Retry Session is clicked', () => {
    render(<ErrorScreen error={mockError} reset={mockReset} onHome={mockOnHome} />);
    
    const button = screen.getByText('Retry Session');
    fireEvent.click(button);
    
    expect(mockReset).toHaveBeenCalledTimes(1);
  });

  it('calls onHome when Return to Pits is clicked', () => {
    render(<ErrorScreen error={mockError} reset={mockReset} onHome={mockOnHome} />);
    
    const button = screen.getByText('Return to Pits');
    fireEvent.click(button);
    
    expect(mockOnHome).toHaveBeenCalledTimes(1);
  });

  it('toggles technical logs visibility', () => {
    render(<ErrorScreen error={mockError} reset={mockReset} onHome={mockOnHome} />);
    
    expect(screen.queryByText('test-stack-trace')).toBeNull();
    
    const toggle = screen.getByText('Show Technical Logs');
    fireEvent.click(toggle);
    
    expect(screen.getByText(/test-stack-trace/)).toBeDefined();
    expect(screen.getByText(/Digest: test-digest/)).toBeDefined();
    
    fireEvent.click(screen.getByText('Hide Technical Logs'));
    expect(screen.queryByText(/test-stack-trace/)).toBeNull();
  });
});
