#!/usr/bin/env bash
# CHECK-q62-lane-census -- Q62 leg (a): the lane census still answers its
# question, and the three quill verifiers it repaired are honest.
#
# Legs
#   selftest    9 fixture cells on a throwaway git repo: docstring, plain
#               bind, stated-next-line, fixture code string, live read,
#               graceful skip, write-out, stated-above, env write
#   lanes       the census over this tree: quill / beam / null read 0
#               live-unstated, the total is still > 0 (Q62 is open, so a
#               census that reads 0 everywhere would be lying), and the
#               census exits 1 on that remainder
#   runs        the three repaired quill verifiers exit 0 from the repo
#   hostfree    verify_placeholder_ids.py still exits 0 with
#               QUILL_HOST_ROOT pointed at a path that does not exist --
#               the host tree is an optional extra root, not a requirement
#   teeth       in a temp dir holding only verifier + subject, each pair is
#               clean rc 0 and mutated rc 1 (a green it cannot lose is not a
#               measurement)
#   text        exactly one absolute host literal survives in the three
#               files, and it is the stated, env-overridable default
#
# rc 0 every leg as expected | 1 a graded fault | 2 could not grade.
# Run from anywhere; the repo is found from this file's place.
set -u

HERE="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
REPO="$(cd "$HERE/../.." && pwd)"
TOOL="$HERE/q62_lane_census.py"
QUILL="$REPO/wizard-work/quill"
legs=0
faults=0

leg() { legs=$((legs + 1)); printf '  %-10s %s\n' "$1" "$2"; }
pass() { leg "$1" "PASS -- $2"; }
fail() { leg "$1" "FAULT -- $2"; faults=$((faults + 1)); }

cd "$REPO" || { echo "could not grade: no repo at $REPO"; exit 2; }

# 1 -- selftest
out=$(python3 "$TOOL" --selftest 2>&1); rc=$?
if [ "$rc" -eq 0 ] && printf '%s' "$out" | grep -q '9/9 cells'; then
  pass selftest "9/9 fixture cells"
else
  fail selftest "rc=$rc :: $(printf '%s' "$out" | tail -1)"
fi

# 2 -- lanes
out=$(python3 "$TOOL" --repo . 2>&1); rc=$?
lu() { printf '%s' "$out" | awk -v lane="$1" '$1==lane{for(i=1;i<=NF;i++){if($i=="live-unstated="){print $(i+1); exit} else if($i ~ /^live-unstated=/){sub(/^live-unstated=/,"",$i); print $i; exit}}}'; }
q=$(lu quill); b=$(lu beam); n=$(lu null); t=$(lu TOTAL)
if [ "${q:-x}" = "0" ] && [ "${b:-x}" = "0" ] && [ "${n:-x}" = "0" ] && [ "${t:-0}" -gt 0 ] && [ "$rc" -eq 1 ]; then
  pass lanes "quill=0 beam=0 null=0 total=$t (Q62 still open), census rc=1"
else
  fail lanes "quill=${q:-?} beam=${b:-?} null=${n:-?} total=${t:-?} census_rc=$rc"
fi

# 3 -- the three repaired files run
bad=""
for f in test_md_heading_normalize.py verify-runbook.py verify_placeholder_ids.py; do
  python3 "$QUILL/$f" >/dev/null 2>&1 || bad="$bad $f"
done
if [ -z "$bad" ]; then
  pass runs "3/3 quill verifiers exit 0"
else
  fail runs "non-zero:$bad"
fi

# 4 -- host tree not required
if QUILL_HOST_ROOT=/nonexistent/none python3 "$QUILL/verify_placeholder_ids.py" >/dev/null 2>&1; then
  pass hostfree "rc 0 with the host quill tree absent"
else
  fail hostfree "still depends on /root/.hermes/wizard-outputs/quill"
fi

# 5 -- teeth
out=$(python3 "$HERE/q62_portability_probe.py" 2>&1); rc=$?
if [ "$rc" -eq 0 ]; then
  pass teeth "$(printf '%s' "$out" | tail -1)"
else
  fail teeth "rc=$rc :: $(printf '%s' "$out" | tail -2 | tr '\n' ' ')"
fi

# 6 -- exactly one host literal in CODE, and it is the stated default.
# Comments are excluded here: the `# subject:` line that justifies the pin is
# prose, and counting it would make the leg demand two defects or one.
cnt=$(grep -h -n '/root/\.hermes/wizard-outputs' "$QUILL/test_md_heading_normalize.py" \
        "$QUILL/verify-runbook.py" "$QUILL/verify_placeholder_ids.py" \
        | grep -v -E ':[[:space:]]*#' | wc -l | tr -d ' ')
if [ "$cnt" = "1" ]; then
  pass text "1 host literal in code (the env-overridable, stated pin)"
else
  fail text "$cnt host literal(s) in code across the three repaired files, expected 1"
fi

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