#!/usr/bin/env bash
# Run the agent-first-http test suite inside the test container.
#
#   tests/test.sh                       # default gates only (no browser)
#   tests/test.sh static                # fmt + build + clippy
#   tests/test.sh unit                  # lib + bin unit tests
#   tests/test.sh integration           # full browser-integration suite
#   tests/test.sh takeover              # display takeover proxy + KasmVNC smoke
#   tests/test.sh all                   # static + unit + integration
#   tests/test.sh release               # everything: all + takeover (release gate)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MODE="${1:-default}"

run() { "$SCRIPT_DIR/run-in-docker.sh" bash -c "$1"; }

case "$MODE" in
  static)
    run "bash tests/check_container_third_party_versions.sh"
    run "cargo fmt --all --check"
    run "cargo build --all-features"
    run "cargo build --no-default-features --features sdk"
    run "cargo clippy --all-features --all-targets -- -D warnings"
    run "cargo clippy --no-default-features --features sdk --all-targets -- -D warnings"
    ;;
  unit)
    run "cargo test --lib --bins --features sdk,cli"
    run "bash tests/check_regressions.sh"
    ;;
  integration)
    run "cargo test --all-features \
      --test fetch_http_only \
      --test health_capabilities \
      --test cdp_proxy \
      --test browser_fetch \
      --test fingerprint_chromium \
      --test lightpanda_backend \
      --test camoufox_foxbridge \
      --test takeover_backends \
      --test cookie_jar \
      --test cookie_jar_isolation \
      --test retry_backoff \
      --test tabs_management \
      --test proxy_tls \
      --test env_isolation \
      --test display_takeover \
      --test ui_takeover \
      --test host_extras \
      --test network_artifact \
      --test storage_artifact \
      --test stream_redirect \
      --test unix_listener"
    ;;
  takeover)
    run "cargo test --all-features --test display_takeover -- --test-threads=1"
    run "cargo test --all-features --test display_takeover -- --ignored --test-threads=1"
    ;;
  default)
    run "cargo fmt --all --check"
    run "cargo build --all-features"
    run "cargo build --no-default-features --features sdk"
    run "cargo clippy --all-features --all-targets -- -D warnings"
    run "cargo test --lib --bins --features sdk,cli"
    ;;
  all)
    "$0" static
    "$0" unit
    "$0" integration
    ;;
  release)
    # The full release gate: everything, including the flaky-by-design
    # display-takeover suite that `all` deliberately omits.
    "$0" all
    "$0" takeover
    ;;
  *)
    echo "Usage: $0 [static|unit|integration|takeover|default|all|release]" >&2
    exit 2
    ;;
esac
