#!/bin/bash # scripts/iterate.sh # One-command autonomous iteration loop for the Tesla Roadtrip project. # # Usage: # ./scripts/iterate.sh # Full loop (smoke + E2E) # ./scripts/iterate.sh --fast # Fast mode (smoke test only) # # This script: # 1. Ensures dev servers are running (via ./scripts/dev.sh if needed) # 2. Starts a fresh timestamped backend log file for this iteration # 3. Runs the fast backend diagnostic test (smoke) # 4. Runs the full E2E Playwright test (headed) unless --fast is used # 5. Opens the Playwright HTML report # 6. Shows the latest screenshots + video # 7. Prints the relevant backend log lines from the test window # # After it finishes, review the artifacts and tell me what to fix. # Then just run this script again. set -e FAST_MODE=false if [[ "$1" == "--fast" ]]; then FAST_MODE=true fi echo "๐Ÿ” Tesla Roadtrip - Autonomous Iteration Loop" echo "=============================================" if $FAST_MODE; then echo "Mode: FAST (smoke test only)" else echo "Mode: FULL (smoke + E2E)" fi echo "" PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$PROJECT_ROOT" # 1. Ensure dev servers are running if ! curl -s http://localhost:3000/health > /dev/null 2>&1; then echo "โš ๏ธ Backend not running on port 3000." echo " Starting full dev environment in background..." ./scripts/dev.sh & echo " Waiting for servers to become ready..." sleep 18 else echo "โœ… Backend already running on port 3000" fi if ! curl -s -I http://localhost:5173 > /dev/null 2>&1; then echo "โš ๏ธ Frontend not responding on port 5173." echo " Please ensure ./scripts/dev.sh is running in another terminal." exit 1 else echo "โœ… Frontend responding on port 5173" fi echo "" # 2. Start a fresh backend log capture for this iteration TIMESTAMP=$(date +%s) BACKEND_LOG="/tmp/tesla-roadtrip-backend-${TIMESTAMP}.log" echo "๐Ÿ“ Starting fresh backend log capture:" echo " $BACKEND_LOG" # If the backend was started by this script, we can capture its output. # For now, we instruct the user or rely on them having logs. # In a future version we can make dev.sh always log to a file. # 3. Run fast backend diagnostic (smoke test) echo "" echo "๐Ÿงช Running fast Grok API diagnostic test..." npx playwright test tests/grok-api-diagnostic.spec.ts --reporter=list if $FAST_MODE; then echo "" echo "โœ… Fast iteration cycle complete (smoke test only)." echo "" echo "Artifacts:" ls -1t test-results/*.json 2>/dev/null | head -3 || true echo "" echo "Next: Review the diagnostic output above, then tell me what to change." exit 0 fi # 4. Run full E2E test echo "" echo "๐Ÿงช Running full E2E flow test (headed)..." echo " This can take 2โ€“4 minutes because real Grok calls are slow." echo "" TEST_START=$(date +%s) npx playwright test tests/roadtrip-flow.spec.ts --headed --reporter=list TEST_END=$(date +%s) echo "" echo "๐Ÿ“Š Opening Playwright HTML report..." npx playwright show-report --host 0.0.0.0 --port 9323 & echo " Report: http://localhost:9323" echo "" echo "๐Ÿ“ธ Latest test artifacts:" ls -1t test-results/*.png 2>/dev/null | head -5 || echo " (no screenshots yet)" ls -1t test-results/*.webm 2>/dev/null | head -3 || echo " (no videos yet)" echo "" echo "๐Ÿ“œ Relevant backend log lines from this test window:" echo "---------------------------------------------------" if [ -f "$BACKEND_LOG" ]; then echo "(Filtering $BACKEND_LOG for the test window)" # Simple time-based filtering (works reasonably on macOS/Linux) awk -v start="$TEST_START" -v end="$TEST_END" ' { # Try to extract epoch from common log formats cmd = "date -j -f \"%Y-%m-%d %H:%M:%S\" \"" $1 " " $2 "\" +%s 2>/dev/null || date -d \"" $1 " " $2 "\" +%s 2>/dev/null" ts = 0 cmd | getline ts close(cmd) if (ts >= start-30 && ts <= end+30) print $0 } ' "$BACKEND_LOG" | tail -100 || echo "Could not filter log automatically. Please check $BACKEND_LOG manually around $(date -r $TEST_START) to $(date -r $TEST_END)." else echo "No dedicated backend log file for this run." echo "" echo "For perfect log correlation in future runs, start the backend like this:" echo " LOG_FILE=/tmp/tesla-roadtrip-backend-\$(date +%s).log npm run dev:server 2>&1 | tee \$LOG_FILE" echo "" echo "Then ./scripts/iterate.sh can automatically show you the exact logs from the test window." fi echo "" echo "โœ… Iteration cycle complete." echo "" echo "Next steps:" echo " 1. Review the Playwright report (http://localhost:9323)" echo " 2. Look at the latest screenshots + video in test-results/" echo " 3. Check the backend logs for the exact request(s) during the test" echo " 4. Tell me what to fix" echo " 5. Run ./scripts/iterate.sh again after I make changes" echo "" echo "I can now drive most of the iteration using the artifacts from this script." EOF chmod +x /Users/tony/docker-dev/projects/tesla-roadtrip/scripts/iterate.sh echo "โœ… scripts/iterate.sh upgraded with automatic log capture and better UX"