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.

8.7 KB
Raw Blame
import { EventEmitter } from 'node:events';
import { PassThrough } from 'node:stream';

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

import { AcpStdioClient } from './acpStdioClient';

const { spawnMock } = vi.hoisted(() => ({ spawnMock: vi.fn() }));
const originalPlatform = process.platform;

vi.mock('node:child_process', async (importOriginal) => {
  const actual = await importOriginal<Record<string, unknown>>();
  return { ...actual, spawn: spawnMock };
});

const createProcess = () => {
  const child = new EventEmitter() as any;
  const stdout = new PassThrough();
  const writes: Array<Record<string, unknown>> = [];
  child.pid = 123_456;
  child.killed = false;
  child.kill = vi.fn(() => true);
  child.stdout = stdout;
  child.stderr = new PassThrough();
  child.stdin = {
    once: vi.fn(),
    write: vi.fn((chunk: string) => {
      writes.push(JSON.parse(chunk.trim()));
      return true;
    }),
  };
  return { child, stdout, writes };
};

afterEach(() => {
  vi.useRealTimers();
  Object.defineProperty(process, 'platform', { configurable: true, value: originalPlatform });
  vi.restoreAllMocks();
  spawnMock.mockReset();
});

describe('AcpStdioClient', () => {
  it('marks request timeouts so callers can distinguish them from command failures', async () => {
    vi.useFakeTimers();
    const { child } = createProcess();
    spawnMock.mockReturnValue(child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const client = new AcpStdioClient({
      args: [],
      commandPath: 'agent',
      cwd: '/workspace',
      env: {},
      onMessage: vi.fn(),
      onRawMessage: vi.fn(),
      onStderr: vi.fn(),
    });
    await client.start();
    const timedOut = expect(client.request('initialize', {}, 100)).rejects.toMatchObject({
      code: 'ETIMEDOUT',
    });
    await Promise.all([timedOut, vi.advanceTimersByTimeAsync(100)]);
    client.close();
  });

  it('frames split NDJSON and correlates concurrent responses by request id', async () => {
    const { child, stdout, writes } = createProcess();
    spawnMock.mockReturnValue(child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const messages: unknown[] = [];
    const client = new AcpStdioClient({
      args: ['agent', 'stdio'],
      commandPath: 'agent',
      cwd: '/workspace',
      env: { ...process.env },
      onMessage: (message) => {
        messages.push(message);
      },
      onRawMessage: vi.fn(),
      onStderr: vi.fn(),
    });
    await client.start();

    const first = client.request<{ value: string }>('first');
    const second = client.request<{ value: string }>('second');
    const firstId = writes[0].id;
    const secondId = writes[1].id;
    stdout.write(`${JSON.stringify({ id: secondId, jsonrpc: '2.0', result: { value: 'two' } })}\n`);
    const firstLine = `${JSON.stringify({
      id: firstId,
      jsonrpc: '2.0',
      result: { value: 'one' },
    })}\n`;
    stdout.write(firstLine.slice(0, 12));
    stdout.write(firstLine.slice(12));

    await expect(Promise.all([first, second])).resolves.toEqual([
      { value: 'one' },
      { value: 'two' },
    ]);
    expect(messages).toHaveLength(2);
    client.close();
  });

  it('drains a final structured RPC error before applying the process-exit fallback', async () => {
    const { child, stdout, writes } = createProcess();
    spawnMock.mockReturnValue(child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const onMessage = vi.fn();
    const client = new AcpStdioClient({
      args: ['agent', 'stdio'],
      commandPath: 'agent',
      cwd: '/workspace',
      env: { ...process.env },
      onMessage,
      onRawMessage: vi.fn(),
      onStderr: vi.fn(),
    });
    await client.start();

    const request = client.request('authenticate');
    stdout.write(
      `${JSON.stringify({
        error: { code: -32_000, message: 'Authentication required' },
        id: writes[0].id,
        jsonrpc: '2.0',
      })}\n`,
    );
    stdout.end();
    child.emit('close', 1, null);

    await expect(request).rejects.toThrow(
      'ACP request failed (authenticate): Authentication required',
    );
    expect(onMessage).toHaveBeenCalledWith(
      expect.objectContaining({ requestMethod: 'authenticate' }),
    );
    client.close();
  });

  it('delivers later notifications while a server request is still waiting on the host', async () => {
    const { child, stdout, writes } = createProcess();
    spawnMock.mockReturnValue(child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    let releaseServerRequest: ((result: unknown) => void) | undefined;
    const messages: unknown[] = [];
    const client = new AcpStdioClient({
      args: ['agent', 'stdio'],
      commandPath: 'agent',
      cwd: '/workspace',
      env: { ...process.env },
      onMessage: (message) => {
        messages.push(message);
      },
      onRawMessage: vi.fn(),
      onServerRequest: () =>
        new Promise((resolve) => {
          releaseServerRequest = resolve;
        }),
      onStderr: vi.fn(),
    });
    await client.start();

    stdout.write(
      `${JSON.stringify({
        id: 'ask-1',
        jsonrpc: '2.0',
        method: 'cursor/ask_question',
        params: { toolCallId: 'q1' },
      })}\n`,
    );
    await vi.waitFor(() => expect(releaseServerRequest).toBeTypeOf('function'));

    stdout.write(
      `${JSON.stringify({
        jsonrpc: '2.0',
        method: 'session/update',
        params: {
          update: {
            content: { text: 'still streaming', type: 'text' },
            sessionUpdate: 'agent_message_chunk',
          },
        },
      })}\n`,
    );
    await vi.waitFor(() =>
      expect(messages).toContainEqual(expect.objectContaining({ method: 'session/update' })),
    );

    releaseServerRequest!({ outcome: { outcome: 'cancelled' } });
    await vi.waitFor(() =>
      expect(writes).toContainEqual(
        expect.objectContaining({
          id: 'ask-1',
          result: { outcome: { outcome: 'cancelled' } },
        }),
      ),
    );
    client.close();
  });

  it('does not dispatch queued or subsequent stdout messages after host close', async () => {
    const { child, stdout } = createProcess();
    spawnMock.mockReturnValue(child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    let releaseFirstMessage: (() => void) | undefined;
    const onMessage = vi.fn().mockImplementationOnce(
      () =>
        new Promise<void>((resolve) => {
          releaseFirstMessage = resolve;
        }),
    );
    const client = new AcpStdioClient({
      args: ['agent', 'stdio'],
      commandPath: 'agent',
      cwd: '/workspace',
      env: { ...process.env },
      onMessage,
      onRawMessage: vi.fn(),
      onStderr: vi.fn(),
    });
    await client.start();

    stdout.write(`${JSON.stringify({ method: 'first' })}\n`);
    stdout.write(`${JSON.stringify({ method: 'queued' })}\n`);
    await vi.waitFor(() => expect(releaseFirstMessage).toBeTypeOf('function'));

    client.close();
    stdout.write(`${JSON.stringify({ method: 'after-close' })}\n`);
    releaseFirstMessage!();
    await client.drain();

    expect(onMessage).toHaveBeenCalledOnce();
    expect(onMessage).toHaveBeenCalledWith({ method: 'first' });
  });

  it('inherits an outer wrapper process group and signals the child directly', async () => {
    const { child } = createProcess();
    spawnMock.mockReturnValue(child);
    const processKill = vi.spyOn(process, 'kill').mockImplementation(() => true);
    const client = new AcpStdioClient({
      args: ['agent', 'stdio'],
      commandPath: 'agent',
      cwd: '/workspace',
      detached: false,
      env: { ...process.env },
      onMessage: vi.fn(),
      onRawMessage: vi.fn(),
      onStderr: vi.fn(),
    });
    await client.start();

    expect(spawnMock).toHaveBeenCalledWith(
      'agent',
      ['agent', 'stdio'],
      expect.objectContaining({ detached: false }),
    );
    client.close('SIGKILL');

    expect(child.kill).toHaveBeenCalledWith('SIGKILL');
    expect(processKill).not.toHaveBeenCalled();
  });

  it('terminates the child process tree with taskkill on Windows', async () => {
    Object.defineProperty(process, 'platform', { configurable: true, value: 'win32' });
    const { child } = createProcess();
    spawnMock.mockReturnValue(child);
    const client = new AcpStdioClient({
      args: ['agent', 'stdio'],
      commandPath: 'agent',
      cwd: 'C:\\workspace',
      env: { ...process.env },
      onMessage: vi.fn(),
      onRawMessage: vi.fn(),
      onStderr: vi.fn(),
    });
    await client.start();

    client.close('SIGKILL');

    expect(spawnMock).toHaveBeenNthCalledWith(2, 'taskkill', ['/pid', '123456', '/T', '/F'], {
      stdio: 'ignore',
    });
    expect(child.kill).not.toHaveBeenCalled();
  });
});