#!/bin/bash
# Stub-only failure-path exercises for todlando's fp-driver.sh.
# doyle's scope (HSFEJQ4E, JI5JVVJF): stubs only -- no cargo, NetSecurity,
# listeners, elevation or firewall changes; record actual requested ACTIONS AND
# ORDERING, not messages.
#
# The subject file is NOT edited. A copy is made and ONLY its config block is
# repointed at the rig; the runner proves that by diffing copy against original
# and refusing if anything outside the config lines differs.
set -u
RIG="$(cd "$(dirname "$0")" && pwd)"
SUBJECT='C:/Users/decid/AppData/Local/Temp/claude/C--Users-decid-Documents-projects-spt-core/71de7b8b-0f13-494d-8fa4-c01f4a215d10/scratchpad/fp-driver.sh'
W='C:/Users/decid/Documents/projects/spt-core/.worktrees/304-w2-repr'
export RIGBIN="$RIG/bin"

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

# ---------- build the driver copy, config repointed, body proven unchanged ----
build_copy() { # build_copy DEST [guarded]
  local dest="$1" guarded="${2:-}"
  local home="$RIG/work/fp-home" run="$RIG/work/fp-run"
  sed -e "s#^SP='.*'#SP='$RIG/work'#" \
      -e "s#^H=\"\$SP/fp-home\".*#H=\"\$SP/fp-home\"#" \
      -e "s#^EXE=\".*\"#EXE=\"$RIG/spt-stub.sh\"#" \
      "$SUBJECT" > "$dest"
  if [ -n "$guarded" ]; then
    # RIG CONTROL ONLY -- not a proposed patch, not todlando's fix. One guard so
    # the rig can be shown capable of reporting a PASS as well as a FAIL.
    sed -i 's#^  record "CLEANUP_BEGIN"#  record "CLEANUP_BEGIN"\n  [ "${SETUP_TOUCHED:-0}" -eq 1 ] || { record "CLEANUP_SKIPPED_NOTHING_CREATED"; return 0; }#' "$dest"
  fi
  # doyle (SV37RYUP): a changed-line COUNT supplements but does not replace
  # checking WHICH lines changed. The count stays as a cheap tripwire; the real
  # assertion is on CONTENT — every changed line must assign one of the config
  # names, or the copy is refused and no arm runs.
  local difftxt drc count offending
  difftxt=$(diff "$SUBJECT" "$dest"); drc=$?
  if [ "$drc" -gt 1 ]; then
    echo "REFUSED: diff INSTRUMENT FAILURE (exit $drc) comparing subject to $dest"; exit 9
  fi
  count=$(printf '%s\n' "$difftxt" | grep -c '^[<>]')
  printf 'copy %s: %d changed lines vs subject\n' "$(basename "$dest")" "$count"
  printf '%s\n' "$difftxt" | grep '^[<>]' | sed 's/^/    /'
  if [ "$count" -eq 0 ]; then
    echo "REFUSED: zero changed lines — an empty diff is also what a broken comparison prints"; exit 9
  fi
  offending=$(printf '%s\n' "$difftxt" | grep '^[<>]' \
    | grep -vE '^[<>] *(SP=|H=|EXE=)' \
    | grep -vc 'SETUP_TOUCHED.*CLEANUP_SKIPPED_NOTHING_CREATED')
  if [ "$offending" -ne 0 ]; then
    echo "REFUSED: $offending changed line(s) outside the config block; the body is not unmodified"; exit 9
  fi
  echo "    body unmodified: all $count changed lines assign SP=/H=/EXE= (or the named control guard)"
}

reset_run() {
  rm -rf "$RIG/work"; mkdir -p "$RIG/work"
  export RIGLOG="$RIG/work"; : > "$RIGLOG/actions.log"
}

# =============================================================================
echo "=== ARM 1 -- EARLY PREFLIGHT FAILURE (capacity below the floor)"
echo "    doyle's condition: an early preflight failure must perform NO product cleanup."
reset_run
build_copy "$RIG/work/driver-asis.sh"
chmod +x "$RIG/work/driver-asis.sh"
( export PATH="$RIGBIN:$PATH" RIGLOG="$RIG/work" FP_FREE_GIB=1.00 FP_SPT_MODE=noop
  bash "$RIG/work/driver-asis.sh" ) > "$RIG/work/arm1.out" 2>&1
