import pathlib, subprocess, sys, time
session, output = sys.argv[1:]
output=pathlib.Path(output)
output.mkdir(parents=True,exist_ok=False)
def command(*args):
 return subprocess.run(['agent-device',*map(str,args),'--session',session],check=True,capture_output=True,text=True).stdout
def shot(label):
 for i in range(8):
  a=output/f'{label}-settle-{i}-a.png'; b=output/f'{label}-settle-{i}-b.png'
  command('screenshot',a); time.sleep(.5); command('screenshot',b)
  if a.read_bytes()==b.read_bytes():
   (output/f'{label}.png').write_bytes(b.read_bytes()); return
 raise RuntimeError('Screen did not settle')
# Start on the fully loaded completed thread at its end, wrapping disabled.
shot('tall-narrow')
command('record','start',output/'interaction.mp4','--fps','5','--hide-touches')
try:
 command('swipe',200,250,200,730,'--count',15,'--pause-ms',200)
 shot('initial-unwrapped')
 command('swipe',350,420,70,420)
 shot('horizontal-scroll')
 command('press',334,232)
 shot('wrapped')
 command('press',334,232)
 shot('unwrapped-again')
finally:
 command('record','stop')
print(output)
