import { afterEach, describe, expect, it, vi } from 'vitest';

import { renderStartupError } from './startup-error';

describe('startup error fallback', () => {
  afterEach(() => {
    document.body.replaceChildren();
  });

  it('renders a recoverable error state and reloads on demand', () => {
    const root = document.createElement('div');
    const reload = vi.fn();
    document.body.append(root);

    expect(renderStartupError({ reload, root })).toBe(true);
    expect(root.querySelector('[role="alert"]')).not.toBeNull();
    expect(root.textContent).toContain('页面加载失败');

    const retryButton = root.querySelector<HTMLButtonElement>('button');
    retryButton?.click();
    expect(reload).toHaveBeenCalledOnce();
  });

  it('returns false when the application root is missing', () => {
    expect(renderStartupError({ root: null })).toBe(false);
  });
});
