#!/usr/bin/env bash
# Run separately for each commit, from its installed checkout.
# Arguments: fresh T3 home, synthetic git workspace, reachable proxy origin, device session.
# Start dev:server with T3CODE_DEV_SEED=0, start Metro from this checkout,
# and run proxy.mjs <backend-port> <proxy-port> first.
# Install this checkout using mobile:install; open Metro with disableAutoLaunch=1&disableFab=1.
set -euo pipefail
home_dir=$1
workspace=$2
proxy_origin=$3
session=$4
output_dir=$5/$(date -u +%Y%m%dT%H%M%SZ)
mkdir -p "$output_dir"
node apps/server/src/bin.ts project add "$workspace" --title 'Upload proof' --base-dir "$home_dir"
node apps/server/src/bin.ts thread start "$workspace" 'Upload retry proof' --title 'Document upload' --provider devReplay --model multi-turn --wait --json --base-dir "$home_dir"
# Pair with pair-client.sh using proxy_origin; download proxy_origin/sample.pdf
# in Chrome once, using no account. Cancel the offer to open the downloaded PDF.
# Open Document upload in T3 Code, then drive the same steps on each side:
agent-device press 'label="Ask anything"' --session "$session" --settle
agent-device press 'label="Add attachment"' --session "$session" --settle
agent-device press 'text="Choose Files"' --session "$session" --settle
agent-device wait 'id="com.google.android.documentsui:id/item_root"' 10000 --session "$session"
# The fixture is the only recent file. Android's multi-select picker uses longpress + Select.
agent-device longpress 'id="com.google.android.documentsui:id/item_root"' 1000 --session "$session" --settle
agent-device press 'label="Select"' --session "$session" --settle
agent-device wait 'label="Retry uploading upload-proof.pdf"' 20000 --session "$session"
agent-device record start "$output_dir/retry.mp4" --hide-touches --session "$session"
agent-device press 'label="Retry uploading upload-proof.pdf"' --session "$session" --settle
agent-device screenshot "$output_dir/retrying.png" --session "$session"
# Observe progress and the proxy's HTTP 204, then the Send button and surviving inline chip.
agent-device wait 'label="Send"' 100000 --session "$session"
agent-device record stop --session "$session"
# Public evidence is cropped identically on both sides to remove the machine label:
ffmpeg -loglevel error -i "$output_dir/retry.mp4" -vf 'crop=1080:2204:0:220,scale=540:1102' -c:v libx264 -crf 24 -an "$output_dir/public-retry.mp4"
