#!/usr/bin/env bash
# CHECK-q151 -- the two later check-in censuses, graded at the revisions they
# describe, plus the mutant that proves the green is not the only thing they
# can print.
#
# WHAT WAS WRONG (measured at d42f577, before this tick's edits)
#   Both gates were Python files under a `bash CHECK` line, so the documented
#   invocation exited 2 without grading anything (`line 15: import: command
#   not found`, `line 10: ROOT: command not found`). Run with python3 they were
#   red on every clone: their scope was the LIVE directory or index, so each new
#   check-in moved the numbers they compare against, and 2026-10-28's table
#   carried a 101st row for `checkins/2026-10-27-sentinel.md`, a file no
#   revision holds (`git log --all -- checkins/2026-10-27-sentinel.md` -> 0
#   commits; it is untracked in the work tree). Both graders also hardcoded
#   /root/troupe-standup.
#
# WHAT THIS GRADES
#   1. the 2026-10-28 census against `ab7767d:checkins`, cell for cell across
#      the TSV, the MD (inventory, headline, per-signer) and the JSON;
#   2. the 2026-10-29 census against `b21bb10:checkins`, cell for cell across
#      the CSV, the MD table and the JSON;
#   3. a mutant of the 10-29 table in a COPY: one signee cell moved must exit 1,
#      so a green here is a measurement rather than a print.
#
#   rc 0 every leg holds   rc 1 a leg failed   rc 2 could not grade
set -u
ROOT="$(git -C "$(dirname "$0")" rev-parse --show-toplevel 2>/dev/null)" || exit 2
[ -n "$ROOT" ] || exit 2
cd "$ROOT" || exit 2

D28=wizard-work/ledger/2026-10-28-checkin-census
D29=wizard-work/ledger/2026-10-29-checkin-census

legs=0
faults=0
leg() { # leg <name> <rc> <detail>
  legs=$((legs + 1))
  if [ "$2" -eq 0 ]; then printf 'PASS %-22s %s\n' "$1" "$3"
  else printf 'FAIL %-22s %s\n' "$1" "$3"; faults=$((faults + 1)); fi
}

out="$(bash "$D28/CHECK" 2>&1)"; rc=$?
leg census-2026-10-28 "$rc" "$(printf '%s' "$out" | grep -m1 -E '^(VERDICT|FAIL|CANNOT)')"

out="$(bash "$D29/CHECK" 2>&1)"; rc=$?
leg census-2026-10-29 "$rc" "$(printf '%s' "$out" | grep -m1 -E '^(VERDICT|FAIL|CANNOT)')"

tmp="$(mktemp -d "${TMPDIR:-/tmp}/q151-mut-XXXX")" || exit 2
trap 'rm -rf "$tmp"' EXIT
cp -a "$D29" "$tmp/census" || { echo "CANNOT GRADE: cannot copy $D29"; exit 2; }
sed -i 's/^checkins\/2026-09-24-zephyrus-02.md,2026-09-24,zephyrus,/checkins\/2026-09-24-zephyrus-02.md,2026-09-24,zephyrus-02,/' \
  "$tmp/census/checkin-census.csv"
mout="$(python3 "$tmp/census/grade.py" --repo "$ROOT" 2>&1)"; mrc=$?
if [ "$mrc" -eq 1 ] && printf '%s' "$mout" | grep -q '^FAIL: CSV row'; then
  leg mutant 0 "a moved signee cell reads rc 1: $(printf '%s' "$mout" | grep -m1 '^FAIL:')"
else
  leg mutant 1 "mutant did not red the table (rc=$mrc)"
fi

if [ "$faults" -eq 0 ]; then
  echo "q151: $legs leg(s), all passed"
  exit 0
fi
echo "q151: $legs leg(s), $faults fault(s)"
exit 1
