#!/bin/bash
# STUBBED SIGNAL CASE — doyle (L6DKTKMB): prove no resumed trials and cleanup
# entered ONCE. Stubs only; no cargo, listener, elevation or firewall change.
#
# NOT RUN AGAINST THE SUPERSEDED fp-driver.sh. doyle: "Do not keep exercising the
# superseded driver." This file is the case, ready to point at the corrected
# driver when todlando delivers it (set TARGET). Today it runs ONLY against three
# toy targets of mine, which is what proves the case can distinguish — a signal
# case that has never been shown to FAIL would measure nothing.
#
# DETERMINISTIC, NOT TIMED: the target raises its own signal at a defined point
# (kill -TERM $$ inside the first trial) rather than the runner racing a sleep.
# A timing race would make a green intermittent, and an intermittent green is
# zero information.
set -u
RIG="$(cd "$(dirname "$0")" && pwd)"
export RIGBIN="$RIG/bin"
WORK="$RIG/sigwork"

pass=0; fail=0
check() { if [ "$2" = "$3" ]; then printf '    %-50s PASS\n' "$1"; pass=$((pass+1));
          else printf '    %-50s FAIL (want %s, got %s)\n' "$1" "$2" "$3"; fail=$((fail+1)); fi; }

mk_toys() {
  mkdir -p "$WORK"
  # ---- toy_good: signal terminates normal execution and enters cleanup ONCE.
  cat > "$WORK/toy_good.sh" <<'EOG'
#!/bin/bash
CLEANUP_DONE=0
cleanup() { [ "$CLEANUP_DONE" -eq 1 ] && return 0; CLEANUP_DONE=1
            "$RIGBIN/_log" MARK "CLEANUP_ENTER"; "$RIGBIN/_log" SPT "verb=[serve lan --stop]"; }
on_sig()  { "$RIGBIN/_log" MARK "SIGNAL_RECEIVED"; STOP=1; }
trap cleanup EXIT; trap on_sig INT TERM HUP
STOP=0
for n in 1 2 3; do
  [ "$STOP" -eq 1 ] && { "$RIGBIN/_log" MARK "TRIALS_ABANDONED_AFTER_SIGNAL"; break; }
  "$RIGBIN/_log" SPT "verb=[serve lan --bootstrap] trial=$n"
  [ "$n" -eq 1 ] && kill -TERM $$
done
exit 0
EOG
  # ---- toy_resume: the shape doyle warned against — cleanup attached to the
  # signal, then trials RESUME.
  cat > "$WORK/toy_resume.sh" <<'EOR'
#!/bin/bash
CLEANUP_DONE=0
cleanup() { [ "$CLEANUP_DONE" -eq 1 ] && return 0; CLEANUP_DONE=1
            "$RIGBIN/_log" MARK "CLEANUP_ENTER"; "$RIGBIN/_log" SPT "verb=[serve lan --stop]"; }
trap cleanup EXIT; trap cleanup INT TERM HUP
for n in 1 2 3; do
  "$RIGBIN/_log" SPT "verb=[serve lan --bootstrap] trial=$n"
  [ "$n" -eq 1 ] && kill -TERM $$
done
exit 0
EOR
  # ---- toy_twice: cleanup is not idempotent, so the signal path and the EXIT
  # trap both run it.
  cat > "$WORK/toy_twice.sh" <<'EOT'
#!/bin/bash
cleanup() { "$RIGBIN/_log" MARK "CLEANUP_ENTER"; "$RIGBIN/_log" SPT "verb=[serve lan --stop]"; }
on_sig()  { "$RIGBIN/_log" MARK "SIGNAL_RECEIVED"; cleanup; STOP=1; }
trap cleanup EXIT; trap on_sig INT TERM HUP
STOP=0
for n in 1 2 3; do
  [ "$STOP" -eq 1 ] && break
  "$RIGBIN/_log" SPT "verb=[serve lan --bootstrap] trial=$n"
  [ "$n" -eq 1 ] && kill -TERM $$
done
exit 0
EOT
  chmod +x "$WORK"/toy_*.sh
}

run_case() { # run_case NAME TARGET WANT_TRIALS_AFTER WANT_CLEANUPS
  local name="$1" target="$2" want_after="$3" want_cl="$4"
  export RIGLOG="$WORK/$name"; mkdir -p "$RIGLOG"; : > "$RIGLOG/actions.log"
  ( export PATH="$RIGBIN:$PATH"; bash "$target" ) > "$RIGLOG/target.out" 2>&1
  local rc=$?
  local sig_seq trials_after cleanups
  sig_seq=$(grep -n 'SIGNAL_RECEIVED\|CLEANUP_ENTER' "$RIGLOG/actions.log" | head -1 | cut -d: -f1)
  sig_seq=${sig_seq:-0}
  trials_after=$(tail -n +$((sig_seq+1)) "$RIGLOG/actions.log" | grep -c 'bootstrap' || true)
  cleanups=$(grep -c 'CLEANUP_ENTER' "$RIGLOG/actions.log" || true)
  printf '  %s (exit %d)\n' "$name" "$rc"
  sed 's/^/      /' "$RIGLOG/actions.log"
  check "no trial dispatched after the signal"  "$want_after" "$trials_after"
  check "cleanup entered exactly once"          "$want_cl"    "$cleanups"
}

mk_toys
echo "=== SIGNAL CASE — self-test against toy targets (the corrected driver is NOT run)"
echo
echo "[A] toy_good — signal stops trials, cleanup once. The case must PASS here."
run_case good   "$WORK/toy_good.sh"   0 1
echo
echo "[B] toy_resume — NEGATIVE CONTROL: cleanup on signal, then trials resume."
echo "    The case must REJECT this; the two lines below are EXPECTED to read FAIL."
run_case resume "$WORK/toy_resume.sh" 0 1
echo
echo "[C] toy_twice — NEGATIVE CONTROL: cleanup not idempotent, entered twice."
echo "    Expected to read FAIL on the cleanup-count line."
run_case twice  "$WORK/toy_twice.sh"  0 1
echo
echo "=== self-test tally pass=$pass fail=$fail"
echo "    A green case is [A] 2/2 PASS with [B] and [C] each producing at least one FAIL."
echo "    Point TARGET at the corrected driver to run the real case."
