#!/usr/bin/env bash
# CHECK-q84 -- the interval-overlap cells still pin the three things Q84's
# done-means names, and the cells still bite.
#
# Q84 was opened on `wizard-work/beam/interval_overlap.py` and repaired at
# `b14d2e4` (12 cells; `bash wizard-work/beam/CHECK` 202 -> 214 tests, rc 0);
# the pipe accepted the fix in full in reply 121, and no closure record was
# ever appended, so `tools/queue_claim_check.py` kept listing Q84 OPEN. This
# guard is what the closure record cites.
#
# Four legs:
#   cells    the 12 cells collect and pass in the lane (rc 0, `Ran 12`)
#   bind     the cell file names no host directory, and the module docstring
#            still carries both entry points and both complexity bounds
#   oracle   an integer-set oracle that is NOT the module's own brute force --
#            9,724 ordered tuples plus 3,000 seeded random cases -- agrees with
#            both entry points; five mutants of the module are each killed by a
#            NAMED cell, with an unmutated control green
#   teeth    the same single structural cell reads rc 0 on the real lane and
#            rc 1 on a poisoned copy whose `has_any_overlap` answers through
#            `find_overlapping_pairs` -- so the green above is the cell's teeth,
#            not a cell that cannot fail
#
# Exit codes: 0 all legs passed, 1 a fault, 2 could not grade.
set -u
set -o pipefail

cd "$(dirname "${BASH_SOURCE[0]}")" || exit 2
ROOT="$(pwd)"
LANE="$ROOT/wizard-work/beam"
MOD="$LANE/interval_overlap.py"
CELLS="$LANE/test_interval_overlap.py"
VERIFY="$ROOT/wizard-work/volt/q84-closure/q84_verify.py"
PY="${PYTHON:-python3}"
CELL="test_interval_overlap.IntervalOverlapTest.test_has_any_overlap_does_not_build_pair_list"

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

WORK="$(mktemp -d "${TMPDIR:-/tmp}/check-q84.XXXXXX")" || exit 2
trap 'rm -rf "$WORK"' EXIT

passed=0
failed=0
row() { printf '  %-7s %s\n' "$1" "$2"; }
verdict() { if [ "$2" -eq 0 ]; then passed=$((passed + 1)); else failed=$((failed + 1)); fi; row "$1" "$3"; }

# --- leg 1: the cells collect and pass --------------------------------------
out="$(cd "$LANE" && "$PY" -m unittest test_interval_overlap 2>&1)"; crc=$?
ran="$(printf '%s' "$out" | sed -n 's/^Ran \([0-9]*\) test.*/\1/p' | tail -1)"
verdict cells \
    "$([ "$crc" -eq 0 ] && [ "${ran:-0}" -eq 12 ] && echo 0 || echo 1)" \
    "rc=$crc  Ran ${ran:-0} (want 12)  $(printf '%s' "$out" | tail -1)"

# --- leg 2: no host binding, and the docstring still states the claims ------
# Only the code form `^sys.path.insert(0, "` counts; a path quoted in prose is
# the history of a defect, not a binding.
quoted="$(grep -c '^sys\.path\.insert(0, "' "$CELLS")"
FLAT="$WORK/module.flat"
tr -s '[:space:]' ' ' < "$MOD" > "$FLAT"
namespair="$(grep -c 'find_overlapping_pairs' "$FLAT")"
namesbool="$(grep -c 'has_any_overlap' "$FLAT")"
boundk="$(grep -c 'O(n log n + k)' "$FLAT")"
boundn="$(grep -c 'O(n log n) and stops at the first' "$FLAT")"
verdict bind \
    "$([ "$quoted" -eq 0 ] && [ "$namespair" -ge 1 ] && [ "$namesbool" -ge 1 ] \
        && [ "$boundk" -ge 1 ] && [ "$boundn" -ge 1 ] && echo 0 || echo 1)" \
    "quotedBinds=$quoted  names-pair=$namespair  names-bool=$namesbool  bounds=$boundk/$boundn"

# --- leg 3: the independent oracle, and the mutant battery ------------------
"$PY" "$VERIFY" --repo "$ROOT" --json "$WORK/rows.json" > "$WORK/verify.log" 2>&1; vrc=$?
killed="$(sed -n 's/.*"mutants_killed": \([0-9]*\).*/\1/p' "$WORK/rows.json" | head -1)"
cases="$(sed -n 's/.*"cases": \([0-9]*\).*/\1/p' "$WORK/rows.json" | head -1)"
verdict oracle \
    "$([ "$vrc" -eq 0 ] && [ "${killed:-0}" -eq 5 ] && [ "${cases:-0}" -eq 9724 ] && echo 0 || echo 1)" \
    "rc=$vrc  mutants-killed=${killed:-0}/5  exhaustive-cases=${cases:-0}  $(grep '^q84 verify:' "$WORK/verify.log" | tail -1)"

# --- leg 4: the structural cell has teeth -----------------------------------
ctl="$(cd "$LANE" && "$PY" -m unittest "$CELL" 2>&1)"; krc=$?
"$PY" "$VERIFY" --repo "$ROOT" --poison "$WORK/poison" > "$WORK/poison.log" 2>&1; prc=$?
pout="$(cd "$WORK/poison/wizard-work/beam" && "$PY" -m unittest "$CELL" 2>&1)"; zrc=$?
pnamed="$(printf '%s' "$pout" | grep -c 'test_has_any_overlap_does_not_build_pair_list')"
verdict teeth \
    "$([ "$krc" -eq 0 ] && [ "$prc" -eq 0 ] && [ "$zrc" -eq 1 ] && [ "$pnamed" -ge 1 ] && echo 0 || echo 1)" \
    "clean rc=$krc  poison-built rc=$prc  poisoned-cell rc=$zrc (want 1)  named=$pnamed"

if [ "$failed" -eq 0 ]; then
    echo "q84: $passed leg(s), 0 fault(s), rc 0"
    exit 0
fi
echo "q84: $passed passed, $failed fault(s), rc 1"
exit 1
