from pathlib import Path
from PIL import Image
import json
R=Path(__file__).resolve().parent;F=json.loads((R/'fixtures.json').read_text());records=[]
for p in sorted((R/'captures').glob('*.json')):
 j=json.loads(p.read_text());o=j['observed'];e=j['expected'];profile=j['profile'];row=j['fixture']
 assert o['hero']['href']==o['lower']['href']==e['href'],p
 assert all(x['text']==e['label'] for x in o['labels']),p
 assert o['hint']['text']==e['hint'],p
 assert o['navigator']['maxTouchPoints']==profile['maxTouchPoints'],p
 assert o['navigator']['userAgent']==profile['userAgent'],p
 assert o['devicePixelRatio']==profile['deviceScaleFactor'],p
 assert (o['innerWidth'],o['innerHeight'])==(row['viewport']['width'],row['viewport']['height']),p
 assert o['visualViewport']['scale']==1 and o['scrollY']==o['scrollX']==0,p
 assert o['scrollWidth']==row['viewport']['width'],p
 assert o['media']['reduced'] and o['media']['light']==(row['colorScheme']=='light'),p
 assert o['theme']['colorScheme']=='dark',p
 assert not j['errors'],(p,j['errors'])
 for field in ['hero','lower']:
  icons=[i['class'] for i in o[field]['icons'] if i['display']!='none' and i['visibility']=='visible' and i['rect']['width']>0]
  expectedIcon=None if e['os']=='android' else 'apple' if e['os'] in ['mac','ios',None] else 'windows' if e['os']=='win' else 'linux'
  assert len(icons)==(0 if expectedIcon is None else 1),(p,icons)
  if expectedIcon:assert icons[0].endswith(expectedIcon),(p,icons,expectedIcon)
 for family in ['DM Sans','JetBrains Mono']:assert any(f['family']==family and f['status']=='loaded' for f in o['fonts']),p
 if j['stability']:
  img=Image.open(p.with_suffix('.png'));expectedSize=(o['scrollWidth'],o['scrollHeight']);assert img.size==expectedSize,(p,img.size,expectedSize)
  assert j['stability']['identical'],p
 records.append({'id':j['id'],'side':j['side'],'hero':o['hero']['href'],'lower':o['lower']['href'],'platform':o['platform'],'labels':[x['text'] for x in o['labels']],'hint':o['hint']['text'],'requests':j['releaseRequests'],'cacheReloadAdditionalRequests':j.get('reload',{}).get('additionalReleaseRequests'),'status':'PASS'})
(R/'validation.json').write_text(json.dumps(records,indent=2));print(len(records),'transcripts validated')
