#!/bin/bash
# CHECK: verify all beam reliability primitives
#
# Runner note (Q105, 2026-10-28). This lane writes its tests in two styles:
# unittest.TestCase subclasses and bare module-level `def test_*` / `assert`.
# `python3 -m unittest discover` collects only the first -- 276 of the 407
# tests the directory held then -- so a green CHECK said nothing about the
# other 131, and test_fenwick.py's 8 cells could be broken without moving
# the gate.
#
# Runner note (Q128, 2026-10-30). Q105's repair pointed this gate at pytest,
# which collects both styles but is not installed on every interpreter the
# gate runs on: here `python3 -m pytest --version` fails, so the gate printed
# "pytest not importable -- cannot grade" and exited 2 -- honest, and running
# zero of the lane's cells. The gate now runs `run_lane_tests.py`, which
# collects both styles itself on the standard library alone (TestLoader for
# the TestCase classes, FunctionTestCase for every module-level `def test_*`
# with no parameters, and one import-by-path step that turns a module which
# will not import into a fault), so the same cells run with or without
# pytest. Counts are not pinned here -- the runner prints them every run,
# and `python3 run_lane_tests.py --list` prints the collection alone.
#
# Exit codes follow the repo's tools/ contract: 0 clean, 1 graded a fault
# (a failing test, an erroring test, a module that will not import), 2 could
# not grade (no python3, no lane directory, no test files).
#
# `toposort/test_toposort.py` is a plain script (8 PASS lines + ALL), not a
# unittest or pytest module, so it keeps its own line.
set -e
set -o pipefail
cd "$(dirname "$(readlink -f "$0")")"

set +e
python3 run_lane_tests.py
rc=$?
set -e
case $rc in
    0) ;;
    1) echo "CHECK: lane tests failed -- see the failure list above (Q105/Q128)." >&2; exit 1 ;;
    2) echo "CHECK: could not grade -- see the runner's message above." >&2; exit 2 ;;
    127) echo "CHECK: python3 not found -- cannot grade." >&2; exit 2 ;;
    *) echo "CHECK: run_lane_tests.py exited $rc -- internal error (a fault)." >&2; exit 1 ;;
esac

python3 toposort/test_toposort.py
