← back to Govarbitrage

apps/mobile/device-proof-evidence/capture-tap-scroll.sh

83 lines

#!/bin/bash
# Follow-up capture: detail lower-sections (Cost Breakdown & Profit + Scores) scrolled into view
# + Settings "Test Connection" tapped -> "Connected · latency · tier FREE".
#
# PREREQ: iTerm2 must have Accessibility (System Settings > Privacy > Accessibility > iTerm ON),
#         AND iTerm must have been FULLY QUIT + REOPENED after granting (a running app can't see a
#         new grant). Run this from that fresh iTerm. A PREFLIGHT below verifies taps actually land
#         and ABORTS before recording if they don't — so you never get a video full of missed taps.
#
# Writes to /tmp (the simctl daemon can't write the external /Volumes volume), then copies here.
set -u
UDID=2061D117-F2B5-419D-82D6-E9567908D5E8
BID=com.abrams.govarbitrage
IMAC=cmtk1nnat00eysqssvwvbewq7
ISO=$(date -u +%Y-%m-%dT%H%M%SZ)
HERE="$(cd "$(dirname "$0")" && pwd)"
MOV="/tmp/govarbitrage-SIM-tapscroll-${ISO}.mov"

command -v cliclick >/dev/null || { echo "ABORT: cliclick not found (brew install cliclick)"; exit 1; }

# --- calibrated device-region-in-window mapping ---
# Hardcoded to the stable Sim window geometry (no osascript — that call can hang on an
# Automation permission prompt). Override via env if the window is elsewhere:
#   WX=.. WY=.. WW=.. WH=.. ./capture-tap-scroll.sh
WX=${WX:-1473}; WY=${WY:-100}; WW=${WW:-494}; WH=${WH:-1054}
YTOP=28; DEV_W=440; DEV_H=956
DH=$((WH-YTOP))
sx(){ python3 -c "s=($DH)/$DEV_H.0; dw=$DEV_W*s; xp=($WW-dw)/2; print(int($WX+xp+($1/$DEV_W.0)*dw))"; }
sy(){ python3 -c "s=($DH)/$DEV_H.0; print(int($WY+$YTOP+($1/$DEV_H.0)*($DH)))"; }
echo "Sim window ($WX,$WY) ${WW}x${WH}"

activate(){ osascript -e 'tell application "Simulator" to activate' >/dev/null 2>&1; sleep 0.6; }

# --- PREFLIGHT: prove synthetic taps reach the app before recording anything ---
echo "[preflight] verifying taps land (needs iTerm Accessibility + a FRESH iTerm)..."
xcrun simctl terminate "$UDID" "$BID" >/dev/null 2>&1; sleep 1
xcrun simctl launch "$UDID" "$BID" >/dev/null 2>&1; sleep 6
activate
xcrun simctl io "$UDID" screenshot /tmp/pf-before.png >/dev/null 2>&1
cliclick c:$(sx 330),$(sy 915) >/dev/null 2>&1   # Settings tab (right side of bottom bar)
sleep 2
xcrun simctl io "$UDID" screenshot /tmp/pf-after.png >/dev/null 2>&1
if python3 -c "import sys;a=open('/tmp/pf-before.png','rb').read();b=open('/tmp/pf-after.png','rb').read();sys.exit(0 if a==b else 1)"; then
  cat <<'MSG'
ABORT: synthetic taps are NOT reaching the Simulator (the Settings tab did not switch).
  Fix: System Settings > Privacy & Security > Accessibility > turn iTerm ON,
       then FULLY QUIT iTerm (Cmd-Q) and reopen it, and run this script again from the fresh window.
  (Nothing was recorded.)
MSG
  exit 2
fi
echo "[preflight] OK — taps land. Recording the journey."

# --- record + drive ---
xcrun simctl terminate "$UDID" "$BID" >/dev/null 2>&1; sleep 1
activate
xcrun simctl io "$UDID" recordVideo --codec=h264 --force "$MOV" & REC=$!
sleep 2

echo "[1] cold launch -> list"; xcrun simctl launch "$UDID" "$BID" >/dev/null 2>&1; sleep 7

echo "[2] detail + scroll to Cost Breakdown & Scores"
xcrun simctl openurl "$UDID" "govarbitrage://listing/$IMAC" >/dev/null 2>&1; sleep 4
xcrun simctl io "$UDID" screenshot /tmp/ts-01-detail-top.png >/dev/null 2>&1
CX=$(sx 220)
for i in 1 2 3; do
  cliclick -e 450 dd:$CX,$(sy 880) du:$CX,$(sy 240) >/dev/null 2>&1; sleep 1.3
  xcrun simctl io "$UDID" screenshot /tmp/ts-02-detail-scroll$i.png >/dev/null 2>&1
done

echo "[3] settings -> tap Test Connection"
xcrun simctl openurl "$UDID" "govarbitrage://settings" >/dev/null 2>&1; sleep 3
cliclick c:$(sx 220),$(sy 690) >/dev/null 2>&1   # Test Connection button
sleep 3
xcrun simctl io "$UDID" screenshot /tmp/ts-03-connected.png >/dev/null 2>&1

sleep 2
kill -INT "$REC" 2>/dev/null; sleep 3

cp "$MOV" "$HERE/" 2>/dev/null
for f in /tmp/ts-*.png; do [ -e "$f" ] && cp "$f" "$HERE/$(basename "$f" .png)-${ISO}.png"; done
echo "DONE -> $HERE/$(basename "$MOV")  + ts-*-${ISO}.png stills"