#!/bin/bash
# CHECK-q146 -- the two entry points Q146 repaired, graded together.
#
#   roster-selftest  the live-wizard-roster grader graded: 10 cells on fixture
#                    copies -- one green control and nine mutants it must name
#                    (a moved Model cell, a moved Lane cell, a moved commit
#                    cell, a dropped row, an absent rev reading rc 2, a moved
#                    blob pin, and the detail formatter, which used to raise
#                    TypeError on the first mismatch instead of printing it).
#   roster-artifact  the committed roster TSV replayed at the rev that added it
#                    (e6ce783), 5 legs: roster, checkins, header, row count,
#                    pinned blob, and the 50 cells field-for-field.
#   todo-entry       the todo-census entry point, which landed as five lines of
#                    prose and died on line 3 with a bash syntax error. A
#                    network failure is SKIP, not a fault: that verifier
#                    re-fetches a live API by design, so "could not grade" must
#                    not be reported as "the artifact is wrong".
#   baseline-rows    both paths are named in the q143 sweep's baseline with code
#                    0, so the set-level gate agrees with this one.
#
# Paths are named from $HERE and their absence is rc 2 (Q144's lesson: a
# repo-root wrapper that derives an artist path from its own directory reads
# UNGRAD on every clone).
#
# Exit codes: 0 every leg green, 1 a leg faulted, 2 a leg could not grade.
set -u

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROSTER="$HERE/wizard-work/ledger/CHECK-live-wizard-roster-2026-10-29"
TODO="$HERE/wizard-work/ledger/2026-11-01-todo-census/CHECK"
BASE="$HERE/wizard-work/volt/q143-check-sweep/check_entry_baseline.tsv"
PY="${PYTHON:-python3}"

for f in "$ROSTER" "$TODO" "$BASE"; do
    if [ ! -f "$f" ]; then
        printf 'CANNOT GRADE: missing %s\n' "$f"
        exit 2
    fi
done

legs=0
faults=0
rc=0

run_leg() {
    name="$1"
    shift
    want="$1"
    shift
    legs=$((legs + 1))
    out="$("$@" 2>&1)"
    lrc=$?
    if [ "$lrc" -eq "$want" ]; then
        printf '  %-16s PASS   rc=%d  %s\n' "$name" "$lrc" "$(printf '%s' "$out" | tail -1)"
    elif [ "$lrc" -eq 2 ] && printf '%s' "$out" | grep -q 'CANNOT GRADE'; then
        printf '  %-16s SKIP   rc=2  %s\n' "$name" "$(printf '%s' "$out" | tail -1)"
    else
        faults=$((faults + 1))
        printf '  %-16s FAULT  rc=%d  %s\n' "$name" "$lrc" "$(printf '%s' "$out" | tail -1)"
        rc=1
    fi
}

run_leg roster-selftest 0 bash "$ROSTER" --selftest
run_leg roster-artifact 0 bash "$ROSTER"
run_leg todo-entry      0 bash "$TODO"
run_leg baseline-rows   0 "$PY" - "$BASE" <<'PY'
import sys

need = {
    "wizard-work/ledger/CHECK-live-wizard-roster-2026-10-29": 0,
    "wizard-work/ledger/2026-11-01-todo-census/CHECK": 0,
}
rows = {}
for line in open(sys.argv[1], encoding="utf-8"):
    if line.startswith("#") or not line.strip():
        continue
    parts = line.rstrip("\n").split("\t")
    if len(parts) >= 2 and parts[1].strip().isdigit():
        rows[parts[0]] = int(parts[1])
bad = ["%s=%s want=%d" % (p, rows.get(p, "absent"), c)
       for p, c in need.items() if rows.get(p) != c]
print("baseline rows: %d graded, %d agreed" % (len(need), len(need) - len(bad)))
if bad:
    print("FAIL " + "; ".join(bad))
    sys.exit(1)
sys.exit(0)
PY

if [ "$rc" -eq 0 ]; then
    echo "q146: $legs leg(s), no fault"
else
    echo "q146: $legs leg(s), $faults fault(s)"
fi
exit "$rc"
