#!/usr/bin/env node /** * Zulip集成测试运行脚本 * * 功能描述: * - 运行Zulip消息发送的各种测试 * - 检查环境配置 * - 提供测试结果报告 * * 使用方法: * npm run test:zulip-integration * 或 * node scripts/test-zulip-integration.js * * @author moyin * @version 1.0.0 * @since 2026-01-10 */ const { execSync } = require('child_process'); const fs = require('fs'); const path = require('path'); // 颜色输出 const colors = { reset: '\x1b[0m', bright: '\x1b[1m', red: '\x1b[31m', green: '\x1b[32m', yellow: '\x1b[33m', blue: '\x1b[34m', magenta: '\x1b[35m', cyan: '\x1b[36m', }; function colorLog(color, message) { console.log(`${colors[color]}${message}${colors.reset}`); } function checkEnvironment() { colorLog('cyan', '\n🔍 检查环境配置...\n'); const requiredEnvVars = [ 'ZULIP_SERVER_URL', 'ZULIP_BOT_EMAIL', 'ZULIP_BOT_API_KEY' ]; const optionalEnvVars = [ 'ZULIP_TEST_STREAM', 'ZULIP_TEST_TOPIC' ]; let hasRequired = true; // 检查必需的环境变量 requiredEnvVars.forEach(varName => { if (process.env[varName]) { colorLog('green', `✅ ${varName}: ${process.env[varName].substring(0, 20)}...`); } else { colorLog('red', `❌ ${varName}: 未设置`); hasRequired = false; } }); // 检查可选的环境变量 optionalEnvVars.forEach(varName => { if (process.env[varName]) { colorLog('yellow', `🔧 ${varName}: ${process.env[varName]}`); } else { colorLog('yellow', `🔧 ${varName}: 使用默认值`); } }); if (!hasRequired) { colorLog('red', '\n❌ 缺少必需的环境变量!'); colorLog('yellow', '\n请设置以下环境变量:'); colorLog('yellow', 'export ZULIP_SERVER_URL="https://your-zulip-server.com"'); colorLog('yellow', 'export ZULIP_BOT_EMAIL="your-bot@example.com"'); colorLog('yellow', 'export ZULIP_BOT_API_KEY="your-api-key"'); colorLog('yellow', '\n可选配置:'); colorLog('yellow', 'export ZULIP_TEST_STREAM="test-stream"'); colorLog('yellow', 'export ZULIP_TEST_TOPIC="API Test"'); return false; } colorLog('green', '\n✅ 环境配置检查通过!\n'); return true; } function runTest(testFile, description) { colorLog('blue', `\n🧪 运行测试: ${description}`); colorLog('blue', `📁 文件: ${testFile}\n`); try { const command = `npm test -- ${testFile} --verbose`; execSync(command, { stdio: 'inherit', cwd: process.cwd() }); colorLog('green', `✅ ${description} - 测试通过\n`); return true; } catch (error) { colorLog('red', `❌ ${description} - 测试失败\n`); return false; } } function main() { colorLog('bright', '🚀 Zulip集成测试运行器\n'); colorLog('bright', '=' .repeat(50)); // 检查环境配置 if (!checkEnvironment()) { process.exit(1); } const tests = [ { file: 'src/core/zulip_core/services/zulip_message_integration.spec.ts', description: 'Zulip消息发送集成测试' }, { file: 'test/zulip_integration/chat_message_e2e.spec.ts', description: '聊天消息端到端测试' }, { file: 'test/zulip_integration/real_zulip_api.spec.ts', description: '真实Zulip API测试' } ]; let passedTests = 0; let totalTests = tests.length; // 运行所有测试 tests.forEach(test => { if (fs.existsSync(test.file)) { if (runTest(test.file, test.description)) { passedTests++; } } else { colorLog('yellow', `⚠️ 测试文件不存在: ${test.file}`); totalTests--; } }); // 输出测试结果 colorLog('bright', '\n' + '=' .repeat(50)); colorLog('bright', '📊 测试结果汇总'); colorLog('bright', '=' .repeat(50)); if (passedTests === totalTests) { colorLog('green', `🎉 所有测试通过!(${passedTests}/${totalTests})`); colorLog('green', '\n✨ Zulip集成功能正常工作!'); } else { colorLog('red', `❌ 部分测试失败 (${passedTests}/${totalTests})`); colorLog('yellow', '\n请检查失败的测试并修复问题。'); } // 提供有用的信息 colorLog('cyan', '\n💡 提示:'); colorLog('cyan', '- 确保Zulip服务器可访问'); colorLog('cyan', '- 检查API Key权限'); colorLog('cyan', '- 确认测试Stream存在'); colorLog('cyan', '- 查看详细日志了解错误原因'); process.exit(passedTests === totalTests ? 0 : 1); } // 处理命令行参数 if (process.argv.includes('--help') || process.argv.includes('-h')) { console.log(` Zulip集成测试运行器 用法: node scripts/test-zulip-integration.js [选项] 选项: --help, -h 显示帮助信息 --check-env 仅检查环境配置 环境变量: ZULIP_SERVER_URL Zulip服务器地址 (必需) ZULIP_BOT_EMAIL 机器人邮箱 (必需) ZULIP_BOT_API_KEY API密钥 (必需) ZULIP_TEST_STREAM 测试Stream名称 (可选) ZULIP_TEST_TOPIC 测试Topic名称 (可选) 示例: export ZULIP_SERVER_URL="https://your-zulip.com" export ZULIP_BOT_EMAIL="bot@example.com" export ZULIP_BOT_API_KEY="your-api-key" node scripts/test-zulip-integration.js `); process.exit(0); } if (process.argv.includes('--check-env')) { checkEnvironment(); process.exit(0); } // 运行主程序 main();