/**
 * MCP mcp_reload 功能测试
 */

const { Framework } = require('../src');
const { MCPExecutorPlugin } = require('../src/executors/mcp-executor');
const fs = require('fs');
const path = require('path');

async function test() {
  console.log('=== Testing mcp_reload ===\n');

  // 创建框架
  const framework = new Framework({ debug: true });

  // 创建 MCP 插件
  const mcpPlugin = new MCPExecutorPlugin({
    servers: [{ name: 'fetch', command: 'uvx', args: ['mcp-server-fetch'] }],
  });

  // 加载并启动
  await framework.loadPlugin(mcpPlugin);
  await framework.pluginManager.startAll();

  // 等待连接
  await new Promise((r) => setTimeout(r, 2000));

  console.log('\n1. 初始服务器列表:');
  const initialServers = mcpPlugin.getServers();
  console.log(JSON.stringify(initialServers, null, 2));

  // 测试 mcp_reload
  console.log('\n2. 调用 mcp_reload...');
  const result = await framework.executeTool('mcp_reload', {});
  console.log('mcp_reload 结果:');
  console.log(JSON.stringify(result, null, 2));

  // 检查配置
  console.log('\n3. 当前配置文件:');
  const configPath = path.resolve('.agent/mcp_config.json');
  if (fs.existsSync(configPath)) {
    const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
    console.log(JSON.stringify(config, null, 2));
  } else {
    console.log('配置文件不存在: ' + configPath);
  }

  // 清理
  await framework.destroy();

  console.log('\n=== 测试完成 ===');
}

test()
  .then(() => process.exit(0))
  .catch((err) => {
    console.error('测试失败:', err);
    process.exit(1);
  });
