#!/bin/sh
# Real terminal sessions for checks 10, 10b and 12 in CARD.md. Drives tmux on a
# private socket, writes each pane capture to out/<name>.txt and a plain HTML
# rendering of it to out/<name>.html for the screenshots.
# From the project folder:
#   sh test/tmux.sh readme   six runs of sleep 61 in /tmp/proj, then the log (about seven minutes)
#   sh test/tmux.sh edges    usage, a verdict, a failed run, Ctrl-C, a loop stopped by one Ctrl-C
set -eu
go build -o usually .
mkdir -p out
T="tmux -L usually"
trap '$T kill-server 2>/dev/null || true' EXIT
B=/tmp/usually-bin
mkdir -p $B && cp usually $B/

fail() { echo "FAIL $*" >&2; exit 1; }
pane() { # session dir log: a clean shell with a plain prompt and nothing personal in it
  $T new-session -d -s "$1" -x 140 -y 80 -c "$2" \
    "env -i HOME=$2 TERM=xterm-256color PATH=$B:/usr/bin:/bin USUALLY_LOG=$3 PS1='\$ ' bash --norc --noprofile"
  waitidle "$1"
}
idle() { $T capture-pane -p -t "$1" | grep -v '^$' | tail -n 1 | grep -qx '\$'; }
waitidle() { sleep 1; while ! idle "$1"; do sleep 1; done; }
typed() { $T send-keys -t "$1" "$2" Enter; waitidle "$1"; }
sleeping() { pgrep -f '^sleep 30$' >/dev/null; }
capture() { # session file: the whole history, without trailing blank lines or the last idle prompt
  $T capture-pane -p -S - -t "$1" | awk 'NF { while (n > 0) { print ""; n-- } print; next } { n++ }' |
    sed '${/^\$$/d;}' >"$2"
}
render() { # name title
  {
    printf '%s\n' '<!doctype html>' '<html lang="en"><head><meta charset="utf-8">' \
      '<meta name="viewport" content="width=device-width, initial-scale=1">' "<title>$2</title>" \
      '<style>html,body{margin:0;background:#FCF0DD;color:#1A1A1A}pre{margin:0;padding:16px;font:14px/1.45 "DejaVu Sans Mono",Menlo,monospace;white-space:pre}</style>' \
      '</head><body><pre>'
    sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' "out/$1.txt"
    printf '%s\n' '</pre></body></html>'
  } >"out/$1.html"
}
both() { # session name title: capture at 140 columns, then reflowed at 42
  capture "$1" "out/$2.txt" && render "$2" "$3"
  $T resize-window -t "$1" -x 42
  capture "$1" "out/$2-narrow.txt" && render "$2-narrow" "$3"
}

case "${1:-}" in
readme)
  rm -rf /tmp/proj && mkdir -p /tmp/proj
  pane r /tmp/proj /tmp/proj/usually.log
  i=1
  while [ $i -le 6 ]; do
    [ $i = 6 ] && date '+sixth run sent at %H:%M:%S on %A; plus 61 seconds is the time to round' >out/clock-check.txt
    typed r 'usually sleep 61'
    i=$((i + 1))
  done
  typed r 'cat /tmp/proj/usually.log'
  both r session 'Usually: a real session'
  grep 'Expect to finish' out/session.txt >>out/clock-check.txt || fail "10 no clock sentence on the sixth run"
  cat out/clock-check.txt
  ;;
edges)
  E=/tmp/usually-edges
  rm -rf $E && mkdir -p $E
  printf '%s\n' 'sleep ${S:-3}; [ -z "$FAIL" ] || exit 3' >$E/run.sh
  pane e $E $E/usually.log
  typed e 'usually -h'
  typed e 'for i in 1 2 3 4 5; do usually sh run.sh; done'
  typed e 'S=9 usually sh run.sh'
  typed e 'FAIL=1 usually sh run.sh; echo "exit status $?"'
  typed e 'usually sh run.sh'
  # 10: Ctrl-C reaches the child, the run is kept apart as exit 130.
  $T send-keys -t e 'usually sleep 30' Enter
  sleep 2 && sleeping || fail "10 sleep 30 never started"
  $T send-keys -t e C-c && waitidle e
  ! sleeping || fail "10 sleep 30 survived Ctrl-C"
  typed e 'echo "exit status $?"'
  $T capture-pane -p -t e | grep -qx 'exit status 130' || fail "10 status was not 130"
  tail -n 1 $E/usually.log | grep -q ', exit 130\. ' || fail "10 log line"
  echo "10 ok: Ctrl-C after 2 seconds, status 130, sleep 30 gone, last log line: $(tail -n 1 $E/usually.log)"
  # 10b: one Ctrl-C stops a loop; a fresh log keeps exactly one exit 130 line.
  typed e "export USUALLY_LOG=$E/loop.log"
  $T send-keys -t e 'for i in 1 2 3; do usually sleep 30; done' Enter
  sleep 2 && sleeping || fail "10b sleep 30 never started"
  $T send-keys -t e C-c
  sleep 3
  idle e || fail "10b the loop went on"
  ! sleeping || fail "10b a second sleep 30 started"
  [ "$(grep -c 'seconds, exit' $E/loop.log)" = 1 ] && grep -q ', exit 130\. ' $E/loop.log || fail "10b log"
  typed e "cat $E/loop.log"
  echo "10b ok: one Ctrl-C ended the loop, prompt back, no second sleep 30, loop.log has one run line: exit 130"
  both e edges 'Usually: verdicts, failures, Ctrl-C'
  ;;
*)
  echo "usage: sh test/tmux.sh readme|edges" >&2
  exit 2
  ;;
esac
