#!/usr/bin/env node /** * 姓名管理系统API测试脚本 */ const axios = require('axios'); const BASE_URL = 'http://localhost:3000'; // 测试配置 const testConfig = { baseURL: BASE_URL, timeout: 10000, headers: { 'Content-Type': 'application/json' } }; // 测试结果记录 const testResults = { total: 0, passed: 0, failed: 0, results: [] }; // 测试辅助函数 function logTest(name, success, data = null, error = null) { testResults.total++; if (success) { testResults.passed++; console.log(`✅ ${name}`); if (data) console.log(` 数据: ${JSON.stringify(data, null, 2)}`); } else { testResults.failed++; console.log(`❌ ${name}`); if (error) console.log(` 错误: ${error.message}`); } testResults.results.push({ name, success, data, error: error?.message }); } // 主测试函数 async function runTests() { console.log('🧪 开始测试姓名管理系统...\n'); // 1. 测试健康检查 try { const response = await axios.get(`${BASE_URL}/api/health`, testConfig); logTest('服务器健康检查', response.status === 200, response.data); } catch (error) { logTest('服务器健康检查', false, null, error); return; // 如果服务器不可用,直接返回 } // 2. 测试获取支持的选项(无需认证) try { const response = await axios.get(`${BASE_URL}/nameTemplate/supportedOptions`, testConfig); logTest('获取支持的选项', response.status === 200, response.data); } catch (error) { logTest('获取支持的选项', false, null, error); } // 3. 测试生成器状态(无需认证) try { const response = await axios.get(`${BASE_URL}/nameTemplate/generatorStatus`, testConfig); logTest('获取生成器状态', response.status === 200, response.data); } catch (error) { logTest('获取生成器状态', false, null, error); } // 4. 测试数据迁移验证(无需认证) try { const response = await axios.get(`${BASE_URL}/nameTemplate/migrate/validate`, testConfig); logTest('验证数据迁移', response.status === 200, response.data); } catch (error) { logTest('验证数据迁移', false, null, error); } // 5. 测试姓名生成(可能需要认证,先尝试不带认证) try { const generateData = { platform: 'telegram', culture: 'cn', gender: 'neutral', ageGroup: 'adult' }; const response = await axios.post(`${BASE_URL}/nameTemplate/generate`, generateData, testConfig); logTest('生成姓名', response.status === 200, response.data); } catch (error) { if (error.response?.status === 401) { logTest('生成姓名', false, null, new Error('需要认证 (符合预期)')); } else { logTest('生成姓名', false, null, error); } } // 6. 测试获取模板列表(可能需要认证) try { const listData = { culture: 'cn' }; const response = await axios.post(`${BASE_URL}/nameTemplate/list`, listData, testConfig); logTest('获取模板列表', response.status === 200, response.data); } catch (error) { if (error.response?.status === 401) { logTest('获取模板列表', false, null, new Error('需要认证 (符合预期)')); } else { logTest('获取模板列表', false, null, error); } } // 7. 测试热门模板(可能需要认证) try { const popularData = { culture: 'cn', limit: 5 }; const response = await axios.post(`${BASE_URL}/nameTemplate/popular`, popularData, testConfig); logTest('获取热门模板', response.status === 200, response.data); } catch (error) { if (error.response?.status === 401) { logTest('获取热门模板', false, null, new Error('需要认证 (符合预期)')); } else { logTest('获取热门模板', false, null, error); } } // 8. 测试文化统计(可能需要认证) try { const statsData = { culture: 'cn' }; const response = await axios.post(`${BASE_URL}/nameTemplate/stats/culture`, statsData, testConfig); logTest('获取文化统计', response.status === 200, response.data); } catch (error) { if (error.response?.status === 401) { logTest('获取文化统计', false, null, new Error('需要认证 (符合预期)')); } else { logTest('获取文化统计', false, null, error); } } // 测试总结 console.log('\n📊 测试结果总结:'); console.log(`总测试数: ${testResults.total}`); console.log(`通过: ${testResults.passed}`); console.log(`失败: ${testResults.failed}`); console.log(`成功率: ${((testResults.passed / testResults.total) * 100).toFixed(1)}%`); // 评估系统状态 if (testResults.passed >= testResults.total * 0.5) { console.log('\n🎉 系统基本功能正常!'); if (testResults.failed === 0) { console.log('✨ 所有测试通过,系统完全正常!'); } else { console.log('⚠️ 部分功能可能需要认证或进一步配置'); } } else { console.log('\n❌ 系统存在问题,需要进一步检查'); } return testResults; } // 运行测试 if (require.main === module) { runTests() .then(results => { process.exit(results.failed === 0 ? 0 : 1); }) .catch(error => { console.error('测试运行失败:', error); process.exit(1); }); } module.exports = runTests;