Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

37.5 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { GatewayClient } from './client';

// Flag to control mock WS behavior
let mockWsShouldThrow = false;
// When set, the socket never opens — the real-world black-holed TLS handshake,
// which raises no event at all rather than an error.
let mockWsShouldHang = false;
const mockWsInstances: { options?: unknown; url: string }[] = [];

// Mock ws module — must use dynamic import for EventEmitter to avoid hoisting issues
vi.mock('ws', async () => {
  const { EventEmitter } = await import('node:events');
  class MockWebSocket extends EventEmitter {
    static OPEN = 1;
    static CONNECTING = 0;
    static CLOSING = 2;
    static CLOSED = 3;
    readyState = 1; // OPEN

    constructor(
      public url: string,
      public options?: unknown,
    ) {
      super();
      if (mockWsShouldThrow) {
        mockWsShouldThrow = false;
        throw new Error('connection refused');
      }
      mockWsInstances.push(this);
      if (mockWsShouldHang) {
        this.readyState = 0; // CONNECTING, and it stays there
        return;
      }
      // Simulate async open
      setTimeout(() => this.emit('open'), 0);
    }

    send = vi.fn();
    close = vi.fn();
    override removeAllListeners = vi.fn(() => {
      return this;
    });
  }
  return { default: MockWebSocket };
});

// Mock os
vi.mock('node:os', () => ({
  default: {
    hostname: () => 'test-host',
  },
}));

