import { describe, it, expect, vi, beforeEach } from 'vitest';
import { WebChatAdapter } from './adapter.js';
import type { GatewayServer } from '../../gateway/index.js';

/** Mock GatewayServer — the adapter wraps this but doesn't manage its lifecycle. */
const mockGateway = {
  start: vi.fn(),
  stop: vi.fn(),
  getWss: vi.fn(() => null),
  getHttpServer: vi.fn(() => null),
  getSessionBridge: vi.fn(),
  getMethods: vi.fn(() => []),
} as unknown as GatewayServer;

describe('WebChatAdapter', () => {
  let adapter: WebChatAdapter;

  beforeEach(() => {
    vi.clearAllMocks();
    adapter = new WebChatAdapter({ gateway: mockGateway });
  });

  it('has correct name', () => {
    expect(adapter.name).toBe('webchat');
  });

  it('starts as disconnected', () => {
    expect(adapter.status).toBe('disconnected');
  });

  it('connect sets status to connected', async () => {
    await adapter.connect();
    expect(adapter.status).toBe('connected');
  });

  it('disconnect sets status to disconnected', async () => {
    await adapter.connect();
    expect(adapter.status).toBe('connected');

    await adapter.disconnect();
    expect(adapter.status).toBe('disconnected');
  });

  it('connect does not call gateway.start', async () => {
    await adapter.connect();
    expect(mockGateway.start).not.toHaveBeenCalled();
  });

  it('disconnect does not call gateway.stop', async () => {
    await adapter.connect();
    await adapter.disconnect();
    expect(mockGateway.stop).not.toHaveBeenCalled();
  });

  it('send is a no-op', async () => {
    // Should not throw
    await adapter.send('peer1', { text: 'hello' });
  });

  it('getGateway returns the gateway instance', () => {
    expect(adapter.getGateway()).toBe(mockGateway);
  });
});
