#!/bin/bash

set -euo pipefail

mkdir -p build

# Compute default workers, clamped to [1, 2]
DEFAULT_WORKERS="$(uv run --no-sync -- python - <<'PY'
import os
cpus = os.cpu_count() or 2
print(int(max(1, min(cpus * 0.5, 4))))
PY
)"

# Allow override via env var, otherwise use the default
PYTEST_N="${PYTEST_N:-$DEFAULT_WORKERS}"

echo 'Running unit and functional tests...'

COVERAGE_FILE=build/.coverage \
PYTHONDONTWRITEBYTECODE=1 \
uv run --group dev --frozen -- pytest \
    --cov abacus_state \
    --cov-report term \
    --cov-report xml:build/coverage.xml \
    --dist=worksteal \
    --ignore=tests/integration \
    --junitxml=build/pyunit.xml \
    -n "$PYTEST_N" \
    -p no:cacheprovider \
    -v \
    tests/

echo 'Done!'
