#!/usr/bin/env bash
# Q115 / Q131 gate: the pipe change-monitor's folder-read failure path.
#
# The artifact under repair is NOT in this repository -- the pipe's monitor lives
# at /root/.hermes/scripts/troupe_pipe_monitor.py because the cron job runs it by
# host path -- so this gate has to read a host path. That is stated, not hidden:
# it is the same host-pin class Q106 filed, and here it is unavoidable unless the
# monitor is moved into the tree, which is the pipe's decision, not this item's.
#
# What it grades, all through the deployed module (imported, never re-implemented):
#   - the seven authorship cases the harness was written for in Q15
#   - s1b/s1c: a folder that fails to read carries its last good listing and the
#     tick's delta is empty (the Q115 fault: a read error rendered as deletions)
#   - s1d: non-vacuity -- without the carry that same tick loses those lines
#   - s1e: a genuine deletion on the next good read is still reported
#   - s2/s3: no baseline and a corrupt cache print NO-BASELINE rather than rows
#   - s4: the run leaves no host cache behind
set -u
HARNESS=/root/.hermes/scripts/troupe_pipe_monitor_harness.py

if [ ! -f "$HARNESS" ]; then
    echo "FAIL: harness missing at $HARNESS" >&2
    exit 2
fi

out="$(python3 "$HARNESS" 2>&1)"
rc=$?
printf '%s\n' "$out" | grep -E '^\[FAIL\]|cases pass' || true

if [ "$rc" -ne 0 ]; then
    echo "FAIL: harness rc=$rc (a cell above is red)" >&2
    exit 1
fi
if ! printf '%s\n' "$out" | grep -q '^15/15 cases pass'; then
    echo "FAIL: expected 15/15 cases, got: $(printf '%s\n' "$out" | tail -1)" >&2
    exit 1
fi
echo "q115: folder-read failure carries its listing, tokens name the folder -- OK"
exit 0
