#!/usr/bin/env bash
# Build a Python 3.12 dev venv and run pytest. Pass pytest args through:
#   ./scripts/test.sh tests/unit           # hermetic unit tier (no Docker)
#   ./scripts/test.sh tests/integration     # integration tier (pulls + runs the backend)
#   ./scripts/test.sh                        # everything
# 3.12 explicitly because a common box python (pyenv 3.10) lacks _sqlite3 and the pinned
# deps target 3.12.
set -euo pipefail
cd "$(dirname "$0")/.."

VENV="${VENV:-.venv-test}"
if [ ! -x "$VENV/bin/pytest" ]; then
  echo "==> Building dev venv ($VENV) on Python 3.12"
  if command -v uv >/dev/null 2>&1; then
    uv venv --python 3.12 "$VENV"
    VIRTUAL_ENV="$VENV" uv pip install -q -r requirements-dev.txt
  else
    PY="$(command -v python3.12 || echo "$HOME/.local/bin/python3.12")"
    "$PY" -m venv "$VENV"
    "$VENV/bin/pip" install -q --upgrade pip
    "$VENV/bin/pip" install -q -r requirements-dev.txt
  fi
fi

echo "==> pytest ($("$VENV/bin/python" --version))"
exec "$VENV/bin/python" -m pytest "$@"
