# 测试和健康检查脚本 ## 使用流程 ### 1️⃣ 启动 Redis(如果未启动) ```bash # 启动 Redis redis-server --daemonize yes # 或者使用后台启动 redis-server & ``` ### 2️⃣ 启动 Identity Service ```bash # 在项目根目录 npm run start:dev ``` ### 3️⃣ 运行健康检查 ```bash # 进入 scripts 目录 cd scripts # 运行健康检查 ./health-check.sh ``` **期望输出:** ``` 🏥 开始健康检查... === 数据库服务 === Checking PostgreSQL ... ✓ OK === 缓存服务 === Checking Redis ... ✓ OK === 消息队列服务 === Checking Kafka ... ✓ OK === 应用服务 === Checking Identity Service ... ✓ OK === API 文档 === Checking Swagger UI ... ✓ OK ====================================== 健康检查完成! 正常: 5 异常: 0 ====================================== ✓ 所有服务正常! 现在可以运行测试: ./scripts/quick-test.sh ``` ### 4️⃣ 运行快速功能测试 ```bash ./quick-test.sh ``` 这个脚本会自动测试所有核心功能: - ✅ 账户创建 - ✅ 个人资料管理 - ✅ 设备管理 - ✅ Token 刷新 - ✅ 助记词恢复 - ✅ 推荐码查询 - ✅ KYC 提交 - ✅ 错误场景处理 --- ## 如果健康检查失败 当 `health-check.sh` 显示服务异常时,它会给出修复建议: ```bash ✗ 存在异常的服务! 修复建议: • Redis: redis-server --daemonize yes • Identity Service: npm run start:dev 或者运行一键启动脚本: ./scripts/start-all.sh ``` ### 手动修复步骤 #### Redis 未启动 ```bash redis-server --daemonize yes ``` #### Identity Service 未启动 ```bash # 在项目根目录 npm run start:dev ``` #### PostgreSQL 未启动 ```bash # Ubuntu/Debian sudo systemctl start postgresql # macOS brew services start postgresql # Windows # 使用 PostgreSQL 服务管理器 ``` --- ## 一键启动(推荐) 如果想一次性启动所有服务: ```bash ./scripts/start-all.sh ``` 这个脚本会: 1. ✅ 启动 Redis(如果未运行) 2. ✅ 检查 PostgreSQL 状态 3. ✅ 检查 Kafka 状态 4. ✅ 启动 Identity Service 5. ✅ 等待服务就绪 6. ✅ 自动运行健康检查 --- ## 脚本说明 ### `health-check.sh` - **作用**: 检查所有依赖服务是否正常运行 - **使用场景**: 部署前、调试时 - **前置条件**: 所有服务需要已启动 ### `quick-test.sh` - **作用**: 快速测试所有核心 API 功能 - **使用场景**: 验证功能完整性、回归测试 - **前置条件**: `health-check.sh` 通过 ### `start-all.sh` - **作用**: 一键启动所有服务 - **使用场景**: 初次启动、快速启动环境 - **前置条件**: 依赖已安装(PostgreSQL、Redis、Kafka) --- ## 常见问题 ### Q: 为什么 `curl: command not found`? **A:** 安装 curl ```bash # Ubuntu/Debian sudo apt-get install curl # macOS (通常已安装) brew install curl ``` ### Q: 为什么 `jq: command not found`? **A:** `quick-test.sh` 需要 jq 来解析 JSON ```bash # Ubuntu/Debian sudo apt-get install jq # macOS brew install jq ``` ### Q: Redis 启动失败? **A:** 检查是否已经在运行 ```bash # 查看 Redis 进程 ps aux | grep redis # 如果已运行,先停止 redis-cli shutdown # 然后重新启动 redis-server --daemonize yes ``` ### Q: Identity Service 启动失败? **A:** 检查日志 ```bash # 查看启动日志 npm run start:dev # 常见问题: # 1. 端口 3000 已被占用 → 修改 PORT 环境变量 # 2. 数据库连接失败 → 检查 .env 配置 # 3. Redis 连接失败 → 确保 Redis 已启动 ``` --- ## 完整测试流程 ```bash # 1. 进入项目目录 cd ~/work/rwadurian/backend/services/identity-service # 2. 安装依赖(首次) npm install # 3. 启动所有服务 ./scripts/start-all.sh # 4. 运行健康检查 ./scripts/health-check.sh # 5. 运行快速测试 ./scripts/quick-test.sh # 6. 运行完整测试 npm test npm run test:e2e ``` --- ## 下一步 所有脚本通过后,可以进行: - 📊 [集成测试](../test/integration-checklist.md) - 🔒 [安全测试](../test/security-test-checklist.md) - ⚡ [性能测试](../test/performance-test.md) - 📝 [手动测试](../test/manual-test-scenarios.md)