from pathlib import Path
import subprocess,re,asyncio
root=Path(__file__).parent
async def run(side,up,port):
 log=(root/f'{side}-run.log').open('w')
 log.write(f'{side}: real T3 server; fresh database; Node silent client; TCP proxy upstream idle timeout 60000ms\n');log.flush()
 proxy=subprocess.Popen(['node',str(root/'idleproxy.mjs'),str(port),str(up),'60000'],stdout=log,stderr=log)
 try:
  await asyncio.sleep(1)
  token=re.search(r'Token: (\w+)',(root/f'{side}-server.log').read_text())[1]
  client=await asyncio.create_subprocess_exec('node',str(root/'silentclient.mjs'),f'http://127.0.0.1:{port}',token,stdout=log,stderr=log)
  await client.wait()
 finally:
  proxy.terminate();proxy.wait();log.close()
async def main(): await asyncio.gather(run('base',47927,47928),run('head',47929,47930))
asyncio.run(main())
