#!/usr/bin/env bash
set -euo pipefail
# Run after installing and pairing the commit-specific iOS client with a fresh
# replay-seeded thread named "GIF paste" open and its composer empty.
# Serve copy.html and animation.gif together; copy.html disables text selection
# so Safari's image menu exposes Copy (alongside Save to Photos).
# The same clipboard source and interaction are used for both commits.
: "${GIF_FIXTURE_ORIGIN:?Set to the reachable fixture origin}"
: "${EVIDENCE_DIR:?Set to a new output directory}"
: "${SIDE:?Set to before or after}"
session_name=${GIF_DEVICE_SESSION:-gif-manual}
agent-device open com.apple.mobilesafari "$GIF_FIXTURE_ORIGIN/copy.html" --session "$session_name"
agent-device snapshot -i --session "$session_name"
# iPhone 17 Pro, 402x874: the 320px image occupies x8..328 and y170..490.
agent-device longpress 160 320 1500 --session "$session_name" --settle
agent-device press 'label="Copy" role=cell' --session "$session_name" --settle
agent-device open com.t3tools.t3code.dev --session "$session_name"
agent-device snapshot -i --session "$session_name"
agent-device record start "$EVIDENCE_DIR/$SIDE-paste.mp4" --hide-touches --session "$session_name"
agent-device longpress 'role=text-view' 1000 --session "$session_name" --settle
if ! agent-device press 'label="Paste" role=cell' --session "$session_name" --settle; then
  agent-device press 'label="Paste" role=menu-item' --session "$session_name" --settle
fi
agent-device screenshot "$EVIDENCE_DIR/$SIDE-attachment.png" --session "$session_name"
case "$SIDE" in
  before) extension=png ;;
  after) extension=gif ;;
  *) echo "SIDE must be before or after" >&2; exit 2 ;;
esac
agent-device press "label=\"Open pasted-image.$extension\" role=button" --session "$session_name" --settle
# Capture animation for several cycles; this is a recording duration, not an assertion wait.
sleep 6
agent-device record stop --session "$session_name"
