2
0

run-e2e-tests.ps1 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # E2E 测试运行脚本(PowerShell 版本)
  2. #
  3. # 功能:
  4. # 1. 启动 Next.js 开发服务器
  5. # 2. 等待服务器就绪
  6. # 3. 运行 E2E 测试
  7. # 4. 清理并停止服务器
  8. #
  9. # 使用方法:
  10. # .\scripts\run-e2e-tests.ps1
  11. $ErrorActionPreference = "Stop"
  12. Write-Host "🚀 E2E 测试运行脚本" -ForegroundColor Cyan
  13. Write-Host "====================" -ForegroundColor Cyan
  14. Write-Host ""
  15. # ==================== 1. 检查数据库连接 ====================
  16. Write-Host "🔍 检查数据库连接..." -ForegroundColor Cyan
  17. $postgresRunning = docker ps | Select-String "claude-code-hub-db-dev"
  18. if ($postgresRunning) {
  19. Write-Host "✅ PostgreSQL 已运行" -ForegroundColor Green
  20. } else {
  21. Write-Host "❌ PostgreSQL 未运行,正在启动..." -ForegroundColor Yellow
  22. docker compose up -d postgres redis
  23. Write-Host "⏳ 等待数据库启动..." -ForegroundColor Yellow
  24. Start-Sleep -Seconds 5
  25. }
  26. Write-Host ""
  27. # ==================== 2. 启动开发服务器 ====================
  28. Write-Host "🚀 启动 Next.js 开发服务器..." -ForegroundColor Cyan
  29. # 后台启动服务器
  30. $env:PORT = "13500"
  31. $serverProcess = Start-Process -FilePath "bun" -ArgumentList "run", "dev" -PassThru -NoNewWindow -RedirectStandardOutput "$env:TEMP\nextjs-dev.log" -RedirectStandardError "$env:TEMP\nextjs-dev-error.log"
  32. Write-Host " 服务器 PID: $($serverProcess.Id)" -ForegroundColor Gray
  33. Write-Host "⏳ 等待服务器就绪..." -ForegroundColor Yellow
  34. # 等待服务器启动(最多等待 60 秒)
  35. $timeout = 60
  36. $counter = 0
  37. $serverReady = $false
  38. while ($counter -lt $timeout) {
  39. try {
  40. $response = Invoke-WebRequest -Uri "http://localhost:13500/api/actions/health" -UseBasicParsing -ErrorAction SilentlyContinue
  41. if ($response.StatusCode -eq 200) {
  42. Write-Host ""
  43. Write-Host "✅ 服务器已就绪" -ForegroundColor Green
  44. $serverReady = $true
  45. break
  46. }
  47. } catch {
  48. # 继续等待
  49. }
  50. $counter++
  51. Write-Host "." -NoNewline
  52. Start-Sleep -Seconds 1
  53. }
  54. if (-not $serverReady) {
  55. Write-Host ""
  56. Write-Host "❌ 服务器启动超时" -ForegroundColor Red
  57. Stop-Process -Id $serverProcess.Id -Force -ErrorAction SilentlyContinue
  58. exit 1
  59. }
  60. Write-Host ""
  61. # ==================== 3. 运行 E2E 测试 ====================
  62. Write-Host "🧪 运行 E2E 测试..." -ForegroundColor Cyan
  63. Write-Host ""
  64. # 设置环境变量
  65. $env:API_BASE_URL = "http://localhost:13500/api/actions"
  66. $env:AUTO_CLEANUP_TEST_DATA = "true"
  67. # 运行 E2E 测试
  68. $testExitCode = 0
  69. try {
  70. bun run test tests/e2e/
  71. $testExitCode = $LASTEXITCODE
  72. } catch {
  73. $testExitCode = 1
  74. }
  75. Write-Host ""
  76. # ==================== 4. 清理并停止服务器 ====================
  77. Write-Host "🧹 停止开发服务器..." -ForegroundColor Cyan
  78. Stop-Process -Id $serverProcess.Id -Force -ErrorAction SilentlyContinue
  79. Write-Host "✅ 服务器已停止" -ForegroundColor Green
  80. Write-Host ""
  81. # ==================== 5. 输出测试结果 ====================
  82. if ($testExitCode -eq 0) {
  83. Write-Host "✅ E2E 测试全部通过" -ForegroundColor Green
  84. exit 0
  85. } else {
  86. Write-Host "❌ E2E 测试失败" -ForegroundColor Red
  87. exit $testExitCode
  88. }