#!/usr/bin/env node /** * 前后端集成测试脚本 * 用于验证前端项目与后端API的连接是否正常 */ const http = require('http'); // 测试配置 const TEST_CONFIG = { // Uniapp测试配置 uniapp: { baseURL: 'http://127.0.0.1:8081', endpoints: [ '/client/health', '/client/calendar/convert', '/client/lunar-calendar/convert', '/client/fortune/daily', '/client/ziwei/analyze' ] }, // Admin测试配置 admin: { baseURL: 'http://127.0.0.1:8082', endpoints: [ '/admin/health', '/admin/auth/login', '/admin/user/list', '/admin/role/list', '/admin/menu/list' ] } }; // 颜色输出 const colors = { reset: '\x1b[0m', green: '\x1b[32m', red: '\x1b[31m', yellow: '\x1b[33m', blue: '\x1b[36m' }; // 测试结果统计 const results = { uniapp: { passed: 0, failed: 0, total: 0 }, admin: { passed: 0, failed: 0, total: 0 } }; /** * 发送HTTP请求 */ function request(url, method = 'GET', data = null) { return new Promise((resolve, reject) => { const urlObj = new URL(url); const options = { hostname: urlObj.hostname, port: urlObj.port, path: urlObj.pathname + urlObj.search, method: method, headers: { 'Content-Type': 'application/json' }, timeout: 5000 }; const req = http.request(options, (res) => { let body = ''; res.on('data', (chunk) => body += chunk); res.on('end', () => { try { resolve({ statusCode: res.statusCode, headers: res.headers, body: body ? JSON.parse(body) : null }); } catch (e) { resolve({ statusCode: res.statusCode, headers: res.headers, body: body }); } }); }); req.on('error', reject); req.on('timeout', () => { req.destroy(); reject(new Error('Request timeout')); }); if (data) { req.write(JSON.stringify(data)); } req.end(); }); } /** * 测试单个端点 */ async function testEndpoint(baseURL, endpoint, project) { const url = `${baseURL}${endpoint}`; results[project].total++; try { const response = await request(url); // 检查响应状态码 const isSuccess = response.statusCode >= 200 && response.statusCode < 500; if (isSuccess) { results[project].passed++; console.log(`${colors.green}✓${colors.reset} ${url} [${response.statusCode}]`); } else { results[project].failed++; console.log(`${colors.red}✗${colors.reset} ${url} [${response.statusCode}]`); } return isSuccess; } catch (error) { results[project].failed++; console.log(`${colors.red}✗${colors.reset} ${url} [${error.message}]`); return false; } } /** * 测试项目 */ async function testProject(projectName, config) { console.log(`\n${colors.blue}=== 测试 ${projectName} ===${colors.reset}`); console.log(`Base URL: ${config.baseURL}\n`); for (const endpoint of config.endpoints) { await testEndpoint(config.baseURL, endpoint, projectName); } } /** * 打印测试结果 */ function printResults() { console.log(`\n${colors.blue}=== 测试结果汇总 ===${colors.reset}\n`); // Uniapp结果 const uniappRate = results.uniapp.total > 0 ? ((results.uniapp.passed / results.uniapp.total) * 100).toFixed(2) : 0; console.log(`Uniapp:`); console.log(` 通过: ${colors.green}${results.uniapp.passed}${colors.reset} / ${results.uniapp.total}`); console.log(` 失败: ${colors.red}${results.uniapp.failed}${colors.reset} / ${results.uniapp.total}`); console.log(` 成功率: ${uniappRate}%\n`); // Admin结果 const adminRate = results.admin.total > 0 ? ((results.admin.passed / results.admin.total) * 100).toFixed(2) : 0; console.log(`Admin:`); console.log(` 通过: ${colors.green}${results.admin.passed}${colors.reset} / ${results.admin.total}`); console.log(` 失败: ${colors.red}${results.admin.failed}${colors.reset} / ${results.admin.total}`); console.log(` 成功率: ${adminRate}%\n`); // 总体结果 const totalPassed = results.uniapp.passed + results.admin.passed; const totalFailed = results.uniapp.failed + results.admin.failed; const total = results.uniapp.total + results.admin.total; const totalRate = total > 0 ? ((totalPassed / total) * 100).toFixed(2) : 0; console.log(`${colors.blue}总计:${colors.reset}`); console.log(` 通过: ${colors.green}${totalPassed}${colors.reset} / ${total}`); console.log(` 失败: ${colors.red}${totalFailed}${colors.reset} / ${total}`); console.log(` 成功率: ${totalRate}%\n`); // 返回是否全部通过 return totalFailed === 0; } /** * 主函数 */ async function main() { console.log(`${colors.blue}========================================${colors.reset}`); console.log(`${colors.blue} 前后端集成测试${colors.reset}`); console.log(`${colors.blue}========================================${colors.reset}`); try { // 测试Uniapp await testProject('uniapp', TEST_CONFIG.uniapp); // 测试Admin await testProject('admin', TEST_CONFIG.admin); // 打印结果 const allPassed = printResults(); // 退出码 process.exit(allPassed ? 0 : 1); } catch (error) { console.error(`${colors.red}测试执行失败:${colors.reset}`, error.message); process.exit(1); } } // 执行测试 main();