learning_ai_invt_trdg/scripts/tests/run-e2e.sh
Saravana Achu Mac a217fb409f fix(test): correct path calculation in run-e2e.sh
Fixed PROJECT_ROOT path calculation from ../ to ../..
Script is in scripts/tests/, so need to go up two levels to reach project root
WEB_DIR is now correctly set to project root/web
2026-05-09 13:35:04 -07:00

274 lines
7.4 KiB
Bash
Executable File

#!/bin/bash
set -e
# Test runner script for E2E tests
# Runs Playwright tests, builds reports, and checks health
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
WEB_DIR="$PROJECT_ROOT/web"
REPORTS_DIR="$SCRIPT_DIR/reports"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
cd "$WEB_DIR"
echo "========================================="
echo "E2E Test Runner"
echo "========================================="
echo "Project root: $PROJECT_ROOT"
echo "Web directory: $WEB_DIR"
echo "Reports directory: $REPORTS_DIR"
echo "Timestamp: $TIMESTAMP"
echo "========================================="
# Create reports directory
mkdir -p "$REPORTS_DIR"
mkdir -p playwright-report
mkdir -p test-results
# Initialize report files
REPORT_JSON="$REPORTS_DIR/test-report-$TIMESTAMP.json"
REPORT_MD="$REPORTS_DIR/test-report-$TIMESTAMP.md"
SUMMARY_JSON="$REPORTS_DIR/summary-$TIMESTAMP.json"
# Create initial JSON structure
cat > "$REPORT_JSON" << EOF
{
"timestamp": "$TIMESTAMP",
"project_root": "$PROJECT_ROOT",
"web_dir": "$WEB_DIR",
"status": "running",
"tests": [],
"summary": {
"total": 0,
"passed": 0,
"failed": 0,
"skipped": 0,
"duration": 0
},
"failures": [],
"health_check": {
"status": "pending",
"app_running": false,
"app_url": "http://localhost:3050"
}
}
EOF
# Check if node_modules exists
if [ ! -d "node_modules" ]; then
echo "Installing dependencies..."
pnpm install
fi
# Check if Playwright is installed
if ! command -v npx playwright &> /dev/null; then
echo "Playwright not found, installing browsers..."
pnpm exec playwright install chromium
fi
echo ""
echo "Running E2E tests..."
echo "========================================="
# Run Playwright tests with JSON reporter for machine-readable output
START_TIME=$(date +%s)
pnpm exec playwright test \
--reporter=json \
--reporter=list \
--output="$REPORTS_DIR/results-$TIMESTAMP.json" \
2>&1 | tee "$REPORTS_DIR/test-output-$TIMESTAMP.log"
TEST_EXIT_CODE=$?
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
echo ""
echo "========================================="
echo "Test Results"
echo "========================================="
# Parse JSON results if available
if [ -f "$REPORTS_DIR/results-$TIMESTAMP.json" ]; then
# Extract summary from Playwright JSON output
TOTAL_TESTS=$(cat "$REPORTS_DIR/results-$TIMESTAMP.json" | grep -o '"expected":' | wc -l || echo "0")
PASSED_TESTS=$(cat "$REPORTS_DIR/results-$TIMESTAMP.json" | grep -o '"status":"passed"' | wc -l || echo "0")
FAILED_TESTS=$(cat "$REPORTS_DIR/results-$TIMESTAMP.json" | grep -o '"status":"failed"' | wc -l || echo "0")
echo "Total tests: $TOTAL_TESTS"
echo "Passed: $PASSED_TESTS"
echo "Failed: $FAILED_TESTS"
echo "Duration: ${DURATION}s"
else
TOTAL_TESTS="unknown"
PASSED_TESTS="unknown"
FAILED_TESTS="unknown"
echo "Test count: unknown (JSON report not available)"
fi
if [ $TEST_EXIT_CODE -eq 0 ]; then
echo "✅ All tests passed"
STATUS="passed"
else
echo "❌ Some tests failed"
STATUS="failed"
fi
echo ""
echo "Report location: $REPORTS_DIR"
echo "========================================="
# Update JSON report with results
cat > "$REPORT_JSON" << EOF
{
"timestamp": "$TIMESTAMP",
"project_root": "$PROJECT_ROOT",
"web_dir": "$WEB_DIR",
"status": "$STATUS",
"exit_code": $TEST_EXIT_CODE,
"tests": {
"total": $TOTAL_TESTS,
"passed": $PASSED_TESTS,
"failed": $FAILED_TESTS,
"skipped": 0,
"duration": $DURATION
},
"report_files": {
"json": "$REPORTS_DIR/results-$TIMESTAMP.json",
"log": "$REPORTS_DIR/test-output-$TIMESTAMP.log",
"html": "$WEB_DIR/playwright-report/index.html"
},
"health_check": {
"status": "pending",
"app_running": false,
"app_url": "http://localhost:3050"
},
"actionable": {
"if_failed": "Review test-output-$TIMESTAMP.log for specific failure details. Run individual tests with: pnpm exec playwright test --project=chromium <test-file>",
"if_passed": "All tests passed. Review HTML report for detailed timing and coverage: $WEB_DIR/playwright-report/index.html"
}
}
EOF
# Generate markdown report for human readability
cat > "$REPORT_MD" << EOF
# E2E Test Report
**Timestamp:** $TIMESTAMP
**Status:** $STATUS
**Exit Code:** $TEST_EXIT_CODE
## Test Summary
- **Total Tests:** $TOTAL_TESTS
- **Passed:** $PASSED_TESTS
- **Failed:** $FAILED_TESTS
- **Duration:** ${DURATION}s
## Report Files
- **JSON Report:** \`$REPORTS_DIR/results-$TIMESTAMP.json\`
- **Log Output:** \`$REPORTS_DIR/test-output-$TIMESTAMP.log\`
- **HTML Report:** \`$WEB_DIR/playwright-report/index.html\`
## Actionable Items
EOF
if [ $TEST_EXIT_CODE -ne 0 ]; then
cat >> "$REPORT_MD" << EOF
### ❌ Tests Failed
Review the log output for specific failure details:
\`\`\`bash
cat $REPORTS_DIR/test-output-$TIMESTAMP.log
\`\`\`
To debug individual tests:
\`\`\`bash
cd $WEB_DIR
pnpm exec playwright test --project=chromium <test-file>
\`\`\`
Common failure reasons:
- Application not running on http://localhost:3050
- Test selectors changed or elements not found
- Network timeouts or API failures
- Browser compatibility issues
EOF
else
cat >> "$REPORT_MD" << EOF
### ✅ All Tests Passed
Review the HTML report for detailed timing and coverage:
\`\`\`bash
open $WEB_DIR/playwright-report/index.html
\`\`\`
EOF
fi
# Check health
echo ""
echo "Checking application health..."
echo "========================================="
APP_RUNNING=false
if curl -s http://localhost:3050 > /dev/null 2>&1; then
echo "✅ Application is running on http://localhost:3050"
APP_RUNNING=true
HEALTH_STATUS="healthy"
else
echo "⚠️ Application is not running on http://localhost:3050"
echo "Start it with: cd web && pnpm dev -- -p 3050"
HEALTH_STATUS="unhealthy"
fi
# Update health check in JSON
cat > "$SUMMARY_JSON" << EOF
{
"timestamp": "$TIMESTAMP",
"test_status": "$STATUS",
"test_exit_code": $TEST_EXIT_CODE,
"tests_total": $TOTAL_TESTS,
"tests_passed": $PASSED_TESTS,
"tests_failed": $FAILED_TESTS,
"duration_seconds": $DURATION,
"health_check": {
"status": "$HEALTH_STATUS",
"app_running": $APP_RUNNING,
"app_url": "http://localhost:3050"
},
"report_dir": "$REPORTS_DIR",
"latest_report": "$REPORT_JSON",
"latest_markdown": "$REPORT_MD",
"for_ai_agents": {
"quick_summary": "$STATUS - $PASSED_TESTS/$TOTAL_TESTS tests passed in ${DURATION}s",
"if_failed_check": "$REPORTS_DIR/test-output-$TIMESTAMP.log",
"html_report": "$WEB_DIR/playwright-report/index.html",
"next_action": $([ $TEST_EXIT_CODE -ne 0 ] && echo "\"Review failures in log and fix broken selectors or API issues\"" || echo "\"Review HTML report for performance optimization\"")
}
}
EOF
echo ""
echo "========================================="
echo "Summary"
echo "========================================="
echo "Tests run: Complete"
echo "Status: $STATUS"
echo "JSON Report: $REPORT_JSON"
echo "Markdown Report: $REPORT_MD"
echo "AI Summary: $SUMMARY_JSON"
echo "HTML Report: $WEB_DIR/playwright-report/index.html"
echo "Health check: $HEALTH_STATUS"
echo "========================================="
# Create symlink to latest report
ln -sf "$REPORT_JSON" "$REPORTS_DIR/latest.json"
ln -sf "$REPORT_MD" "$REPORTS_DIR/latest.md"
ln -sf "$SUMMARY_JSON" "$REPORTS_DIR/latest-summary.json"
echo "Latest reports linked to: $REPORTS_DIR/latest.{json,md,summary.json}"
echo "========================================="
exit $TEST_EXIT_CODE