#!/usr/bin/env bash
set -u
GW=$1; OUT=$GW/.spt-repin3
mkdir "$OUT" 2>/dev/null || { echo "REFUSE: exists" >&2; exit 4; }
cd "$GW" || exit 9
unset OWL_SESSION_ID SPT_AGENT_ID SPT_ENDPOINT_ID SPT_HOME
cargo nextest run -p spt -E 'binary(io_events_undriven_kinds_e2e)' --no-fail-fast > "$OUT/cell4.raw" 2>&1
echo $? > "$OUT/cell4.exit"
{
  echo "head: $(git rev-parse HEAD)"
  echo "Summary=$(grep -c 'Summary' "$OUT/cell4.raw")"
  run=$(grep -oE '[0-9]+ tests? run' "$OUT/cell4.raw" | grep -oE '^[0-9]+'); echo "run-count=${run:-0}"
  echo "fixture-gap-hits=$(grep -c 'required test fixture' "$OUT/cell4.raw")"
  # NEEDLE WIDENED: doyle+deployah both counted 3 reds against a Summary of 4
  # because quickstart prints "FAIL + LEAK [". Match the verdict word, not the
  # bracket that follows it.
  echo "fail-lines(narrow 'FAIL [')=$(grep -c 'FAIL \[' "$OUT/cell4.raw")"
  echo "fail-lines(wide '^ *FAIL')=$(grep -cE '^ *FAIL' "$OUT/cell4.raw")"
  echo "doubled-type(escaped form)=$(grep -c 'type=\\\"msg\\\" type=\\\"msg\\\"' "$OUT/cell4.raw")"
} > "$OUT/census.txt"
