#!/bin/bash
# =====================================================================
# run-all-tests.sh - Run all tests
# =====================================================================
# This script runs all tests in the correct order:
# 1. Run pre-test setup
# 2. Run integration tests
# 3. Run accessibility tests
# =====================================================================

set -e

echo "=== Running all tests ==="

# Get the directory of this script
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

# Get the base URL from the command line or use the default
if [ -z "$1" ]; then
  BASE_URL="http://localhost:8080"
else
  BASE_URL="$1"
fi

# Run pre-test setup
echo "=== Running pre-test setup ==="
"$SCRIPT_DIR/pre-test-setup.sh"

# Run integration tests
echo "=== Running integration tests ==="
for test_script in "$SCRIPT_DIR/integration"/*.sh; do
  if [ -f "$test_script" ]; then
    echo "Running $test_script..."
    if "$test_script" "$BASE_URL"; then
      echo "✅ $(basename "$test_script") passed"
    else
      echo "❌ $(basename "$test_script") failed"
      FAILED_TESTS=$((FAILED_TESTS + 1))
    fi
  fi
done

# Run accessibility tests
echo "=== Running accessibility tests ==="
if [ -f "$SCRIPT_DIR/accessibility/run-accessibility-tests.sh" ]; then
  if "$SCRIPT_DIR/accessibility/run-accessibility-tests.sh" "$BASE_URL"; then
    echo "✅ Accessibility tests passed"
  else
    echo "❌ Accessibility tests failed"
    FAILED_TESTS=$((FAILED_TESTS + 1))
  fi
else
  echo "⚠️ Accessibility tests not found, skipping"
fi

# Print summary
echo "=== Test Summary ==="
if [ "$FAILED_TESTS" -gt 0 ]; then
  echo "❌ $FAILED_TESTS test suites failed"
  exit 1
else
  echo "✅ All tests passed"
  exit 0
fi
