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

describe('FatalErrorTemplate', () => {
  const mockError = new Error('Fatal system error');
  const mockViewData: FatalErrorViewData = {
    error: mockError
  };

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

  it('renders the error message via ErrorScreen', () => {
    render(<FatalErrorTemplate viewData={mockViewData} reset={mockReset} onHome={mockOnHome} />);
    
    expect(screen.getByText('Fatal system error')).toBeDefined();
    expect(screen.getByText('System Malfunction')).toBeDefined();
  });

  it('calls reset when Retry Session is clicked', () => {
    render(<FatalErrorTemplate viewData={mockViewData} 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(<FatalErrorTemplate viewData={mockViewData} reset={mockReset} onHome={mockOnHome} />);
    
    const button = screen.getByText('Return to Pits');
    fireEvent.click(button);
    
    expect(mockOnHome).toHaveBeenCalledTimes(1);
  });
});
