#!/usr/bin/env bash
set -euo pipefail
# Run from the checkout being proved, after installing the client, seeding,
# opening its Metro bundle, pairing, and opening the seeded thread.
# Arguments: agent-device session, isolated T3 home, capture directory.
session="$1"
home_dir="$2"
out="$3"
ad() { agent-device "$@" --session "$session"; }
chmod 555 "$home_dir/userdata/attachments"
trap 'chmod 755 "$home_dir/userdata/attachments"' EXIT
ad record start "$out/reproduction.mp4" --hide-touches
ad press 'label="Add attachment"' --settle
ad press 'text="Choose Files"' --settle
ad longpress 'text="report.pdf"' --settle
ad press 'label="Select"' --settle
ad wait 'label="Retry or remove the failed attachment"' 15000
ad snapshot -i
# Focus the editor using its current text-field reference from the snapshot.
# On base, tapping its chip opens the PDF preview, which has no retry action.
# Close that preview, then restore write permission on both sides.
chmod 755 "$home_dir/userdata/attachments"
sleep 16
ad keyboard dismiss
ad screenshot "$out/failed-document.png"
ad snapshot -i
# On head, execute the following without removing or reattaching the document:
# ad press 'label="Retry uploading report.pdf"' --settle
# ad wait 'label="Send"' 15000
# ad screenshot "$out/retry-complete.png"
ad record stop
