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.

10.9 KB
Raw Blame
import type * as DeviceGatewayClientModule from '@lobechat/device-gateway-client';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import type * as ApiClientModule from '../../api/client';
import { cliVersion } from '../../pkg';
import type * as ServiceStatusModule from '../../service/status';
import type { ConnectServiceStatus } from '../../service/status';
import type * as SettingsModule from '../../settings';
import type * as ProbesModule from '../probes';
import { makeContext, runCheck } from '../testUtils';
import { deviceChecks } from './device';

const state = vi.hoisted(() => ({
  daemonPid: null as number | null,
  daemonStatus: null as any,
  devices: [] as any[],
  localDeviceId: undefined as string | undefined,
  services: [] as ConnectServiceStatus[],
}));

const removePid = vi.hoisted(() => vi.fn());
const removeStatus = vi.hoisted(() => vi.fn());

vi.mock('../../daemon/manager', () => ({
  getRunningDaemonPid: () => state.daemonPid,
  readStatus: () => state.daemonStatus,
  removePid,
  removeStatus,
}));

vi.mock('../../service/status', async (importOriginal) => ({
  ...(await importOriginal<typeof ServiceStatusModule>()),
  readConnectServices: async () => ({ services: state.services, unavailableScopes: [] }),
}));

vi.mock('../../utils/device', () => ({ resolveLocalDeviceId: () => state.localDeviceId }));

const credentialServer = vi.hoisted(() => ({ value: 'https://app.lobehub.com' }));

vi.mock('../probes', async (importOriginal) => ({
  // `probeDeviceGateway` stays real: the check must resolve like `lh connect`.
  ...(await importOriginal<typeof ProbesModule>()),
  probeCredential: async () => ({
    serverUrl: credentialServer.value,
    token: 't',
    tokenType: 'jwt',
    userId: 'u',
  }),
  probeDevices: async () => state.devices,
}));

const getGlobalConfig = vi.hoisted(() => vi.fn());
const createPublicLambdaClient = vi.hoisted(() =>
  vi.fn(() => ({ config: { getGlobalConfig: { query: getGlobalConfig } } })),
);
vi.mock('../../api/client', async (importOriginal) => ({
  ...(await importOriginal<typeof ApiClientModule>()),
  createPublicLambdaClient,
}));

const savedGateway = vi.hoisted(() => ({ value: undefined as string | undefined }));
vi.mock('../../settings', async (importOriginal) => ({
  ...(await importOriginal<typeof SettingsModule>()),
  loadDeviceGatewayUrl: () => savedGateway.value,
}));

const gatewayClients = vi.hoisted(() => [] as any[]);
vi.mock('@lobechat/device-gateway-client', async (importOriginal) => ({
  ...(await importOriginal<typeof DeviceGatewayClientModule>()),
  GatewayClient: vi.fn().mockImplementation(function (options: any) {
    const listeners: Record<string, (...args: any[]) => void> = {};
    const client = {
      connect: vi.fn(async () => listeners.connected?.()),
      disconnect: vi.fn(),
      on: (event: string, listener: (...args: any[]) => void) => {
        listeners[event] = listener;
      },
      options,
    };
    gatewayClients.push(client);
    return client;
  }),
}));

