#!/usr/bin/env bash set -euo pipefail # e2e-orchestrator.sh # Orquesta backend, frontend y ejecuta smoke_test.py; guarda logs en packages/broadcast-panel/e2e/out ROOT_DIR=$(cd "$(dirname "$0")/.." && pwd) OUT_DIR="$ROOT_DIR/packages/broadcast-panel/e2e/out" mkdir -p "$OUT_DIR" LOG_BACKEND="$OUT_DIR/backend_run.log" LOG_FRONTEND="$OUT_DIR/frontend_run.log" LOG_SMOKE="$OUT_DIR/smoke_run.log" PLAYWRIGHT_WS=${PLAYWRIGHT_WS:-ws://192.168.1.20:3003} BACKEND_URL=${BACKEND_URL:-http://127.0.0.1:4000} FRONTEND_URL=${FRONTEND_URL:-http://127.0.0.1:5175} # Defaults tuned for reliability HEALTH_TIMEOUT=${HEALTH_TIMEOUT:-60} HEALTH_POLL_INTERVAL=${HEALTH_POLL_INTERVAL:-2} START_RETRY_INTERVAL=${START_RETRY_INTERVAL:-2} # Keep track of processes we started to cleanup on exit STARTED_PIDS=() STARTED_LOGS=() echo "OUT_DIR=$OUT_DIR" echo "Logs: backend=$LOG_BACKEND frontend=$LOG_FRONTEND smoke=$LOG_SMOKE" action_fail() { echo "ERROR: $1" >&2 echo "--- last 200 lines of backend log ---" [ -f "$LOG_BACKEND" ] && tail -n 200 "$LOG_BACKEND" || echo '(no backend log)' echo "--- last 200 lines of frontend log ---" [ -f "$LOG_FRONTEND" ] && tail -n 200 "$LOG_FRONTEND" || echo '(no frontend log)' # attempt cleanup of started pids cleanup exit 1 } # Helper to start a command in background and save pid start_bg() { local cmd="$1" local logfile="$2" echo "Starting: $cmd -> $logfile" nohup bash -lc "$cmd" > "$logfile" 2>&1 & local pid=$! echo $pid > "$logfile.pid" STARTED_PIDS+=("$pid") STARTED_LOGS+=("$logfile") # give short boot time sleep $START_RETRY_INTERVAL } # Cleanup handler kills started pids gracefully cleanup() { echo "Cleaning up started processes..." for pid in "${STARTED_PIDS[@]:-}"; do if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then echo "Killing pid $pid" kill "$pid" || true sleep 1 if kill -0 "$pid" 2>/dev/null; then echo "Force killing $pid" kill -9 "$pid" || true fi fi done } trap 'echo "Signal received, running cleanup"; cleanup; exit 130' INT TERM trap 'cleanup' EXIT wait_for_health() { local url="$1" local timeout=${2:-$HEALTH_TIMEOUT} local interval=${3:-$HEALTH_POLL_INTERVAL} local start start=$(date +%s) echo "Waiting for health $url (timeout ${timeout}s)" while true; do if curl -sSf "$url" >/dev/null 2>&1; then echo "Healthy: $url" return 0 fi now=$(date +%s) elapsed=$((now - start)) if [ $elapsed -ge $timeout ]; then echo "Timeout waiting for $url" return 1 fi sleep $interval done } # 1) Start backend if ! ss -ltnp | grep -q ':4000\b'; then echo 'Starting backend-api...' start_bg "npm --prefix $ROOT_DIR/packages/backend-api run dev" "$LOG_BACKEND" else echo 'Port 4000 already listening; skipping backend start' fi # Wait backend health (with retries) if ! wait_for_health "$BACKEND_URL/health" "$HEALTH_TIMEOUT" "$HEALTH_POLL_INTERVAL"; then action_fail "backend did not become healthy within ${HEALTH_TIMEOUT}s" fi # 2) Start frontend if ! ss -ltnp | grep -q ':5175\b'; then echo 'Starting broadcast-panel (Vite)...' start_bg "cd $ROOT_DIR/packages/broadcast-panel && PORT=5175 npm run dev -- --port 5175" "$LOG_FRONTEND" else echo 'Port 5175 already listening; skipping frontend start' fi # Wait frontend health (index) if ! wait_for_health "$FRONTEND_URL/" "$HEALTH_TIMEOUT" "$HEALTH_POLL_INTERVAL"; then action_fail "frontend did not become healthy within ${HEALTH_TIMEOUT}s" fi # Give services a moment to stabilize sleep 1 # 3) Run smoke test (create session, poll token, validate, navigate) # Ensure previous smoke log is rotated if [ -f "$LOG_SMOKE" ]; then mv "$LOG_SMOKE" "$LOG_SMOKE.$(date +%s)" || true fi echo 'Running smoke_test.py (this will validate token and optionally navigate via Playwright)' python3 "$ROOT_DIR/packages/broadcast-panel/e2e/smoke_test.py" \ --backend "$BACKEND_URL" \ --frontend "$FRONTEND_URL" \ --ws "$PLAYWRIGHT_WS" \ --room smoke-room \ --username smoke-tester \ --poll-timeout 30 > "$LOG_SMOKE" 2>&1 || true # 4) Collect summary echo '--- SUMMARY: logs and out dir ---' printf '\n-- last lines of backend log --\n' [ -f "$LOG_BACKEND" ] && tail -n 200 "$LOG_BACKEND" || echo '(no backend log)' printf '\n-- last lines of frontend log --\n' [ -f "$LOG_FRONTEND" ] && tail -n 200 "$LOG_FRONTEND" || echo '(no frontend log)' printf '\n-- smoke log --\n' [ -f "$LOG_SMOKE" ] && sed -n '1,400p' "$LOG_SMOKE" || echo '(no smoke log)' printf '\n-- out directory listing --\n' ls -la "$OUT_DIR" echo '\nOrchestrator finished.'