"""Shared offline/live predicates: facts precede scientific classification. CONTRACT: full RGB comparisons have no mask/tolerance; grabs require real logs. INVARIANT: dismissal, client destruction, clipping and layout remain distinct. """ import re from PIL import Image,ImageChops COLORS=dict(TOPLEVEL=(35,68,204),POPUP_A=(224,192,32),POPUP_B=(32,208,160)) class Defect(RuntimeError): """A directly measured scientific contract failure, distinct from fixture ambiguity.""" def paint(im,target): im=im.convert('RGB');assert im.size==(800,600) mask=Image.new('L',im.size);mask.putdata([255 if p==COLORS[target] else 0 for p in im.get_flattened_data()]) return dict(count=sum(mask.histogram()[1:]),bbox=mask.getbbox()) def point(im,target,origin): box=paint(im,target)['bbox'];assert box,('no measured paint',target) # WHY: A may be occluded by B; bbox center alone is not a valid hit point. x1,y1,x2,y2=box for y in range(y1+10,y2-10,5): for x in range(x1+10,x2-10,5): if all(im.getpixel((xx,yy))==COLORS[target] for yy in range(y-8,y+9) for xx in range(x-8,x+9)): return [x+origin,y] raise AssertionError('no solid measured interior '+target) def equal(a,b): assert a.size==b.size==(800,600) rows=[(i%800,i//800,x,y) for i,(x,y) in enumerate(zip(a.get_flattened_data(),b.get_flattened_data())) if x!=y] return dict(equal=not rows,changed=len(rows),bbox=ImageChops.difference(a,b).getbbox(),pixels=rows) def reference(a,b): result=equal(a,b) assert result['equal'] and all(c==(48,48,56) for c in a.get_flattened_data()),'unstable reference' return result def configured(text,name): rows=re.findall(rf'{name} ROLE_CONFIGURE x=(-?\d+) y=(-?\d+) w=(\d+) h=(\d+)',text) assert rows,(name,'no configure') return list(map(int,rows[-1])) def geometry(text,origin): a=configured(text,'POPUP_A');b=configured(text,'POPUP_B') A=[origin[0]+a[0],origin[1]+a[1],a[2],a[3]] B=[A[0]+b[0],A[1]+b[1],b[2],b[3]] assert all(r[0]<0=2 assert request.start() POPUP_A -> POPUP_B',status='PASS') def pair(lines,target): events=[] for line in lines: m=re.fullmatch(r'POINTER_BUTTON target=(\w+) serial=(\d+) button=(\d+) state=(\d+)',line);assert m events.append([m[1],*map(int,m.groups()[1:])]) if len(events)!=2 or any(e[0]!=target for e in events) or [e[2:] for e in events]!=[[273,1],[273,0]]: raise Defect('wrong actual button recipient/pair: '+str(events)) assert 00,'permitted popup color absent' if mosaic and result['H2'][name]['count']: raise Defect('forbidden popup pixels '+name) if not mosaic and not result['H2'][name]['count']: raise AssertionError('standard popup continuation absent '+name) # CONTRACT: compare the exact color region against configured geometry, # subtracting B's occlusion of A, not against a predicted painted bbox. for side,im,ox in [('H1',left,-800),('H2',right,0)]: for y in range(600): for x in range(800): gx=x+ox inside=rect[0]<=gx