#!/bin/sh
# Provision the hegel property-test server with a working Hypothesis version.
#
# hegeltest installs hegel-core into .hegel/venv without constraining Hypothesis, so it
# resolves to the latest release. Recent Hypothesis renamed
# ConjectureRunner.interesting_examples to interesting_test_cases; hegel-core 0.2.3 still
# reads the old name and raises AttributeError *after* running each test. The reply never
# arrives, so the Rust side blocks forever and `cargo test` hangs at zero CPU with no output.
# Hypothesis older than 6.150 fails the other way: Verbosity('normal') raises ValueError.
#
# hegeltest re-runs `uv venv --clear` — wiping any pin — unless .hegel/venv/hegel-version
# matches the version it expects. Writing that marker is what makes the pin stick.
set -eu

cd "$(dirname "$0")/.."

# Must equal HEGEL_SERVER_VERSION in the hegeltest crate, or it will wipe this venv.
HEGEL_SERVER_VERSION='0.2.3'
PIN_HYPOTHESIS='hypothesis==6.160.0'

rm -rf .hegel/venv

if command -v uv >/dev/null 2>&1; then
    uv venv --python 3.11 .hegel/venv
    uv pip install --python .hegel/venv/bin/python \
        "hegel-core==${HEGEL_SERVER_VERSION}" "$PIN_HYPOTHESIS"
else
    python3 -m venv .hegel/venv
    .hegel/venv/bin/pip install --quiet --upgrade pip
    .hegel/venv/bin/pip install --quiet \
        "hegel-core==${HEGEL_SERVER_VERSION}" "$PIN_HYPOTHESIS"
fi

printf '%s' "$HEGEL_SERVER_VERSION" > .hegel/venv/hegel-version

printf 'hegel test server ready: %s (%s)\n' \
    "$(.hegel/venv/bin/hegel --version)" "$PIN_HYPOTHESIS"
