Files
WoW-Hybrid-gateway/tests/source_block_mip_normalization_test.py
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

106 lines
5.5 KiB
Python

#!/usr/bin/env python3
"""Native block extents and authored-level preservation for Source rectangles."""
import hashlib
import io
import json
import struct
import sys
import unittest
from pathlib import Path
from PIL import Image
BASE=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(BASE/'ops'))
from octo_icon_assets import normalize_source_block_rectangular_mips
from map_asset_preflight import global_tex_mip_cache_candidate
POLICY='source-block-malformed-tail-from-valid-authored-mip-box-v1'
JOB=BASE/'runtime/continuation-20261026/global-TEX-mip-cache-producer'
def levels(raw):
width,height=struct.unpack_from('<II',raw,12)
offsets=struct.unpack_from('<16I',raw,20);sizes=struct.unpack_from('<16I',raw,84)
return [(max(1,width>>k),max(1,height>>k),raw[offsets[k]:offsets[k]+sizes[k]])
for k in range(max(width,height).bit_length())]
def fixture():
header=bytearray(148);header[:4]=b'BLP2';struct.pack_into('<I',header,4,1)
header[8]=2;header[11]=1;struct.pack_into('<II',header,12,8,32)
red=struct.pack('<HHI',0xf800,0,0);green=struct.pack('<HHI',0x07e0,0,0)
cursor=148;payloads=[]
for k in range(6):
w,h=max(1,8>>k),max(1,32>>k);size=((w+3)//4)*((h+3)//4)*8
if k==2:size=8
body=(green if k>=3 else red)*(size//8)
struct.pack_into('<I',header,20+4*k,cursor);struct.pack_into('<I',header,84+4*k,size)
payloads.append(body);cursor+=size
return bytes(header)+b''.join(payloads)
class SourceBlockMipsTest(unittest.TestCase):
def test_only_bad_mip_regenerated_and_later_authored_mips_preserved(self):
raw=fixture();out,audit=normalize_source_block_rectangular_mips(raw,presentation_policy=POLICY)
self.assertEqual(audit['regenerated_levels'],[2])
before,after=levels(raw),levels(out)
for k in (0,1,3,4,5):self.assertEqual(after[k],before[k])
w,h,body=after[2];self.assertEqual(len(body),16)
image=Image.frombytes('RGBA',(w,h),body,'bcn',(1,'DXT1'))
self.assertEqual(image.convert('RGB').tobytes(),bytes((255,0,0))*w*h)
same,second=normalize_source_block_rectangular_mips(out,presentation_policy=POLICY)
self.assertEqual(same,out);self.assertFalse(second['changed'])
def test_unsupported_or_ambiguous_input_refused(self):
raw=fixture();bad=[]
for at,value in ((8,1),(9,8),(10,1)):
x=bytearray(raw);x[at]=value;bad.append(bytes(x))
x=bytearray(raw);struct.pack_into('<I',x,84,8);bad.append(bytes(x))
bad.extend((raw+b'unknown',raw[:-1]))
for value in bad:
with self.assertRaises(ValueError):
normalize_source_block_rectangular_mips(value,presentation_policy=POLICY)
with self.assertRaises(ValueError):
normalize_source_block_rectangular_mips(raw,presentation_policy='implicit')
def test_actual27_source_family_and_native_cache_constructor(self):
receipt=json.loads((JOB/'Source27-rectangular-mip-family-provenance.json').read_text())
self.assertEqual(len(receipt['rows']),27)
output=JOB/'future-normalized-Source27';output.mkdir(exist_ok=True)
bindings=[];normalized={}
for source in receipt['rows']:
raw=Path(source['file']).read_bytes()
out,audit=normalize_source_block_rectangular_mips(raw,presentation_policy=POLICY)
self.assertTrue(audit['changed'])
first=struct.unpack_from('<I',raw,20)[0]
self.assertEqual(raw[:20],out[:20]);self.assertEqual(raw[148:first],out[148:first])
before,after=levels(raw),levels(out)
block_bytes=8 if raw[9] in (0,1) else 16
for k,(w,h,body) in enumerate(after):
self.assertEqual(len(body),((w+3)//4)*((h+3)//4)*block_bytes)
if k not in audit['regenerated_levels']:self.assertEqual(body,before[k][2])
with Image.open(io.BytesIO(raw)) as a,Image.open(io.BytesIO(out)) as b:
self.assertEqual(a.convert('RGBA').tobytes(),b.convert('RGBA').tobytes())
sha=hashlib.sha256(out).hexdigest();file=output/(sha+'.blp');file.write_bytes(out)
normalized[source['file_data_id']]=out
bindings.append(dict(source_path=source['source_path'],
original_file_data_id=source['file_data_id'],
original_content_key_reused=source['asset_content_key'],
prepared_file=str(file),path='wowhybrid/native-mip-normalized/'+sha+'.blp',
sha256=sha,content_key=hashlib.md5(out).hexdigest(),bytes=len(out),
kind='texture',file_data_id=None,normalization=audit))
cache,cache_audit=global_tex_mip_cache_candidate(normalized,
native_tail_policy='native-tail-area1024-max64-v1')
self.assertEqual(cache_audit['records'],27)
(output/'normalization-constructor-validation-only.tex').write_bytes(cache)
result=dict(status='PASS_SOURCE27_RECTANGULAR_MIP_NATIVE_PRESENTATION_NORMALIZATION',
policy=POLICY,rows=bindings,highest_resolution_decoded_pixels_equal=True,
every_valid_authored_mip_preserved=True,native_block_extents_all_levels=True,
native_TEX_constructor_records=27,validation_cache_uses_old_IDs_only=True,
new_IDs_and_reference_rebinding_required=True,current_or_framed_assets_changed=False,
runtime_verified=False,published=False)
(JOB/'future-normalized-Source27-focused-result.json').write_text(json.dumps(result,indent=2)+'\n')
if __name__=='__main__':unittest.main()