import subprocess,sys,pathlib,datetime
session,root,side=sys.argv[1:4]
android = len(sys.argv)>4 and sys.argv[4]=='android'
ys = [654,1090,1530,1960] if android else [272,418,565,708]
x = '350' if android else '130'
out=pathlib.Path(root)/datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
out.mkdir(parents=True)
def run(*args):
 p=subprocess.run(['agent-device',*args,'--session',session],text=True,capture_output=True)
 if p.returncode: raise RuntimeError(p.stderr)
 return p.stdout
print(run('record','start',str(out/(side+'-taps.mp4'))),flush=True)
for label,y in zip(['single-line','lf','crlf','blockquote'],ys):
 print(run('press',x,str(y),'--settle'),flush=True)
 snap=run('snapshot','--raw') if android else run('snapshot','-i')
 # Only the synthetic target result is retained in the public driver output.
 target='The file was not found at /home/alice/my report.pdf.'
 print(label, 'target verified' if target in snap else 'no file preview opened',flush=True)
 if 'Could not open preview' in snap:
  print(run('press','label="OK"','--settle'),flush=True)
print(run('record','stop'),flush=True)
print('CAPTURE',out/(side+'-taps.mp4'),flush=True)
