#!/usr/bin/env bash
# Run the test suite, starting and stopping the dev server as needed.
# All arguments are passed through to testit.py.
#
# Usage:
#   ./bin/run_tests                  # run all tests
#   ./bin/run_tests -t test_accounts # run a specific module
#   ./bin/run_tests -s               # stop on first failure

set -euo pipefail

# Ensure Homebrew and common tool paths are available
export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# Load .env from repo root if present (secrets like LLM_HANDLER_API_KEY)
if [ -f "$REPO_ROOT/.env" ]; then
    set -a
    source "$REPO_ROOT/.env"
    set +a
fi

# Start server if not already running; track so we know to stop it after
STARTED_SERVER=0
if ! "$SCRIPT_DIR/asgi_local" status | grep -q "Running"; then
    "$SCRIPT_DIR/asgi_local" start
    sleep 3
    STARTED_SERVER=1
fi

# Run tests — capture exit code so we always stop the server
EXIT_CODE=0
uv run "$SCRIPT_DIR/testit.py" "$@" || EXIT_CODE=$?

if [ "$STARTED_SERVER" -eq 1 ]; then
    "$SCRIPT_DIR/asgi_local" stop
fi

exit $EXIT_CODE
