#!/usr/bin/env bash
# Run the Playwright test suite in parallel using pytest-xdist.
#
# Usage:
#   ./run_parallel.sh                          # both browsers, auto workers
#   ./run_parallel.sh --browser firefox        # override browser selection
#   ./run_parallel.sh test_step3_exercises.py  # single file, both browsers
#
# Environment variables:
#   PYTEST_WORKERS   number of parallel workers (default: auto = one per CPU core)
#   PYTEST_BROWSERS  space-separated list of browsers (default: "firefox chromium")
#
# Examples:
#   PYTEST_WORKERS=4 ./run_parallel.sh
#   PYTEST_WORKERS=2 PYTEST_BROWSERS="firefox" ./run_parallel.sh

set -euo pipefail

cd "$(dirname "$0")"

WORKERS="${PYTEST_WORKERS:-auto}"
BROWSERS="${PYTEST_BROWSERS:-firefox chromium}"

# Build --browser flags from the BROWSERS variable
BROWSER_FLAGS=()
for b in $BROWSERS; do
    BROWSER_FLAGS+=(--browser "$b")
done

.venv/bin/python -m pytest \
    "${BROWSER_FLAGS[@]}" \
    --dist loadfile \
    -n "$WORKERS" \
    -v \
    "$@"
