import { render, screen, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import App from './App';

describe('Polyform dashboard', () => {
  beforeEach(() => {
    vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('offline')));
  });

  afterEach(() => {
    vi.unstubAllGlobals();
  });

  it('keeps the full observability surface available when the API is offline', async () => {
    render(<App />);
    expect(screen.getByRole('heading', { name: 'Population health' })).toBeInTheDocument();
    expect(screen.getByRole('heading', { name: 'Failure risk by implementation' })).toBeInTheDocument();
    expect(screen.getByRole('heading', { name: 'Risky combinations' })).toBeInTheDocument();
    expect(screen.getByRole('heading', { name: 'Healthy executions after quarantine' })).toBeInTheDocument();
    expect(screen.getAllByRole('button', { name: /Restore / })).toHaveLength(2);
    await waitFor(() => expect(screen.getByText('API unavailable.')).toBeInTheDocument());
  });

  it('labels the telemetry privacy boundary', () => {
    render(<App />);
    expect(screen.getByText(/No filenames or file contents collected/i)).toBeInTheDocument();
  });
});
