#!/usr/bin/env bash
# One command to verify a change.
#
#   scripts/check         lint + the fast tests (about 15 seconds; no real browser needed)
#   scripts/check full    lint + EVERY test (about 6 minutes; needs Google Chrome)
#
# Run `full` before you hand work back if you touched the engine, pool, drivers, server, record,
# repair, MCP bridge or anything a real browser exercises. A new test file that needs a browser
# is NOT skipped by the fast run (it just makes it slower), so the fast run can only be
# stricter than this list, never weaker. Re-check the list below when you add browser tests.
set -euo pipefail

root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$root"

case "$root" in
  "$HOME"/Desktop/*|"$HOME"/Documents/*)
    echo "warning: $root is in an iCloud-synced folder. Synced files can vanish or be duplicated" >&2
    echo "         (' 2' copies, a broken virtualenv). Work in ~/dev/api-everything instead." >&2
    ;;
esac

mode="${1:-fast}"
if [[ "$mode" != "fast" && "$mode" != "full" ]]; then
  echo "usage: scripts/check [fast|full]" >&2
  exit 2
fi

echo "== lint =="
uv run ruff check

# Test files that drive a real browser end to end (the slow ones).
browser_tests=(
  tests/test_attach.py
  tests/test_drivers.py
  tests/test_engine.py
  tests/test_engine_actions.py
  tests/test_pool_chrome.py
  tests/test_record_session.py
  tests/test_repair.py
  tests/test_server_chrome.py
)

if [[ "$mode" == "full" ]]; then
  echo "== all tests (about 6 minutes) =="
  uv run pytest -q -rs -p no:cacheprovider
else
  ignores=()
  for file in "${browser_tests[@]}"; do ignores+=(--ignore="$file"); done
  echo "== fast tests (skipping ${#browser_tests[@]} real-browser files) =="
  uv run pytest -q -rs -p no:cacheprovider "${ignores[@]}"
  echo
  echo "fast check passed. Not run: ${browser_tests[*]}"
  echo "Run 'scripts/check full' before finishing work that touches a browser path."
fi
