import subprocess,time,sys,pathlib
cli,cwd,out,side=sys.argv[1:]
p=pathlib.Path(out); p.mkdir(parents=True,exist_ok=True)
def run(*args):
 r=subprocess.run([cli,*args,'--session','proof40610'],cwd=cwd,text=True,capture_output=True)
 if r.returncode: raise RuntimeError(r.stderr)
 print(r.stdout,flush=True)
run('record','start',str(p/(side+'-cycle.mp4')))
try:
 if side=='before':
  # Base needs this second tap to reach the release step.
  run('find','Take control','click')
  time.sleep(2)
 run('find','Browser tab options','click')
 run('find','Release control','click')
 run('find','role=button label="Close browser"','click')
 run('find','role=button label="View browser"','click')
 time.sleep(12)
 run('find','Take control','click')
 time.sleep(5)
 run('screenshot',str(p/'after-reopen-check.png'))
 time.sleep(.5)
 run('screenshot',str(p/'after-reopen.png'))
 print(run('snapshot','-i'))
finally: run('record','stop')
