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。

3.7 KB
Raw Blame
/**
 * Server smoke tests: hello handshake + unknown method + initialized gate.
 *
 * Uses an in-memory pair of net.Socket via net.createServer + net.connect to a
 * Unix socket in OS temp dir. Skipped if the platform can't create unix sockets
 * (rare; Windows since build 17063 supports them).
 */

import * as net from 'node:net';
import * as path from 'node:path';
import * as os from 'node:os';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { RpcClient, RpcClientError } from '../src/client.js';
import { ManagerServer } from '../src/server.js';
import { PROTOCOL_VERSION } from '../src/protocol.js';

interface Ctx {
  server: ManagerServer;
  socketPath: string;
  socket: net.Socket;
  client: RpcClient;
}

let ctx: Ctx | null = null;

/**
 * Cross-platform IPC path:
 *   - posix: unix socket file under os.tmpdir()
 *   - win32: named pipe under \\.\pipe\ (unix sockets in tmpdir get EACCES)
 *
 * Node's net.createServer.listen() accepts both formats transparently.
 */
function makeIpcPath(): string {
  const uniq = `cc-mgr-test-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
  if (process.platform === 'win32') {
    return `\\\\.\\pipe\\${uniq}`;
  }
  return path.join(os.tmpdir(), `${uniq}.sock`);
}

beforeEach(async () => {
  const socketPath = makeIpcPath();
  const server = new ManagerServer({
    socketPath,
    managerVersion: 'test-0.0.0',
    logger: {
      debug: () => undefined,
      info: () => undefined,
      warn: () => undefined,
      error: () => undefined,
    },
  });
  await server.start();
  const socket = net.connect(socketPath);
  await new Promise<void>((resolve, reject) => {
    socket.once('connect', () => resolve());
    socket.once('error', reject);
  });
  const client = new RpcClient(socket);
  ctx = { server, socketPath, socket, client };
});

afterEach(async () => {
  if (!ctx) return;
  ctx.client.dispose();
  ctx.socket.destroy();
  await ctx.server.stop();
  ctx = null;
});

describe('ManagerServer Phase 1 skeleton', () => {
  it('returns hello response with server version', async () => {
    const res = await ctx!.client.hello();
    expect(res.protocolVersion).toBe(PROTOCOL_VERSION);
    expect(res.managerVersion).toBe('test-0.0.0');
  });

  it('rejects mismatched protocol version', async () => {
    await expect(
      ctx!.client.request('protocol/hello', { protocolVersion: 999 }),
    ).rejects.toBeInstanceOf(RpcClientError);
    try {
      await ctx!.client.request('protocol/hello', { protocolVersion: 999 });
    } catch (err) {
      expect((err as RpcClientError).rpcError.code).toBe('INVALID_PROTOCOL_VERSION');
    }
  });

  it('returns NOT_INITIALIZED for any method before hello', async () => {
    try {
      await ctx!.client.request('query/start', { sessionId: 'x' });
      throw new Error('expected rejection');
    } catch (err) {
      expect(err).toBeInstanceOf(RpcClientError);
      expect((err as RpcClientError).rpcError.code).toBe('NOT_INITIALIZED');
    }
  });

  it('returns UNKNOWN_METHOD for an unregistered method (after hello)', async () => {
    await ctx!.client.hello();
    try {
      await ctx!.client.request('totally/made-up', {});
      throw new Error('expected rejection');
    } catch (err) {
      expect(err).toBeInstanceOf(RpcClientError);
      expect((err as RpcClientError).rpcError.code).toBe('UNKNOWN_METHOD');
    }
  });

  it('honors setHandler for custom methods', async () => {
    ctx!.server.setHandler('echo/test', async (params) => ({ echoed: params }));
    await ctx!.client.hello();
    const result = await ctx!.client.request<{ echoed: unknown }>('echo/test', { hello: 'world' });
    expect(result.echoed).toEqual({ hello: 'world' });
  });
});