from pathlib import Path import csv,json,hashlib,sys,platform,datetime HERE=Path(__file__).resolve().parent def read_csv(name): with (HERE/'input'/f'{name}.csv').open(encoding='utf-8-sig',newline='') as f:return list(csv.DictReader(f)) def hashfile(path):return hashlib.sha256(path.read_bytes()).hexdigest() def png_chunks(data): import struct,zlib if data[:8]!=b'\x89PNG\r\n\x1a\n':raise ValueError('not PNG') pos=8 while posI',data[pos:pos+4])[0];kind=data[pos+4:pos+8];payload=data[pos+8:pos+8+length];crc=struct.unpack('>I',data[pos+8+length:pos+12+length])[0] if zlib.crc32(kind+payload)&0xffffffff!=crc:raise ValueError('PNG CRC failed') yield kind,payload pos+=length+12 def read_png(path): import struct,zlib chunks=list(png_chunks(path.read_bytes()));head=dict(chunks)[b'IHDR'];w,h,depth,ctype,_,_,interlace=struct.unpack('>IIBBBBB',head) if (depth,ctype,interlace)!=(8,2,0):raise ValueError('fixture must be RGB 8-bit noninterlaced PNG') raw=zlib.decompress(b''.join(v for k,v in chunks if k==b'IDAT'));stride=w*3;prior=bytearray(stride);decoded=bytearray();pos=0 def paeth(a,b,c): p=a+b-c;pa,pb,pc=abs(p-a),abs(p-b),abs(p-c) return a if pa<=pb and pa<=pc else b if pb<=pc else c for y in range(h): filt=raw[pos];row=bytearray(raw[pos+1:pos+1+stride]);pos+=stride+1 for x in range(stride): a=row[x-3] if x>=3 else 0;b=prior[x];c=prior[x-3] if x>=3 else 0 if filt==1:row[x]=(row[x]+a)&255 elif filt==2:row[x]=(row[x]+b)&255 elif filt==3:row[x]=(row[x]+((a+b)//2))&255 elif filt==4:row[x]=(row[x]+paeth(a,b,c))&255 elif filt!=0:raise ValueError('unsupported PNG filter') decoded.extend(row);prior=row return w,h,bytes(decoded) import xml.etree.ElementTree as ET source=ET.parse(HERE/'images/source.svg');shared=ET.parse(HERE/'images/shared.svg') texts=lambda tree:' '.join((n.text or '') for n in tree.iter() if n.tag.endswith('text')) src_text=texts(source);out_text=texts(shared) w,h,src=read_png(HERE/'images/source.png');w2,h2,out=read_png(HERE/'images/shared.png') mask=(40,45,530,155);x0,y0,x1,y1=mask black=sum(out[(y*w+x)*3:(y*w+x)*3+3]==b'\x00\x00\x00' for y in range(y0,y1) for x in range(x0,x1)) region=(x1-x0)*(y1-y0);changed=sum(src[(y*w+x)*3:(y*w+x)*3+3]!=out[(y*w+x)*3:(y*w+x)*3+3] for y in range(y0,y1) for x in range(x0,x1)) chunks=lambda path:[t.decode('ascii') for t,_ in png_chunks(path.read_bytes())] actual={'source_has_fixture_contact':all(t in src_text for t in ['TEST KIM','010-0000-0000']),'shared_has_contact':any(t in out_text for t in ['TEST KIM','010-0000-0000']),'dimensions_match':(w,h)==(w2,h2),'mask_pixels':region,'black_mask_pixels':black,'mask_changed':changed>0,'outside_pixel_preserved':src[(220*w+700)*3:(220*w+700)*3+3]==out[(220*w+700)*3:(220*w+700)*3+3],'shared_metadata_chunks':[t for t in chunks(HERE/'images/shared.png') if t in ['tEXt','iTXt','zTXt','eXIf']],'original_hash_matches_manifest':hashfile(HERE/'images/source.png')==json.loads((HERE/'input/source-hash.json').read_text())['sha256'],'marked_is_separate_file':hashfile(HERE/'images/marked.svg')!=hashfile(HERE/'images/shared.svg')} import datetime as _lab_clock expected=json.loads((HERE/'expected/report.json').read_text()) checks=[{'field':k,'pass':actual.get(k)==v,'actual':actual.get(k),'expected':v} for k,v in expected.items()] passed=all(x['pass'] for x in checks) and set(actual)==set(expected) report={'lab_id':HERE.name,'synthetic_fixture':True,'environment':{'python':platform.python_version(),'platform':platform.system(),'processor':'Python standard library; PNG rendering only uses ImageMagick during artifact preparation'},'run_utc':_lab_clock.datetime.now(_lab_clock.timezone.utc).isoformat(),'status':'PASS' if passed else 'FAIL','assertion_count':len(checks),'actual':actual,'assertions':checks,'limits':'Synthetic test data only; not a real building inspection, legal/safety verdict, or Excel application execution.'} (HERE/'results.json').write_text(json.dumps(report,ensure_ascii=False,indent=2)) print(json.dumps(report,ensure_ascii=False,indent=2)) if not passed:raise SystemExit(1)