describe('device.gateway', () => {
  const SELF_HOSTED = 'https://lobe.internal';

  beforeEach(() => {
    gatewayClients.length = 0;
    credentialServer.value = SELF_HOSTED;
    savedGateway.value = undefined;
    getGlobalConfig.mockReset();
    createPublicLambdaClient.mockClear();
  });

  it("handshakes with the gateway the credential's server advertises", async () => {
    getGlobalConfig.mockResolvedValue({
      serverConfig: { deviceGatewayUrl: 'https://gw.lobe.internal/edge/' },
    });

    const outcome = await runCheck(deviceChecks, 'device.gateway', makeContext());

    expect(createPublicLambdaClient).toHaveBeenCalledWith(SELF_HOSTED);
    expect(gatewayClients.map((client) => client.options.gatewayUrl)).toEqual([
      'https://gw.lobe.internal/edge',
    ]);
    expect(outcome).toMatchObject({
      detail: 'Authenticated to https://gw.lobe.internal/edge (advertised by the server).',
      evidence: { gatewaySource: 'server', serverUrl: SELF_HOSTED },
      status: 'ok',
    });
  });

  it('handshakes with the address saved for that server ahead of its advertisement', async () => {
    savedGateway.value = 'http://127.0.0.1:8788';
    getGlobalConfig.mockResolvedValue({
      serverConfig: { deviceGatewayUrl: 'https://gw.lobe.internal/edge/' },
    });

    const outcome = await runCheck(deviceChecks, 'device.gateway', makeContext());

    expect(getGlobalConfig).not.toHaveBeenCalled();
    expect(gatewayClients.map((client) => client.options.gatewayUrl)).toEqual([
      'http://127.0.0.1:8788',
    ]);
    expect(outcome).toMatchObject({
      detail: 'Authenticated to http://127.0.0.1:8788 (saved for this server).',
      evidence: { gatewaySource: 'manual', serverUrl: SELF_HOSTED },
      status: 'ok',
    });
  });

  it('says how to configure a self-hosted server that advertises no gateway', async () => {
    getGlobalConfig.mockResolvedValue({ serverConfig: {} });

    const outcome = await runCheck(deviceChecks, 'device.gateway', makeContext());

    expect(outcome.status).toBe('fail');
    expect(outcome.detail).toContain('does not advertise a device gateway');
    expect(outcome.fix).toContain('--gateway <url>');
    expect(gatewayClients).toHaveLength(0);
  });

  it('fails a broken lookup instead of probing a default', async () => {
    getGlobalConfig.mockRejectedValue(new Error('INTERNAL_SERVER_ERROR'));

    const outcome = await runCheck(deviceChecks, 'device.gateway', makeContext());

    expect(outcome.status).toBe('fail');
    expect(outcome.detail).toContain('Could not read the device gateway address');
    expect(gatewayClients).toHaveLength(0);
  });
});

