Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

4.4 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import type { OwnedMachine } from '@shared/core/managed-agents/managed-agents';
import { type BuddyProvider, buddyProviders, startButtons } from './buddy-providers';

const PROVIDERS = [
  { id: 'claude', name: 'Claude Code' },
  { id: 'codex', name: 'Codex' },
  { id: 'opencode', name: 'OpenCode' },
  { id: 'cursor', name: 'Cursor' },
  { id: 'antigravity', name: 'Antigravity' },
];

function machine(providers: OwnedMachine['providers']): OwnedMachine {
  return {
    id: 'm1',
    name: 'laptop',
    kind: 'console',
    state: 'online',
    providers,
    acceptsLogins: false,
    cloud: false,
    workspacesDir: null,
    os: 'linux',
    local: { kind: 'this-computer' },
  };
}

describe('buddyProviders', () => {
  it('reads each provider from what the controller reported', () => {
    const states = buddyProviders(
      PROVIDERS,
      machine([
        { provider: 'claude', ready: true, problem: null, detail: null, loginHelps: true },
        {
          provider: 'codex',
          ready: false,
          problem: 'not logged in',
          detail: null,
          loginHelps: true,
        },
        {
          provider: 'opencode',
          ready: false,
          problem: 'not installed',
          detail: null,
          loginHelps: true,
        },
        {
          provider: 'cursor',
          ready: false,
          problem: 'login not checked yet',
          detail: null,
          loginHelps: true,
        },
      ])
    );
    expect(states.map((p) => [p.id, p.state])).toEqual([
      ['claude', 'ready'],
      ['codex', 'sign-in'],
      ['opencode', 'install'],
      ['cursor', 'checking'],
      ['antigravity', 'checking'],
    ]);
  });

  it('asks to sign in, with the reason, when a login could not be checked', () => {
    const [claude] = buddyProviders(
      PROVIDERS,
      machine([
        {
          provider: 'claude',
          ready: false,
          problem: 'login could not be checked',
          detail: 'It may be too old.',
          loginHelps: true,
        },
      ])
    );
    expect(claude).toMatchObject({ id: 'claude', state: 'sign-in', detail: 'It may be too old.' });
  });

  it('passes on that signing in would not make a provider ready', () => {
    const [, codex] = buddyProviders(
      PROVIDERS,
      machine([
        {
          provider: 'codex',
          ready: false,
          problem: 'not logged in',
          detail: 'An administrator pins where Codex keeps logins.',
          loginHelps: false,
        },
      ])
    );
    expect(codex).toMatchObject({ id: 'codex', state: 'sign-in', loginHelps: false });
  });

  it('is still checking everything before this computer reports', () => {
    expect(buddyProviders(PROVIDERS, null).every((p) => p.state === 'checking')).toBe(true);
  });
});

describe('startButtons', () => {
  const states = (entries: Record<string, BuddyProvider['state']>): BuddyProvider[] =>
    PROVIDERS.map((provider) => ({
      ...provider,
      state: entries[provider.id] ?? 'checking',
      detail: null,
      loginHelps: true,
    }));
  const ids = (buttons: BuddyProvider[] | null) => buttons?.map((provider) => provider.id) ?? null;

  it('decides as soon as Claude Code and Codex are known and one is ready', () => {
    expect(ids(startButtons(states({ claude: 'ready', codex: 'ready' }), false))).toEqual([
      'claude',
      'codex',
    ]);
    expect(ids(startButtons(states({ claude: 'ready', codex: 'sign-in' }), false))).toEqual([
      'claude',
    ]);
  });

  it('waits while one of them is still being checked', () => {
    expect(startButtons(states({ claude: 'ready' }), false)).toBeNull();
    expect(startButtons(states({ codex: 'ready' }), false)).toBeNull();
  });

  it('waits for the rest when neither is ready, then offers what is', () => {
    expect(startButtons(states({ claude: 'install', codex: 'sign-in' }), false)).toBeNull();
    expect(
      ids(
        startButtons(
          states({
            claude: 'install',
            codex: 'sign-in',
            opencode: 'ready',
            cursor: 'install',
            antigravity: 'install',
          }),
          false
        )
      )
    ).toEqual(['opencode']);
  });

  it('offers what is ready once out of patience', () => {
    expect(ids(startButtons(states({ codex: 'ready' }), true))).toEqual(['codex']);
    expect(ids(startButtons(states({}), true))).toEqual([]);
  });
});