import subprocess,sys,time,json,pathlib
root=pathlib.Path(__file__).parent
side,checkout=sys.argv[1:]
out=root/(side+'-recorded')
cast=root/(side+'.cast')
start=time.monotonic()
with cast.open('w') as f:
 f.write(json.dumps({'version':2,'width':120,'height':24,'title':side+' — real server RPC reproduction'})+'\n')
 p=subprocess.Popen(['node',str(root/'driver.mjs'),checkout,str(out),side],stdout=subprocess.PIPE,stderr=subprocess.STDOUT,text=True)
 lines=[]
 for line in p.stdout:
  lines.append(line);sys.stdout.write(line);sys.stdout.flush()
  f.write(json.dumps([round(time.monotonic()-start,3),'o',line.replace('\n','\r\n')])+'\n');f.flush()
 code=p.wait()
(root/(side+'.txt')).write_text(''.join(lines))
sys.exit(code)