describe('device.daemon', () => {
  beforeEach(() => {
    state.daemonPid = null;
    state.daemonStatus = null;
    state.services = [];
  });

  it('survives a platform without the connect service', async () => {
    const outcome = await runCheck(deviceChecks, 'device.daemon');

    expect(outcome.status).toBe('warn');
    expect(outcome.detail).toContain('No connect daemon');
  });

  it('recognizes a connected system service without suggesting another daemon', async () => {
    state.services = [
      {
        active: true,
        enabled: true,
        fragmentPath: '/etc/systemd/system/lobehub-connect.service',
        mainPid: 42,
        scope: 'system',
        serviceName: 'lobehub-connect.service',
        subState: 'running',
        user: 'ubuntu',
      },
    ];
    state.daemonStatus = {
      cliVersion,
      connectionStatus: 'connected',
      deviceId: 'dev_1',
      gatewayUrl: 'wss://gw',
      pid: 42,
      startedAt: '',
    };
    const outcome = await runCheck(deviceChecks, 'device.daemon');
    expect(outcome.status).toBe('ok');
    expect(outcome.detail).toContain('system');
    expect(outcome.detail).toContain('dev_1');
    expect(outcome.fix).toBeUndefined();
  });

  it('flags a status file with no daemon behind it', async () => {
    state.daemonStatus = {
      connectionStatus: 'connected',
      deviceId: 'dev_1',
      gatewayUrl: 'wss://gw',
      pid: 4,
      startedAt: '',
    };

    const outcome = await runCheck(deviceChecks, 'device.daemon');

    expect(outcome.status).toBe('warn');
    expect(outcome.detail).toContain('left over');
  });

  it('repairs only the stale process observed by the check', async () => {
    state.daemonStatus = {
      pid: 4,
      connectionStatus: 'connected',
      gatewayUrl: 'wss://gw',
      startedAt: '',
    };
    const outcome = await runCheck(deviceChecks, 'device.daemon');
    state.daemonStatus = { ...state.daemonStatus, pid: 42 };
    const check = deviceChecks.find((item) => item.id === 'device.daemon')!;
    await check.repair!(makeContext(), {
      ...outcome,
      durationMs: 0,
      group: 'device',
      id: check.id,
      title: check.title,
    });
    expect(removePid).toHaveBeenCalledWith(4);
    expect(removeStatus).toHaveBeenCalledWith(4);
  });

  it('does not call a live daemon connected without its own report', async () => {
    state.daemonPid = 42;
    state.daemonStatus = null;

    const outcome = await runCheck(deviceChecks, 'device.daemon');

    expect(outcome.status).toBe('warn');
    expect(outcome.detail).toContain('has not reported a connection state');
  });

  it('reports a healthy daemon', async () => {
    state.daemonPid = 42;
    state.daemonStatus = {
      cliVersion,
      connectionStatus: 'connected',
      deviceId: 'dev_1',
      gatewayUrl: 'wss://gw',
      pid: 42,
      startedAt: '',
    };

    const outcome = await runCheck(deviceChecks, 'device.daemon');

    expect(outcome.status).toBe('ok');
    expect(outcome.detail).toContain('dev_1');
  });

  it.each([
    ['0.0.1', 'running CLI 0.0.1'],
    [undefined, 'did not report its CLI version'],
  ])(
    'flags a connected daemon still running code from before an update (%s)',
    async (running, detail) => {
      state.daemonPid = 42;
      state.daemonStatus = {
        cliVersion: running,
        connectionStatus: 'connected',
        deviceId: 'dev_1',
        gatewayUrl: 'wss://gw',
        pid: 42,
        startedAt: '',
      };

      const outcome = await runCheck(deviceChecks, 'device.daemon');

      expect(outcome.status).toBe('warn');
      expect(outcome.detail).toContain(detail);
      expect(outcome.detail).toContain(`installed CLI is ${cliVersion}`);
      expect(outcome.fix).toContain('lh connect restart');
    },
  );

  it('flags a daemon whose last known state was not connected', async () => {
    state.daemonPid = 42;
    state.daemonStatus = {
      connectionStatus: 'reconnecting',
      deviceId: 'dev_1',
      gatewayUrl: 'wss://gw',
      pid: 42,
      startedAt: '',
    };

    const outcome = await runCheck(deviceChecks, 'device.daemon');

    expect(outcome.status).toBe('warn');
    expect(outcome.detail).toContain('reconnecting');
  });
});

describe('device.registration', () => {
  beforeEach(() => {
    state.devices = [];
    state.localDeviceId = undefined;
  });

  it('fails when this machine has never connected and nothing else is online', async () => {
    const outcome = await runCheck(deviceChecks, 'device.registration', makeContext());

    expect(outcome.status).toBe('fail');
  });

  it('only warns when other devices are online', async () => {
    state.devices = [{ deviceId: 'dev_other', online: true }];

    const outcome = await runCheck(deviceChecks, 'device.registration', makeContext());

    expect(outcome.status).toBe('warn');
  });

  it('fails when the local device is registered but offline', async () => {
    state.localDeviceId = 'dev_local';
    state.devices = [{ deviceId: 'dev_local', online: false }];

    const outcome = await runCheck(deviceChecks, 'device.registration', makeContext());

    expect(outcome.status).toBe('fail');
    expect(outcome.detail).toContain('offline');
  });

  it('passes when the local device is online', async () => {
    state.localDeviceId = 'dev_local';
    state.devices = [{ deviceId: 'dev_local', online: true }];

    const outcome = await runCheck(deviceChecks, 'device.registration', makeContext());

    expect(outcome.status).toBe('ok');
  });
});