// REQ-USR-003: 路由级 ErrorBoundary 子组件抛错兜底（spec § 3 error / D7）
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import AppErrorBoundary from '../../src/router/AppErrorBoundary';

function Boom(): React.ReactElement {
  throw new Error('boom');
}

describe('AppErrorBoundary', () => {
  // 抑制 React 在 ErrorBoundary 测试时打印的 error 噪声
  let spy: ReturnType<typeof vi.spyOn>;
  beforeEach(() => {
    spy = vi.spyOn(console, 'error').mockImplementation(() => {});
  });
  afterEach(() => {
    spy.mockRestore();
  });

  it('renders fallback with 返回主页 when child throws', () => {
    render(
      <MemoryRouter>
        <AppErrorBoundary>
          <Boom />
        </AppErrorBoundary>
      </MemoryRouter>,
    );
    expect(screen.getByText('页面出错，请刷新或返回主页')).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '返回主页' })).toBeInTheDocument();
  });
});
