#!/bin/sh
set -eu
BUNDLE=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
REPO=$(CDPATH= cd -- "${1:?usage: sh reproduce.sh ENGINE_CHECKOUT OUTPUT_DIRECTORY}" && pwd)
mkdir -p "${2:?output directory required}"
OUTPUT=$(CDPATH= cd -- "$2" && pwd)
RUNTIME=${RUNTIME_IMAGE:-fluxmeter-api:latest}
REDIS_IMAGE=${REDIS_IMAGE:-redis:7-alpine}
DEPS=${EXPERIMENT_DEPS:-$BUNDLE/deps}
if [ ! -d "$DEPS/pytest" ]; then echo 'Prepare test dependencies as described in README.txt' >&2; exit 1; fi
NAME=fluxmeter-exp001-public-$$
docker run -d --rm --network none --name "$NAME" "$REDIS_IMAGE" redis-server --save '' --appendonly no
trap 'docker stop "$NAME" >/dev/null' EXIT HUP INT TERM
# Only this disposable Redis is flushed by the harness. It has no host ports or volumes.
docker run --rm --network "container:$NAME" --entrypoint python \
  -v "$REPO:/repo:ro" -v "$BUNDLE/test_unknown.py:/tests/test_unknown.py:ro" \
  -v "$DEPS:/deps:ro" -v "$OUTPUT:/evidence" -w /repo \
  -e PYTHONDONTWRITEBYTECODE=1 -e PYTHONPATH=/repo/api:/deps \
  -e PRICING_FILE=/repo/config/pricing.json -e FLUXMETER_AUTH_OPTIONAL=true \
  -e GATEWAY_OUTBOX_WORKER=false -e WATERMARK_HEARTBEAT_ENABLED=false \
  "$RUNTIME" -m pytest /tests/test_unknown.py -vv -s -p no:cacheprovider \
  --junitxml=/evidence/experiment.xml > "$OUTPUT/experiment.log" 2>&1
docker run --rm --network none --entrypoint python \
  -v "$REPO:/repo:ro" -v "$DEPS:/deps:ro" -v "$OUTPUT:/evidence" -w /repo \
  -e PYTHONDONTWRITEBYTECODE=1 -e PYTHONPATH=/repo/api:/deps \
  -e PRICING_FILE=/repo/config/pricing.json -e FLUXMETER_AUTH_OPTIONAL=true \
  -e GATEWAY_OUTBOX_WORKER=false -e WATERMARK_HEARTBEAT_ENABLED=false \
  "$RUNTIME" -m pytest tests/test_gateway.py tests/test_reservation_expiry.py \
  tests/test_reservation_contract.py tests/test_deep_modules.py tests/test_custody_unit.py \
  tests/test_ingestion_contract.py tests/test_proof_demo.py -q -p no:cacheprovider \
  --junitxml=/evidence/regression.xml > "$OUTPUT/regression.log" 2>&1
tail -1 "$OUTPUT/experiment.log"
tail -1 "$OUTPUT/regression.log"
