"""Inspect the exact browser downloads; do not infer tags or text from previews.
Run after scripts/benchmark-privacy-proof.mjs:
  python client/public/benchmarks/verify-privacy-results.py
Requires PyMuPDF and Pillow.
"""
from pathlib import Path
import json, hashlib
import fitz
from PIL import Image

folder=Path(__file__).resolve().parent/'2026-09-30'
report_path=folder/'results.json'
report=json.loads(report_path.read_text())
assert len([r for r in report['results'] if r['action']=='compress'])==3
assert any(r['action']=='merge' and r.get('offline') and r.get('status')=='passed' for r in report['results'])
assert len([r for r in report['results'] if r['action']=='split' and r.get('status')=='passed'])==2
assert any(r['action']=='exif' and r.get('offline') for r in report['results'])
assert report['processingRequestsWithBodies']==[]
for fixture in report['fixtures']['fixtures']:
    data=(folder/fixture['name']).read_bytes()
    assert len(data)==fixture['bytes']
    assert hashlib.sha256(data).hexdigest()==fixture['sha256']
for result in report['results']:
    if 'name' not in result: continue
    path=folder/result['name'];data=path.read_bytes()
    assert len(data)==result['bytes']
    assert hashlib.sha256(data).hexdigest()==result['sha256']
    if path.suffix=='.pdf':
        document=fitz.open(path)
        texts=[p.get_text().strip() for p in document]
        result['verification']={'pages':len(document),'textCharacters':sum(len(t) for t in texts),'pageText':texts,'pageDimensions':[[p.rect.width,p.rect.height] for p in document]}
        if result['action']=='merge':
            assert len(document)==2 and 'ALPHA' in texts[0] and 'BETA' in texts[1]
        if result['action']=='split':
            assert len(document)==1
        if result['action']=='compress':
            assert result['bytes']<=result['targetBytes']
            if result['fixture']=='synthetic-scan-four-pages.pdf': assert len(document)==4
            if result['fixture']=='selectable-text.pdf': assert 'ABC-2041' in texts[0]
    elif path.suffix=='.png':
        image=Image.open(path);tags=image.getexif()
        marker=b'SYNTHETIC-METADATA-MARKER'
        result['verification']={'format':image.format,'width':image.width,'height':image.height,'exifTags':len(tags),'gpsPresent':34853 in tags,'sourceCommentMarkerPresent':marker in data,'pngInfoKeys':sorted(image.info.keys())}
        assert image.size==(640,480) and len(tags)==0 and marker not in data
report['verificationEnvironment']={'PyMuPDF':fitz.VersionBind,'Pillow':Image.__version__}
report_path.write_text(json.dumps(report,indent=2)+'\n')
print(json.dumps([{'name':r.get('name'),'verification':r.get('verification'),'status':r.get('status')} for r in report['results']],indent=2))
