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.

31.0 KB
Raw Blame
import * as childProcess from 'node:child_process';
import type * as os from 'node:os';
import path from 'node:path';

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

const {
  detectDshCommandMock,
  listCodexModelsMock,
  listDroidAcpModelsMock,
  listDshAcpModelsMock,
  listTraeAcpModelsMock,
} = vi.hoisted(() => ({
  detectDshCommandMock: vi.fn(),
  listCodexModelsMock: vi.fn(),
  listDroidAcpModelsMock: vi.fn(),
  listDshAcpModelsMock: vi.fn(),
  listTraeAcpModelsMock: vi.fn(),
}));

vi.mock('node:os', async () => {
  const actual = await vi.importActual<typeof os>('node:os');
  return { ...actual, platform: vi.fn(() => 'darwin') };
});

vi.mock('node:child_process', () => ({
  exec: vi.fn(),
  execFile: vi.fn(),
}));

vi.mock('../spawn/traeAcpSession', () => ({
  listTraeAcpModels: listTraeAcpModelsMock,
}));

vi.mock('../spawn/droidAcpSession', () => ({
  listDroidAcpModels: listDroidAcpModelsMock,
}));

vi.mock('../spawn/dshAcpSession', () => ({
  DSH_COMMAND: 'dsh',
  detectDshCommand: detectDshCommandMock,
  listDshAcpModels: listDshAcpModelsMock,
}));

vi.mock('./codex', () => ({ listCodexModels: listCodexModelsMock }));

// Catalog parsing uses a mocked process; directory failures exercise the real spawn separately.
vi.mock('../spawn/workingDirectory', () => ({
  resolveHeteroSpawnCwd: vi.fn((cwd: string) => cwd),
}));

const execFileMock = vi.mocked(childProcess.execFile);

const resolveExecFile = (stdout: string, stderr = '') => {
  execFileMock.mockImplementationOnce(((file: string, args: any, options: any, callback: any) => {
    callback(null, { stderr, stdout });
    return {} as any;
  }) as any);
};

const rejectExecFile = (error: Error) => {
  execFileMock.mockImplementationOnce(((file: string, args: any, options: any, callback: any) => {
    callback(error, { stderr: '', stdout: '' });
    return {} as any;
  }) as any);
};

const importModule = () => import('./listHeterogeneousAgentModels');

