import subprocess,sys,time,json
from pathlib import Path
session,side,out=sys.argv[1:]
out=Path(out)
def call(*args):
 p=subprocess.run(['agent-device',*map(str,args),'--session',session],capture_output=True,text=True)
 print(' '.join(map(str,args)),p.stdout,p.stderr,flush=True)
 if p.returncode: raise RuntimeError(p.stderr)
 return p.stdout
def shot(label):
 for n in range(4):
  a=out/f'{side}-{label}-a.png'; b=out/f'{side}-{label}-b.png'
  call('screenshot',a);time.sleep(.5);call('screenshot',b)
  if a.read_bytes()==b.read_bytes():return
 raise RuntimeError('Screenshot did not stabilize')
call('record','start',out/f'{side}-home-flow.mp4','--fps','10','--hide-touches')
call('swipe',200,650,200,200,'--count',3)
shot('collapsed-bottom')
call('press','label="3 settled threads"','--settle')
call('swipe',200,650,200,200,'--count',3)
shot('expanded-bottom')
call('press','label="Details"','--settle')
shot('expanded-pill')
call('press','label="Details"','--settle')
shot('recollapsed-pill')
call('record','stop')