rc1=$?
cp "$RIG/work/actions.log" "$RIG/arm1-actions.log"
echo "--- ordered actions requested (arm 1):"
sed 's/^/    /' "$RIG/arm1-actions.log"
spt_calls=$(grep -c '^[0-9]* SPT ' "$RIG/arm1-actions.log" || true)
cargo_calls=$(grep -c '^[0-9]* CARGO ' "$RIG/arm1-actions.log" || true)
echo "--- assertions (arm 1, driver AS WRITTEN):"
check "driver exits 1 (die)"                       1 "$rc1"
check "NO product command requested"               0 "$spt_calls"
check "NO cargo requested (pool never claimed)"    0 "$cargo_calls"

echo
echo "=== ARM 1-CONTROL -- same arm, one rig-only cleanup guard added"
echo "    Proves the rig can report a PASS, not only a FAIL. NOT a proposed patch."
reset_run
build_copy "$RIG/work/driver-guarded.sh" guarded
chmod +x "$RIG/work/driver-guarded.sh"
( export PATH="$RIGBIN:$PATH" RIGLOG="$RIG/work" FP_FREE_GIB=1.00 FP_SPT_MODE=noop
  bash "$RIG/work/driver-guarded.sh" ) > "$RIG/work/arm1c.out" 2>&1
rc1c=$?
cp "$RIG/work/actions.log" "$RIG/arm1c-actions.log"
spt_c=$(grep -c '^[0-9]* SPT ' "$RIG/arm1c-actions.log" || true)
cargo_c=$(grep -c '^[0-9]* CARGO ' "$RIG/arm1c-actions.log" || true)
echo "--- assertions (arm 1 control):"
check "driver exits 1 (die)"                       1 "$rc1c"
check "NO product command requested"               0 "$spt_c"
check "NO cargo requested"                         0 "$cargo_c"

echo
echo "=== ARM 2 -- SETUP REJECTION (Arm B setup refused)"
echo "    doyle's condition: preserve diagnostics, then request only authorized teardown."
reset_run
build_copy "$RIG/work/driver-asis.sh"
chmod +x "$RIG/work/driver-asis.sh"
( export PATH="$RIGBIN:$PATH" RIGLOG="$RIG/work" FP_FREE_GIB=124.00 \
         FP_SPT_MODE=b_setup_reject FP_CENSUS_GROUP=0 FP_CENSUS_29470=0 \
         SPT_DEBUG_RELEASE_SEED=$(printf 'a%.0s' $(seq 64))
  bash "$RIG/work/driver-asis.sh" ) > "$RIG/work/arm2.out" 2>&1
rc2=$?
cp "$RIG/work/actions.log" "$RIG/arm2-actions.log"
echo "--- ordered PRODUCT commands requested (arm 2), in order:"
grep '^[0-9]* SPT ' "$RIG/arm2-actions.log" | sed 's/^/    /'
setup_seq=$(grep -n 'bootstrap' "$RIG/arm2-actions.log" | tail -1 | cut -d: -f1)
after_setup=$(tail -n +$((${setup_seq:-0}+1)) "$RIG/arm2-actions.log" | grep -c '^[0-9]* SPT ' || true)
echo "--- assertions (arm 2):"
check "driver exits 3 (Arm B held)"                          3 "$rc2"
check "findings record the non-acceptance"                   1 "$(grep -c 'ARM_B_SETUP_NOT_ACCEPTED' "$RIG/work/fp-run/findings.txt" 2>/dev/null || echo 0)"
check "setup stderr preserved and non-empty"                 1 "$([ -s "$RIG/work/fp-run/b-setup.err" ] && echo 1 || echo 0)"
check "the enforcement diagnostic survives in that capture"  1 "$(grep -c 'ActiveStore enforcement is' "$RIG/work/fp-run/b-setup.err" 2>/dev/null || echo 0)"
check "the HELD diagnostic is recorded for the operator"     1 "$(grep -c 'Populated trials HELD' "$RIG/work/fp-run/findings.txt" 2>/dev/null || echo 0)"
echo "    product commands requested AFTER the refused setup: $after_setup"
echo "    (verbs and their opt-out state are listed above; no privilege is inferred)"

echo
echo "=== TOTAL  pass=$pass fail=$fail"
