#!/bin/bash
# CHECK-q143 -- three legs over every CHECK entry point in this tree.
#
#   selftest  20 cells on fixture trees: discovery (a near-miss named CHECKER is
#             not an entry point), the five codes the runner can produce
#             (0, 1, 2, 124 timeout, python shebang), the four mutants the
#             grader must name (new path, missing path, 0->1, 1->0), a baseline
#             that is absent or malformed reading rc 2, and two takes being
#             byte-identical.
#   paths     the entry-point SET, graded against the baseline, without running
#             anything -- 0.2 s, and it is the leg that notices evidence leaving
#             the tree.
#   sweep     every entry point run, its exit code compared with the code the
#             baseline records. Both directions: a green that went red is a
#             reverted repair, a red that went green is a repair nobody signed.
#
# Exit codes: 0 all legs green, 1 a leg faulted, 2 a leg could not grade.
set -u
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$HERE/../../.." && pwd)"
TOOL="$HERE/check_sweep.py"
BASE="$HERE/check_entry_baseline.tsv"
PY="${PYTHON:-python3}"
rc=0
legs=0
faults=0

run_leg() {
    name="$1"; shift
    legs=$((legs + 1))
    out="$("$PY" "$TOOL" --repo "$ROOT" --baseline "$BASE" "$@" 2>&1)"
    lrc=$?
    if [ "$lrc" -eq 0 ]; then
        printf '  %-8s PASS   rc=0  %s\n' "$name" "$(printf '%s' "$out" | tail -1)"
    elif [ "$lrc" -eq 1 ]; then
        faults=$((faults + 1))
        printf '  %-8s FAULT  rc=1  %s\n' "$name" "$(printf '%s' "$out" | tail -1)"
        printf '%s\n' "$out" | grep -E '^(FAIL|!)' | head -8
    else
        faults=$((faults + 1))
        printf '  %-8s UNGRAD rc=%s  %s\n' "$name" "$lrc" "$(printf '%s' "$out" | tail -1)"
    fi
    [ "$lrc" -eq 0 ] || rc=1
}

run_leg selftest --selftest
run_leg paths --paths
run_leg sweep --check

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