#!/usr/bin/env bash
# THE PROBE IS THE ARTIFACT.
#
# The claim "`display-message -p -t <target> \"ok\"` cannot fail, `has-session`
# can" rests on live tmux behaviour — a version-dependent source with no artifact
# to pin. So it is not asserted in prose and not pinned to a version: re-running
# this re-derives it or fails loudly. Same pattern as probe-tmux-injection.sh
# (LESSONS #26, and the worked example in the playbook's EVIDENCE.md).
#
# NOT in the node suite deliberately: it can only run where a tmux server exists,
# and a conditionally-skipped test breaks check-test-count's contract that the
# totals add up — CI reported "348 tests, 347 pass, 0 fail" and failed on exactly
# that.
#
# BOTH DIRECTIONS ARE REQUIRED. An instrument that rejects a dead target proves
# nothing until it also accepts a live one: without the second, "always fails" and
# "discriminates" are the same reading.
set -uo pipefail

if [ -z "${TMUX:-}" ]; then
  echo "probe-tmux-liveness: not inside tmux — the claim CANNOT be checked here, which is not the same as checked and true." >&2
  exit 2
fi

DEAD="%99999"
LIVE="${TMUX_PANE:?TMUX_PANE unset inside tmux}"
fail=0

tmux has-session -t "$DEAD" >/dev/null 2>&1
if [ $? -eq 0 ]; then echo "FAIL: has-session accepted a nonexistent target ($DEAD) — it has lost its discriminating power" >&2; fail=1
else echo "ok  has-session rejects a dead target ($DEAD)"; fi

tmux has-session -t "$LIVE" >/dev/null 2>&1
if [ $? -ne 0 ]; then echo "FAIL: has-session rejected a LIVE pane ($LIVE) — the positive control failed, so a rejection above proves nothing" >&2; fail=1
else echo "ok  has-session accepts a live pane ($LIVE)"; fi

tmux display-message -p -t "$DEAD" "ok" >/dev/null 2>&1
if [ $? -ne 0 ]; then
  echo "NOTE: display-message now REJECTS a dead target on tmux $(tmux -V). The defect this guards may be gone;" >&2
  echo "      re-read the rule before relying on it. This is not a failure — it is the claim expiring." >&2
else echo "ok  display-message exits 0 for a dead target — confirms it is unusable as a liveness probe"; fi

if [ "$fail" -ne 0 ]; then echo "probe-tmux-liveness: FAIL (tmux $(tmux -V))" >&2; exit 1; fi
echo "probe-tmux-liveness: ok on $(tmux -V) — behaviour holds; the claim is pinned to this, not to a version string"
