/**
 * audit3-followup (2026-05-29) — component-test scaffold proof point #3.
 *
 * ErrorBoundary is a class component with React's error lifecycle.
 * Testing it validates that the scaffold handles non-functional
 * components and error boundaries — patterns the broader suite will
 * lean on for larger components like ChatWidget and NOCDisplay.
 */
import { render, screen } from '@testing-library/react';
import { ErrorBoundary } from '../ErrorBoundary';

// Suppress React's "uncaught error" console noise from the synthesised
// throw inside the test — it's expected and not a real failure.
const originalError = console.error;
beforeAll(() => {
    console.error = jest.fn();
});
afterAll(() => {
    console.error = originalError;
});

function Boom({ message = 'kaboom' }: { message?: string }): React.JSX.Element {
    throw new Error(message);
}

describe('<ErrorBoundary>', () => {
    it('renders children when no error', () => {
        render(
            <ErrorBoundary>
                <p>healthy content</p>
            </ErrorBoundary>,
        );
        expect(screen.getByText('healthy content')).toBeInTheDocument();
    });

    it('catches a thrown error and shows the default fallback', () => {
        render(
            <ErrorBoundary>
                <Boom message="simulated render failure" />
            </ErrorBoundary>,
        );
        expect(screen.getByText('Something went wrong')).toBeInTheDocument();
        expect(screen.getByText(/simulated render failure/)).toBeInTheDocument();
        expect(screen.getByText('Reload Page')).toBeInTheDocument();
    });

    it('honours a custom fallback when provided', () => {
        render(
            <ErrorBoundary fallback={<p>custom fallback markup</p>}>
                <Boom />
            </ErrorBoundary>,
        );
        expect(screen.getByText('custom fallback markup')).toBeInTheDocument();
        // Default fallback markers must NOT render when override is set.
        expect(screen.queryByText('Something went wrong')).not.toBeInTheDocument();
    });
});
