"""Drive the reported UI flow on a paired Pixel 9 (1080x2424, light theme).
Run seed.py separately for each checkout, launch its own server and Metro,
install with mobile:install, pair, then run from the agent-device session's cwd.
Usage: python3 reproduce.py SESSION OUTPUT_DIRECTORY
"""
import pathlib, subprocess, sys, time
from PIL import Image, ImageChops
session, output = sys.argv[1:]
output = pathlib.Path(output)
output.mkdir(parents=True, exist_ok=True)
def command(*args):
 return subprocess.run(['agent-device', *args, '--session', session], check=True, capture_output=True, text=True).stdout
def stable(label):
 previous = output / (label+'-settle.png')
 final = output / (label+'.png')
 command('screenshot',str(previous))
 for _ in range(12):
  time.sleep(.5)
  command('screenshot',str(final))
  if ImageChops.difference(Image.open(previous),Image.open(final)).getbbox() is None:
   return
  previous.write_bytes(final.read_bytes())
 raise RuntimeError('Screen did not stabilize: '+label)
command('click','role=button label="Review blank lines"')
command('click','role=button label="Git controls"')
command('record','start',str(output/'review-flow.mp4'),'--hide-touches')
# The Android native Git menu combines its title and subtitle in the tree.
command('click','615','620')
stable('review-open')
command('longpress','400','574','1000')
command('click','400','688')
# Selection action is at the bottom of the native review screen.
command('click','472','2258')
command('fill','role=text-field text="Leave a comment..."','Range comment')
command('click','role=button label="Comment"')
command('click','400','688')
command('fill','role=text-field text="Leave a comment..."','Blank only')
command('click','role=button label="Comment"')
command('click','role=button label="Navigate up"')
command('click','role=button label="Send"')
stable('sent')
command('click','600','1678')
stable('range-card')
command('click','role=button label="Navigate up"')
command('click','907','1678')
stable('blank-card')
print(command('record','stop'))
