import subprocess,sys,time,pathlib,datetime
from PIL import Image,ImageChops
side=sys.argv[1]; session=sys.argv[2]; root=pathlib.Path(sys.argv[3]); out=root/(side+'-'+datetime.datetime.now().strftime('%Y%m%d-%H%M%S'));out.mkdir()
def run(*args):
 p=subprocess.run(['agent-device',*args,'--session',session],text=True,capture_output=True); print(p.stdout);p.check_returncode();return p.stdout
def stable(label):
 for n in range(4):
  a=out/(label+'-check.png'); b=out/(label+'.png')
  run('screenshot',str(a));time.sleep(.5);run('screenshot',str(b))
  if ImageChops.difference(Image.open(a).convert('RGB'),Image.open(b).convert('RGB')).getbbox() is None:return
 raise RuntimeError('screenshots did not stabilize')
# Start on the fixture MR Overview in portrait, light theme, Pixel 9 Android API 36.
run('record','start',str(out/'flow.mp4'))
run('press','label="Activity"','--settle')
run('swipe','970','1170','180','1170')
run('swipe','970','1170','180','1170')
stable('collapsed')
run('press','label="Show more"','--settle')
run('swipe','970','1170','180','1170')
run('swipe','970','1170','180','1170')
stable('expanded')
run('record','stop')
print('CAPTURE_DIR',out)
