#!/usr/bin/env bash # # 一键跑核心业务测试:书籍 大纲 → 内容 → 音频 完整链路 # 适用场景:每次修改 book-generator / 每次发布上线前 # # 用法: # ./scripts/run-core-business-test.sh # 自动起后端+前端再跑 # FRONTEND_URL=http://localhost:5173 \ # BACKEND_URL=http://127.0.0.1:3000 \ # ./scripts/run-core-business-test.sh # 手动起服务 # SKIP_SERVER=1 ./scripts/run-core-business-test.sh # 假设服务已起好 # set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" cd "$ROOT_DIR" BACKEND_URL="${BACKEND_URL:-http://127.0.0.1:3000}" FRONTEND_URL="${FRONTEND_URL:-http://127.0.0.1:5173}" SKIP_SERVER="${SKIP_SERVER:-0}" echo "==========================================" echo " 核心业务测试:书籍 大纲→内容→音频" echo "==========================================" echo " BACKEND_URL=$BACKEND_URL" echo " FRONTEND_URL=$FRONTEND_URL" echo " SKIP_SERVER=$SKIP_SERVER" echo "==========================================" # 1. 健康检查(如果服务没起,会尝试自动起) wait_for_url() { local url="$1" local label="$2" local max_attempts="${3:-30}" for i in $(seq 1 "$max_attempts"); do if curl -sf "$url" >/dev/null 2>&1; then echo " ✅ $label 就绪 ($url)" return 0 fi sleep 1 done return 1 } if [ "$SKIP_SERVER" != "1" ]; then echo "[1/4] 检查后端..." if ! wait_for_url "$BACKEND_URL/health" "后端" 5; then echo " ⚠️ 后端未启动,尝试自动启动..." (cd server && nohup npx tsx src/app.ts > /tmp/core-test-backend.log 2>&1 &) wait_for_url "$BACKEND_URL/health" "后端" 30 || { echo " ❌ 后端启动失败,查看 /tmp/core-test-backend.log" tail -50 /tmp/core-test-backend.log exit 1 } fi echo "[2/4] 检查前端..." if ! wait_for_url "$FRONTEND_URL" "前端" 5; then echo " ⚠️ 前端未启动,尝试自动启动..." (cd my-uniapp-vue3 && nohup npm run dev:h5 > /tmp/core-test-frontend.log 2>&1 &) wait_for_url "$FRONTEND_URL" "前端" 60 || { echo " ❌ 前端启动失败,查看 /tmp/core-test-frontend.log" tail -80 /tmp/core-test-frontend.log exit 1 } fi fi # 2. 安装依赖检查(首次运行需要) echo "[3/4] 检查 Playwright..." if [ ! -d "$ROOT_DIR/node_modules/@playwright" ]; then echo " ⚠️ 安装 Playwright..." npm install --no-audit --no-fund fi if ! npx playwright --version >/dev/null 2>&1; then echo " ❌ playwright 不可用" exit 1 fi # 3. 跑核心业务测试 echo "[4/4] 运行核心业务测试..." set +e BACKEND_URL="$BACKEND_URL" FRONTEND_URL="$FRONTEND_URL" \ npx playwright test \ --project=core-business \ --config=tests/playwright.config.ts \ --reporter=list EXIT_CODE=$? set -e echo "" echo "==========================================" if [ "$EXIT_CODE" -eq 0 ]; then echo " ✅ 核心业务链路全部通过" else echo " ❌ 核心业务链路失败(exit=$EXIT_CODE)" echo " 报告位置:tests/test-results/report" echo " 结构化结果:tests/test-results/results.json" fi echo "==========================================" exit "$EXIT_CODE"