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.

43.3 KB
Raw Blame
import { chmod, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';

import type * as DeviceControlModule from '@lobechat/device-control';
import type * as DeviceGatewayClientModule from '@lobechat/device-gateway-client';
import { GatewayClient } from '@lobechat/device-gateway-client';
import { Command } from 'commander';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import type * as ApiClientModule from '../api/client';
import type * as RefreshModule from '../auth/refresh';
import { resolveToken } from '../auth/resolveToken';
import {
  removePid,
  removeStatus,
  spawnDaemon,
  stopDaemon,
  stopDaemonAndWait,
  writeStatus,
} from '../daemon/manager';
import type * as DeviceRegister from '../device/register';
import type * as SystemInfoModule from '../device/systemInfo';
import { cliVersion } from '../pkg';
import type * as ServiceStatusModule from '../service/status';
import { loadDeviceGatewayUrl, saveDeviceGatewayUrl } from '../settings';
import { executeToolCall } from '../tools';
import { cleanupAllProcesses } from '../tools/shell';
import { log, setVerbose } from '../utils/logger';
import { registerConnectCommand } from './connect';

const systemService = vi.hoisted(() => ({ enabled: false }));
vi.mock('../service/status', async (importOriginal) => ({
  ...(await importOriginal<typeof ServiceStatusModule>()),
  readConnectServices: async () => ({
    services: systemService.enabled
      ? [
          {
            active: true,
            enabled: true,
            fragmentPath: '/etc/systemd/system/lobehub-connect.service',
            mainPid: 4567,
            scope: 'system',
            serviceName: 'lobehub-connect.service',
            subState: 'running',
            user: 'ubuntu',
          },
        ]
      : [],
    unavailableScopes: [],
  }),
}));

// Native runtime probing spawns Codex; connect.nativeRuntimes.test.ts covers it.
vi.mock('../device/systemInfo', async (importOriginal) => ({
  ...(await importOriginal<typeof SystemInfoModule>()),
  probeNativeRuntimes: vi.fn().mockResolvedValue([]),
}));

const registerDeviceMock = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
const mintWorkspaceConnectTokenMock = vi.hoisted(() => vi.fn());
const registerWorkspaceDeviceMock = vi.hoisted(() => vi.fn());

vi.mock('../device/register', async (importOriginal) => {
  const actual = await importOriginal<typeof DeviceRegister>();
  return {
    ...actual,
    mintWorkspaceConnectToken: mintWorkspaceConnectTokenMock,
    registerDevice: registerDeviceMock,
    registerWorkspaceDevice: registerWorkspaceDeviceMock,
  };
});

vi.mock('../auth/refresh', async (importOriginal) => ({
  ...(await importOriginal<typeof RefreshModule>()),
  getValidToken: vi.fn().mockResolvedValue({
    credentials: { accessToken: 'test-token', expiresAt: undefined, refreshToken: 'test-refresh' },
    status: 'ok',
  }),
}));
vi.mock('../auth/resolveToken', () => ({
  resolveToken: vi.fn().mockResolvedValue({
    serverUrl: 'https://app.lobehub.com',
    token: 'test-token',
    tokenType: 'jwt',
    userId: 'test-user',
  }),
}));
vi.mock('../settings', () => ({
  addWorkspaceEnrollment: vi.fn(),
  loadDeviceGatewayUrl: vi.fn(),
  loadOrCreateConnectionId: vi.fn().mockReturnValue('test-connection-id'),
  loadSettings: vi.fn().mockReturnValue(null),
  // Default: no persisted workspace shares, so runConnect skips the restore path.
  loadWorkspaceEnrollments: vi.fn().mockReturnValue([]),
  normalizeUrl: vi.fn((url?: string) => (url ? url.replace(/\/$/, '') : undefined)),
  removeWorkspaceEnrollment: vi.fn(),
  resolveDeviceMetricsBacklogPath: vi.fn((id: string) => `/tmp/device-metrics/${id}.json`),
  saveDeviceGatewayUrl: vi.fn(),
  saveSettings: vi.fn(),
}));

// `config.getGlobalConfig` on the server the token belongs to.
const getGlobalConfig = vi.hoisted(() => vi.fn());
const createPublicLambdaClientMock = vi.hoisted(() =>
  vi.fn(() => ({ config: { getGlobalConfig: { query: getGlobalConfig } } })),
);
// Worktree cleanups that waited for this device, drained on every connect.
const drainWorktreeCleanups = vi.hoisted(() => vi.fn(async () => ({ scheduled: true })));
const createLambdaClientMock = vi.hoisted(() =>
  vi.fn(() => ({ device: { drainWorktreeCleanups: { mutate: drainWorktreeCleanups } } })),
);
vi.mock('../api/client', async (importOriginal) => ({
  ...(await importOriginal<typeof ApiClientModule>()),
  createLambdaClient: createLambdaClientMock,
  createPublicLambdaClient: createPublicLambdaClientMock,
}));

const OFFICIAL_AUTH = {
  serverUrl: 'https://app.lobehub.com',
  token: 'test-token',
  tokenType: 'jwt' as const,
  userId: 'test-user',
};
const SELF_HOSTED = 'https://self-hosted.example.com';

vi.mock('../tools/shell', () => ({
  cleanupAllProcesses: vi.fn(),
}));

let mockRunningPid: number | null = null;
let mockSpawnedPid = 0;
let mockStatus: any = null;
vi.mock('../daemon/manager', () => ({
  appendLog: vi.fn(),
  getLogPath: vi.fn().mockReturnValue('/tmp/test-daemon.log'),
  getRunningDaemonPid: vi.fn().mockImplementation(() => mockRunningPid),
  readStatus: vi.fn().mockImplementation(() => mockStatus),
  removePid: vi.fn(),
  removeStatus: vi.fn(),
  reportDaemonStartupReady: vi.fn().mockResolvedValue(undefined),
  spawnDaemon: vi.fn().mockImplementation(() => {
    mockSpawnedPid = 99999;
    return mockSpawnedPid;
  }),
  stopDaemon: vi.fn().mockImplementation(() => {
    if (mockRunningPid !== null) {
      mockRunningPid = null;
      return true;
    }
    return false;
  }),
  stopDaemonAndWait: vi.fn().mockImplementation(async () => {
    const wasRunning = mockRunningPid !== null;
    mockRunningPid = null;
    return wasRunning;
  }),
  writeStatus: vi.fn(),
}));

vi.mock('../tools', () => ({
  executeToolCall: vi.fn().mockResolvedValue({
    content: 'tool result',
    success: true,
  }),
}));

let clientEventHandlers: Record<string, (...args: any[]) => any> = {};
let clientOptions: any = {};
let connectCalled = false;
let lastSentToolResponse: any = null;
let lastSentSystemInfoResponse: any = null;
const clientReportMetrics = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
const metricsSampler = vi.hoisted(() => ({
  flush: vi.fn().mockResolvedValue(undefined),
  options: undefined as any,
  start: vi.fn().mockResolvedValue(undefined),
  stop: vi.fn().mockResolvedValue(undefined),
}));

vi.mock('@lobechat/device-control', async (importOriginal) => ({
  ...(await importOriginal<typeof DeviceControlModule>()),
  DeviceMetricsSampler: vi.fn().mockImplementation(function (opts: any) {
    metricsSampler.options = opts;
    return metricsSampler;
  }),
}));

vi.mock('@lobechat/device-gateway-client', async (importOriginal) => ({
  // Address resolution is real; only the socket is faked.
  ...(await importOriginal<typeof DeviceGatewayClientModule>()),
  GatewayClient: vi.fn().mockImplementation(function (opts: any) {
    clientOptions = opts;
    clientEventHandlers = {};
    connectCalled = false;
    lastSentToolResponse = null;
    lastSentSystemInfoResponse = null;
    return {
      connect: vi.fn().mockImplementation(async () => {
        connectCalled = true;
      }),
      currentDeviceId: 'mock-device-id',
      disconnect: vi.fn(),
      on: vi.fn().mockImplementation((event: string, handler: (...args: any[]) => any) => {
        clientEventHandlers[event] = handler;
      }),
      reconnect: vi.fn().mockResolvedValue(undefined),
      reportMetrics: clientReportMetrics,
      sendRpcResponse: vi.fn(),
      sendSystemInfoResponse: vi.fn().mockImplementation((data: any) => {
        lastSentSystemInfoResponse = data;
      }),
      sendToolCallResponse: vi.fn().mockImplementation((data: any) => {
        lastSentToolResponse = data;
      }),
      updateToken: vi.fn(),
    };
  }),
}));

describe('connect command', () => {
  let exitSpy: ReturnType<typeof vi.spyOn>;
  let signalListeners: Map<NodeJS.Signals, Set<(...args: unknown[]) => void>>;

  beforeEach(() => {
    vi.stubEnv('LOBEHUB_CONNECT_SERVICE', undefined);
    signalListeners = new Map(
      (['SIGINT', 'SIGTERM'] as const).map((signal) => [
        signal,
        new Set(process.listeners(signal)),
      ]),
    );
    exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => {}) as any);
    mockRunningPid = null;
    mockSpawnedPid = 0;
    mockStatus = null;
    clientOptions = {};
    vi.mocked(resolveToken).mockResolvedValue(OFFICIAL_AUTH);
    vi.mocked(loadDeviceGatewayUrl).mockReturnValue(undefined);
    getGlobalConfig.mockResolvedValue({ serverConfig: {} });
  });

  afterEach(() => {
    for (const [signal, existing] of signalListeners) {
      for (const listener of process.listeners(signal)) {
        if (!existing.has(listener)) process.removeListener(signal, listener);
      }
    }
    exitSpy.mockRestore();
    vi.unstubAllEnvs();
    vi.restoreAllMocks();
    vi.clearAllMocks();
  });

  function createProgram() {
    const program = new Command();
    program.exitOverride();
    registerConnectCommand(program);
    return program;
  }

  it('marks a service child for downstream agent launches', async () => {
    vi.stubEnv('SSH_CONNECTION', 'test-ssh-session');
    await createProgram().parseAsync(['node', 'test', 'connect', '--service-child']);

    expect(process.env.LOBEHUB_CONNECT_SERVICE).toBe('1');
    expect(process.env.SSH_CONNECTION).toBe('test-ssh-session');
  });

  it('should persist deviceId in status for foreground connections', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    expect(writeStatus).toHaveBeenCalledWith(
      expect.objectContaining({ connectionStatus: 'connecting', deviceId: 'mock-device-id' }),
    );

    clientEventHandlers.connected?.();

    expect(writeStatus).toHaveBeenLastCalledWith(
      expect.objectContaining({ connectionStatus: 'connected', deviceId: 'mock-device-id' }),
    );
  });

  it('asks the server to run the worktree cleanups waiting on this device on each connect', async () => {
    drainWorktreeCleanups.mockClear();
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);
    expect(drainWorktreeCleanups).not.toHaveBeenCalled();

    clientEventHandlers.connected?.();
    clientEventHandlers.connected?.();

    expect(drainWorktreeCleanups).toHaveBeenCalledTimes(2);
    expect(drainWorktreeCleanups).toHaveBeenLastCalledWith({ deviceId: 'mock-device-id' });
  });

  it('should persist deviceId in status for daemon child connections', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect', '--daemon-child']);

    expect(writeStatus).toHaveBeenCalledWith(
      expect.objectContaining({
        cliVersion,
        connectionStatus: 'connecting',
        deviceId: 'mock-device-id',
      }),
    );

    clientEventHandlers.connected?.();

    expect(writeStatus).toHaveBeenLastCalledWith(
      expect.objectContaining({ connectionStatus: 'connected', deviceId: 'mock-device-id' }),
    );
  });

  it('samples machine health and pushes the backlog to the gateway on connect', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    expect(metricsSampler.start).toHaveBeenCalled();
    const deviceId = clientOptions.deviceId;
    expect(metricsSampler.options.storagePath).toBe(`/tmp/device-metrics/${deviceId}.json`);

    clientEventHandlers.connected?.();
    expect(metricsSampler.flush).toHaveBeenCalled();

    const samples = [{ observedAt: 1 }] as any;
    await metricsSampler.options.upload(samples);
    expect(clientReportMetrics).toHaveBeenCalledWith(samples);
  });

  it('records the request on arrival and retains its timestamp across connection updates', async () => {
    const pending = Promise.withResolvers<{ content: string; success: boolean }>();
    vi.mocked(executeToolCall).mockImplementationOnce(() => pending.promise);
    await createProgram().parseAsync(['node', 'test', 'connect', '--daemon-child']);

    const running = clientEventHandlers.tool_call_request?.({
      requestId: 'req-1',
      toolCall: { apiName: 'readLocalFile', arguments: '{}', identifier: 'test' },
      type: 'tool_call_request',
    });
    const received = vi.mocked(writeStatus).mock.lastCall?.[0];
    expect(received).toEqual(
      expect.objectContaining({ deviceId: 'mock-device-id', lastRequestAt: expect.any(String) }),
    );
    clientEventHandlers.connected?.();
    expect(vi.mocked(writeStatus).mock.lastCall?.[0]).toEqual(
      expect.objectContaining({
        connectionStatus: 'connected',
        lastRequestAt: received?.lastRequestAt,
      }),
    );
    pending.resolve({ content: 'ok', success: true });
    await running;
  });

  it('should connect to gateway', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    expect(connectCalled).toBe(true);
    expect(log.info).toHaveBeenCalledWith(expect.stringContaining('LobeHub CLI'));
  });

  it('exits with a retryable code when the workspace token mint fails transiently', async () => {
    mintWorkspaceConnectTokenMock.mockRejectedValueOnce(new Error('fetch failed'));

    const program = createProgram();
    await expect(
      program.parseAsync(['node', 'test', 'connect', '--workspace', 'ws_1']),
    ).rejects.toThrow('process.exit');
    expect(exitSpy).toHaveBeenCalledWith(75);
  });

  it('exits without retry when the server refuses the workspace token mint', async () => {
    mintWorkspaceConnectTokenMock.mockRejectedValueOnce(
      Object.assign(new Error('forbidden'), { data: { code: 'FORBIDDEN' } }),
    );

    const program = createProgram();
    await expect(
      program.parseAsync(['node', 'test', 'connect', '--workspace', 'ws_1']),
    ).rejects.toThrow('process.exit');
    expect(exitSpy).toHaveBeenCalledWith(1);
  });

  it.each([
    { error: new Error('fetch failed'), exitCode: 75, reason: 'fails transiently' },
    {
      error: Object.assign(new Error('forbidden'), { data: { code: 'FORBIDDEN' } }),
      exitCode: 1,
      reason: 'is refused by the server',
    },
  ])('exits with $exitCode when workspace registration $reason', async ({ error, exitCode }) => {
    mintWorkspaceConnectTokenMock.mockResolvedValueOnce({ token: 'workspace-token' });
    registerWorkspaceDeviceMock.mockRejectedValueOnce(error);
    exitSpy.mockImplementation(() => {
      throw new Error('process.exit');
    });

    await expect(
      createProgram().parseAsync([
        'node',
        'test',
        'connect',
        '--service-child',
        '--workspace',
        'ws_1',
      ]),
    ).rejects.toThrow('process.exit');

    expect(registerWorkspaceDeviceMock).toHaveBeenCalledOnce();
    expect(exitSpy).toHaveBeenCalledWith(exitCode);
    expect(connectCalled).toBe(false);
  });

  describe('device gateway address', () => {
    const connect = (...args: string[]) =>
      createProgram().parseAsync(['node', 'test', 'connect', ...args]);

    it("connects to the gateway the token's server advertises when none is saved", async () => {
      vi.mocked(resolveToken).mockResolvedValue({ ...OFFICIAL_AUTH, serverUrl: SELF_HOSTED });
      getGlobalConfig.mockResolvedValue({
        serverConfig: { deviceGatewayUrl: 'https://gw.example.com/edge/' },
      });

      await connect();

      expect(createPublicLambdaClientMock).toHaveBeenCalledWith(SELF_HOSTED);
      expect(clientOptions).toMatchObject({
        gatewayUrl: 'https://gw.example.com/edge',
        serverUrl: SELF_HOSTED,
        token: 'test-token',
      });
      expect(writeStatus).toHaveBeenCalledWith(
        expect.objectContaining({
          gatewaySource: 'server',
          gatewayUrl: 'https://gw.example.com/edge',
        }),
      );
      expect(log.info).toHaveBeenCalledWith(
        '  Gateway   : https://gw.example.com/edge (advertised by the server)',
      );
    });

    it.each([OFFICIAL_AUTH.serverUrl, SELF_HOSTED])(
      'uses the address saved for the token’s server ahead of its advertisement (%s)',
      async (serverUrl) => {
        vi.mocked(resolveToken).mockResolvedValue({ ...OFFICIAL_AUTH, serverUrl });
        vi.mocked(loadDeviceGatewayUrl).mockImplementation((server) =>
          server === serverUrl ? 'https://gw.saved.example' : undefined,
        );
        getGlobalConfig.mockResolvedValue({
          serverConfig: { deviceGatewayUrl: 'https://gw.example.com' },
        });

        await connect();

        // Saved, so the server is not even asked: a down server cannot block it.
        expect(getGlobalConfig).not.toHaveBeenCalled();
        expect(clientOptions.gatewayUrl).toBe('https://gw.saved.example');
        expect(log.info).toHaveBeenCalledWith(
          '  Gateway   : https://gw.saved.example (saved for this server)',
        );
        expect(saveDeviceGatewayUrl).not.toHaveBeenCalled();
      },
    );

    it('keeps the official gateway for official cloud when nothing is advertised or saved', async () => {
      await connect();

      expect(clientOptions.gatewayUrl).toBe('https://device-gateway.lobehub.com');
    });

    it('refuses to start without a gateway for a self-hosted server that advertises none', async () => {
      vi.mocked(resolveToken).mockResolvedValue({ ...OFFICIAL_AUTH, serverUrl: SELF_HOSTED });

      await expect(connect()).rejects.toThrow(
        `${SELF_HOSTED} does not advertise a device gateway, and none is saved for it.`,
      );
      expect(clientOptions).toEqual({});
    });

    it.each([OFFICIAL_AUTH.serverUrl, SELF_HOSTED])(
      'does not fall back when the config lookup fails (%s)',
      async (serverUrl) => {
        vi.mocked(resolveToken).mockResolvedValue({ ...OFFICIAL_AUTH, serverUrl });
        getGlobalConfig.mockRejectedValue(new Error('HTTP 502'));

        await expect(connect()).rejects.toThrow('Could not read the device gateway address');
        expect(clientOptions).toEqual({});
      },
    );

    it('uses --gateway without a lookup and saves it for that server', async () => {
      vi.mocked(resolveToken).mockResolvedValue({ ...OFFICIAL_AUTH, serverUrl: SELF_HOSTED });
      vi.mocked(loadDeviceGatewayUrl).mockReturnValue('https://gw.saved.example');
      getGlobalConfig.mockRejectedValue(new Error('server down'));

      await connect('--gateway', 'https://gateway.example.com/');

      expect(getGlobalConfig).not.toHaveBeenCalled();
      expect(clientOptions.gatewayUrl).toBe('https://gateway.example.com');
      expect(saveDeviceGatewayUrl).toHaveBeenCalledWith(SELF_HOSTED, 'https://gateway.example.com');
    });

    it('opens workspace shares on the same gateway as the personal connection', async () => {
      vi.mocked(resolveToken).mockResolvedValue({ ...OFFICIAL_AUTH, serverUrl: SELF_HOSTED });
      getGlobalConfig.mockResolvedValue({
        serverConfig: { deviceGatewayUrl: 'https://gw.example.com' },
      });
      await connect();
      const personal = clientOptions;

      await clientEventHandlers['rpc_request']?.({
        method: 'enrollWorkspace',
        params: { token: 'ws-token', workspaceId: 'ws-1' },
        requestId: 'rpc-1',
        type: 'rpc_request',
      });

      expect(clientOptions).not.toBe(personal);
      expect(clientOptions).toMatchObject({
        gatewayUrl: 'https://gw.example.com',
        serverUrl: SELF_HOSTED,
        token: 'ws-token',
        workspaceId: 'ws-1',
      });
    });

    it('drains the worktree cleanups of a workspace share in its own workspace', async () => {
      vi.mocked(resolveToken).mockResolvedValue({ ...OFFICIAL_AUTH, serverUrl: SELF_HOSTED });
      getGlobalConfig.mockResolvedValue({
        serverConfig: { deviceGatewayUrl: 'https://gw.example.com' },
      });
      await connect();
      await clientEventHandlers['rpc_request']?.({
        method: 'enrollWorkspace',
        params: { token: 'ws-token', workspaceId: 'ws-1' },
        requestId: 'rpc-1',
        type: 'rpc_request',
      });
      createLambdaClientMock.mockClear();
      drainWorktreeCleanups.mockClear();

      // `clientEventHandlers` now belongs to the share connection.
      clientEventHandlers.connected?.();

      expect(createLambdaClientMock).toHaveBeenCalledWith(expect.anything(), 'ws-1');
      expect(drainWorktreeCleanups).toHaveBeenCalledWith({ deviceId: 'mock-device-id' });
    });

    it('passes --gateway through to the daemon child, which resolves the same way', async () => {
      await connect('--daemon', '--gateway', 'https://gateway.example.com');

      expect(spawnDaemon).toHaveBeenCalledWith(
        expect.arrayContaining(['connect', '--gateway', 'https://gateway.example.com']),
      );
      // The parent resolves nothing itself; the child runs `runConnect`.
      expect(getGlobalConfig).not.toHaveBeenCalled();
    });
  });
  it('should pass the resolved serverUrl to GatewayClient', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    expect(clientOptions.serverUrl).toBe('https://app.lobehub.com');
  });

  it('should handle tool call requests', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    // Trigger tool call
    await clientEventHandlers['tool_call_request']?.({
      requestId: 'req-1',
      toolCall: { apiName: 'readLocalFile', arguments: '{"path":"/test"}', identifier: 'test' },
      type: 'tool_call_request',
    });

    // The connected device's credentials ride along so `readFile` can upload images.
    expect(executeToolCall).toHaveBeenCalledWith(
      'readLocalFile',
      '{"path":"/test"}',
      undefined,
      expect.objectContaining({ token: 'test-token' }),
    );
    expect(lastSentToolResponse).toEqual({
      requestId: 'req-1',
      result: {
        content: 'tool result',
        error: undefined,
        // Timed on this machine's clock, so the value is whatever the mock took
        // — what matters is that the device reports one at all: the server can
        // only observe the round trip, and cannot otherwise tell a slow tool
        // from slow transport.
        executionTimeMs: expect.any(Number),
        state: undefined,
        success: true,
      },
    });
    expect(lastSentToolResponse.result.executionTimeMs).toBeGreaterThanOrEqual(0);
  });

  it('should handle system info requests', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    await clientEventHandlers['system_info_request']?.({
      requestId: 'req-2',
      type: 'system_info_request',
    });

    expect(lastSentSystemInfoResponse).toBeDefined();
    expect(lastSentSystemInfoResponse.requestId).toBe('req-2');
    expect(lastSentSystemInfoResponse.result.success).toBe(true);
    expect(lastSentSystemInfoResponse.result.systemInfo).toHaveProperty('homePath');
    expect(lastSentSystemInfoResponse.result.systemInfo).toHaveProperty('arch');
    // `getShellInfo` is async; reading `displayName` off the promise sent nothing.
    expect(lastSentSystemInfoResponse.result.systemInfo.defaultShell).toEqual(expect.any(String));
  });

  it('should handle auth_failed', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    await clientEventHandlers['auth_failed']?.('invalid token');

    expect(log.error).toHaveBeenCalledWith(expect.stringContaining('Authentication failed'));
    expect(cleanupAllProcesses).toHaveBeenCalled();
    expect(exitSpy).toHaveBeenCalledWith(1);
  });

  it('stops when another connect process takes over the connection', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    clientEventHandlers['replaced']?.();

    expect(log.error).toHaveBeenCalledWith(expect.stringContaining('took over'));
    expect(cleanupAllProcesses).toHaveBeenCalled();
    expect(exitSpy).toHaveBeenCalledWith(1);
  });

  it('should retry auth_failed with token refresh when new token available', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    vi.mocked(resolveToken).mockResolvedValueOnce({
      serverUrl: 'https://app.lobehub.com',
      token: 'refreshed-token',
      tokenType: 'jwt',
      userId: 'test-user',
    });

    const mockClient = vi.mocked(GatewayClient).mock.results[0].value;

    await clientEventHandlers['auth_failed']?.('token expired');

    expect(log.info).toHaveBeenCalledWith(expect.stringContaining('Token refreshed'));
    expect(mockClient.updateToken).toHaveBeenCalledWith('refreshed-token');
    expect(exitSpy).not.toHaveBeenCalled();
  });

  it('should refresh token on auth_expired', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    vi.mocked(resolveToken).mockResolvedValueOnce({
      serverUrl: 'https://app.lobehub.com',
      token: 'new-token',
      tokenType: 'jwt',
      userId: 'user',
    });

    const mockClient = vi.mocked(GatewayClient).mock.results[0].value;

    await clientEventHandlers['auth_expired']?.();

    expect(log.info).toHaveBeenCalledWith(expect.stringContaining('Token refreshed'));
    expect(mockClient.updateToken).toHaveBeenCalledWith('new-token');
    expect(mockClient.reconnect).toHaveBeenCalled();
    expect(cleanupAllProcesses).not.toHaveBeenCalled();
    expect(exitSpy).not.toHaveBeenCalled();
  });

  describe.each(['auth_failed', 'auth_expired'] as const)('%s workspace refresh', (event) => {
    it.each([
      { error: new Error('fetch failed'), exitCode: 75, reason: 'a transient outage' },
      {
        error: Object.assign(new Error('forbidden'), { data: { code: 'FORBIDDEN' } }),
        exitCode: 1,
        reason: 'a server refusal',
      },
    ])('exits with $exitCode after $reason', async ({ error, exitCode }) => {
      mintWorkspaceConnectTokenMock.mockResolvedValueOnce({ token: 'workspace-token' });
      registerWorkspaceDeviceMock.mockResolvedValueOnce(undefined);
      await createProgram().parseAsync([
        'node',
        'test',
        'connect',
        '--service-child',
        '--workspace',
        'ws_1',
      ]);
      expect(connectCalled).toBe(true);
      mintWorkspaceConnectTokenMock.mockRejectedValueOnce(error);

      await clientEventHandlers[event]?.('token expired');

      expect(exitSpy).toHaveBeenCalledWith(exitCode);
      expect(cleanupAllProcesses).toHaveBeenCalledOnce();
      const client = vi.mocked(GatewayClient).mock.results[0].value;
      expect(client.disconnect).toHaveBeenCalledOnce();
      expect(client.reconnect).not.toHaveBeenCalled();
    });
  });

  it('should ignore auth_expired for api key auth', async () => {
    vi.mocked(resolveToken).mockResolvedValueOnce({
      serverUrl: 'https://self-hosted.example.com',
      token: 'test-api-key',
      tokenType: 'apiKey',
      userId: 'user',
    });
    getGlobalConfig.mockResolvedValue({
      serverConfig: { deviceGatewayUrl: 'https://gw.self-hosted.example.com' },
    });

    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    await clientEventHandlers['auth_expired']?.();

    expect(log.error).not.toHaveBeenCalled();
    expect(cleanupAllProcesses).not.toHaveBeenCalled();
    expect(exitSpy).not.toHaveBeenCalled();
  });

  it('should handle error event', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    clientEventHandlers['error']?.(new Error('connection lost'));

    expect(log.error).toHaveBeenCalledWith(expect.stringContaining('connection lost'));
  });

  it('should set verbose mode when -v flag is passed', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect', '-v']);

    expect(setVerbose).toHaveBeenCalledWith(true);
  });

  it('cleans up only its own daemon records on SIGINT', async () => {
    const sigintHandlers: Array<() => Promise<void> | void> = [];
    const origOn = process.on;
    vi.spyOn(process, 'on').mockImplementation((event: any, handler: any) => {
      if (event === 'SIGINT') sigintHandlers.push(handler);
      return origOn.call(process, event, handler);
    });

    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect', '--daemon-child']);

    // Trigger SIGINT handler
    for (const handler of sigintHandlers) {
      await handler();
    }

    // Pending health samples get a bounded push before the socket closes.
    expect(metricsSampler.stop).toHaveBeenCalledWith({ flushTimeoutMs: 3000 });
    expect(metricsSampler.stop.mock.invocationCallOrder[0]).toBeLessThan(
      vi.mocked(cleanupAllProcesses).mock.invocationCallOrder[0],
    );
    expect(cleanupAllProcesses).toHaveBeenCalled();
    expect(removeStatus).toHaveBeenCalledWith(process.pid);
    expect(removePid).toHaveBeenCalledWith(process.pid);
  });

  it('should handle auth_expired when refresh fails', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    // After initial connect, mock resolveToken to return falsy for the refresh attempt
    vi.mocked(resolveToken).mockResolvedValueOnce(undefined as any);

    await clientEventHandlers['auth_expired']?.();

    expect(log.error).toHaveBeenCalledWith(expect.stringContaining('Could not refresh'));
    expect(cleanupAllProcesses).toHaveBeenCalled();
  });

  it('should handle SIGTERM', async () => {
    const sigtermHandlers: Array<() => Promise<void> | void> = [];
    const origOn = process.on;
    vi.spyOn(process, 'on').mockImplementation((event: any, handler: any) => {
      if (event === 'SIGTERM') sigtermHandlers.push(handler);
      return origOn.call(process, event, handler);
    });

    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    for (const handler of sigtermHandlers) {
      await handler();
    }

    expect(cleanupAllProcesses).toHaveBeenCalled();
  });

  it('should generate correct system info with Movies for non-linux', async () => {
    const program = createProgram();
    await program.parseAsync(['node', 'test', 'connect']);

    await clientEventHandlers['system_info_request']?.({
      requestId: 'req-3',
      type: 'system_info_request',
    });

    const sysInfo = lastSentSystemInfoResponse.result.systemInfo;
    // On macOS (darwin), video dir should be Movies
    if (process.platform !== 'linux') {
      expect(sysInfo.videosPath).toContain('Movies');
    } else {
      expect(sysInfo.videosPath).toContain('Videos');
    }
  });

  describe.skipIf(process.platform === 'win32')('--auv', () => {
    let auvDir: string;
    let auvBinary: string;

    beforeEach(async () => {
      auvDir = await mkdtemp(path.join(os.tmpdir(), 'lh-connect-auv-'));
      auvBinary = path.join(auvDir, 'auv');
      await writeFile(auvBinary, `#!/bin/sh\nprintf '{"ok":true}'\n`);
      await chmod(auvBinary, 0o755);
      // Keep a developer's own `agent-browser` on PATH out of `supportedTools`.
      vi.stubEnv('PATH', [auvDir, '/usr/bin', '/bin'].join(path.delimiter));
    });

    afterEach(async () => {
      vi.unstubAllEnvs();
      await rm(auvDir, { force: true, recursive: true });
    });

    const computerUseCall = {
      requestId: 'req-cu',
      toolCall: {
        apiName: 'runCommand',
        arguments: JSON.stringify({ argv: ['invoke', 'display.list'] }),
        identifier: 'lobe-computer-use',
      },
      type: 'tool_call_request',
    };

    it('advertises Computer Use and runs it with the given binary', async () => {
      await createProgram().parseAsync(['node', 'test', 'connect', '--auv', auvBinary]);

      await clientEventHandlers['system_info_request']?.({
        requestId: 'r',
        type: 'system_info_request',
      });
      expect(lastSentSystemInfoResponse.result.systemInfo.supportedTools).toEqual([
        'lobe-computer-use',
      ]);

      // `runCommand` is also the shell tool's apiName; the identifier decides.
      await clientEventHandlers['tool_call_request']?.(computerUseCall);
      expect(executeToolCall).not.toHaveBeenCalled();
      expect(lastSentToolResponse.result).toMatchObject({
        state: { exitCode: 0, output: { ok: true } },
        success: true,
      });
    });

    it('does not advertise or run Computer Use without --auv', async () => {
      await createProgram().parseAsync(['node', 'test', 'connect']);

      await clientEventHandlers['system_info_request']?.({
        requestId: 'r',
        type: 'system_info_request',
      });
      expect(lastSentSystemInfoResponse.result.systemInfo).not.toHaveProperty('supportedTools');

      await clientEventHandlers['tool_call_request']?.(computerUseCall);
      expect(executeToolCall).not.toHaveBeenCalled();
      expect(lastSentToolResponse.result).toMatchObject({
        error: expect.stringContaining('--auv'),
        success: false,
      });
    });

    it('passes the absolute binary path to the daemon child', async () => {
      const relative = path.relative(process.cwd(), auvBinary);
      await createProgram().parseAsync(['node', 'test', 'connect', '--daemon', '--auv', relative]);

      expect(vi.mocked(spawnDaemon).mock.calls[0]?.[0]).toEqual(
        expect.arrayContaining(['--auv', auvBinary]),
      );
    });

    describe.skipIf(process.platform === 'win32')('browser tool', () => {
      let binDir: string;
      let binary: string;

      beforeEach(async () => {
        binDir = await mkdtemp(path.join(os.tmpdir(), 'lh-connect-browser-'));
        binary = path.join(binDir, 'agent-browser');
        // Records its argv, stdin and AGENT_BROWSER_* env, then answers like
        // `agent-browser --json batch`.
        await writeFile(
          binary,
          `#!/bin/sh\nprintf '%s ' "$@" > "$0.argv"\nenv | grep '^AGENT_BROWSER_' > "$0.env"\ncat > "$0.stdin"\nprintf '[{"command":["open"],"error":null,"result":{"title":"T","url":"https://e.com/"},"success":true}]'\n`,
        );
        await chmod(binary, 0o755);
        vi.stubEnv('PATH', `${binDir}${path.delimiter}/bin${path.delimiter}/usr/bin`);
      });

      afterEach(async () => {
        vi.unstubAllEnvs();
        await rm(binDir, { force: true, recursive: true });
      });

      const navigateCall = {
        requestId: 'req-browser',
        toolCall: {
          apiName: 'navigate',
          arguments: JSON.stringify({ __topicId: 'tpc_1', url: 'https://e.com' }),
          identifier: 'lobe-browser',
        },
        type: 'tool_call_request',
      };

      it('advertises the browser tool from agent-browser on PATH and runs calls with it', async () => {
        // A user's own agent-browser profile must not reach the model's browser.
        vi.stubEnv('AGENT_BROWSER_PROFILE', 'Default');
        await createProgram().parseAsync(['node', 'test', 'connect']);

        await clientEventHandlers['system_info_request']?.({
          requestId: 'r',
          type: 'system_info_request',
        });
        expect(lastSentSystemInfoResponse.result.systemInfo.supportedTools).toEqual([
          'lobe-browser',
        ]);

        await clientEventHandlers['tool_call_request']?.(navigateCall);
        expect(executeToolCall).not.toHaveBeenCalled();
        expect(lastSentToolResponse.result).toMatchObject({
          state: { title: 'T', url: 'https://e.com/' },
          success: true,
        });
        const argv = await readFile(`${binary}.argv`, 'utf8');
        expect(argv).toMatch(
          /^--namespace lobehub --config \S+agent-browser\.json --headed false --idle-timeout 10m --session lobehub-topic-tpc_1 --json batch --bail $/,
        );
        expect(await readFile(`${binary}.env`, 'utf8')).toBe('');
        expect(JSON.parse(await readFile(`${binary}.stdin`, 'utf8'))).toEqual([
          ['open', 'https://e.com'],
        ]);
      });

      it('rejects browser calls when no agent-browser is available', async () => {
        vi.stubEnv('PATH', '/nonexistent');
        await createProgram().parseAsync(['node', 'test', 'connect']);

        await clientEventHandlers['system_info_request']?.({
          requestId: 'r',
          type: 'system_info_request',
        });
        expect(lastSentSystemInfoResponse.result.systemInfo).not.toHaveProperty('supportedTools');

        await clientEventHandlers['tool_call_request']?.(navigateCall);
        expect(executeToolCall).not.toHaveBeenCalled();
        expect(lastSentToolResponse.result).toMatchObject({
          error: expect.stringContaining('agent-browser'),
          success: false,
        });
      });

      it('passes an explicit --agent-browser to the daemon child', async () => {
        await createProgram().parseAsync([
          'node',
          'test',
          'connect',
          '--daemon',
          '--agent-browser',
          path.relative(process.cwd(), binary),
        ]);

        expect(vi.mocked(spawnDaemon).mock.calls[0]?.[0]).toEqual(
          expect.arrayContaining(['--agent-browser', binary]),
        );
      });
    });
  });

  describe('--daemon flag', () => {
    it('should spawn daemon and exit', async () => {
      const program = createProgram();
      await program.parseAsync(['node', 'test', 'connect', '--daemon']);

      expect(spawnDaemon).toHaveBeenCalled();
      expect(log.info).toHaveBeenCalledWith(expect.stringContaining('Daemon started'));
    });

    it('should refuse if daemon already running', async () => {
      mockRunningPid = 12345;

      const program = createProgram();
      await program.parseAsync(['node', 'test', 'connect', '--daemon']);

      expect(log.error).toHaveBeenCalledWith(expect.stringContaining('already running'));
      expect(exitSpy).toHaveBeenCalledWith(1);
    });
  });

  describe('connect stop', () => {
    it('should stop running daemon', async () => {
      mockRunningPid = 12345;

      const program = createProgram();
      await program.parseAsync(['node', 'test', 'connect', 'stop']);

      expect(stopDaemon).toHaveBeenCalled();
      expect(log.info).toHaveBeenCalledWith(expect.stringContaining('Daemon stopped'));
    });

    it('should warn if no daemon is running', async () => {
      const program = createProgram();
      await program.parseAsync(['node', 'test', 'connect', 'stop']);

      expect(log.warn).toHaveBeenCalledWith(expect.stringContaining('No daemon'));
    });
  });

  describe('disconnect (alias for connect stop)', () => {
    it('should stop running daemon', async () => {
      mockRunningPid = 12345;

      const program = createProgram();
      await program.parseAsync(['node', 'test', 'disconnect']);

      expect(stopDaemon).toHaveBeenCalled();
      expect(log.info).toHaveBeenCalledWith(expect.stringContaining('Daemon stopped'));
    });

    it('should warn if no daemon is running', async () => {
      const program = createProgram();
      await program.parseAsync(['node', 'test', 'disconnect']);

      expect(log.warn).toHaveBeenCalledWith(expect.stringContaining('No daemon'));
    });
  });

  describe('connect status', () => {
    it.each([['status'], ['service', 'status']])(
      'finds a system-level connector through %j',
      async (...command) => {
        vi.spyOn(process, 'platform', 'get').mockReturnValue('linux');
        systemService.enabled = true;
        try {
          await createProgram().parseAsync(['node', 'test', 'connect', ...command, '--verbose']);
          const output = vi.mocked(log.info).mock.calls.flat().join('\n');
          expect(output).toContain('system');
          expect(output).toContain('4567');
          expect(output).toContain('ubuntu');
          expect(output).toContain('sudo systemctl --no-block restart lobehub-connect.service');
          expect(output).not.toContain('No connect service is installed');
          expect(output).not.toContain('No daemon is running');
        } finally {
          systemService.enabled = false;
        }
      },
    );

    it('should show no daemon running', async () => {
      const program = createProgram();
      await program.parseAsync(['node', 'test', 'connect', 'status']);

      expect(log.info).toHaveBeenCalledWith(expect.stringContaining('No managed connect'));
    });

    it('should show daemon status', async () => {
      mockRunningPid = 12345;
      mockStatus = {
        connectionStatus: 'connected',
        gatewayUrl: 'https://gateway.test.com',
        pid: 12345,
        startedAt: new Date(Date.now() - 3600_000).toISOString(),
      };

      const program = createProgram();
      await program.parseAsync(['node', 'test', 'connect', 'status']);

      expect(log.info).toHaveBeenCalledWith(expect.stringContaining('Daemon Status'));
      expect(log.info).toHaveBeenCalledWith(expect.stringContaining('connected'));
    });
  });

  describe('connect restart', () => {
    it.each([['--daemon']])('does not create a competing daemon through %j', async (...command) => {
      systemService.enabled = true;
      const exitCode = process.exitCode;
      try {
        await createProgram().parseAsync(['node', 'test', 'connect', ...command]);
        expect(spawnDaemon).not.toHaveBeenCalled();
        expect(log.error).toHaveBeenCalledWith(
          expect.stringContaining('sudo systemctl --no-block restart lobehub-connect.service'),
        );
        expect(process.exitCode).toBe(1);
      } finally {
        systemService.enabled = false;
        process.exitCode = exitCode;
      }
    });
    it('should stop and start daemon', async () => {
      mockRunningPid = 12345;

      const program = createProgram();
      await program.parseAsync(['node', 'test', 'connect', 'restart']);

      expect(stopDaemonAndWait).toHaveBeenCalled();
      expect(log.info).toHaveBeenCalledWith(expect.stringContaining('Stopped existing'));
      expect(spawnDaemon).toHaveBeenCalled();
    });

    it.each([
      [
        'connect',
        'restart',
        '--device-id',
        'fixture-self',
        '--gateway',
        'wss://gateway.example.test',
      ],
      [
        'connect',
        '--device-id',
        'fixture-self',
        '--gateway',
        'wss://gateway.example.test',
        'restart',
      ],
    ])('preserves explicit connection options: %j', async (...args) => {
      mockRunningPid = 12345;
      await createProgram().parseAsync(['node', 'test', ...args]);
      expect(spawnDaemon).toHaveBeenCalledWith(
        expect.arrayContaining([
          '--device-id',
          'fixture-self',
          '--gateway',
          'wss://gateway.example.test',
        ]),
      );
    });

    it('does not launch a replacement until shutdown has finished', async () => {
      mockRunningPid = 12345;
      let finishStop!: (stopped: boolean) => void;
      vi.mocked(stopDaemonAndWait).mockImplementationOnce(
        () =>
          new Promise((resolve) => {
            finishStop = resolve;
          }),
      );
      const restarting = createProgram().parseAsync(['node', 'test', 'connect', 'restart']);
      await vi.waitFor(() => expect(finishStop).toBeDefined());
      expect(spawnDaemon).not.toHaveBeenCalled();
      mockRunningPid = null;
      finishStop(true);
      await restarting;
      expect(spawnDaemon).toHaveBeenCalledOnce();
    });

    it('does not launch a replacement when the previous daemon will not stop', async () => {
      mockRunningPid = 12345;
      vi.mocked(stopDaemonAndWait).mockRejectedValueOnce(new Error('previous daemon did not exit'));
      await expect(
        createProgram().parseAsync(['node', 'test', 'connect', 'restart']),
      ).rejects.toThrow('did not exit');
      expect(spawnDaemon).not.toHaveBeenCalled();
    });

    it('should start daemon even if none was running', async () => {
      const program = createProgram();
      await program.parseAsync(['node', 'test', 'connect', 'restart']);

      expect(spawnDaemon).toHaveBeenCalled();
      expect(log.info).toHaveBeenCalledWith(expect.stringContaining('Daemon started'));
    });
  });
});