#!/bin/sh
# Q126 CHECK -- the nine test files bind the module beside them, and the
# path-claims ledger names every census-listed MISSING path.
# rc 0 = import-resolve 0 faults, path-claims 0 new, all nine files green.
set -u
REPO="$(cd "$(dirname "$0")/../.." && pwd)"
cd "$REPO" || exit 2

# (a) no committed test file binds a module to a path outside the repo
python3 tools/import_resolve_check.py --repo . --rev WORKTREE --quiet || exit 1

# (b) every MISSING path the census produces is named in the ledger
OUT="${TMPDIR:-/tmp}/q126-pathclaims.$$"
python3 tools/path_claim_check.py --repo . --rev WORKTREE --fail-on-missing \
    >"$OUT" 2>&1 || { cat "$OUT"; rm -f "$OUT"; exit 1; }
grep -q " 0 new against the baseline" "$OUT" || { cat "$OUT"; rm -f "$OUT"; exit 3; }
rm -f "$OUT"

# (c) the eight rebound files run green from their own directory, and the host
# probe either passes or says SKIP -- never a green it did not measure
for t in wizard-work/beam/test_canonical_url.py wizard-work/beam/test_median.py \
         wizard-work/spark/test_array_intersection.py \
         wizard-work/spark/test_counter_util.py \
         wizard-work/spark/test_email_validator.py \
         wizard-work/spark/test_prime_factorizer.py \
         wizard-work/spark/test_url_title_extractor.py \
         wizard-work/spark/test_validator.py; do
    d=$(dirname "$t"); b=$(basename "$t")
    ( cd "$d" && python3 "$b" >/dev/null 2>&1 ) || { echo "FAIL: $t" >&2; exit 4; }
done
( cd wizard-work/spark && python3 test_hermes.py >"${OUT}.hermes" 2>&1 )
rc=$?
grep -qE "^(PASS|SKIP):" "${OUT}.hermes" || { cat "${OUT}.hermes"; rm -f "${OUT}.hermes"; exit 5; }
rm -f "${OUT}.hermes"
[ "$rc" = 0 ] || [ "$rc" = 77 ] || { echo "FAIL: test_hermes.py rc=$rc" >&2; exit 6; }

echo "Q126 OK: import-resolve 0 faults; path-claims 0 new; 9 files green"
