browser_check.py

back to table · edit · history · where entries came from · files · download

3484 bytes, as of the version from 2026-11-18 22:18 (current). Recorded here, not run.

"""Inspect exact read-back draft sections through the live public renderer.
No key is sent to the browser or a non-API route. Direct private page access
requires an unavailable login session, so its status is recorded separately.
"""
import os,json,pathlib,urllib.parse,urllib.request,re,sys
sys.path.insert(0,os.getcwd())
from numberdb_app.flatten import to_records
from playwright.sync_api import sync_playwright
w=pathlib.Path(os.environ['TMPDIR']);doc=json.loads((w/'stored-document.json').read_text());doc['Numbers']=to_records(doc)
fontpath=pathlib.Path('/home/ubuntu/.cache/numberdb/inspection-font.woff2')
base={k:doc[k] for k in ['Title','Parameters','Display properties']}
sections=[['Definition','Tags','Keywords'],['Comments'],['Formulas'],['Similar tables'],['References','Links'],['Programs'],['Data properties']]
cases=[]
for i,names in enumerate(sections):
    d={**base,**{n:doc[n] for n in names},'Numbers':doc['Numbers'][:2]}
    cites=set(re.findall(r'CITE\{([^}]+)\}',json.dumps(d)))
    for section in ['References','Links']:
        extra={k:v for k,v in doc[section].items() if k in cites}
        if extra:d[section]={**d.get(section,{}),**extra}
    cases.append(('prose-'+str(i+1),d,names))
for i in range(0,len(doc['Numbers']),14):cases.append(('entries-'+str(i//14+1),{**base,'Numbers':doc['Numbers'][i:i+14]},['Numbers']))
results={'table':'T753','cases':[],'limitations':['The private T753 page returns 404 without a login session. Exact authenticated API read-back content is inspected in live preview sections.','The shared inspection font is applied for this browser environment when available.']}
with sync_playwright() as p:
    browser=p.chromium.launch(headless=True,args=['--no-sandbox','--disable-dev-shm-usage'])
    page=browser.new_page(viewport={'width':1440,'height':1050})
    for label,d,names in cases:
        url='https://numberdb.org/preview?'+urllib.parse.urlencode({'table':json.dumps(d,ensure_ascii=False,separators=(',',':'))})
        response=page.goto(url,wait_until='networkidle',timeout=60000)
        assert response.status==200,(label,response.status,len(url))
        if fontpath.exists():
            page.evaluate("""async bytes=>{const f=new FontFace('NumberDBInspection',new Uint8Array(bytes).buffer);await f.load();document.fonts.add(f);} """,list(fontpath.read_bytes()))
            page.add_style_tag(content="body,body * {font-family:'NumberDBInspection' !important;} .CodeMirror {height:120px !important;}")
        page.wait_for_timeout(300)
        (w/('rendered-'+label+'.html')).write_text(page.content());page.screenshot(path=str(w/('rendered-'+label+'.png')),full_page=True)
        errors=page.locator('.katex-error,merror,.MathJax_Error').all_text_contents()
        r={'case':label,'sections':names,'status':response.status,'url_length':len(url),'math_errors':errors,'screenshot':'rendered-'+label+'.png'}
        if 'Similar tables' in names:
            paths={urllib.parse.urlparse(a).path.rstrip('/') for a in page.locator('a[href]').evaluate_all('(nodes)=>nodes.map(n=>n.href)')}
            r['intended_companion_links']={tid:('/'+tid) in paths for tid in ['T752','T733','T15','T36']}
            assert all(r['intended_companion_links'].values()),r
        assert not errors,r
        assert len(page.inner_text('body'))>400,r
        results['cases'].append(r);print('RENDER',json.dumps(r),flush=True)
    browser.close()
(w/'browser-results.json').write_text(json.dumps(results,indent=2)+'\n')