/**
 * @vitest-environment happy-dom
 */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { cleanup, render, screen, waitFor } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import SeerPage from './SeerPage';
import { routerFuture } from '../routerFuture';
import { api } from '../api/client';

vi.mock('../hooks/useWebSocket', () => ({
  useWebSocket: () => ({ latestMessage: null, isConnected: true }),
}));

function renderSeer() {
  return render(
    <MemoryRouter initialEntries={['/seer']} future={routerFuture}>
      <SeerPage />
    </MemoryRouter>,
  );
}

describe('SeerPage', () => {
  beforeEach(() => {
    vi.spyOn(api, 'getSeerStream').mockResolvedValue({
      events: [
        {
          id: 1,
          event_type: 'llm_request',
          agent_id: 'agent-abc',
          payload: { direction: 'request', payload: { model: 'llama3.2' } },
          ts: '2026-06-07T12:00:00.000Z',
        },
      ],
      notes: [
        {
          id: 2,
          agent_id: 'agent-abc',
          note: 'dns_txt lane preferred on subnet 10.0.1.0/24',
          source: 'abc123',
          ts: '2026-06-07T12:00:01.000Z',
        },
      ],
    });
  });

  afterEach(() => {
    vi.restoreAllMocks();
    cleanup();
  });

  it('renders black terminal panes without input', async () => {
    renderSeer();
    expect(await screen.findByRole('heading', { name: /The Seer/i })).toBeInTheDocument();
    expect(screen.getByText(/FLEET AI STREAM/i)).toBeInTheDocument();
    expect(screen.getByText(/SEER NOTES/i)).toBeInTheDocument();
    expect(screen.queryByRole('textbox')).toBeNull();
    expect(screen.queryByRole('searchbox')).toBeNull();
    await waitFor(() => {
      expect(screen.getByText(/llama3.2/)).toBeInTheDocument();
      expect(screen.getByText(/dns_txt lane preferred/i)).toBeInTheDocument();
    });
  });

  it('shows live status when websocket connected', async () => {
    renderSeer();
    expect(await screen.findByText(/LIVE — streaming fleet AI payloads/i)).toBeInTheDocument();
  });
});
