import subprocess,time,json,sys,pathlib
side=sys.argv[1]
root=pathlib.Path('/tmp/lead-33512-r1/capture')
log=open(root/(side+'-r01-actions.txt'),'w',buffering=1)
t0=time.monotonic()
def run(*args):
    started=time.monotonic()-t0
    p=subprocess.run(['agent-device',*args,'--session','lead-33512-cap'],cwd='/home/ubuntu/wt/pr-33512',text=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    log.write(f'{started:.2f}s: agent-device '+repr(args)+f' [exit {p.returncode}]\n'+p.stdout+'\n')
    print(f'{started:.2f}s',args[0],p.returncode,flush=True)
    if p.returncode: raise RuntimeError(p.stdout)
    return p.stdout
run('record','start',str(root/(side+'-r01-raw.mp4')),'--hide-touches')
run('snapshot','-i')
time.sleep(1)
run('press','role="text-view"','--settle')
run('type','Draft')
run('snapshot','-i')
time.sleep(1)
run('type','\nSecond line for the iPad composer.')
run('snapshot','-i')
time.sleep(1)
run('press','label="Send"','--settle')
run('wait','text','Second line for the iPad composer.','20000')
run('snapshot','-i')
time.sleep(2)
run('keyboard','dismiss')
run('snapshot','-i')
time.sleep(2)
run('record','stop')
