| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- /**
- * 全局测试初始化
- * 在所有测试运行前执行,验证服务健康、准备测试数据
- */
- import { initApiClient, apiGet, apiPost, setAuthToken } from './helpers/api-client';
- async function globalSetup() {
- console.log('\n========================================');
- console.log(' 自动化测试 - 全局初始化');
- console.log('========================================\n');
- // 1. 初始化 API 客户端(默认走 IPv4,规避 Node.js 22 的 IPv6 解析)
- await initApiClient();
- // 2. 健康检查
- console.log('[setup] 检查服务健康状态...');
- try {
- const health = await apiGet('/health');
- if (health.code === 0) {
- console.log(' ✅ 后端服务正常');
- } else {
- console.warn(' ⚠ 后端服务状态异常:', health);
- }
- } catch (e: any) {
- console.error(' ❌ 后端服务不可达!', e.message);
- throw new Error(`后端服务不可达: ${e.message}`);
- }
- // 3. 检查数据库连接
- console.log('[setup] 检查数据库连接...');
- try {
- const db = await apiGet('/api/tts/test-db');
- if (db.code === 0) {
- console.log(' ✅ 数据库连接正常');
- } else {
- console.warn(' ⚠ 数据库连接异常:', db);
- }
- } catch (e: any) {
- console.warn(' ⚠ 数据库检查失败:', e.message);
- }
- // 4. 创建测试用户并获取 token
- console.log('[setup] 创建测试用户...');
- try {
- // 使用固定测试手机号
- const codeRes = await apiPost('/api/auth/send-code', { phone: '13800000001' });
- const code = codeRes.data?.code || '123456';
- const loginRes = await apiPost('/api/auth/login', { phone: '13800000001', code });
- const token = loginRes.data?.token;
- if (token) {
- setAuthToken(token);
- }
- console.log(` ✅ 测试用户就绪 (token: ${token ? '已获取' : '未获取'})`);
- } catch (e: any) {
- console.warn(' ⚠ 测试用户创建失败:', e.message);
- }
- // 5. 检查关键 API 列表是否可用
- console.log('[setup] 验证核心 API 端点...');
- const checks = [
- { name: '音色列表', path: '/api/tts/voices' },
- { name: '音频列表', path: '/api/player/audio/list' },
- { name: '书籍列表', path: '/api/book-generator/langgraph/books' },
- { name: '专辑列表', path: '/api/book-generator/albums' },
- { name: '历史记录', path: '/api/history' },
- ];
- for (const check of checks) {
- try {
- const res = await apiGet(check.path);
- if (res.code === 0) {
- console.log(` ✅ ${check.name}`);
- } else {
- console.warn(` ⚠ ${check.name}: code=${res.code}`);
- }
- } catch (e: any) {
- console.warn(` ⚠ ${check.name}: ${e.message}`);
- }
- }
- console.log('\n========================================');
- console.log(' 全局初始化完成,开始运行测试...');
- console.log('========================================\n');
- }
- export default globalSetup;
|