describe('heterogeneous agent model discovery', () => {
  beforeEach(() => {
    execFileMock.mockReset();
    detectDshCommandMock.mockReset();
    listCodexModelsMock.mockReset();
    listDroidAcpModelsMock.mockReset();
    listDshAcpModelsMock.mockReset();
    listTraeAcpModelsMock.mockReset();
  });

  afterEach(() => {
    vi.resetModules();
  });

  it('asks for Antigravity sign-in when the device model catalog is inaccessible', async () => {
    rejectExecFile(
      Object.assign(new Error('Command failed'), {
        stderr: 'Error: Please sign in to view available models.',
      }),
    );
    const { listHeterogeneousAgentModels } = await importModule();
    await expect(
      listHeterogeneousAgentModels({ command: '/custom/agy', type: 'antigravity' }),
    ).resolves.toMatchObject({ error: { code: 'auth_required' }, status: 'error' });
  });

  it.each([
    { agent: 'antigravity', service: '1', stripped: true },
    { agent: 'antigravity', service: undefined, stripped: false },
    { agent: 'opencode', service: '1', stripped: false },
  ] as const)(
    'passes the right SSH environment for $agent in service mode $service',
    async ({ agent, service, stripped }) => {
      const env = {
        DBUS_SESSION_BUS_ADDRESS: 'unix:path=/run/user/1000/bus',
        ...(service ? { LOBEHUB_CONNECT_SERVICE: service } : {}),
        SSH_AUTH_SOCK: '/tmp/ssh-agent.sock',
        SSH_CLIENT: 'test-client',
        SSH_CONNECTION: 'test-connection',
        SSH_TTY: '/dev/pts/1',
      };
      resolveExecFile('gemini-test  Gemini Test');
      const { listHeterogeneousAgentModels } = await importModule();

      await listHeterogeneousAgentModels({ command: '/custom/agent-cli', env, type: agent });

      const subprocessEnv = execFileMock.mock.calls[0][2]?.env;
      expect(subprocessEnv).toMatchObject({
        DBUS_SESSION_BUS_ADDRESS: env.DBUS_SESSION_BUS_ADDRESS,
        SSH_AUTH_SOCK: env.SSH_AUTH_SOCK,
      });
      for (const key of ['SSH_CLIENT', 'SSH_CONNECTION', 'SSH_TTY'] as const) {
        expect(subprocessEnv?.[key]).toBe(stripped ? undefined : env[key]);
      }
      expect(env.SSH_CONNECTION).toBe('test-connection');
    },
  );

  it('queries DeepSeek Harness through ACP instead of --list-models', async () => {
    const models = [{ id: '["gateway","model"]', modelId: 'model', providerId: 'gateway' }];
    listDshAcpModelsMock.mockResolvedValue(models);
    const { listHeterogeneousAgentModels } = await importModule();

    await expect(
      listHeterogeneousAgentModels({
        args: ['--config', '/config'],
        command: '/custom/dsh',
        cwd: '/repo',
        env: { DSH_TEST: 'value' },
        type: 'deepseek-harness',
      }),
    ).resolves.toMatchObject({ models, status: 'success' });

    expect(listDshAcpModelsMock).toHaveBeenCalledWith(
      expect.objectContaining({
        args: ['--config', '/config'],
        commandPath: '/custom/dsh',
        cwd: '/repo',
        env: expect.objectContaining({ DSH_TEST: 'value' }),
      }),
    );
    expect(detectDshCommandMock).not.toHaveBeenCalled();
    expect(execFileMock).not.toHaveBeenCalled();
  });

  it('resolves the default dsh executable before ACP discovery', async () => {
    listDshAcpModelsMock.mockResolvedValue([]);
    detectDshCommandMock.mockResolvedValue({
      available: true,
      path: '/opt/dsh/bin/dsh',
      resolvedPathEnv: '/opt/dsh/bin',
    });
    const { listHeterogeneousAgentModels } = await importModule();

    await listHeterogeneousAgentModels({
      cwd: '/repo',
      env: { PATH: '/usr/bin' },
      type: 'deepseek-harness',
    });

    expect(listDshAcpModelsMock).toHaveBeenCalledWith(
      expect.objectContaining({ commandPath: '/opt/dsh/bin/dsh' }),
    );
  });

  it('queries the configured Antigravity binary for its native model catalog', async () => {
    resolveExecFile('gemini-4-argon-high  Gemini 4 Argon (High)');
    const { listHeterogeneousAgentModels } = await importModule();
    await expect(
      listHeterogeneousAgentModels({
        command: '/custom/agy',
        cwd: '/repo',
        env: { CUSTOM_ENV: 'value' },
        type: 'antigravity',
      }),
    ).resolves.toMatchObject({
      status: 'success',
      models: [
        { id: 'gemini-4-argon-high', label: 'Gemini 4 Argon (High)', providerId: 'antigravity' },
      ],
    });
    expect(execFileMock).toHaveBeenCalledWith(
      '/custom/agy',
      ['models'],
      expect.objectContaining({ cwd: '/repo', env: { CUSTOM_ENV: 'value' }, timeout: 15_000 }),
      expect.any(Function),
    );
  });

  it('reports a stalled Antigravity model query as a retryable catalog timeout', async () => {
    rejectExecFile(Object.assign(new Error('timed out'), { killed: true }));
    const { listHeterogeneousAgentModels } = await importModule();
    await expect(
      listHeterogeneousAgentModels({ command: '/custom/agy', type: 'antigravity' }),
    ).resolves.toMatchObject({ status: 'error', error: { code: 'timeout' } });
  });

  it('loads Pi provider extensions with the same discovery policy as the configured run', async () => {
    resolveExecFile('custom  fixture-model  4K  512  no  no');
    const { listHeterogeneousAgentModels } = await importModule();
    const result = await listHeterogeneousAgentModels({
      args: [
        '-e',
        '/config/provider.mjs',
        '-ne',
        '--offline',
        '--mode',
        'rpc',
        '--',
        '-e',
        '/ignored',
      ],
      command: '/custom/pi',
      type: 'pi',
    });
    expect(result).toMatchObject({ models: [{ modelId: 'fixture-model' }], status: 'success' });
    expect(execFileMock).toHaveBeenCalledWith(
      '/custom/pi',
      ['--list-models', '-e', '/config/provider.mjs', '-ne', '--offline'],
      expect.any(Object),
      expect.any(Function),
    );
  });

  it.each(['droid', 'trae'] as const)(
    'reports an unsupported %s ACP discovery method as a client capability error',
    async (type) => {
      const { AcpRpcResponseError } = await import('../spawn/acpStdioClient');
      const discover = type === 'droid' ? listDroidAcpModelsMock : listTraeAcpModelsMock;
      discover.mockRejectedValue(
        new AcpRpcResponseError('session/new', {
          code: -32_601,
          message: 'Method not found',
        }),
      );
      const { listHeterogeneousAgentModels } = await importModule();
      await expect(
        listHeterogeneousAgentModels({ command: `/custom/${type}`, type }),
      ).resolves.toMatchObject({ error: { code: 'unsupported_client' }, status: 'error' });
    },
  );

  it('queries Codex independently of task transport and forwards the target configuration', async () => {
    const models = [{ id: 'future-model', modelId: 'future-model', providerId: 'codex' }];
    listCodexModelsMock.mockResolvedValue(models);
    const { listHeterogeneousAgentModels } = await importModule();
    await expect(
      listHeterogeneousAgentModels({
        args: ['--profile', 'team'],
        command: '/custom/codex',
        cwd: '/repo',
        env: { CODEX_HOME: '/config' },
        type: 'codex',
      }),
    ).resolves.toMatchObject({ models, status: 'success' });
    expect(listCodexModelsMock).toHaveBeenCalledWith({
      args: ['--profile', 'team'],
      commandPath: '/custom/codex',
      cwd: '/repo',
      env: { CODEX_HOME: '/config' },
      timeoutMs: 15_000,
    });
    expect(execFileMock).not.toHaveBeenCalled();
  });

  it.each([
    ['unsupported_client', 'unsupported_client'],
    ['unsupported_configuration', 'unsupported_configuration'],
    ['ETIMEDOUT', 'timeout'],
  ])('returns a catalog error for Codex %s so the picker can fall back', async (code, expected) => {
    listCodexModelsMock.mockRejectedValue(Object.assign(new Error('failure'), { code }));
    const { listHeterogeneousAgentModels } = await importModule();
    await expect(
      listHeterogeneousAgentModels({ command: '/custom/codex', type: 'codex' }),
    ).resolves.toMatchObject({ error: { code: expected }, status: 'error' });
  });

  it('parses opaque model ids, splitting only at the first slash and preserving order', async () => {
    const { parseOpenCodeModelCatalog } = await importModule();

    expect(
      parseOpenCodeModelCatalog(
        [
          'openai/gpt-5.6',
          '',
          'openrouter/google/gemini-2.5-pro',
          'cloudflare/@cf/meta/llama-3.1-8b-instruct',
          'openai/gpt-5.6',
          'PATH=/usr/local/bin:/usr/bin',
          'diagnostic-without-model-id',
        ].join('\n'),
      ),
    ).toEqual([
      { id: 'openai/gpt-5.6', modelId: 'gpt-5.6', providerId: 'openai' },
      {
        id: 'openrouter/google/gemini-2.5-pro',
        modelId: 'google/gemini-2.5-pro',
        providerId: 'openrouter',
      },
      {
        id: 'cloudflare/@cf/meta/llama-3.1-8b-instruct',
        modelId: '@cf/meta/llama-3.1-8b-instruct',
        providerId: 'cloudflare',
      },
    ]);
  });

  it('discovers only model IDs accepted by CodeBuddy --model', async () => {
    const stdout = [
      'Usage: codebuddy [options]',
      '  --model <model>  Model for the current session. Currently supported: (default-model,',
      '                   gemini-3.1-pro, gpt-5.4, deepseek-v3-2-volc, gpt-5.4)',
      '  --effort <level> Reasoning effort level',
    ].join('\n');
    resolveExecFile(stdout);
    const { listHeterogeneousAgentModels, parseCodeBuddyModelCatalog } = await importModule();

    expect(parseCodeBuddyModelCatalog(stdout)).toEqual([
      { id: 'gemini-3.1-pro', modelId: 'gemini-3.1-pro', providerId: 'codebuddy' },
      { id: 'gpt-5.4', modelId: 'gpt-5.4', providerId: 'codebuddy' },
      {
        id: 'deepseek-v3-2-volc',
        modelId: 'deepseek-v3-2-volc',
        providerId: 'codebuddy',
      },
    ]);

    await expect(
      listHeterogeneousAgentModels({
        command: '/custom/codebuddy',
        cwd: '/repo',
        env: { CODEBUDDY_CODE_API_KEY: 'test-key' },
        type: 'codebuddy',
      }),
    ).resolves.toMatchObject({
      models: [
        { id: 'gemini-3.1-pro', modelId: 'gemini-3.1-pro', providerId: 'codebuddy' },
        { id: 'gpt-5.4', modelId: 'gpt-5.4', providerId: 'codebuddy' },
        {
          id: 'deepseek-v3-2-volc',
          modelId: 'deepseek-v3-2-volc',
          providerId: 'codebuddy',
        },
      ],
      status: 'success',
    });
    expect(execFileMock).toHaveBeenLastCalledWith(
      '/custom/codebuddy',
      ['--help'],
      expect.objectContaining({
        cwd: '/repo',
        env: { CODEBUDDY_CODE_API_KEY: 'test-key' },
      }),
      expect.any(Function),
    );
  });

  it('fails discovery when CodeBuddy exits successfully without reporting a model catalog', async () => {
    resolveExecFile(
      [
        'Usage: codebuddy [options]',
        '  --model <model>  Model for the current session. Please provide the model ID.',
      ].join('\n'),
    );
    const { listHeterogeneousAgentModels } = await importModule();

    await expect(
      listHeterogeneousAgentModels({
        command: '/custom/codebuddy',
        env: { CODEBUDDY_DISABLE_BUILTIN_MODELS: '1' },
        type: 'codebuddy',
      }),
    ).resolves.toMatchObject({
      error: { code: 'command_failed' },
      status: 'error',
    });
    expect(execFileMock).toHaveBeenCalledTimes(1);
  });

  it('parses a CodeBuddy model catalog written to stderr', async () => {
    resolveExecFile(
      '',
      [
        'Usage: codebuddy [options]',
        '  --model <model>  Model for the current session. Currently supported: (default-model,',
        '                   gpt-5.4)',
      ].join('\n'),
    );
    const { listHeterogeneousAgentModels } = await importModule();

    await expect(
      listHeterogeneousAgentModels({ command: '/custom/codebuddy', type: 'codebuddy' }),
    ).resolves.toMatchObject({
      models: [{ id: 'gpt-5.4', modelId: 'gpt-5.4', providerId: 'codebuddy' }],
      status: 'success',
    });
  });

  it('accepts an explicit CodeBuddy catalog containing only the default model', async () => {
    resolveExecFile(
      '  --model <model>  Model for the current session. Currently supported: (default-model)',
    );
    const { listHeterogeneousAgentModels } = await importModule();

    await expect(
      listHeterogeneousAgentModels({ command: '/custom/codebuddy', type: 'codebuddy' }),
    ).resolves.toMatchObject({ models: [], status: 'success' });
  });

  it.each([
    ['an empty body', '()'],
    ['comma-only entries', '(, ,)'],
  ])('rejects a CodeBuddy catalog containing %s', async (_, catalog) => {
    resolveExecFile(
      `  --model <model>  Model for the current session. Currently supported: ${catalog}`,
    );
    const { listHeterogeneousAgentModels } = await importModule();

    await expect(
      listHeterogeneousAgentModels({ command: '/custom/codebuddy', type: 'codebuddy' }),
    ).resolves.toMatchObject({
      error: { code: 'command_failed' },
      status: 'error',
    });
  });

  it('parses adversarial CodeBuddy help output without polynomial backtracking', async () => {
    const stdout = `${'--model <model>'.repeat(1000)}${'Currently supported:(('.repeat(1000)}`;
    const { parseCodeBuddyModelCatalog } = await importModule();
    const startedAt = performance.now();

    expect(parseCodeBuddyModelCatalog(stdout)).toEqual([]);
    expect(performance.now() - startedAt).toBeLessThan(100);
  });

  it('parses and discovers Cursor model slugs and labels', async () => {
    const stdout = [
      'Available models',
      '',
      'auto (default) - Auto',
      'claude-sonnet-4-6-thinking - Claude 4.6 Sonnet Thinking',
      'gpt-5.5-medium-fast (current) - GPT-5.5 Medium Fast',
      'claude-sonnet-4-6-thinking - Duplicate label',
      'diagnostic-without-a-label',
    ].join('\n');
    resolveExecFile(stdout);
    const { listHeterogeneousAgentModels, parseCursorModelCatalog } = await importModule();

    expect(parseCursorModelCatalog(stdout)).toEqual([
      { id: 'auto', label: 'Auto', modelId: 'auto', providerId: 'cursor' },
      {
        id: 'claude-sonnet-4-6-thinking',
        label: 'Claude 4.6 Sonnet Thinking',
        modelId: 'claude-sonnet-4-6-thinking',
        providerId: 'cursor',
      },
      {
        id: 'gpt-5.5-medium-fast',
        label: 'GPT-5.5 Medium Fast',
        modelId: 'gpt-5.5-medium-fast',
        providerId: 'cursor',
      },
    ]);

    await expect(
      listHeterogeneousAgentModels({
        command: '/custom/agent',
        cwd: '/repo',
        env: { CURSOR_API_KEY: 'test-key' },
        type: 'cursor',
      }),
    ).resolves.toMatchObject({
      models: [
        { id: 'auto', label: 'Auto', modelId: 'auto', providerId: 'cursor' },
        {
          id: 'claude-sonnet-4-6-thinking',
          label: 'Claude 4.6 Sonnet Thinking',
          modelId: 'claude-sonnet-4-6-thinking',
          providerId: 'cursor',
        },
        {
          id: 'gpt-5.5-medium-fast',
          label: 'GPT-5.5 Medium Fast',
          modelId: 'gpt-5.5-medium-fast',
          providerId: 'cursor',
        },
      ],
      status: 'success',
    });
    expect(execFileMock).toHaveBeenLastCalledWith(
      '/custom/agent',
      ['--list-models'],
      expect.objectContaining({
        cwd: '/repo',
        env: { CURSOR_API_KEY: 'test-key' },
      }),
      expect.any(Function),
    );
  });

  it('parses and discovers Devin model variants from JSON', async () => {
    const stdout = JSON.stringify({
      families: [
        {
          family_label: 'Claude Sonnet 4.6',
          variants: [
            { label: 'Claude Sonnet 4.6', model_uid: 'claude-sonnet-4-6' },
            { label: 'Claude Sonnet 4.6 Thinking', model_uid: 'claude-sonnet-4-6-thinking' },
          ],
        },
        {
          family_label: 'Duplicate',
          variants: [{ label: 'Duplicate', model_uid: 'claude-sonnet-4-6' }, {}],
        },
      ],
    });
    resolveExecFile(stdout);
    const { listHeterogeneousAgentModels, parseDevinModelCatalog } = await importModule();

    expect(parseDevinModelCatalog(stdout)).toEqual([
      {
        id: 'claude-sonnet-4-6',
        label: 'Claude Sonnet 4.6',
        modelId: 'claude-sonnet-4-6',
        providerId: 'devin',
      },
      {
        id: 'claude-sonnet-4-6-thinking',
        label: 'Claude Sonnet 4.6 Thinking',
        modelId: 'claude-sonnet-4-6-thinking',
        providerId: 'devin',
      },
    ]);
    expect(parseDevinModelCatalog('not json')).toEqual([]);

    await expect(
      listHeterogeneousAgentModels({
        command: '/custom/devin',
        cwd: '/repo',
        env: { DEVIN_MODEL: 'sonnet' },
        type: 'devin',
      }),
    ).resolves.toMatchObject({
      models: [
        { id: 'claude-sonnet-4-6', providerId: 'devin' },
        { id: 'claude-sonnet-4-6-thinking', providerId: 'devin' },
      ],
      status: 'success',
    });
    expect(execFileMock).toHaveBeenLastCalledWith(
      '/custom/devin',
      ['models', 'list', '--format', 'json'],
      expect.objectContaining({ cwd: '/repo', env: { DEVIN_MODEL: 'sonnet' } }),
      expect.any(Function),
    );
  });

  it('parses and discovers Grok Build models', async () => {
    const stdout = [
      'You are not authenticated.',
      '',
      'Default model: grok-4.6',
      '',
      'Available models:',
      '  * grok-4.6 (default)',
      '  - grok-4.5',
      '  - grok-4.6',
    ].join('\n');
    resolveExecFile(stdout);
    const { listHeterogeneousAgentModels, parseGrokBuildModelCatalog } = await importModule();

    expect(parseGrokBuildModelCatalog(stdout)).toEqual([
      { id: 'grok-4.6', modelId: 'grok-4.6', providerId: 'grok-build' },
      { id: 'grok-4.5', modelId: 'grok-4.5', providerId: 'grok-build' },
    ]);

    await expect(
      listHeterogeneousAgentModels({
        command: '/custom/grok',
        cwd: '/repo',
        env: { XAI_API_KEY: 'test-key' },
        type: 'grok-build',
      }),
    ).resolves.toMatchObject({
      models: [
        { id: 'grok-4.6', modelId: 'grok-4.6', providerId: 'grok-build' },
        { id: 'grok-4.5', modelId: 'grok-4.5', providerId: 'grok-build' },
      ],
      status: 'success',
    });
    expect(execFileMock).toHaveBeenLastCalledWith(
      '/custom/grok',
      ['models'],
      expect.objectContaining({ cwd: '/repo', env: { XAI_API_KEY: 'test-key' } }),
      expect.any(Function),
    );
  });

  it('runs the configured binary with plugins enabled and forwards cwd/env', async () => {
    resolveExecFile('openai/gpt-5.6\nopenrouter/google/gemini-2.5-pro\n');
    const { listHeterogeneousAgentModels } = await importModule();

    const result = await listHeterogeneousAgentModels({
      command: '/custom/opencode',
      cwd: '/repo',
      env: { OPENCODE_CONFIG_DIR: '/config', PATH: '/custom/bin' },
      type: 'opencode',
    });

    expect(result).toMatchObject({
      models: [
        { id: 'openai/gpt-5.6', modelId: 'gpt-5.6', providerId: 'openai' },
        {
          id: 'openrouter/google/gemini-2.5-pro',
          modelId: 'google/gemini-2.5-pro',
          providerId: 'openrouter',
        },
      ],
      status: 'success',
    });
    expect(execFileMock).toHaveBeenCalledWith(
      '/custom/opencode',
      ['models'],
      expect.objectContaining({
        cwd: '/repo',
        env: { OPENCODE_CONFIG_DIR: '/config', PATH: '/custom/bin' },
        timeout: 15_000,
      }),
      expect.any(Function),
    );
  });

  it('keeps the resolver login-shell PATH when the caller also provides PATH', async () => {
    const originalShell = process.env.SHELL;
    process.env.SHELL = '/bin/zsh';
    execFileMock.mockImplementation(((
      file: string,
      args: string[],
      options: any,
      callback: any,
    ) => {
      if (file === '/bin/zsh') {
        callback(null, { stderr: '', stdout: '/login/bin:/usr/bin' });
      } else if (file === 'which' && options.env?.PATH?.includes('/login/bin')) {
        callback(null, { stderr: '', stdout: '/login/bin/opencode\n' });
      } else if (file === '/login/bin/opencode' && args.includes('--version')) {
        callback(null, { stderr: '', stdout: '1.18.3' });
      } else if (file === '/login/bin/opencode' && args.includes('models')) {
        callback(null, { stderr: '', stdout: 'openai/gpt-5.6\n' });
      } else {
        callback(new Error('unavailable in inherited environment'), { stderr: '', stdout: '' });
      }
      return {} as any;
    }) as any);

    try {
      const { listHeterogeneousAgentModels } = await importModule();
      const result = await listHeterogeneousAgentModels({
        env: { PATH: '/inherited/bin' },
        type: 'opencode',
      });

      expect(result.status).toBe('success');
      const [command, args, options] = execFileMock.mock.calls.at(-1) as unknown as [
        string,
        string[],
        { env: NodeJS.ProcessEnv },
      ];
      expect(command).toBe('/login/bin/opencode');
      expect(args).toEqual(['models']);
      expect(options.env.PATH?.split(path.delimiter)).toEqual(
        expect.arrayContaining(['/inherited/bin', '/login/bin', '/usr/bin']),
      );
    } finally {
      if (originalShell === undefined) delete process.env.SHELL;
      else process.env.SHELL = originalShell;
    }
  });

  it('returns a stable missing-CLI error', async () => {
    rejectExecFile(Object.assign(new Error('spawn ENOENT'), { code: 'ENOENT' }));
    const { listHeterogeneousAgentModels } = await importModule();

    await expect(
      listHeterogeneousAgentModels({ command: '/missing/opencode', type: 'opencode' }),
    ).resolves.toMatchObject({
      error: { code: 'cli_not_found' },
      status: 'error',
    });
  });

  it('returns a stable timeout error', async () => {
    rejectExecFile(Object.assign(new Error('timed out'), { killed: true, signal: 'SIGTERM' }));
    const { listHeterogeneousAgentModels } = await importModule();

    await expect(
      listHeterogeneousAgentModels({ command: '/slow/opencode', type: 'opencode' }),
    ).resolves.toMatchObject({
      error: { code: 'timeout' },
      status: 'error',
    });
  });

  it('parses and discovers Pi provider/model rows', async () => {
    resolveExecFile(
      [
        'provider   model                              context  max-out  thinking  images',
        'anthropic  claude-sonnet-4-5                  200K     64K      yes       yes',
        'google     gemini-2.5-pro                     1M       64K      yes       yes',
        'openrouter  google/gemini-2.5-pro             1M       64K      yes       yes',
        'anthropic  claude-sonnet-4-5                  200K     64K      yes       yes',
      ].join('\n'),
    );
    const { listHeterogeneousAgentModels, parsePiModelCatalog } = await importModule();
    const stdout = [
      'provider   model                 context  max-out  thinking  images',
      'anthropic  claude-sonnet-4-5     200K     64K      yes       yes',
      'google     gemini-2.5-pro        1M       64K      yes       yes',
    ].join('\n');

    expect(parsePiModelCatalog(stdout)).toEqual([
      {
        id: 'anthropic/claude-sonnet-4-5',
        modelId: 'claude-sonnet-4-5',
        providerId: 'anthropic',
      },
      { id: 'google/gemini-2.5-pro', modelId: 'gemini-2.5-pro', providerId: 'google' },
    ]);

    await expect(
      listHeterogeneousAgentModels({
        command: '/custom/pi',
        cwd: '/repo',
        env: { PI_CODING_AGENT_DIR: '/config' },
        type: 'pi',
      }),
    ).resolves.toMatchObject({
      models: [
        {
          id: 'anthropic/claude-sonnet-4-5',
          modelId: 'claude-sonnet-4-5',
          providerId: 'anthropic',
        },
        { id: 'google/gemini-2.5-pro', modelId: 'gemini-2.5-pro', providerId: 'google' },
        {
          id: 'openrouter/google/gemini-2.5-pro',
          modelId: 'google/gemini-2.5-pro',
          providerId: 'openrouter',
        },
      ],
      status: 'success',
    });
    expect(execFileMock).toHaveBeenLastCalledWith(
      '/custom/pi',
      ['--list-models'],
      expect.objectContaining({
        cwd: '/repo',
        env: { PI_CODING_AGENT_DIR: '/config' },
      }),
      expect.any(Function),
    );
  });

  it('parses and discovers Qoder built-in names and custom model IDs', async () => {
    const stdout = [
      'MODEL',
      'Auto',
      'Claude Sonnet 4.5',
      'Team Gateway (team-model-id)',
      'Team Gateway (team-model-id)',
      'Long custom model (model-id-that-was-truncated-at-forty-ch…)',
      '',
    ].join('\n');
    resolveExecFile(stdout);
    const { listHeterogeneousAgentModels, parseQoderModelCatalog } = await importModule();

    expect(parseQoderModelCatalog(stdout)).toEqual([
      { id: 'Auto', modelId: 'Auto', providerId: 'qoder' },
      { id: 'Claude Sonnet 4.5', modelId: 'Claude Sonnet 4.5', providerId: 'qoder' },
      {
        id: 'team-model-id',
        label: 'Team Gateway',
        modelId: 'team-model-id',
        providerId: 'qoder',
      },
    ]);

    await expect(
      listHeterogeneousAgentModels({
        command: '/custom/qodercli',
        cwd: '/repo',
        env: { QODER_CONFIG_DIR: '/config' },
        type: 'qoder',
      }),
    ).resolves.toMatchObject({
      models: [
        { id: 'Auto', modelId: 'Auto', providerId: 'qoder' },
        { id: 'Claude Sonnet 4.5', modelId: 'Claude Sonnet 4.5', providerId: 'qoder' },
        {
          id: 'team-model-id',
          label: 'Team Gateway',
          modelId: 'team-model-id',
          providerId: 'qoder',
        },
      ],
      status: 'success',
    });
    expect(execFileMock).toHaveBeenLastCalledWith(
      '/custom/qodercli',
      ['--list-models'],
      expect.objectContaining({
        cwd: '/repo',
        env: { QODER_CONFIG_DIR: '/config' },
      }),
      expect.any(Function),
    );
  });

  it('parses and discovers Kimi Code models from provider list JSON', async () => {
    const stdout = JSON.stringify({
      models: {
        'kimi-code/k3': {
          defaultEffort: 'high',
          displayName: 'K3',
          model: 'k3',
          provider: 'managed:kimi-code',
          supportEfforts: ['low', 'high', 'max'],
        },
        'kimi-code/kimi-for-coding-highspeed': {
          displayName: 'K2.7 Code Highspeed',
          model: 'kimi-for-coding-highspeed',
          provider: 'managed:kimi-code',
        },
        'broken-alias': { provider: 'managed:kimi-code' },
      },
      providers: {
        'managed:kimi-code': { baseUrl: 'https://api.kimi.com/coding/v1', type: 'kimi' },
      },
    });
    resolveExecFile(stdout);
    const { listHeterogeneousAgentModels, parseKimiCodeModelCatalog } = await importModule();

    const expected = [
      {
        id: 'kimi-code/k3',
        label: 'K3',
        modelId: 'k3',
        providerId: 'kimi-code',
      },
      {
        id: 'kimi-code/kimi-for-coding-highspeed',
        label: 'K2.7 Code Highspeed',
        modelId: 'kimi-for-coding-highspeed',
        providerId: 'kimi-code',
      },
    ];
    expect(parseKimiCodeModelCatalog(stdout)).toEqual(expected);
    expect(parseKimiCodeModelCatalog('not json')).toEqual([]);
    expect(parseKimiCodeModelCatalog('{}')).toEqual([]);

    await expect(
      listHeterogeneousAgentModels({
        command: '/custom/kimi',
        cwd: '/repo',
        env: { KIMI_CODE_HOME: '/config' },
        type: 'kimi-code',
      }),
    ).resolves.toMatchObject({ models: expected, status: 'success' });
    expect(execFileMock).toHaveBeenLastCalledWith(
      '/custom/kimi',
      ['provider', 'list', '--json'],
      expect.objectContaining({
        cwd: '/repo',
        env: { KIMI_CODE_HOME: '/config' },
      }),
      expect.any(Function),
    );
  });

  it('discovers TRAE models through ACP and forwards provider arguments', async () => {
    listTraeAcpModelsMock.mockResolvedValue([
      { id: 'seed-2.0-code', modelId: 'seed-2.0-code', providerId: 'trae' },
    ]);
    const { listHeterogeneousAgentModels } = await importModule();

    await expect(
      listHeterogeneousAgentModels({
        args: ['--feature=test'],
        command: '/custom/traecli',
        cwd: '/repo',
        env: { TRAE_CONFIG_DIR: '/config' },
        type: 'trae',
      }),
    ).resolves.toMatchObject({
      models: [{ id: 'seed-2.0-code', modelId: 'seed-2.0-code', providerId: 'trae' }],
      status: 'success',
    });
    expect(listTraeAcpModelsMock).toHaveBeenCalledWith(
      expect.objectContaining({
        args: ['--feature=test'],
        commandPath: '/custom/traecli',
        cwd: '/repo',
        env: { TRAE_CONFIG_DIR: '/config' },
      }),
    );
    expect(execFileMock).not.toHaveBeenCalled();
  });

  it('discovers Factory Droid models through ACP and forwards only safe provider arguments', async () => {
    listDroidAcpModelsMock.mockResolvedValue([
      { id: 'gpt-5.4', modelId: 'gpt-5.4', providerId: 'droid' },
    ]);
    const { listHeterogeneousAgentModels } = await importModule();

    await expect(
      listHeterogeneousAgentModels({
        args: ['--tag', 'lobe'],
        command: '/custom/droid',
        cwd: '/repo',
        env: { FACTORY_API_KEY: 'test-key' },
        type: 'droid',
      }),
    ).resolves.toMatchObject({
      models: [{ id: 'gpt-5.4', modelId: 'gpt-5.4', providerId: 'droid' }],
      status: 'success',
    });
    expect(listDroidAcpModelsMock).toHaveBeenCalledWith(
      expect.objectContaining({
        args: ['--tag', 'lobe'],
        commandPath: '/custom/droid',
        cwd: '/repo',
        env: { FACTORY_API_KEY: 'test-key' },
      }),
    );
    expect(execFileMock).not.toHaveBeenCalled();
  });
});