import json,subprocess,sys
from pathlib import Path
root=Path(sys.argv[1]); out=Path(sys.argv[2])
for side in ['base','head']:
    raw=next((root/f'scripts/.e2e/prove/{side}/results').glob('*/attempt-1/screenshots/001-emoji-link.png'))
    nodes=json.loads((root/f'scripts/.e2e/emoji-dom-{side}.json').read_text())
    text='\n\n'.join([f'{"Before" if side=="base" else "After"}: captured DOM inspection (below original app screenshot)', 'External link text nodes read from the running app:', json.dumps(nodes['textNodes'],ensure_ascii=True), 'Lone surrogate nodes: '+json.dumps(nodes['loneSurrogates'],ensure_ascii=True)])
    footer=out/f'{side}-footer.png'
    subprocess.run(['convert','-size','1280x240','xc:#f1f5f9','-font','DejaVu-Sans-Mono','-pointsize','16','-fill','#0f172a','-gravity','NorthWest','-annotate','+20+20',text.replace(chr(92), chr(92)*2),str(footer)],check=True)
    subprocess.run(['convert',str(raw),str(footer),'-append',str(out/f'{side}-dom-inspection.png')],check=True)
