test-http-client.js•6.15 kB
#!/usr/bin/env node
/**
* HTTP 版本的 MCP 抽签工具测试客户端
*/
const BASE_URL = 'http://localhost:3000';
// 发送 HTTP 请求的辅助函数
async function sendRequest(endpoint, method = 'GET', body = null) {
const url = `${BASE_URL}${endpoint}`;
const options = {
method,
headers: {
'Content-Type': 'application/json',
},
};
if (body) {
options.body = JSON.stringify(body);
}
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
} catch (error) {
throw new Error(`请求失败: ${error.message}`);
}
}
// 测试用例
async function runTests() {
console.log('🧪 开始测试 HTTP 版本的 MCP 抽签工具...\n');
try {
// 测试1: 健康检查
console.log('❤️ 测试1: 健康检查');
const healthResponse = await sendRequest('/health');
console.log('健康状态:', healthResponse.status);
console.log('✅ 测试1 通过\n');
// 测试2: 获取工具列表
console.log('📋 测试2: 获取工具列表');
const toolsResponse = await sendRequest('/tools');
console.log('可用工具:', toolsResponse.result.tools.map(t => t.name).join(', '));
console.log('✅ 测试2 通过\n');
// 测试3: 简单抽签
console.log('🎲 测试3: 简单抽签');
const lotteryResponse = await sendRequest('/tools/call', 'POST', {
name: 'draw_lottery',
arguments: {
options: ['苹果', '香蕉', '橙子', '葡萄', '草莓']
}
});
console.log('抽签结果:', lotteryResponse.result.content[0].text);
console.log('✅ 测试3 通过\n');
// 测试4: 多选抽签
console.log('🎯 测试4: 多选抽签');
const multiLotteryResponse = await sendRequest('/tools/call', 'POST', {
name: 'draw_lottery',
arguments: {
options: ['一等奖', '二等奖', '三等奖', '四等奖', '五等奖'],
count: 3,
allow_duplicate: false
}
});
console.log('多选结果:', multiLotteryResponse.result.content[0].text);
console.log('✅ 测试4 通过\n');
// 测试5: 投骰子
console.log('🎲 测试5: 投骰子');
const diceResponse = await sendRequest('/tools/call', 'POST', {
name: 'roll_dice',
arguments: {
sides: 20,
count: 3
}
});
console.log('骰子结果:', diceResponse.result.content[0].text);
console.log('✅ 测试5 通过\n');
// 测试6: 抛硬币
console.log('🪙 测试6: 抛硬币');
const coinResponse = await sendRequest('/tools/call', 'POST', {
name: 'flip_coin',
arguments: {
count: 5
}
});
console.log('硬币结果:', coinResponse.result.content[0].text);
console.log('✅ 测试6 通过\n');
// 测试7: 错误处理
console.log('❌ 测试7: 错误处理');
const errorResponse = await sendRequest('/tools/call', 'POST', {
name: 'draw_lottery',
arguments: {
options: []
}
});
console.log('错误处理:', errorResponse.result.content[0].text);
console.log('✅ 测试7 通过\n');
console.log('🎉 所有测试通过!');
console.log('\n💡 提示:');
console.log(`- 访问 http://localhost:3000 查看 Web 界面`);
console.log(`- MCP 端点: http://localhost:3000/mcp`);
console.log(`- 健康检查: http://localhost:3000/health`);
} catch (error) {
console.error('❌ 测试失败:', error.message);
console.log('\n🔧 请确保 HTTP 服务器正在运行:');
console.log('npm run start:http');
}
}
// 交互式测试模式
async function interactiveMode() {
console.log('🎮 进入交互式测试模式');
console.log('输入 "test" 运行自动测试,输入 "quit" 退出\n');
const readline = await import('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', async (input) => {
const command = input.trim();
if (command === 'quit') {
console.log('👋 再见!');
rl.close();
return;
}
if (command === 'test') {
await runTests();
return;
}
if (command.startsWith('lottery ')) {
const options = command.substring(8).split(',').map(s => s.trim());
try {
const response = await sendRequest('/tools/call', 'POST', {
name: 'draw_lottery',
arguments: { options }
});
console.log('结果:', response.result.content[0].text);
} catch (error) {
console.error('错误:', error.message);
}
return;
}
if (command.startsWith('dice ')) {
const [sides, count] = command.substring(5).split(' ').map(Number);
try {
const response = await sendRequest('/tools/call', 'POST', {
name: 'roll_dice',
arguments: { sides: sides || 6, count: count || 1 }
});
console.log('结果:', response.result.content[0].text);
} catch (error) {
console.error('错误:', error.message);
}
return;
}
if (command.startsWith('coin ')) {
const count = parseInt(command.substring(5)) || 1;
try {
const response = await sendRequest('/tools/call', 'POST', {
name: 'flip_coin',
arguments: { count }
});
console.log('结果:', response.result.content[0].text);
} catch (error) {
console.error('错误:', error.message);
}
return;
}
console.log('可用命令:');
console.log(' test - 运行自动测试');
console.log(' lottery 选项1,选项2,选项3 - 抽签');
console.log(' dice 面数 数量 - 投骰子');
console.log(' coin 次数 - 抛硬币');
console.log(' quit - 退出');
});
}
// 主程序
async function main() {
const args = process.argv.slice(2);
if (args.includes('--test')) {
await runTests();
} else {
await interactiveMode();
}
}
main().catch(console.error);