from pathlib import Path
import sqlite3,json,hashlib,re,subprocess
r=Path(__file__).parent
for side,sha in [('before','f7b81e1d1115bc7284b39161d3cd6c7f3184eb1c'),('after','79b586f276a23c07b8aecbed2fab677b9f9ed28b')]:
 lines=[f'{side.upper()} | {sha[:12]}','REAL SERVER OUTPUT EXCERPTS + READ-ONLY SQLITE CHECKS','Environment: Linux, Node server, isolated synthetic database','', 'Case A: ledger ends at 069; schema already includes 070-072.']
 for suffix in ('','72'):
  s=side+suffix
  if suffix: lines += ['', 'Case B: same schema; ledger ends at 071 (isolates migration 072).']
  raw=(r/f'{s}.raw.log').read_text()
  if side=='before':
   lines += [v.strip() for v in raw.splitlines() if 'MigrationError:' in v or '[cause]: Error:' in v]
   lines += ['Server process exited with status 1; did not reach Listening on.']
  else:
   migrations=re.search(r'migrations: \[.*?\n  \]',raw,re.S).group(0)
   lines += migrations.splitlines()
   lines += [v for v in raw.splitlines() if 'Listening on ' in v]
  c=sqlite3.connect(f'file:{r/s}/userdata/statev2.sqlite?mode=ro',uri=True)
  f=sqlite3.connect(f'file:{r}/fixture/statev2.sqlite?mode=ro',uri=True)
  rows=c.execute('select * from peer_environment_links order by environment_id').fetchall()
  original=f.execute('select * from peer_environment_links order by environment_id').fetchall()
  lines += ['SQL: max(effect_sql_migrations.migration_id) = '+str(c.execute('select max(migration_id) from effect_sql_migrations').fetchone()[0]),'SQL: synthetic peer row unchanged = '+str(rows==original),'SQL: synthetic peer fenced = '+str(rows[0][-1]),'SQL: PRAGMA integrity_check = '+c.execute('pragma integrity_check').fetchone()[0]]
 lines += ['', 'Seed peer-row SHA256 (same on all four databases):','e5608f8cfe0c2d68527c7c2668c616d626b08fd2efce1f78a7a7d67d4b4a28cc']
 if side=='after': lines += ['', 'Additional observed client checks:', 'CLI project add completed successfully through the running server.', 'Chromium web client paired and loaded the chat view (1280 x 800).']
 (r/f'{side}.txt').write_text('\n'.join(lines)+'\n')
 subprocess.run(['convert','-background','#111827','-fill','#e5e7eb','-font','DejaVu-Sans-Mono','-pointsize','16','-interline-spacing','4','label:'+ (r/f'{side}.txt').read_text(),'-bordercolor','#111827','-border','24',str(r/f'{side}.png')],check=True)
