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.

4.8 KB
Raw Blame
import type * as childProcess from 'node:child_process';
import { EventEmitter } from 'node:events';
import type * as os from 'node:os';
import { PassThrough } from 'node:stream';

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

import { spawnAgent } from './spawnAgent';

const spawnCalls: Array<{ env: NodeJS.ProcessEnv }> = [];

vi.mock('node:child_process', async () => {
  const actual = await vi.importActual<typeof childProcess>('node:child_process');
  return {
    ...actual,
    spawn: vi.fn((_command: string, _args: string[], options: { env: NodeJS.ProcessEnv }) => {
      spawnCalls.push({ env: options.env });
      const proc = new EventEmitter() as any;
      proc.stdin = { end: vi.fn(), once: vi.fn(), write: vi.fn(() => true) };
      proc.stdout = new PassThrough();
      proc.stderr = new PassThrough();
      proc.kill = vi.fn();
      proc.pid = 4321;
      return proc;
    }),
  };
});

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

const spawnClaudeCode = async (env?: Record<string, string>) => {
  const handle = await spawnAgent({
    agentType: 'claude-code',
    env,
    operationId: 'op-preset-env',
    prompt: 'hello',
  });
  handle.kill();
  return spawnCalls.at(-1)!.env;
};

describe('spawnAgent preset env', () => {
  beforeEach(() => {
    spawnCalls.length = 0;
  });

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

  it.each(['antigravity', 'claude-code', 'codex', 'opencode'] as const)(
    'only filters inherited SSH session data for Antigravity service runs (%s)',
    async (agentType) => {
      vi.stubEnv('LOBEHUB_CONNECT_SERVICE', '1');
      vi.stubEnv('SSH_CONNECTION', 'test-connection');
      vi.stubEnv('DBUS_SESSION_BUS_ADDRESS', 'unix:path=/run/user/1000/bus');

      const handle = await spawnAgent({
        agentType,
        command: '/custom/agent-cli',
        operationId: 'op',
        prompt: 'ping',
      });
      handle.kill();

      const { env } = spawnCalls.at(-1)!;
      expect(env.SSH_CONNECTION).toBe(agentType === 'antigravity' ? undefined : 'test-connection');
      expect(env.DBUS_SESSION_BUS_ADDRESS).toBe('unix:path=/run/user/1000/bus');
      expect(process.env.SSH_CONNECTION).toBe('test-connection');
    },
  );

  // claude.ai connectors finish connecting after the first model call.
  it('makes Claude Code wait for MCP servers before its first turn', async () => {
    vi.stubEnv('MCP_CONNECTION_NONBLOCKING', undefined);

    const env = await spawnClaudeCode();

    expect(env.MCP_CONNECTION_NONBLOCKING).toBe('false');
  });

  it('lets an agent-configured env override the preset', async () => {
    const env = await spawnClaudeCode({ MCP_CONNECTION_NONBLOCKING: 'true' });

    expect(env.MCP_CONNECTION_NONBLOCKING).toBe('true');
  });

  it('leaves the entrypoint and terminal title to Claude Code by default', async () => {
    vi.stubEnv('CLAUDE_CODE_ENTRYPOINT', undefined);
    vi.stubEnv('CLAUDE_CODE_DISABLE_TERMINAL_TITLE', undefined);

    const env = await spawnClaudeCode();

    expect(env.CLAUDE_CODE_ENTRYPOINT).toBeUndefined();
    expect(env.CLAUDE_CODE_DISABLE_TERMINAL_TITLE).toBeUndefined();
  });

  // A non-default entrypoint makes Claude Code spend a request on a terminal title.
  it('turns off terminal titles for an agent that sets its own entrypoint', async () => {
    vi.stubEnv('CLAUDE_CODE_DISABLE_TERMINAL_TITLE', undefined);

    const env = await spawnClaudeCode({ CLAUDE_CODE_ENTRYPOINT: 'lobehub' });

    expect(env.CLAUDE_CODE_ENTRYPOINT).toBe('lobehub');
    expect(env.CLAUDE_CODE_DISABLE_TERMINAL_TITLE).toBe('1');
  });

  // Turned off means no custom entrypoint, even one the launcher inherited.
  it('does not pass on an entrypoint the agent cleared', async () => {
    vi.stubEnv('CLAUDE_CODE_ENTRYPOINT', 'inherited');
    vi.stubEnv('CLAUDE_CODE_DISABLE_TERMINAL_TITLE', undefined);

    const env = await spawnClaudeCode({ CLAUDE_CODE_ENTRYPOINT: '' });

    expect('CLAUDE_CODE_ENTRYPOINT' in env).toBe(false);
    expect(env.CLAUDE_CODE_DISABLE_TERMINAL_TITLE).toBeUndefined();
  });

  it('keeps an agent-configured terminal title setting', async () => {
    const env = await spawnClaudeCode({
      CLAUDE_CODE_DISABLE_TERMINAL_TITLE: '0',
      CLAUDE_CODE_ENTRYPOINT: 'lobehub',
    });

    expect(env.CLAUDE_CODE_DISABLE_TERMINAL_TITLE).toBe('0');
  });

  it('does not apply the Claude Code preset to other agents', async () => {
    vi.stubEnv('MCP_CONNECTION_NONBLOCKING', undefined);

    const handle = await spawnAgent({ agentType: 'codebuddy', operationId: 'op-cb', prompt: 'hi' });
    handle.kill();

    expect(spawnCalls.at(-1)!.env.MCP_CONNECTION_NONBLOCKING).toBeUndefined();
    expect(spawnCalls.at(-1)!.env.CODEBUDDY_CODE_DISABLE_BACKGROUND_TASKS).toBe('1');
  });
});