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

vi.mock('next/navigation', () => ({
  useRouter: vi.fn(),
}));

describe('ServerErrorPageClient', () => {
  it('renders the server error page with correct content', () => {
    const push = vi.fn();
    (useRouter as any).mockReturnValue({ push });

    render(<ServerErrorPageClient />);

    expect(screen.getByText('CRITICAL_SYSTEM_FAILURE')).toBeDefined();
    expect(screen.getByText(/The application engine encountered an unrecoverable state/)).toBeDefined();
    expect(screen.getByText(/Internal Server Error/)).toBeDefined();
  });

  it('handles home navigation', () => {
    const push = vi.fn();
    (useRouter as any).mockReturnValue({ push });

    render(<ServerErrorPageClient />);

    const homeButton = screen.getByText('Return to Pits');
    fireEvent.click(homeButton);

    expect(push).toHaveBeenCalledWith('/');
  });

  it('handles retry via page reload', () => {
    const push = vi.fn();
    (useRouter as any).mockReturnValue({ push });
    
    const reloadFn = vi.fn();
    vi.stubGlobal('location', { reload: reloadFn });

    render(<ServerErrorPageClient />);

    const retryButton = screen.getByText('Retry Session');
    fireEvent.click(retryButton);

    expect(reloadFn).toHaveBeenCalled();
    
    vi.unstubAllGlobals();
  });
});
