#!/usr/bin/env python3
from pathlib import Path
import subprocess,json,sys
root=Path('/tmp/lead-28684-r1/evidence-capture')
side=sys.argv[1]
scenarios=sys.argv[2:] or ['collapsed-light','expanded-light','mobile-dark','short-dark','floating-dark','switch-dark']
previous=root/f'{side}-final-runs.json'
results=json.loads(previous.read_text()) if previous.exists() else []
for scenario in scenarios:
 log=root/f'{side}-{scenario}-final.log'
 with log.open('w') as out:
  p=subprocess.run(['node',str(root/'capture-driver.mjs'),side,scenario,'10'],stdout=out,stderr=subprocess.STDOUT)
 print(side,scenario,p.returncode,flush=True)
 if p.returncode: print(log.read_text()[-6000:],flush=True);sys.exit(p.returncode)
 line=log.read_text().splitlines()[-1];results.append(json.loads(line))
 (root/f'{side}-final-runs.json').write_text(json.dumps(results,indent=2))
