#!/bin/bash
# CHECK-q62-citation -- the Q62 lane-census report names no path a reader cannot open.
#
# The 2026-11-05 census report cited `wizard-work/volt/q62_hostpath_census.py` as
# the instrument it replaced. No ref holds that file -- `git log --all
# --diff-filter=A` reads 0 commits and it is untracked here -- so the repo's
# path-claims leg read "1 missing path(s) the baseline does not name" and the
# gate was red at 15f70fe. The paragraph now calls it an uncommitted draft and
# gives the worktree copy's size and sha256 instead of a path a reader can follow.
#
# Legs
#   clean     path_claim_check --rev WORKTREE --fail-on-missing, rc 0
#   teeth     the same reading at 15f70fe, rc 1 naming exactly that one path
#   ref       the report's claim re-derived: 0 add-commits for that name
#   text      the report carries no slash-form token for that path
#   sha       the sha256 the report quotes is the bytes on disk
#   selftest  q62_lane_census.py --selftest still green after the docstring edit
#
# 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)"
REPORT="$HERE/2026-11-05-q62-lane-census/REPORT.md"
DRAFT="$HERE/q62_hostpath_census.py"
NAME="q62_hostpath_census.py"
PRE="15f70fe"
legs=0
faults=0
ungradable=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"; }
skip(){ legs=$((legs + 1)); ungradable=$((ungradable + 1)); printf 'skip  %-9s %s\n' "$1" "$2"; }

[ -f "$REPORT" ] || { echo "cannot grade -- no report: $REPORT"; exit 2; }
[ -f "$REPO/tools/path_claim_check.py" ] || { echo "cannot grade -- no path_claim_check.py"; exit 2; }
command -v python3 >/dev/null || { echo "cannot grade -- no python3"; exit 2; }

# clean -- the tree about to be committed
out="$(python3 "$REPO/tools/path_claim_check.py" --repo "$REPO" --rev WORKTREE --fail-on-missing 2>&1)"; rc=$?
line="$(printf '%s' "$out" | tail -n 1)"
if [ "$rc" -eq 0 ] && printf '%s' "$line" | grep -q '0 new against the baseline'; then
  ok clean "$line"
else
  bad clean "rc $rc: $line"
fi

# teeth -- the same tool still reads red on the commit that carried the claim
if git -C "$REPO" cat-file -e "$PRE^{commit}" 2>/dev/null; then
  out="$(python3 "$REPO/tools/path_claim_check.py" --repo "$REPO" --rev "$PRE" --fail-on-missing --only missing 2>&1)"; rc=$?
  if [ "$rc" -eq 1 ] && printf '%s' "$out" | grep -qF "wizard-work/volt/$NAME" \
     && printf '%s' "$out" | grep -q '1 missing path(s) the baseline does not name'; then
    ok teeth "$PRE reads rc 1 naming the path the claim made"
  else
    bad teeth "rc $rc, want 1 naming wizard-work/volt/$NAME; last: $(printf '%s' "$out" | tail -n 1)"
  fi
else
  skip teeth "$PRE not in this clone"
fi

# ref -- the report's own "no ref holds it" assertion, re-derived
adds="$(git -C "$REPO" log --all --diff-filter=A --format=%H -- "*$NAME" | wc -l)"
tracked="$(git -C "$REPO" ls-files -- "*$NAME" | wc -l)"
if [ "$adds" -eq 0 ] && [ "$tracked" -eq 0 ]; then
  ok ref "0 add-commits on any ref, 0 tracked paths: the draft is uncommitted"
else
  bad ref "adds=$adds tracked=$tracked, want 0/0 -- the report's claim is now false"
fi

# text -- no slash-form token in the report (the scanner grades paths with a '/')
if grep -qE "(^|[^A-Za-z0-9_.-])wizard-work/volt/$NAME" "$REPORT"; then
  bad text "the report still cites wizard-work/volt/$NAME as a repo path"
else
  ok text "no slash-form token for $NAME in the report"
fi

# sha -- the bytes the report quotes
if [ -f "$DRAFT" ]; then
  want="$(grep -o 'sha256 `[0-9a-f]\{64\}`' "$REPORT" | head -n 1 | tr -d '`' | sed 's/^sha256 //')"
  got="$(sha256sum "$DRAFT" | cut -d' ' -f1)"
  if [ -n "$want" ] && [ "$want" = "$got" ]; then
    ok sha "REPORT quotes $got, the worktree copy hashes to it"
  else
    bad sha "REPORT quotes '${want:-none}', disk hashes $got"
  fi
else
  skip sha "no worktree copy at $DRAFT"
fi

# selftest -- the instrument the report describes still passes its own cells
out="$(python3 "$HERE/q62_lane_census.py" --selftest 2>&1)"; rc=$?
line="$(printf '%s' "$out" | tail -n 1)"
if [ "$rc" -eq 0 ] && printf '%s' "$line" | grep -qE 'selftest: ([0-9]+)/\1 cells'; then
  ok selftest "$line"
else
  bad selftest "rc $rc: $line"
fi

printf -- '--\n%d leg(s), %d fault(s), %d ungradable\n' "$legs" "$faults" "$ungradable"
if [ "$faults" -gt 0 ]; then exit 1; fi
if [ "$ungradable" -gt 0 ]; then exit 2; fi
exit 0