describe('GatewayClient', () => {
  let client: GatewayClient;

  beforeEach(() => {
    vi.useFakeTimers();
    mockWsShouldHang = false;
    mockWsInstances.length = 0;
    client = new GatewayClient({
      autoReconnect: false,
      deviceId: 'test-device-id',
      gatewayUrl: 'https://gateway.test.com',
      serverUrl: 'https://app.test.com',
      token: 'test-token',
      tokenType: 'apiKey',
      userId: 'test-user',
    });
  });

  afterEach(() => {
    client.disconnect();
    vi.useRealTimers();
    vi.restoreAllMocks();
  });

  describe('constructor', () => {
    it('should set default values', () => {
      const c = new GatewayClient({ token: 'tok' });
      expect(c.connectionStatus).toBe('disconnected');
      expect(c.currentDeviceId).toBeDefined();
    });

    it('should use provided options', () => {
      expect(client.currentDeviceId).toBe('test-device-id');
      expect(client.connectionStatus).toBe('disconnected');
    });
  });

  describe('connect', () => {
    it('should transition to connecting then authenticating on open', async () => {
      const statusChanges: string[] = [];
      client.on('status_changed', (s) => statusChanges.push(s));

      client.connect();
      expect(client.connectionStatus).toBe('connecting');

      // Let the mock WebSocket emit 'open'
      await vi.advanceTimersByTimeAsync(1);

      expect(client.connectionStatus).toBe('authenticating');
      expect(statusChanges).toContain('connecting');
      expect(statusChanges).toContain('authenticating');

      const ws = (client as any).ws;
      expect(ws.send).toHaveBeenCalledWith(
        JSON.stringify({
          serverUrl: 'https://app.test.com',
          token: 'test-token',
          tokenType: 'apiKey',
          type: 'auth',
        }),
      );
    });

    it('should not reconnect if already connected', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      // Simulate auth success
      const handler = (client as any).handleMessage;
      handler(JSON.stringify({ type: 'auth_success' }));

      expect(client.connectionStatus).toBe('connected');

      // Calling connect again should be a no-op
      client.connect();
      expect(client.connectionStatus).toBe('connected');
    });

    it('should not reconnect if connecting', () => {
      client.connect();
      expect(client.connectionStatus).toBe('connecting');
      client.connect(); // no-op
      expect(client.connectionStatus).toBe('connecting');
    });

    it('should build correct WebSocket URL with https', () => {
      client.connect();
      const ws = (client as any).ws;
      expect(ws.url).toContain('wss://gateway.test.com/ws');
      expect(ws.url).toContain('deviceId=test-device-id');
      expect(ws.url).toContain('hostname=test-host');
      expect(ws.url).toContain('userId=test-user');
    });

    it('should include connectionId and channel in the URL when provided', () => {
      const c = new GatewayClient({
        autoReconnect: false,
        channel: 'cli',
        connectionId: 'conn-123',
        deviceId: 'dev-1',
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
      });
      c.connect();
      const ws = (c as any).ws;
      expect(ws.url).toContain('connectionId=conn-123');
      expect(ws.url).toContain('channel=cli');
      expect(c.currentConnectionId).toBe('conn-123');
      c.disconnect();
    });

    it('should default connectionId to a generated UUID when omitted', () => {
      const c = new GatewayClient({ autoReconnect: false, token: 'tok' });
      c.connect();
      const ws = (c as any).ws;
      expect(ws.url).toContain(`connectionId=${c.currentConnectionId}`);
      expect(ws.url).not.toContain('channel=');
      c.disconnect();
    });

    it('should build ws URL for http gateway', () => {
      const c = new GatewayClient({
        autoReconnect: false,
        gatewayUrl: 'http://localhost:3000',
        token: 'tok',
      });
      c.connect();
      const ws = (c as any).ws;
      expect(ws.url).toContain('ws://localhost:3000/ws');
      c.disconnect();
    });

    it('should keep a public path prefix and ignore a trailing slash', () => {
      const c = new GatewayClient({
        autoReconnect: false,
        gatewayUrl: 'https://edge.example.com/device-gateway/',
        token: 'tok',
      });
      c.connect();
      const ws = new URL((c as any).ws.url);
      expect(`${ws.protocol}//${ws.host}${ws.pathname}`).toBe(
        'wss://edge.example.com/device-gateway/ws',
      );
      c.disconnect();
    });

    it('should include user agent header when provided', () => {
      const c = new GatewayClient({
        autoReconnect: false,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
        userAgent: 'LobeHub Desktop/1.2.3',
      });
      c.connect();
      const ws = (c as any).ws;
      expect(ws.options).toEqual({
        handshakeTimeout: 15_000,
        headers: { 'User-Agent': 'LobeHub Desktop/1.2.3' },
      });
      c.disconnect();
    });
  });

  describe('connect watchdog', () => {
    const makeClient = (connectTimeoutMs = 15_000) =>
      new GatewayClient({
        autoReconnect: true,
        connectTimeoutMs,
        deviceId: 'test-device-id',
        gatewayUrl: 'https://gateway.test.com',
        serverUrl: 'https://app.test.com',
        token: 'test-token',
        tokenType: 'apiKey',
        userId: 'test-user',
      });

    it('bounds the handshake inside ws itself', () => {
      client.connect();

      expect(mockWsInstances.at(-1)?.options).toMatchObject({ handshakeTimeout: 15_000 });
    });

    it('retries when the handshake hangs without raising an event', () => {
      // A black-holed TLS handshake emits no open/error/close, so nothing ever
      // called scheduleReconnect: the daemon reported `connecting` while the
      // device sat offline for 15+ minutes, and the run silently fell through
      // to the cloud sandbox.
      mockWsShouldHang = true;
      const hung = makeClient(1000);
      hung.connect();

      expect(hung.connectionStatus).toBe('connecting');
      expect(mockWsInstances).toHaveLength(1);

      vi.advanceTimersByTime(1000);
      expect(hung.connectionStatus).toBe('reconnecting');

      // ...and the backoff actually produces a fresh attempt.
      mockWsShouldHang = false;
      vi.advanceTimersByTime(1000);
      expect(mockWsInstances.length).toBeGreaterThan(1);

      hung.disconnect();
    });

    it('retries when the socket opens but auth never comes back', async () => {
      // `startHeartbeat` only runs on auth_success, so a lost auth reply left
      // the client parked in `authenticating` with no watchdog of any kind.
      const stalled = makeClient(1000);
      stalled.connect();
      await vi.advanceTimersByTimeAsync(0);
      expect(stalled.connectionStatus).toBe('authenticating');

      vi.advanceTimersByTime(1000);
      expect(stalled.connectionStatus).toBe('reconnecting');

      stalled.disconnect();
    });

    it('disarms once authenticated', async () => {
      const connected = makeClient(1000);
      connected.connect();
      await vi.advanceTimersByTimeAsync(0);
      mockWsInstances.at(-1);
      connected['handleMessage'](JSON.stringify({ type: 'auth_success' }));
      expect(connected.connectionStatus).toBe('connected');

      const attemptsBefore = mockWsInstances.length;
      vi.advanceTimersByTime(10_000);

      expect(connected.connectionStatus).toBe('connected');
      expect(mockWsInstances).toHaveLength(attemptsBefore);

      connected.disconnect();
    });
  });

  describe('message handling', () => {
    let handler: (data: any) => void;

    beforeEach(async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);
      handler = (client as any).handleMessage;
    });

    it('should handle auth_success', () => {
      const connectedCb = vi.fn();
      client.on('connected', connectedCb);

      handler(JSON.stringify({ type: 'auth_success' }));

      expect(client.connectionStatus).toBe('connected');
      expect(connectedCb).toHaveBeenCalled();
    });

    it('should handle auth_failed', () => {
      const authFailedCb = vi.fn();
      client.on('auth_failed', authFailedCb);

      handler(JSON.stringify({ type: 'auth_failed', reason: 'invalid token' }));

      expect(authFailedCb).toHaveBeenCalledWith('invalid token');
    });

    it('should handle auth_failed with no reason', () => {
      const authFailedCb = vi.fn();
      client.on('auth_failed', authFailedCb);

      handler(JSON.stringify({ type: 'auth_failed' }));

      expect(authFailedCb).toHaveBeenCalledWith('Unknown reason');
    });

    it('should handle heartbeat_ack', () => {
      const heartbeatCb = vi.fn();
      client.on('heartbeat_ack', heartbeatCb);

      handler(JSON.stringify({ type: 'heartbeat_ack' }));

      expect(heartbeatCb).toHaveBeenCalled();
    });

    it('should handle tool_call_request', () => {
      const toolCallCb = vi.fn();
      client.on('tool_call_request', toolCallCb);

      const msg = {
        type: 'tool_call_request',
        requestId: 'req-1',
        toolCall: { apiName: 'readFile', arguments: '{}', identifier: 'test' },
      };
      handler(JSON.stringify(msg));

      expect(toolCallCb).toHaveBeenCalledWith(msg);
    });

    it('should handle message_api_request', () => {
      const messageApiCb = vi.fn();
      client.on('message_api_request', messageApiCb);

      const msg = {
        api: {
          apiName: 'sendText',
          payload: { chatGuid: 'chat-1', message: 'hello' },
          platform: 'imessage',
        },
        requestId: 'req-message-1',
        type: 'message_api_request',
      };
      handler(JSON.stringify(msg));

      expect(messageApiCb).toHaveBeenCalledWith(msg);
    });

    it('should handle system_info_request', () => {
      const sysInfoCb = vi.fn();
      client.on('system_info_request', sysInfoCb);

      const msg = { type: 'system_info_request', requestId: 'req-2' };
      handler(JSON.stringify(msg));

      expect(sysInfoCb).toHaveBeenCalledWith(msg);
    });

    it('should handle rpc_request', () => {
      const rpcCb = vi.fn();
      client.on('rpc_request', rpcCb);

      const msg = {
        method: 'initWorkspace',
        params: { scope: '/proj' },
        requestId: 'req-rpc',
        type: 'rpc_request',
      };
      handler(JSON.stringify(msg));

      expect(rpcCb).toHaveBeenCalledWith(msg);
    });

    it('should handle auth_expired', () => {
      const expiredCb = vi.fn();
      client.on('auth_expired', expiredCb);

      handler(JSON.stringify({ type: 'auth_expired' }));

      expect(expiredCb).toHaveBeenCalled();
    });

    it('should handle unknown message type', () => {
      // Should not throw
      handler(JSON.stringify({ type: 'unknown_type' }));
    });

    it('should handle invalid JSON', () => {
      // Should not throw
      handler('not json');
    });
  });

  describe('disconnect', () => {
    it('should set status to disconnected', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      await client.disconnect();

      expect(client.connectionStatus).toBe('disconnected');
    });
  });

  describe('sendToolCallResponse', () => {
    it('should send tool call response message', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const ws = (client as any).ws;
      client.sendToolCallResponse({
        requestId: 'req-1',
        result: { content: 'result', success: true },
      });

      expect(ws.send).toHaveBeenCalledWith(
        JSON.stringify({
          requestId: 'req-1',
          result: { content: 'result', success: true },
          type: 'tool_call_response',
        }),
      );
    });
  });

  describe('sendMessageApiResponse', () => {
    it('should send message API response message', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const ws = (client as any).ws;
      client.sendMessageApiResponse({
        requestId: 'req-message-1',
        result: { content: '{"ok":true}', success: true },
      });

      expect(ws.send).toHaveBeenCalledWith(
        JSON.stringify({
          requestId: 'req-message-1',
          result: { content: '{"ok":true}', success: true },
          type: 'message_api_response',
        }),
      );
    });
  });

  describe('sendSystemInfoResponse', () => {
    it('should send system info response message', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const ws = (client as any).ws;
      client.sendSystemInfoResponse({
        requestId: 'req-2',
        result: {
          success: true,
          systemInfo: {
            arch: 'x64',
            desktopPath: '/home/test/Desktop',
            documentsPath: '/home/test/Documents',
            downloadsPath: '/home/test/Downloads',
            homePath: '/home/test',
            musicPath: '/home/test/Music',
            picturesPath: '/home/test/Pictures',
            userDataPath: '/home/test/.lobehub',
            videosPath: '/home/test/Videos',
            workingDirectory: '/home/test',
          },
        },
      });

      expect(ws.send).toHaveBeenCalled();
      const sentData = JSON.parse(ws.send.mock.calls.at(-1)[0]);
      expect(sentData.type).toBe('system_info_response');
      expect(sentData.requestId).toBe('req-2');
    });
  });

  describe('sendRpcResponse', () => {
    it('should send rpc response message', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const ws = (client as any).ws;
      client.sendRpcResponse({
        requestId: 'req-rpc',
        result: { data: { skills: [] }, success: true },
      });

      expect(ws.send).toHaveBeenCalledWith(
        JSON.stringify({
          requestId: 'req-rpc',
          result: { data: { skills: [] }, success: true },
          type: 'rpc_response',
        }),
      );
    });
  });

  describe('sendMessage when ws not open', () => {
    it('should not send when ws is null', () => {
      // Not connected, ws is null
      client.sendToolCallResponse({
        requestId: 'req-1',
        result: { content: 'result', success: true },
      });
      // Should not throw
    });

    it('should not send when ws is not OPEN', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const ws = (client as any).ws;
      ws.readyState = 3; // CLOSED

      client.sendToolCallResponse({
        requestId: 'req-1',
        result: { content: 'result', success: true },
      });

      // send should not have been called after auth message
      // (auth send happens when readyState was OPEN)
      const calls = ws.send.mock.calls;
      // Only the auth message was sent
      expect(calls.length).toBe(1);
    });
  });

  describe('heartbeat', () => {
    it('should send heartbeat after connection', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const handler = (client as any).handleMessage;
      handler(JSON.stringify({ type: 'auth_success' }));

      const ws = (client as any).ws;
      ws.send.mockClear();

      // Advance 30 seconds for heartbeat
      await vi.advanceTimersByTimeAsync(30_000);

      expect(ws.send).toHaveBeenCalledWith(JSON.stringify({ type: 'heartbeat' }));
    });
  });

  describe('reportMetrics', () => {
    const samples = [{ observedAt: 1 }] as any;

    const connectAndAuth = async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);
      (client as any).handleMessage(JSON.stringify({ type: 'auth_success' }));
      return (client as any).ws;
    };

    it('rejects when not connected', async () => {
      await expect(client.reportMetrics(samples)).rejects.toThrow('not connected');
    });

    it('resolves on the matching ack', async () => {
      const ws = await connectAndAuth();
      const pending = client.reportMetrics(samples);

      const sent = JSON.parse(ws.send.mock.calls.at(-1)[0]);
      expect(sent).toMatchObject({ samples, type: 'device_metrics' });
      (client as any).handleMessage(
        JSON.stringify({ accepted: 1, batchId: sent.batchId, type: 'device_metrics_ack' }),
      );

      await expect(pending).resolves.toBeUndefined();
    });

    it('rejects when no ack arrives in time', async () => {
      await connectAndAuth();
      const pending = client.reportMetrics(samples);
      const assertion = expect(pending).rejects.toThrow('Timed out');

      await vi.advanceTimersByTimeAsync(15_000);
      await assertion;
    });

    it('rejects in-flight batches when the socket goes away', async () => {
      await connectAndAuth();
      const pending = client.reportMetrics(samples);
      const assertion = expect(pending).rejects.toThrow('closed');

      await client.disconnect();
      await assertion;
    });
  });

  describe('reportToolCallCompletion', () => {
    const completion = {
      operationId: 'op-1',
      result: { content: 'done', success: true },
      toolCallId: 'call-1',
    };
    const connectAndAuth = async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);
      (client as any).handleMessage(JSON.stringify({ type: 'auth_success' }));
      return (client as any).ws;
    };
    const ack = (status: string) =>
      (client as any).handleMessage(
        JSON.stringify({
          ...completion,
          result: undefined,
          status,
          type: 'tool_call_completion_ack',
        }),
      );

    it('resolves with the gateway verdict for the same call', async () => {
      const ws = await connectAndAuth();
      const pending = client.reportToolCallCompletion(completion);

      expect(JSON.parse(ws.send.mock.calls.at(-1)[0])).toEqual({
        ...completion,
        type: 'tool_call_completion',
      });
      ack('retry');

      await expect(pending).resolves.toMatchObject({ status: 'retry', toolCallId: 'call-1' });
    });

    it('rejects when not connected, so the watcher keeps the result', async () => {
      await expect(client.reportToolCallCompletion(completion)).rejects.toThrow('not connected');
    });

    it('rejects an in-flight report when the socket goes away', async () => {
      await connectAndAuth();
      const assertion = expect(client.reportToolCallCompletion(completion)).rejects.toThrow(
        'closed',
      );

      await client.disconnect();
      await assertion;
    });
  });

  describe('tunnel serving', () => {
    it('forwards tunnel frames to the tunnel host', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);
      (client as any).handleMessage(JSON.stringify({ type: 'auth_success' }));

      const handleFrame = vi.spyOn((client as any).tunnelHost, 'handleFrame');
      const frame = { bytes: 64, connId: 'c1', type: 'tunnel_ack' };
      (client as any).handleMessage(JSON.stringify(frame));

      expect(handleFrame).toHaveBeenCalledWith(frame);
    });

    it('releases tunnels on a forced reconnect, not just on a clean close', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);
      (client as any).handleMessage(JSON.stringify({ type: 'auth_success' }));

      const closeAll = vi.spyOn((client as any).tunnelHost, 'closeAll');
      // Miss enough heartbeat acks to trip forceReconnect, which detaches
      // handleClose and so never reaches the normal close cleanup.
      await vi.advanceTimersByTimeAsync(30_000 * 5);

      expect(closeAll).toHaveBeenCalledWith('DEVICE_DISCONNECTED');
    });

    it('reports the tunnel host active count, and 0 when serving is off', () => {
      vi.spyOn((client as any).tunnelHost, 'activeCount', 'get').mockReturnValue(2);
      expect(client.activeTunnelCount).toBe(2);

      const offClient = new GatewayClient({
        autoReconnect: false,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
        tunnel: false,
      });
      expect(offClient.activeTunnelCount).toBe(0);
    });

    it('answers tunnel_open with TUNNEL_DISABLED when serving is off', async () => {
      const offClient = new GatewayClient({
        autoReconnect: false,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
        tunnel: false,
      });
      offClient.connect();
      await vi.advanceTimersByTimeAsync(1);
      (offClient as any).handleMessage(JSON.stringify({ type: 'auth_success' }));
      const ws = (offClient as any).ws;
      ws.send.mockClear();

      (offClient as any).handleMessage(
        JSON.stringify({
          connId: 'c1',
          head: { headers: [], method: 'GET', path: '/' },
          target: { host: '127.0.0.1', port: 3000 },
          type: 'tunnel_open',
        }),
      );

      expect(ws.send).toHaveBeenCalledWith(
        JSON.stringify({
          connId: 'c1',
          error: 'TUNNEL_DISABLED',
          ok: false,
          type: 'tunnel_open_ack',
        }),
      );
      offClient.disconnect();
    });
  });

  describe('reconnection', () => {
    it('should reconnect on close when autoReconnect is true', async () => {
      const reconnectClient = new GatewayClient({
        autoReconnect: true,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
      });
      const reconnectingCb = vi.fn();
      reconnectClient.on('reconnecting', reconnectingCb);

      reconnectClient.connect();
      await vi.advanceTimersByTimeAsync(1);

      // Simulate close
      const closeHandler = (reconnectClient as any).handleClose;
      closeHandler(1000, Buffer.from('normal'));

      expect(reconnectClient.connectionStatus).toBe('reconnecting');
      expect(reconnectingCb).toHaveBeenCalledWith(1000); // initial delay

      reconnectClient.disconnect();
    });

    it('should not reconnect on intentional disconnect', async () => {
      const reconnectClient = new GatewayClient({
        autoReconnect: true,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
      });

      reconnectClient.connect();
      await vi.advanceTimersByTimeAsync(1);

      await reconnectClient.disconnect();

      const disconnectedCb = vi.fn();
      reconnectClient.on('disconnected', disconnectedCb);

      // handleClose called after disconnect
      const closeHandler = (reconnectClient as any).handleClose;
      closeHandler(1000, Buffer.from(''));

      expect(reconnectClient.connectionStatus).toBe('disconnected');
    });

    it('should use exponential backoff', async () => {
      const reconnectClient = new GatewayClient({
        autoReconnect: true,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
      });
      const delays: number[] = [];
      reconnectClient.on('reconnecting', (delay) => delays.push(delay));

      reconnectClient.connect();
      await vi.advanceTimersByTimeAsync(1);

      // First close → scheduleReconnect with delay=1000, then reconnectDelay doubles to 2000
      const closeHandler = (reconnectClient as any).handleClose;
      closeHandler(1000, Buffer.from(''));
      expect(delays[0]).toBe(1000);

      // Advance to trigger reconnect → doConnect → new WS → 'open' fires → reconnectDelay resets to 1000
      // Then close again → scheduleReconnect with delay=1000 (reset by handleOpen)
      // To test true backoff, we need closes before 'open' fires.
      // Instead, verify the internal reconnectDelay doubles after scheduleReconnect
      expect((reconnectClient as any).reconnectDelay).toBe(2000);

      // Second close without letting open fire first
      closeHandler(1000, Buffer.from(''));
      expect(delays[1]).toBe(2000);
      expect((reconnectClient as any).reconnectDelay).toBe(4000);

      closeHandler(1000, Buffer.from(''));
      expect(delays[2]).toBe(4000);
      expect((reconnectClient as any).reconnectDelay).toBe(8000);

      reconnectClient.disconnect();
    });

    it('stays down when another client with the same connection id takes over', async () => {
      const replacedClient = new GatewayClient({
        autoReconnect: true,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
      });
      const replacedCb = vi.fn();
      const reconnectingCb = vi.fn();
      replacedClient.on('replaced', replacedCb);
      replacedClient.on('reconnecting', reconnectingCb);

      replacedClient.connect();
      await vi.advanceTimersByTimeAsync(1);
      const sockets = mockWsInstances.length;

      (replacedClient as any).handleClose(1000, Buffer.from('Replaced by new connection'));
      await vi.advanceTimersByTimeAsync(60_000);

      expect(replacedCb).toHaveBeenCalledTimes(1);
      expect(reconnectingCb).not.toHaveBeenCalled();
      expect(replacedClient.connectionStatus).toBe('disconnected');
      // No new socket: reconnecting would knock the other client off in turn.
      expect(mockWsInstances.length).toBe(sockets);

      replacedClient.disconnect();
    });

    const abandonThenReplace = async (
      abandonMidHandshake: boolean,
      {
        connectTimeoutMs,
        takeoverAfterMs = 0,
      }: { connectTimeoutMs?: number; takeoverAfterMs?: number } = {},
    ) => {
      const racingClient = new GatewayClient({
        autoReconnect: true,
        connectTimeoutMs,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
      });
      const replacedCb = vi.fn();
      racingClient.on('replaced', replacedCb);

      mockWsShouldHang = abandonMidHandshake;
      racingClient.connect();
      await vi.advanceTimersByTimeAsync(1);
      // Abandon the socket ourselves (watchdog path), then let the retry open.
      (racingClient as any).forceReconnect('stalled');
      mockWsShouldHang = false;
      await vi.advanceTimersByTimeAsync(1_001 + takeoverAfterMs);

      (racingClient as any).handleClose(1000, Buffer.from('Replaced by new connection'));
      const status = racingClient.connectionStatus;
      racingClient.disconnect();
      return { replaced: replacedCb.mock.calls.length > 0, status };
    };

    it('still reconnects when its own mid-handshake socket arrives late', async () => {
      // The abandoned upgrade reaches the gateway after the retry and knocks it off.
      expect(await abandonThenReplace(true)).toEqual({ replaced: false, status: 'reconnecting' });
    });

    it('lets one abandoned socket excuse only one takeover', async () => {
      const racingClient = new GatewayClient({
        autoReconnect: true,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
      });
      const replacedCb = vi.fn();
      racingClient.on('replaced', replacedCb);

      mockWsShouldHang = true;
      racingClient.connect();
      await vi.advanceTimersByTimeAsync(1);
      (racingClient as any).forceReconnect('stalled'); // abandons A1 mid-handshake
      mockWsShouldHang = false;
      await vi.advanceTimersByTimeAsync(1_001);

      // A1 lands late and knocks off A2: our own socket, so reconnect.
      (racingClient as any).handleClose(1000, Buffer.from('Replaced by new connection'));
      expect(replacedCb).not.toHaveBeenCalled();
      await vi.advanceTimersByTimeAsync(1_001);

      // Still inside the window, a real second client replaces A3: stay down.
      (racingClient as any).handleClose(1000, Buffer.from('Replaced by new connection'));
      expect(replacedCb).toHaveBeenCalledTimes(1);
      expect(racingClient.connectionStatus).toBe('disconnected');

      racingClient.disconnect();
    });

    it('sizes the self-takeover window by the configured connect timeout', async () => {
      // A 40s handshake budget: an upgrade abandoned 20s ago can still land.
      expect(
        await abandonThenReplace(true, { connectTimeoutMs: 40_000, takeoverAfterMs: 20_000 }),
      ).toEqual({ replaced: false, status: 'reconnecting' });
      // A 5s budget: 8s later that upgrade is gone, so the takeover is a peer.
      expect(
        await abandonThenReplace(true, { connectTimeoutMs: 5_000, takeoverAfterMs: 8_000 }),
      ).toEqual({ replaced: true, status: 'disconnected' });
    });

    it('treats a takeover after abandoning an opened socket as another client', async () => {
      // An opened socket was registered before the retry, so it cannot replace it.
      expect(await abandonThenReplace(false)).toEqual({ replaced: true, status: 'disconnected' });
    });

    it('should emit disconnected when autoReconnect is false and ws closes', async () => {
      const disconnectedCb = vi.fn();
      client.on('disconnected', disconnectedCb);

      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const closeHandler = (client as any).handleClose;
      closeHandler(1000, Buffer.from(''));

      expect(disconnectedCb).toHaveBeenCalled();
    });
  });

  describe('handleError', () => {
    it('should emit error event', async () => {
      const errorCb = vi.fn();
      client.on('error', errorCb);

      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const errorHandler = (client as any).handleError;
      errorHandler(new Error('test error'));

      expect(errorCb).toHaveBeenCalledWith(expect.objectContaining({ message: 'test error' }));
    });
  });

  describe('doConnect error', () => {
    it('should handle WebSocket constructor error with autoReconnect false', () => {
      mockWsShouldThrow = true;

      const disconnectedCb = vi.fn();
      const c = new GatewayClient({
        autoReconnect: false,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
      });
      c.on('disconnected', disconnectedCb);

      c.connect();

      expect(c.connectionStatus).toBe('disconnected');
      expect(disconnectedCb).toHaveBeenCalled();
    });

    it('should schedule reconnect on constructor error with autoReconnect true', () => {
      mockWsShouldThrow = true;

      const reconnectingCb = vi.fn();
      const c = new GatewayClient({
        autoReconnect: true,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
      });
      c.on('reconnecting', reconnectingCb);

      c.connect();

      expect(reconnectingCb).toHaveBeenCalled();
      c.disconnect();
    });
  });

  describe('setStatus no-op for same status', () => {
    it('should not emit status_changed if status is the same', () => {
      const statusCb = vi.fn();
      client.on('status_changed', statusCb);

      // Call setStatus with 'disconnected' (already the current status)
      (client as any).setStatus('disconnected');

      expect(statusCb).not.toHaveBeenCalled();
    });
  });

  describe('closeWebSocket edge cases', () => {
    it('should keep suppressing close errors until the socket closes', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const ws = (client as any).ws;
      ws.readyState = 0; // CONNECTING
      ws.close = vi.fn(() => {
        setTimeout(() => {
          ws.emit('error', new Error('WebSocket was closed before the connection was established'));
          ws.emit('close', 1006, Buffer.from(''));
        }, 0);
      });

      expect(() => (client as any).closeWebSocket()).not.toThrow();
      await vi.advanceTimersByTimeAsync(1);
      expect(ws.close).toHaveBeenCalled();
      expect(() => ws.emit('error', new Error('listener should be removed after close'))).toThrow(
        'listener should be removed after close',
      );
    });

    it('should handle ws.close throwing synchronously', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const ws = (client as any).ws;
      ws.readyState = 1; // OPEN
      ws.close = vi.fn(() => {
        throw new Error('close failed');
      });

      expect(() => (client as any).closeWebSocket()).not.toThrow();
      expect(ws.close).toHaveBeenCalled();
    });

    it('should handle ws in CLOSED state', async () => {
      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const ws = (client as any).ws;
      ws.readyState = 3; // CLOSED
      ws.close = vi.fn();

      (client as any).closeWebSocket();
      expect(ws.close).not.toHaveBeenCalled();
    });
  });

  describe('updateToken', () => {
    it('should update the internal token', () => {
      expect((client as any).token).toBe('test-token');
      client.updateToken('new-token');
      expect((client as any).token).toBe('new-token');
    });

    it('should use the new token on next connect', async () => {
      client.updateToken('refreshed-token');
      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const ws = (client as any).ws;
      expect(ws.send).toHaveBeenCalledWith(expect.stringContaining('"token":"refreshed-token"'));
    });
  });

  describe('reconnect', () => {
    it('should close existing connection and reconnect', async () => {
      client = new GatewayClient({
        autoReconnect: false,
        gatewayUrl: 'https://gateway.test.com',
        token: 'old-token',
      });

      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const handler = (client as any).handleMessage;
      handler(JSON.stringify({ type: 'auth_success' }));
      expect(client.connectionStatus).toBe('connected');

      // Update token and reconnect
      client.updateToken('new-token');
      await client.reconnect();
      await vi.advanceTimersByTimeAsync(1);

      expect(client.connectionStatus).toBe('authenticating');
      const ws = (client as any).ws;
      expect(ws.send).toHaveBeenCalledWith(expect.stringContaining('"token":"new-token"'));
    });

    it('should reset reconnect delay', async () => {
      const reconnectClient = new GatewayClient({
        autoReconnect: true,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
      });

      reconnectClient.connect();
      await vi.advanceTimersByTimeAsync(1);

      // Force backoff delay up
      const closeHandler = (reconnectClient as any).handleClose;
      closeHandler(1000, Buffer.from(''));
      closeHandler(1000, Buffer.from(''));
      expect((reconnectClient as any).reconnectDelay).toBe(4000);

      // reconnect() should reset
      await reconnectClient.reconnect();
      expect((reconnectClient as any).reconnectDelay).toBe(1000);

      reconnectClient.disconnect();
    });
  });

  describe('missed heartbeat ack detection', () => {
    it('should force reconnect after exceeding missed heartbeat threshold', async () => {
      const reconnectClient = new GatewayClient({
        autoReconnect: true,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
      });
      const reconnectingCb = vi.fn();
      reconnectClient.on('reconnecting', reconnectingCb);

      reconnectClient.connect();
      await vi.advanceTimersByTimeAsync(1);

      // Authenticate
      const handler = (reconnectClient as any).handleMessage;
      handler(JSON.stringify({ type: 'auth_success' }));
      expect(reconnectClient.connectionStatus).toBe('connected');

      // Advance 4 heartbeat intervals without any ack (30s × 4 = 120s)
      // After 3 missed, the 4th tick triggers forced reconnect
      await vi.advanceTimersByTimeAsync(30_000 * 4);

      expect(reconnectClient.connectionStatus).toBe('reconnecting');
      expect(reconnectingCb).toHaveBeenCalled();

      reconnectClient.disconnect();
    });

    it('should reset missed counter on heartbeat_ack', async () => {
      const reconnectClient = new GatewayClient({
        autoReconnect: true,
        gatewayUrl: 'https://gateway.test.com',
        token: 'tok',
      });

      reconnectClient.connect();
      await vi.advanceTimersByTimeAsync(1);

      const handler = (reconnectClient as any).handleMessage;
      handler(JSON.stringify({ type: 'auth_success' }));

      // 2 heartbeats without ack
      await vi.advanceTimersByTimeAsync(30_000 * 2);
      expect((reconnectClient as any).missedHeartbeats).toBe(2);

      // Receive ack — counter resets
      handler(JSON.stringify({ type: 'heartbeat_ack' }));
      expect((reconnectClient as any).missedHeartbeats).toBe(0);

      reconnectClient.disconnect();
    });

    it('should emit disconnected when autoReconnect is false and heartbeats missed', async () => {
      const disconnectedCb = vi.fn();
      // client has autoReconnect: false
      client.on('disconnected', disconnectedCb);

      client.connect();
      await vi.advanceTimersByTimeAsync(1);

      const handler = (client as any).handleMessage;
      handler(JSON.stringify({ type: 'auth_success' }));

      // Advance past threshold: 4 intervals
      await vi.advanceTimersByTimeAsync(30_000 * 4);

      expect(client.connectionStatus).toBe('disconnected');
      expect(disconnectedCb).toHaveBeenCalled();
    });
  });
});