/**
 * @vitest-environment happy-dom
 */
import { describe, expect, it } from 'vitest';
import { renderHook } from '@testing-library/react';
import { WebSocketContext, useWebSocketContext } from './WebSocketContext';
import { mockAgent } from '../test/fixtures';

describe('WebSocketContext', () => {
  it('useWebSocketContext returns default value outside provider', () => {
    const { result } = renderHook(() => useWebSocketContext());

    expect(result.current.isConnected).toBe(false);
    expect(result.current.agents).toEqual([]);
    expect(result.current.commandResults).toEqual([]);
    expect(result.current.latestMessage).toBeNull();
  });

  it('useWebSocketContext reads provider value', () => {
    const value = {
      isConnected: true,
      agents: [mockAgent()],
      recentShares: [],
      fleetAlerts: [],
      poolStatus: [],
      aiActivity: [],
      agentLogs: { 'agent-001-uuid': 'log line' },
      commandResults: [{ agent_id: 'a1', action: 'pause', success: true, _seq: 1 }],
      latestMessage: null,
      sendDashboardMessage: () => {},
    };

    const wrapper = ({ children }: { children: React.ReactNode }) => (
      <WebSocketContext.Provider value={value}>{children}</WebSocketContext.Provider>
    );

    const { result } = renderHook(() => useWebSocketContext(), { wrapper });

    expect(result.current.isConnected).toBe(true);
    expect(result.current.agents).toHaveLength(1);
    expect(result.current.agentLogs['agent-001-uuid']).toBe('log line');
    expect(result.current.commandResults[0]._seq).toBe(1);
  });
});
