"""Run after seed.py, loading the side's Metro bundle and opening its seeded thread.
Usage: python drive.py SESSION SIDE OUTPUT_DIRECTORY
SIDE is before, after, or full. The full side needs its full-access link paired first.
"""
import pathlib,subprocess,sys,time
session,side,output=sys.argv[1:];out=pathlib.Path(output);out.mkdir(parents=True,exist_ok=True)
def device(*args):
 p=subprocess.run(['agent-device',*args,'--session',session],capture_output=True,text=True,check=True)
 print(p.stdout)
 return p.stdout
device('snapshot','-i')
device('record','start',str(out/(side+'.mp4')))
device('press','label="Save to workspace"','--settle')
if side!='after':
 device('screenshot',str(out/(side+'-dialog.png')))
 device('press','text="Save"','--settle')
device('snapshot','-i')
for attempt in range(5):
 a=out/(side+'-settle.png');b=out/(side+'-result.png')
 device('screenshot',str(a));time.sleep(.5);device('screenshot',str(b))
 if a.read_bytes()==b.read_bytes(): break
device('record','stop')
