#!/usr/bin/env bash
TEST_SHELL=/bin/bash
SESSION="wnl"
WINDOW=0
NTESTS=5
PANE_EXECUTOR=0
PANE_TEST1=1
PANE_TEST2=2
PANE_TEST3=3
PANE_TEST4=4
PANE_TEST5=5
SLOT_TEST1=11
SLOT_TEST2=12
SLOT_TEST3=13
SLOT_TEST4=14
SLOT_TEST5=15
RUNTIME_DIR=${RUNTIME_DIR:-${XDG_RUNTIME_DIR:-/tmp}}/wnltest

if ! tmux has-session -t $SESSION 2>/dev/null; then
  tmux new-session -d -s $SESSION
else
  echo "Reusing existing tmux session '$SESSION'"
fi

# Ensure we start from a clean first window
tmux send-keys -t $SESSION:0 'clear' C-m

while [ "$(tmux list-panes -t "$SESSION:$WINDOW" 2>/dev/null | wc -l | tr -d ' ')" -le $NTESTS ]; do
  tmux split-window -h -t "$SESSION:$WINDOW"
done

tmux select-layout -t $SESSION even-vertical

# configure
tmux send-keys -t $SESSION:$WINDOW.$PANE_TEST1    "export RC_FILE=/dev/null RUNTIME_DIR=${RUNTIME_DIR} DOUBLE_TAP_REQUIRED=false RESTART_MODE=false ; $TEST_SHELL" C-m
tmux send-keys -t $SESSION:$WINDOW.$PANE_TEST2    "export RC_FILE=/dev/null RUNTIME_DIR=${RUNTIME_DIR} DOUBLE_TAP_REQUIRED=false RESTART_MODE=false ; $TEST_SHELL" C-m
tmux send-keys -t $SESSION:$WINDOW.$PANE_TEST3    "export RC_FILE=/dev/null RUNTIME_DIR=${RUNTIME_DIR} DOUBLE_TAP_REQUIRED=true  RESTART_MODE=false ; $TEST_SHELL" C-m
tmux send-keys -t $SESSION:$WINDOW.$PANE_TEST4    "export RC_FILE=/dev/null RUNTIME_DIR=${RUNTIME_DIR} DOUBLE_TAP_REQUIRED=false RESTART_MODE=true  ; $TEST_SHELL" C-m
tmux send-keys -t $SESSION:$WINDOW.$PANE_TEST5    "export RC_FILE=/dev/null RUNTIME_DIR=${RUNTIME_DIR} DOUBLE_TAP_REQUIRED=false RESTART_MODE=true  ; $TEST_SHELL" C-m
tmux send-keys -t $SESSION:$WINDOW.$PANE_EXECUTOR "export RC_FILE=/dev/null RUNTIME_DIR=${RUNTIME_DIR}                                              ; $TEST_SHELL" C-m

# start the test instances
tmux send-keys -t $SESSION:$WINDOW.$PANE_TEST1 "wnl $SLOT_TEST1 sleep 1" C-m
tmux send-keys -t $SESSION:$WINDOW.$PANE_TEST2 "wnl $SLOT_TEST2 sleep 1" C-m
tmux send-keys -t $SESSION:$WINDOW.$PANE_TEST3 "wnl $SLOT_TEST3 sleep 1" C-m
tmux send-keys -t $SESSION:$WINDOW.$PANE_TEST4 "wnl $SLOT_TEST4 sleep 1" C-m
tmux send-keys -t $SESSION:$WINDOW.$PANE_TEST5 "wnl $SLOT_TEST5 sh -c 'echo testing shell; sleep 1 && echo done testing shell'" C-m
sleep 1

# run the tests
# should result in a single, successful execution
tmux send-keys -t $SESSION:$WINDOW.$PANE_EXECUTOR "wnlctl $SLOT_TEST1 &" C-m
# should result in a single, cancelled execution with exit code 130
tmux send-keys -t $SESSION:$WINDOW.$PANE_EXECUTOR "wnlctl $SLOT_TEST2; sleep 0.2; SIGNAL=USR2 wnlctl $SLOT_TEST2 &" C-m
# should result in no execution
tmux send-keys -t $SESSION:$WINDOW.$PANE_EXECUTOR "wnlctl $SLOT_TEST3; sleep 1; wnlctl $SLOT_TEST3 &" C-m
# should result in a terminated execution, then a clean execution
tmux send-keys -t $SESSION:$WINDOW.$PANE_EXECUTOR "wnlctl $SLOT_TEST4; sleep 0.5; wnlctl $SLOT_TEST4 &" C-m
# should result in a single, cancelled execution with exit code 130
tmux send-keys -t $SESSION:$WINDOW.$PANE_EXECUTOR "wnlctl $SLOT_TEST5; sleep 0.2; SIGNAL=USR2 wnlctl $SLOT_TEST5 &" C-m
