Files
WoW-Hybrid-gateway/tests/booty_bay_shore_bc1_test.py

49 lines
2.9 KiB
Python

"""Gate the actual emitted BC1 tile, pinned inputs, and isolated physical inverse."""
import hashlib, json, os, sys
from pathlib import Path
import numpy as np
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
import booty_bay_shore as m
from content_candidate_rehearsal import rehearse
def main():
v=os.environ.get('GW_BOOTY_BAY_SHORE_BC1_ROOT')
if not v or not Path(v).is_dir():
print('SKIP: GW_BOOTY_BAY_SHORE_BC1_ROOT not set (staged artifact lives outside the repo)'); sys.exit(77)
r=Path(v)
load=lambda p:json.loads(p.read_text())
digest=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
for path,sha in load(r/'input-pins.json').items():assert digest(Path(path))==sha
selected,held=m.classify(r)
assert selected.sum()==1178 and held.sum()==74 and not (selected&held).any()
parent=(r/'inputs/parent.blp').read_bytes();native=(r/'inputs/native.blp').read_bytes()
candidate=(r/'candidate/map30_58.blp').read_bytes()
measured=m.verify(parent,native,candidate,selected,held)
report=load(r/'report.json');assert all(report[k]==v for k,v in measured.items())
assert measured['selected_pixels_changed']==1178 and measured['touched_blocks']==106
coords=load(r/'preserved-74.json')['coordinates']
assert {(v['y'],v['x']) for v in coords}==set(map(tuple,np.argwhere(held)))
offset=m.base.opaque_bc1(parent,512)
# A damaged untouched block, header, or missing edit must fail the gate.
by,bx=np.argwhere(~selected.reshape(128,4,128,4).any((1,3)))[0]
mutations=[]
bad=bytearray(candidate);bad[offset+(int(by)*128+int(bx))*8]^=1;mutations.append(bytes(bad))
bad=bytearray(candidate);bad[20]^=1;mutations.append(bytes(bad));mutations.append(parent)
for bad in mutations:
try:m.verify(parent,native,bad,selected,held)
except ValueError:pass
else:raise AssertionError('invalid candidate accepted')
rows=load(r/'candidate-assets.json');assert len(rows)==1 and rows[0]['file_data_id']==8704453
assert digest(r/'candidate/map30_58.blp')==rows[0]['after_sha256']
core=load(r/'content-step/candidate/core.json');assert core['complete_root_inverse']
assert {v['file_data_id'] for v in core['assets']}=={8704453}
assert len(core['components'])==1 and core['components'][0]['type']=='root_selector'
frozen=Path(load(r/'current-content-parent-path.json')[0])
before=(frozen/'decoded/root.bin').read_bytes();after=(r/'content-step/candidate/decoded/root.bin').read_bytes()
component=load(r/'content-step/8704453.json')
old=bytes.fromhex(component['original_decoded']['md5']);new=bytes.fromhex(component['after_decoded']['md5'])
assert len(before)==len(after) and before.count(old)==1 and before.replace(old,new)==after
rehearse(r)
print('BC1 decoded pixels, exact header/untouched blocks/74 held, 1178 edits, forward/inverse PASS')
if __name__=='__main__':main()