#!/bin/bash
# Start at Settings > Environments in a fresh development Electron client.
# Usage: bash drive-ssh.sh <agent-browser executable> <session> <output directory> <base|head>
set -eu
browser=$1
session=$2
out=$3
side=$4
mkdir -p "$out"
ab() { "$browser" --session "$session" "$@"; }
ab record start "$out/$side.webm"
ab find role button click --name 'Add environment' --exact
ab wait --text 'Pair another environment'
ab find role button click --name 'SSH Use local SSH config, agent, and tunnels for the backend.' --exact
ab find role combobox fill profile-demo --name 'SSH host or alias' --exact
ab find role button click --name 'Add environment' --exact
if [ "$side" = base ]; then
  ab wait --text 'Syntax error'
else
  ab wait --text 'Environment connected'
  ab wait --text 'Connected'
fi
sleep 3
ab snapshot > "$out/$side-snapshot.txt"
ab screenshot "$out/$side-raw.png"
sleep 0.5
ab screenshot "$out/$side-stability.png"
# UI transitions must have settled before the evidence still is accepted.
if ! cmp -s "$out/$side-raw.png" "$out/$side-stability.png"; then
  sleep 2
  ab screenshot "$out/$side-raw.png"
  sleep 0.5
  ab screenshot "$out/$side-stability.png"
  cmp "$out/$side-raw.png" "$out/$side-stability.png"
fi
ab record stop
