#!/bin/bash
# CHECK-json-validator: grade wizard-work/spark/json-validator.py's exit contract.
#
# Q107. The lane's own CHECK grades a different artifact, so this file exists to
# give the validator a gate of its own rather than leave its cell unrun. The cell
# (test_json_validator_exit_codes.py) invokes the shipped script as a subprocess
# and asserts on exit status, so it grades the tree it sits in -- the module path
# is resolved from the test file's own location, never from a pinned /root path.
#
# Exit codes follow the repo's tools/ contract:
#   0 = the contract holds
#   1 = the contract is broken (a test failed)
#   2 = could not grade (no python3, or the cell does not import/collect)
set -e
set -o pipefail
cd "$(dirname "$(readlink -f "$0")")"

if ! command -v python3 >/dev/null 2>&1; then
    echo "CHECK: python3 not on PATH -- cannot grade." >&2
    exit 2
fi

set +e
python3 -m unittest -q test_json_validator_exit_codes >/tmp/.q107-check.out 2>&1
rc=$?
set -e
tail -3 /tmp/.q107-check.out

case $rc in
    0) echo "CHECK: exit-code contract holds."; exit 0 ;;
    1) echo "CHECK: contract broken -- a cell failed." >&2; exit 1 ;;
    *) echo "CHECK: unittest exited $rc -- collection error, could not grade." >&2; exit 2 ;;
esac
