#!/bin/bash

# Playwright E2E Headless 模式测试脚本
# 用于完整的端到端测试和UAT测试

set -e

echo "========================================"
echo "Playwright E2E Headless 测试脚本"
echo "========================================"

# 设置工作目录
cd /Users/zhangxiang/Codes/Novalon/novalon-manage-system/novalon-manage-web

# 检查前端开发服务器
echo "🔍 检查前端开发服务器..."
if ! lsof -ti:3001 > /dev/null; then
    echo "❌ 前端开发服务器未运行，启动中..."
    npm run dev > /tmp/frontend.log 2>&1 &
    echo "✅ 前端开发服务器已启动（PID: $!）"
    sleep 10
fi

# 检查后端服务
echo "🔍 检查后端服务..."
if ! lsof -ti:8080 > /dev/null; then
    echo "❌ 后端服务未运行，启动中..."
    cd /Users/zhangxiang/Codes/Novalon/novalon-manage-system/novalon-manage-api
    mvn spring-boot:run -pl manage-gateway > /tmp/gateway.log 2>&1 &
    echo "✅ 后端服务已启动（PID: $!）"
    sleep 30
fi

# 回到前端目录
cd /Users/zhangxiang/Codes/Novalon/novalon-manage-system/novalon-manage-web

# 运行 E2E 测试（Headless 模式）
echo "🚀 运行 E2E 测试（Headless 模式）..."
PLAYWRIGHT_HEADLESS=true npx playwright test --project=chromium --reporter=list

# 生成测试报告
echo "📊 生成测试报告..."
npx playwright show-report playwright-report

echo "✅ E2E Headless 测试完成！"
echo "_report: playwright-report/index.html"
