#!/usr/bin/env bash
set -euo pipefail
# Start on the thread with an offline queued message and an unsent draft.
# Arguments: before|after, route label, thread title, output directory, device session.
side="$1"; route_label="$2"; thread_title="$3"; out="$4"; session="$5"; environment_id="$6"; thread_id="$7"
mkdir -p "$out"
ad() { agent-device "$@" --session "$session"; }
still() { python3 "$(dirname "$0")/still.py" "$out/$1.png" "$session"; }
ad record start "$out/flow.mp4" --hide-touches
trap 'ad record stop' EXIT
still draft-and-queue
ad open com.t3tools.t3code.dev "t3code-dev://settings/environments/$environment_id"
ad wait text Routes
ad press 'label="Edit"' --settle
ad snapshot -i > "$out/routes.txt"
still routes-in-edit-mode
if [ "$side" = before ]; then
 ad press "label=\"Remove $route_label route\"" --settle
 still confirmation
 ad press 'text="Remove"' --settle
 ad wait text 'This environment is no longer saved on this device.'
 still environment-deleted
 # A second real navigation makes the loss visible from Chats as well.
 ad press 'label="Chats"' --settle
 ad snapshot -i > "$out/chats.txt"
 still chats-after-removal
else
 ad press 'label="Done"' --settle
 ad open com.t3tools.t3code.dev "t3code-dev://threads/$environment_id/$thread_id"
 ad wait text 'queued message for issue 41820'
 still retained-draft-and-queue
fi
