Files
lmxopcua/docs/ui-consistency/evidence/ot13-equipment-caller/browser.py
T

66 lines
10 KiB
Python

"""Composed Equipment caller/child proof; every write goes only to a deferred synthetic adapter."""
import base64,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];results={'origin':origin,'scenes':[],'voiceOver':'Excluded by explicit user instruction; no speech-output claim'}
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(15000);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('h1')).to_have_text('equipment-a');factor=set_browser_zoom(worker,page,zoom)
session=page.locator('#session').inner_text();modal=page.locator('[role=dialog]')
def state():return page.request.get(origin+'/fixture/state/'+session).json()
def command(path):assert page.request.post(origin+'/fixture/'+path.replace('{id}',session)).ok
def release(outcome='success'):command('release/{id}/'+outcome)
def check(name,value):checks.append({'name':name,'passed':bool(value)});assert value,name
def tab(name):page.locator('.nav-tabs').get_by_role('button',name=name,exact=True).click()
def opened():expect(modal).to_be_visible();expect(modal.locator('[data-initial-focus]')).to_be_focused()
def retry():page.get_by_role('button',name='Retry view load',exact=True).click()
check('Inactive child tabs remain lazy',not any(any(name in read for name in ['LoadReferencesForEquipmentAsync','LoadVirtualTagsForEquipmentAsync','LoadAlarmsForEquipmentAsync']) for read in state()['reads']))
for field in page.locator('form input:not([type=hidden]),form select,form textarea').all():
id=field.get_attribute('id');check('Details field has visible associated label '+str(id),bool(id) and page.locator('label[for="'+id+'"]').count()==1)
check('Details descriptions resolve '+str(id),field.evaluate("n=>(n.getAttribute('aria-describedby')||'').split(/\\s+/).filter(Boolean).every(id=>!!document.getElementById(id))"))
if not field.is_disabled() and field.get_attribute('type')!='checkbox':page.locator('label[for="'+id+'"]').click();expect(field).to_be_focused()
name=page.get_by_label('Name',exact=True);name.fill('');page.get_by_role('button',name='Save changes',exact=True).click();expect(name).to_be_focused();expect(name).to_have_attribute('aria-invalid','true')
check('Invalid required name sends no mutation',state()['operations']==[])
name.fill('equipment-a-updated');year=page.get_by_label('Year of construction',exact=True);year.fill('40000');page.get_by_role('button',name='Save changes',exact=True).click();expect(year).to_be_focused();expect(year).to_have_attribute('aria-invalid','true')
check('Existing short parser error is visible and named',page.locator('[id$=eq-year-error]').inner_text().find('Year of construction')>=0 and state()['operations']==[])
year.fill('2021');page.get_by_role('button',name='Save changes',exact=True).click();expect(name).to_be_disabled()
command('fail-read/{id}/details');release();expect(page.get_by_role('button',name='Retry view load',exact=True)).to_be_visible()
check('Confirmed details write keeps draft and blocks replay on read failure',name.input_value()=='equipment-a-updated' and page.get_by_role('button',name='Save changes',exact=True).is_disabled() and len(state()['operations'])==1)
first=state()['operations'][0];check('Details submitted target and optimistic version retained',first['method']=='UpdateEquipmentAsync' and first['args'][0]=='A' and first['args'][1]['name']=='equipment-a-updated' and first['args'][1]['yearOfConstruction']==2021 and base64.b64decode(first['args'][2])==bytes([1,2]))
page.screenshot(path=str(out/(mode+'-details-recovery.png')));retry();expect(page.get_by_role('button',name='Save changes',exact=True)).to_be_enabled();expect(page.locator('h1')).to_have_text('equipment-a-updated');check('Details Retry is read-only',len(state()['operations'])==1)
tab('Tags');expect(page.locator('tbody tr')).to_have_count(2);row=page.locator('tbody tr').first;override=row.locator('input')
check('Override composite accessible name uses visible row and column',all(text in override.aria_snapshot() for text in ['Display-name override','A-ref-1']))
override.fill('name-override');override.press('Tab');row.get_by_role('button',name='Save name',exact=True).click();expect(row.get_by_role('button',name='Save name',exact=True)).to_be_disabled();release();expect(page.locator('tbody tr').first.locator('th')).to_have_text('name-override');check('Override action targets only its reference',state()['operations'][-1]['args'][0]=='A-ref-1' and state()['operations'][-1]['args'][1]=='name-override')
row=page.locator('tbody tr').first;row.get_by_role('button',name='Remove',exact=True).click();opened();check('Remove requires explicit safe confirmation',len(state()['operations'])==2 and modal.locator('[data-initial-focus]').inner_text().strip()=='Cancel')
page.keyboard.press('Escape');expect(modal).to_have_count(0);check('Cancel does not mutate',len(state()['operations'])==2)
row.get_by_role('button',name='Remove',exact=True).click();opened();modal.locator('.btn-danger').click();expect(modal).to_have_attribute('aria-busy','true');page.wait_for_function("document.querySelector('[role=dialog]')?.contains(document.activeElement)")
command('fail-read/{id}/references');release();expect(modal).to_have_count(0);expect(page.get_by_role('button',name='Retry view load',exact=True)).to_be_visible()
check('Confirmed remove failure-to-refresh retains last-good rows and blocks replay',page.locator('tbody tr').count()==2 and page.get_by_role('button',name='+ Add reference',exact=True).is_disabled() and len(state()['operations'])==3)
retry();expect(page.locator('tbody tr')).to_have_count(1);check('Reference retry only reloads view',len(state()['operations'])==3)
page.get_by_role('button',name='+ Add reference',exact=True).click();opened();modal.get_by_role('checkbox',name='Synthetic raw tag A',exact=True).check();modal.locator('.modal-footer .btn-primary').click();expect(modal).to_have_attribute('aria-busy','true');release();expect(modal).to_have_count(0)
check('Composed AddReference saves captured equipment',state()['operations'][-1]['method']=='AddReferencesAsync' and state()['operations'][-1]['args'][0]=='A' and state()['operations'][-1]['args'][1]==['tag-A'])
tab('Virtual Tags');expect(page.locator('tbody tr')).to_have_count(1);command('hold-read/{id}/LoadVirtualTagAsync/A-vtag');page.locator('tbody tr').get_by_role('button',name='Edit',exact=True).click();command('route/{id}/B');expect(page.locator('h1')).to_have_text('equipment-b');expect(page.locator('tbody')).to_contain_text('virtual-tag-B')
page.get_by_role('button',name='Add virtual tag',exact=True).click();opened();command('release-read/{id}/success');page.wait_for_timeout(150)
check('Late A edit cannot open over B create',modal.locator('.modal-title').inner_text().strip()=='New virtual tag' and modal.get_by_label('VirtualTagId',exact=True).input_value()=='')
modal.locator('[data-initial-focus]').click();expect(modal).to_have_count(0);page.get_by_role('button',name='Add virtual tag',exact=True).click();opened();modal.get_by_label('VirtualTagId',exact=True).fill('b-new-virtual');modal.get_by_label('Name',exact=True).fill('virtual-new');modal.get_by_label('Script',exact=True).select_option('S1');modal.locator('.modal-footer .btn-primary').click();expect(modal).to_have_attribute('aria-busy','true');command('fail-read/{id}/virtual');release();expect(modal).to_have_count(0);expect(page.get_by_role('button',name='Retry view load',exact=True)).to_be_visible()
check('Composed virtual save stays B and refresh failure does not replay',state()['operations'][-1]['method']=='CreateVirtualTagAsync' and state()['operations'][-1]['args'][0]=='B' and page.get_by_role('button',name='Add virtual tag',exact=True).is_disabled())
retry();expect(page.get_by_role('button',name='Add virtual tag',exact=True)).to_be_enabled();check('Virtual Retry is read-only',len(state()['operations'])==5)
tab('Alarms');page.locator('tbody tr').get_by_role('button',name='Edit',exact=True).click();opened();modal.get_by_label('Name',exact=True).fill('alarm-updated');modal.locator('.modal-footer .btn-primary').click();expect(modal).to_have_attribute('aria-busy','true');command('hold-read/{id}/LoadAlarmsForEquipmentAsync/B');release();expect(modal).to_have_count(0)
command('route/{id}/A');expect(page.locator('h1')).to_have_text('equipment-a-updated');expect(page.locator('tbody')).to_contain_text('alarm-A');page.get_by_role('button',name='Add alarm',exact=True).click();expect(modal).to_be_visible();command('release-read/{id}/success');expect(modal.locator('.modal-footer .btn-primary')).to_be_enabled()
check('Saved B callback already reading cannot close new A alarm',modal.locator('.modal-title').inner_text().strip()=='New scripted alarm' and modal.get_by_label('ScriptedAlarmId',exact=True).input_value()=='' and page.locator('tbody').inner_text().find('alarm-A')>=0)
check('Alarm write uses captured row target',state()['operations'][-1]['method']=='UpdateScriptedAlarmAsync' and state()['operations'][-1]['args'][0]=='B-alarm')
page.screenshot(path=str(out/(mode+'-composed-alarm.png')));modal.locator('[data-initial-focus]').click();expect(modal).to_have_count(0)
check('Exactly six synthetic writes, no operational calls',len(state()['operations'])==6 and state()['forbidden']==0)
check('No document overflow/page errors/external hosts',page.evaluate('document.documentElement.scrollWidth<=innerWidth+1') and errors==[] and external==[])
results['scenes'].append({'mode':mode,'zoom':factor,'geometry':page.evaluate('({innerWidth,innerHeight,dpr:devicePixelRatio,scrollWidth:document.documentElement.scrollWidth})'),'checks':checks,'errors':errors,'externalHosts':external,'state':state()});(out/'browser-results.json').write_text(json.dumps(results,indent=2)+'\n')
print(json.dumps({'scenes':len(results['scenes']),'checks':sum(len(s['checks']) for s in results['scenes'])}))