Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

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

import {
  collectProjectDevices,
  projectDisplayLabelWithMachine,
  resolveRemoteProjectMachineIdentity,
} from '@/features/cc-agent/lib/remoteProjectIdentity';
import { remoteSshHostsStore } from '@/lib/remoteSshHostsStore';

function sshHost(
  id: string,
  overrides: Partial<RemoteHostSnapshot['config']> = {},
): RemoteHostSnapshot {
  return {
    config: {
      id,
      hostname: '10.0.0.8',
      port: 22,
      user: 'alice',
      authMethod: 'agent',
      source: 'ssh-config',
      managedByCindy: false,
      ...overrides,
      identityFileConfigured: overrides.identityFileConfigured ?? false,
    },
    status: 'ready',
    statusChangedAt: 0,
    autoConnect: false,
    agentProxy: null,
    agentProxyTunnel: null,
  };
}

const remoteProject = {
  scope: 'remote' as const,
  remoteHostId: 'gpu-box',
  deviceLinkDeviceId: null,
  deviceLinkDeviceName: null,
};

describe('resolveRemoteProjectMachineIdentity', () => {
  it('shows the SSH alias together with user and hostname', () => {
    expect(resolveRemoteProjectMachineIdentity(remoteProject, [sshHost('gpu-box')])).toEqual({
      kind: 'ssh',
      label: 'gpu-box',
      detail: '[email protected]',
      displayLabel: 'gpu-box · [email protected]',
    });
  });

  it('includes a non-default SSH port', () => {
    const identity = resolveRemoteProjectMachineIdentity(remoteProject, [
      sshHost('gpu-box', { hostname: 'example.test', port: 2202, user: 'root' }),
    ]);
    expect(identity?.displayLabel).toBe('gpu-box · [email protected]:2202');
  });

  it('shows a custom display name while retaining the SSH alias', () => {
    const identity = resolveRemoteProjectMachineIdentity(remoteProject, [
      sshHost('gpu-box', { displayName: 'Build box' }),
    ]);
    expect(identity?.displayLabel).toBe('Build box (gpu-box) · [email protected]');
  });

  it('falls back to the persisted remoteHostId when config is missing', () => {
    expect(resolveRemoteProjectMachineIdentity(remoteProject, [])?.displayLabel).toBe('gpu-box');
  });

  // 2026-08-12 用户裁决:设备 ID 是机器指纹哈希,对用户没有可读意义,默认不显示;
  // 同名设备使用简短序号区分。
  it('shows only the device-link friendly name when the name is unambiguous', () => {
    const identity = resolveRemoteProjectMachineIdentity(
      {
        scope: 'remote',
        remoteHostId: null,
        deviceLinkDeviceId: 'device-123',
        deviceLinkDeviceName: 'Office Mac',
      },
      [],
    );
    expect(identity).toEqual({
      kind: 'device-link',
      label: 'Office Mac',
      detail: null,
      displayLabel: 'Office Mac',
    });
  });

  it('uses an ordinal for names shared by more than one device', () => {
    const identity = resolveRemoteProjectMachineIdentity(
      {
        scope: 'remote',
        remoteHostId: null,
        deviceLinkDeviceId: 'device-123',
        deviceLinkDeviceName: 'Office Mac',
      },
      [],
      { devices: [{ deviceId: 'device-123', name: 'Office Mac' }, { deviceId: 'device-456', name: 'Office Mac' }] },
    );
    expect(identity).toEqual({
      kind: 'device-link',
      label: 'Office Mac (1)',
      detail: null,
      displayLabel: 'Office Mac (1)',
    });
  });

  it('uses a readable fallback when no device name resolved', () => {
    const identity = resolveRemoteProjectMachineIdentity(
      {
        scope: 'remote',
        remoteHostId: null,
        deviceLinkDeviceId: 'device-123',
        deviceLinkDeviceName: null,
      },
      [],
    );
    // 无名称也不暴露机器指纹。
    expect(identity).toEqual({
      kind: 'device-link',
      label: 'another computer',
      detail: null,
      displayLabel: 'another computer',
    });
  });

  it('does not add a machine identity to local projects', () => {
    expect(
      resolveRemoteProjectMachineIdentity(
        {
          scope: 'local',
          remoteHostId: null,
          deviceLinkDeviceId: null,
          deviceLinkDeviceName: null,
        },
        [sshHost('gpu-box')],
      ),
    ).toBeNull();
  });
});

describe('collectProjectDevices', () => {
  it('deduplicates project hosts without losing unnamed devices used for display fallback', () => {
    expect(collectProjectDevices([
      { deviceLinkDeviceId: 'b', deviceLinkDeviceName: 'Mac' },
      { deviceLinkDeviceId: 'a', deviceLinkDeviceName: null },
      { deviceLinkDeviceId: 'b', deviceLinkDeviceName: 'Mac' },
      { deviceLinkDeviceId: null, deviceLinkDeviceName: null },
    ])).toEqual([{ deviceId: 'a', name: null }, { deviceId: 'b', name: 'Mac' }]);
  });
});

describe('projectDisplayLabelWithMachine', () => {
  it('serializes the enriched identity for locked search filters', () => {
    expect(
      projectDisplayLabelWithMachine({
        ...remoteProject,
        displayName: 'cindy-moved',
        remoteMachineIdentity: resolveRemoteProjectMachineIdentity(remoteProject, [
          sshHost('gpu-box'),
        ]),
      }),
    ).toBe('cindy-moved (gpu-box · [email protected])');
  });
});

describe('remoteSshHostsStore', () => {
  afterEach(() => {
    remoteSshHostsStore.reset();
    vi.unstubAllGlobals();
  });

  it('publishes a full registry replacement after settings mutations', () => {
    const subscriber = vi.fn();
    const unsubscribe = remoteSshHostsStore.subscribe(subscriber);
    const host = sshHost('gpu-box');

    remoteSshHostsStore.replace([host]);

    expect(remoteSshHostsStore.get()).toEqual([host]);
    expect(subscriber).toHaveBeenCalledOnce();
    unsubscribe();
  });

  it('does not let an older in-flight list overwrite a settings replacement', async () => {
    let resolveList!: (value: { hosts: RemoteHostSnapshot[] }) => void;
    const list = vi.fn(
      () =>
        new Promise<{ hosts: RemoteHostSnapshot[] }>((resolve) => {
          resolveList = resolve;
        }),
    );
    vi.stubGlobal('window', { electronAPI: { remoteSsh: { list } } });

    const ensurePromise = remoteSshHostsStore.ensure();
    expect(list).toHaveBeenCalledOnce();
    const freshHost = sshHost('fresh-host');
    remoteSshHostsStore.replace([freshHost]);
    resolveList({ hosts: [sshHost('stale-host')] });
    await ensurePromise;

    expect(remoteSshHostsStore.get()).toEqual([freshHost]);
  });

  it('removes a deleted host from the shared snapshot', () => {
    remoteSshHostsStore.replace([sshHost('keep'), sshHost('remove')]);

    remoteSshHostsStore.remove('remove');

    expect(remoteSshHostsStore.get()?.map((host) => host.config.id)).toEqual(['keep']);
  });
});