#!/bin/bash
# CHECK-q171 -- the path+str guard still answers its question, and still refuses.
#
# Legs
#   selftest   every fixture cell green (16 today), and the count pinned
#   worktree   every tracked *.py on disk joins with `/` or str(), rc 0
#   head       the same reading at HEAD, rc 0
#   history    --rev bda1513 (the commit this item was opened against) reads
#              rc 1 with 7 faults over 4 files -- a RED CONTROL ON REAL HISTORY,
#              so a guard that had quietly stopped seeing anything would fail
#              here instead of reading green
#   red        a synthetic tree carrying both operand orders reads rc 1, names
#              both files, and leaves the clean one silent
#   runs       the one repaired verifier that needs no network still executes:
#              rc 0. The other three were measured by hand at the landing
#              (gdp: "12/12 economies match the live API"; albums: "100 rows
#              match the fresh extraction"; forge dry: "ALL GREEN") and are
#              pinned by `history` above rather than re-run here, because two
#              of them need the network and the third takes minutes -- a CHECK
#              that reds when the weather changes grades the weather.
#   wiring     tools/verify.py names the leg and runs it at WORKTREE
#
# rc 0 every leg as expected | 1 a graded fault | 2 could not grade.
# Run from anywhere; paths resolve from $0, the repo from this file's place.
set -u

HERE="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
REPO="$(cd "$HERE/../.." && pwd)"
TOOL="$HERE/q171_path_str_add_check.py"
CELLS="16 cells, 16 passed, 0 failed"
RED_REV="bda1513"
RED_FAULTS=7
RED_FILES="wizard-work/ledger/verify_2026_11_02_openmeteo_summary.py wizard-work/ledger/verify_albums_photo_counts.py wizard-work/ledger/verify_gdp_per_capita.py wizard-work/forge/verify_served_drift_summarise_dry.py"
legs=0
faults=0

ok()  { legs=$((legs + 1)); printf 'ok    %-9s %s\n' "$1" "$2"; }
bad() { legs=$((legs + 1)); faults=$((faults + 1)); printf 'FAULT %-9s %s\n' "$1" "$2"; }

[ -f "$TOOL" ] || { echo "cannot grade -- no tool: $TOOL"; exit 2; }
command -v python3 >/dev/null || { echo "cannot grade -- no python3"; exit 2; }
git -C "$REPO" rev-parse --git-dir >/dev/null 2>&1 \
  || { echo "cannot grade -- not a git repository: $REPO"; exit 2; }

# selftest -- the fixture corpus, and the pin on its size
out="$(python3 "$TOOL" --selftest 2>&1)"; rc=$?
if [ $rc -eq 0 ] && printf '%s' "$out" | grep -qF "$CELLS"; then
  ok selftest "$CELLS, rc 0"
else
  bad selftest "rc $rc, want 0 and '$CELLS'; last: $(printf '%s' "$out" | tail -n 1)"
fi

# worktree -- the tree about to be committed
out="$(python3 "$TOOL" --repo "$REPO" 2>&1)"; rc=$?
line="$(printf '%s' "$out" | tail -n 1)"
if [ $rc -eq 0 ] && printf '%s' "$line" | grep -q ', 0 fault(s)$'; then
  ok worktree "$line"
else
  bad worktree "rc $rc: $line"
fi

# head -- the commit
out="$(python3 "$TOOL" --repo "$REPO" --rev HEAD 2>&1)"; rc=$?
line="$(printf '%s' "$out" | tail -n 1)"
if [ $rc -eq 0 ] && printf '%s' "$line" | grep -q ', 0 fault(s)$'; then
  ok head "$line"
else
  bad head "rc $rc: $line"
fi

# history -- RED ON REAL HISTORY: the commit before the repair must still read red
out="$(python3 "$TOOL" --repo "$REPO" --rev "$RED_REV" 2>&1)"; rc=$?
n="$(printf '%s\n' "$out" | grep -c '^FAULT ')"
missing=""
for f in $RED_FILES; do
  printf '%s' "$out" | grep -q "FAULT $f:" || missing="$missing $f"
done
if [ $rc -eq 1 ] && [ "$n" -eq "$RED_FAULTS" ] && [ -z "$missing" ]; then
  ok history "rc 1, $n fault(s) at $RED_REV, all 4 files named"
else
  bad history "rc $rc, $n fault(s) at $RED_REV, wanted rc 1 / $RED_FAULTS; unnamed:$missing"
fi

# red -- the guard can still print red on a tree it has never seen
red="$(mktemp -d "${TMPDIR:-/tmp}/q171-red.XXXXXX")" || { echo "cannot grade -- no tmp"; exit 2; }
mkdir -p "$red/wizard-work/ledger"
printf 'from pathlib import Path\n\nHERE = Path(__file__).resolve().parents[2]\nSRC = HERE + "/a.json"\n' \
  > "$red/wizard-work/ledger/left.py"
printf 'from pathlib import Path\nREPO = Path(__file__).resolve()\nBASE = "file://" + REPO\n' \
  > "$red/wizard-work/ledger/right.py"
printf 'from pathlib import Path\nHERE = Path(__file__).resolve()\nSRC = HERE / "a.json"\n' \
  > "$red/wizard-work/ledger/clean.py"
git -C "$red" init -q . 2>/dev/null
git -C "$red" add -A 2>/dev/null
out="$(python3 "$TOOL" --repo "$red" 2>&1)"; rc=$?
n="$(printf '%s\n' "$out" | grep -c '^FAULT ')"
if [ $rc -eq 1 ] && [ "$n" -eq 2 ] \
   && printf '%s' "$out" | grep -q 'left.py:4' \
   && printf '%s' "$out" | grep -q 'right.py:3' \
   && ! printf '%s' "$out" | grep -q 'clean.py'; then
  ok red "rc 1, $n fault(s), both operand orders named, clean.py silent"
else
  bad red "rc $rc, $n fault(s), want 1/2 with left.py:4 and right.py:3 and clean.py silent"
fi

# runs -- the repaired verifier that needs no network executes
lock="$(mktemp -d "${TMPDIR:-/tmp}/q171-run.XXXXXX")" || { echo "cannot grade -- no tmp"; exit 2; }
out="$(cd "$lock" && python3 "$REPO/wizard-work/ledger/verify_2026_11_02_openmeteo_summary.py" 2>&1)"; rc=$?
if [ $rc -eq 0 ] && printf '%s' "$out" | grep -q '^PASS: 6 rows match'; then
  ok runs "$(printf '%s' "$out" | tail -n 1)"
else
  bad runs "rc $rc, want 0 and 'PASS: 6 rows match'; last: $(printf '%s' "$out" | tail -n 1)"
fi

# wiring -- the gate names the leg and runs it at the worktree
if grep -q '"path-str-add"' "$REPO/tools/verify.py" \
   && grep -q 'q171_path_str_add_check.py' "$REPO/tools/verify.py" \
   && grep -q '"--rev", "WORKTREE", "--quiet"' "$REPO/tools/verify.py"; then
  ok wiring "tools/verify.py: leg path-str-add -> q171 at WORKTREE"
else
  bad wiring "tools/verify.py no longer names the path-str-add leg at WORKTREE"
fi

echo "CHECK-q171: $legs leg(s), $faults fault(s)"
[ $faults -eq 0 ] || exit 1
exit 0
