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

describe('SearchLoadingModal', () => {
  it('renders when open', () => {
    render(<SearchLoadingModal isOpen={true} maxSeconds={30} />);
    expect(screen.getByText(/Searching for specs/i)).toBeInTheDocument();
  });

  it('does not render when closed', () => {
    const { container } = render(<SearchLoadingModal isOpen={false} />);
    expect(container.firstChild?.childNodes.length).toBe(0);
  });

  it('displays countdown timer', () => {
    render(<SearchLoadingModal isOpen={true} maxSeconds={30} />);
    expect(screen.getByText(/30 seconds remaining/i)).toBeInTheDocument();
  });

  it('calls onTimeout when timer expires', async () => {
    const onTimeout = vi.fn();
    render(<SearchLoadingModal isOpen={true} maxSeconds={1} onTimeout={onTimeout} />);

    await waitFor(() => {
      expect(onTimeout).toHaveBeenCalled();
    }, { timeout: 2000 });
  });

  it('shows progress bar', () => {
    const { container } = render(<SearchLoadingModal isOpen={true} maxSeconds={30} />);
    const progressBar = container.querySelector('div[class*="bg-primary"]');
    expect(progressBar).toBeInTheDocument();
  });
});
