#!/usr/bin/env bash
# CHECK-q157 -- the 2026-11-02 untracked-file triage artifact grades its own
# pinned universe, and can still go red.
#
# Regression guard for the Q157 repair. Six legs; exit 0 iff all pass, 1 if any
# leg faults, 2 if the leg could not run at all.
#
# Measured at the commit that lands Q157. The point of legs 3 and 4 is the pair:
# the SAME checker must read 0 when the live untracked set moves (the defect
# this item repaired) and 1 when the pinned artifact itself is wrong (so the
# repair did not simply turn the verdict off).
set -u

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || exit 2
cd "$REPO_ROOT" || exit 2

ENTRY="CHECK-2026-11-02-untracked-classify"
VERIFY="wizard-work/sol/verify_untracked_classify_2026_11_02.py"
MAN="wizard-work/sol/untracked-classify-2026-11-02.tsv"
SCRATCH="$(mktemp -d "${TMPDIR:-/tmp}/check-q157.XXXXXX")" || exit 2
PROBE="zz-q157-drift-probe.txt"
LEGS=0
FAULTS=0

cleanup() { rm -f "$PROBE"; rm -rf "$SCRATCH"; }
trap cleanup EXIT

leg() {  # leg <name> <expected-rc> <actual-rc> [note]
    LEGS=$((LEGS + 1))
    if [ "$2" = "$3" ]; then
        printf '  %-16s PASS  rc=%s %s\n' "$1" "$3" "${4:-}"
    else
        printf '  %-16s FAIL  expected rc=%s got rc=%s %s\n' "$1" "$2" "$3" "${4:-}"
        FAULTS=$((FAULTS + 1))
    fi
}

for f in "$ENTRY" "$VERIFY" "$MAN"; do
    [ -e "$f" ] || { echo "CANNOT GRADE: missing $f"; exit 2; }
done

# 1. the selftest: 9 cells, 0 failed -- the proof the verdict is falsifiable.
out="$(python3 "$VERIFY" --selftest 2>&1)"; rc=$?
echo "$out" | tail -1
leg selftest 0 "$rc" "$(echo "$out" | grep -c '^FAIL' ) failed cell(s)"

# 2. the live grade at the pinned universe.
out="$(bash "$ENTRY" 2>&1)"; rc=$?
echo "$out" | grep -E '^(CHECK OK|VERDICT)' | head -1
leg live-grade 0 "$rc"

# 3. drift tolerance: a new untracked file must NOT redden the census, and the
#    drift must be reported rather than silently swallowed.
echo "q157 drift probe" > "$PROBE"
out="$(bash "$ENTRY" 2>&1)"; rc=$?
echo "$PROBE" | grep -q . && echo "$out" | grep -q 'ADVISORY' && adv=1 || adv=0
rm -f "$PROBE"
leg drift-tolerant 0 "$rc" "advisory_printed=$adv"
[ "$adv" = 1 ] || { echo "  drift-tolerant     FAIL  no ADVISORY line"; FAULTS=$((FAULTS + 1)); }

# 4. falsifiable from outside: a corrupted category in a manifest copy is rc 1.
cp "$MAN" "$SCRATCH/bad.tsv"
python3 - "$SCRATCH/bad.tsv" <<'PY'
import sys
p = sys.argv[1]
lines = open(p).read().splitlines()
head, rows = lines[0], lines[1:]
path, cat = rows[5].split("\t")
rows[5] = "%s\t%s" % (path, "data" if cat != "data" else "other")
open(p, "w").write("\n".join([head] + rows) + "\n")
PY
python3 "$VERIFY" --manifest "$SCRATCH/bad.tsv" --quiet-drift >"$SCRATCH/bad.out" 2>&1; rc=$?
head -1 "$SCRATCH/bad.out"
leg tampered-manifest 1 "$rc"

# 5. a missing artifact is a refusal (rc 2), not a silent pass.
python3 "$VERIFY" --manifest "$SCRATCH/absent.tsv" --quiet-drift >"$SCRATCH/absent.out" 2>&1; rc=$?
leg missing-artifact 2 "$rc" "$(head -1 "$SCRATCH/absent.out")"

# 6. the verifier names no absolute host path (it derived REPO from __file__).
n="$(grep -c '/root/' "$VERIFY")"
leg no-host-path 0 "$n" "hits=$n"

if [ "$FAULTS" -eq 0 ]; then
    echo "q157: $LEGS leg(s), all passed"
    exit 0
fi
echo "q157: $LEGS leg(s), $FAULTS fault(s)"
exit 1
