Files
lmxopcua/docs/ui-consistency/evidence/ot13-scripts-dialog/browser.py
T

61 lines
6.9 KiB
Python

"""Read-only actual Scripts page: synthetic EF database, no application writes or external services."""
import hashlib,json,sys
from pathlib import Path
from urllib.parse import urlsplit
from playwright.sync_api import sync_playwright,expect
sys.path.insert(0,'/Users/dohertj2/Desktop/ignitionoee/docs/ui-validation/router-canary')
from browser_zoom import zoom_browser,set_browser_zoom
out=Path(__file__).parent;origin=sys.argv[1];result={'origin':origin,'scenes':[]}
with sync_playwright() as p:
for mode,width,zoom in [('desktop',1440,1),('phone',390,1),('real200',1440,2)]:
with zoom_browser(p,width=width,height=900) as(ctx,worker):
page=ctx.new_page();page.set_default_timeout(12000);errors=[];external=[];checks=[]
page.on('pageerror',lambda e:errors.append(str(e)))
def route(req):
if urlsplit(req.request.url).hostname not in ['127.0.0.1','localhost']:external.append(urlsplit(req.request.url).hostname);req.abort()
else:req.continue_()
page.route('**/*',route);page.goto(origin);expect(page.locator('#ready')).to_have_text('ready');expect(page.locator('tbody tr')).to_have_count(2);factor=set_browser_zoom(worker,page,zoom)
session=page.locator('#session').inner_text();modal=page.locator('[role=dialog]')
def source(name):return page.locator('tbody tr').filter(has_text=name).get_by_role('button',name='Source',exact=True)
def state():return page.request.get(origin+'/fixture/state/'+session).json()
def check(name,value):checks.append({'name':name,'passed':bool(value)});assert value,name
def opened():expect(modal).to_be_visible();expect(modal.locator('[data-initial-focus]')).to_be_focused()
def release(outcome):assert page.request.post(origin+'/fixture/release/'+session+'/'+outcome).ok
provenance=page.request.get(origin+'/fixture/provenance').json()
check('List retains source hash without loading source code',provenance['alphaSha256'][:12] in page.locator('tbody tr').filter(has_text='Alpha').inner_text() and state()['factoryCalls']==1)
source('Alpha').click();opened();pre=modal.get_by_role('region',name='Source for Alpha',exact=True);expect(pre).to_be_visible()
check('Named read-only dialog',modal.evaluate("n=>document.getElementById(n.getAttribute('aria-labelledby')).textContent.includes('Alpha')") and modal.get_attribute('aria-modal')=='true')
check('Full source rendered literally without executable markup',pre.inner_text().startswith('// <script>window.sourceExecuted=true;</script>') and 'return 1;' in pre.inner_text() and page.evaluate('window.sourceExecuted===undefined') and pre.locator('script').count()==0)
page.locator('#outside').evaluate('n=>n.focus()');check('Outside focus denied',modal.evaluate('n=>n.contains(document.activeElement)'))
for _ in range(12):page.keyboard.press('Tab');assert modal.evaluate('n=>n.contains(document.activeElement)')
check('Twelve real Tab steps remain inside',True)
geometry=modal.locator('.modal-content').evaluate('n=>{const r=n.getBoundingClientRect();return {left:r.left,right:r.right,innerWidth,innerHeight,dpr:devicePixelRatio,scrollWidth:document.documentElement.scrollWidth}}')
check('No page/modal horizontal overflow',geometry['left']>=-1 and geometry['right']<=geometry['innerWidth']+1 and geometry['scrollWidth']<=geometry['innerWidth']+1)
check('Header/footer stay inside viewport',all(modal.locator(selector).evaluate('n=>{const r=n.getBoundingClientRect();return r.top>=-1&&r.bottom<=innerHeight+1}') for selector in ['.modal-header','.modal-footer']))
page.evaluate('document.fonts.ready');page.screenshot(path=str(out/(mode+'-source.png')))
pre.focus();expect(pre).to_be_focused();pre.press('ArrowRight');page.wait_for_function('document.querySelector("pre").scrollLeft>0')
check('Long source is keyboard horizontally scrollable',pre.evaluate('n=>n.scrollLeft>0'))
pre.press('PageDown');page.wait_for_function('document.querySelector(".modal-body").scrollTop>0')
check('Long source is keyboard vertically scrollable',modal.locator('.modal-body').evaluate('n=>n.scrollTop>0'))
page.keyboard.press('Escape');expect(modal).to_have_count(0);expect(source('Alpha')).to_be_focused();check('Escape closes read-only source and returns exact trigger',True)
page.locator('#fail').click();source('Beta').click();opened();expect(modal.get_by_role('alert')).to_contain_text('Synthetic source read failure')
errorrect=modal.get_by_role('alert').evaluate('n=>{const r=n.getBoundingClientRect();return {top:r.top,bottom:r.bottom,innerHeight}}')
check('Long contextual title and header close remain inside frame',modal.locator('.modal-title').evaluate('n=>n.scrollWidth<=n.clientWidth+1') and modal.locator('.btn-close').evaluate('n=>{const r=n.getBoundingClientRect();const m=n.closest(".modal-content").getBoundingClientRect();return r.left>=m.left-1&&r.right<=m.right+1}'))
check('Current read failure stays visible and closable',errorrect['top']>=-1 and errorrect['bottom']<=errorrect['innerHeight']+1 and page.locator('[data-testid=page-error-boundary]').count()==0)
page.screenshot(path=str(out/(mode+'-error.png')));modal.locator('[data-initial-focus]').click();expect(modal).to_have_count(0);expect(source('Beta')).to_be_focused()
page.locator('#hold').click();source('Alpha').click();opened();expect(modal.get_by_role('status')).to_have_text('Loading source…')
check('Read-only loading can close without cancelling the read',modal.locator('[data-initial-focus]').is_enabled() and state()['pending'])
page.keyboard.press('Escape');expect(modal).to_have_count(0);expect(source('Alpha')).to_be_focused();source('Beta').click();opened();expect(modal.locator('pre')).to_have_text('return 2;')
release('failure' if mode=='phone' else 'success');page.wait_for_timeout(150)
check('Old read success/failure cannot overwrite reopened source',modal.locator('pre').inner_text()=='return 2;' and 'Synthetic source read failure' not in modal.inner_text())
modal.locator('[data-initial-focus]').click();expect(modal).to_have_count(0)
page.locator('#hold').click();source('Alpha').click();opened();expect(modal.get_by_role('status')).to_have_text('Loading source…')
assert page.request.post(origin+'/fixture/remove-page/'+session).ok;expect(modal).to_have_count(0);expect(page.locator('tbody tr')).to_have_count(0)
release('success');page.wait_for_timeout(150)
check('Page disposal retires pending read without orphan overlay',modal.count()==0 and page.locator('.modal-backdrop').count()==0 and page.locator('[data-testid=page-error-boundary]').count()==0)
check('No application write attempts',state()['forbiddenWrites']==0)
check('No page errors or external hosts',errors==[] and external==[])
result['scenes'].append({'mode':mode,'zoom':factor,'geometry':geometry,'errorGeometry':errorrect,'checks':checks,'errors':errors,'externalHosts':external,'state':state()})
(out/'browser-results.json').write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps({'scenes':len(result['scenes']),'checks':sum(len(s['checks']) for s in result['scenes'])}))