Files
WoW-Hybrid-gateway/tests/terrain_ocean_material_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

170 lines
8.5 KiB
Python

"""Terrain ocean material correction preserves actual geometry and lake types."""
import struct
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'ops'))
from source_map_wdt import normalize_flat_terrain_ocean_material
def chunk(tag, body):
return tag[::-1].encode() + struct.pack('<I', len(body)) + body
def fixture(typ=2, level=0):
liquid = bytearray(3072)
struct.pack_into('<3I', liquid, 0, 3072, 2, 3120)
liquid += struct.pack('<HHffBBBBII', typ, 42, level, level, 0, 0, 8, 8, 3136, 3144)
liquid += struct.pack('<HHffBBBBII', 1325, 19034, 56, 56, 0, 0, 8, 8, 3136, 3144)
liquid += struct.pack('<QQQ', 0x12345, 0x800, 0x12345) + bytes(range(81))
root = chunk('MVER', struct.pack('<I', 18)) + chunk('MH2O', liquid) + chunk('MCNK', b'unchanged terrain')
patch = struct.pack('<4BHH8Bff8B', 0, 0, 128, 128, typ, 42, *([7] * 8), level, level, *([0] * 8))
lod = chunk('MVER', struct.pack('<I', 18)) + chunk('MASD', b'unchanged shore') + chunk('MALP', patch)
return root, lod
class OceanMaterial(unittest.TestCase):
def test_native_height_and_depth_layout_preserves_geometry(self):
root, lod = fixture()
new, nl, audit = normalize_flat_terrain_ocean_material(root, lod)
self.assertEqual((audit['root_instances'], audit['lod_patches']), (1, 1))
at = 20
pos = at + 3072
old_vertex = struct.unpack_from('<I', root, pos + 20)[0]
new_vertex = struct.unpack_from('<I', new, pos + 20)[0]
# Independent native format0 read: all81 floats precede all81 depths.
self.assertEqual(struct.unpack_from('<81f', new, at + new_vertex), (0.0,) * 81)
self.assertEqual(new[at + new_vertex + 324:at + new_vertex + 405], root[at + old_vertex:at + old_vertex + 81])
self.assertEqual(struct.unpack_from('<H', new, pos)[0], 1250)
self.assertEqual(len(new) - len(root), 405)
# Every prior payload byte survives except its type and vertex offset.
old_size = struct.unpack_from('<I', root, at - 4)[0]
expected = bytearray(root[at:at + old_size])
struct.pack_into('<H', expected, 3072, 1250)
struct.pack_into('<I', expected, 3072 + 20, new_vertex)
self.assertEqual(new[at:at + old_size], expected)
self.assertEqual(new[at + old_size + 405:], root[at + old_size:])
expected_lod = bytearray(lod)
for offset in audit['lod_type_offsets']:
struct.pack_into('<H', expected_lod, offset, 1250)
self.assertEqual(nl, expected_lod)
def test_depth_nan_pattern_is_never_sampled_as_height(self):
root, _ = fixture()
root = bytearray(root)
vertex = struct.unpack_from('<I', root, 20 + 3072 + 20)[0]
root[20 + vertex:20 + vertex + 81] = bytes([255]) * 81
new, _, _ = normalize_flat_terrain_ocean_material(bytes(root))
vertex = struct.unpack_from('<I', new, 20 + 3072 + 20)[0]
self.assertEqual(struct.unpack_from('<81f', new, 20 + vertex), (0.0,) * 81)
self.assertEqual(new[20 + vertex + 324:20 + vertex + 405], bytes([255]) * 81)
def test_split_header_following_chunk_offsets_rebased(self):
root, _ = fixture()
# Add a normal MHDR and a following MTXF top-level chunk.
root = root[:12] + chunk('MHDR', bytes(64)) + root[12:] + chunk('MTXF', b'flags')
root = bytearray(root)
mh2o = root.index(b'O2HM')
mtxf = root.index(b'FXTM')
struct.pack_into('<I', root, 20 + 10 * 4, mh2o - 20)
struct.pack_into('<I', root, 20 + 11 * 4, mtxf - 20)
new, _, _ = normalize_flat_terrain_ocean_material(bytes(root))
self.assertEqual(struct.unpack_from('<I', new, 60)[0], mh2o - 20)
self.assertEqual(struct.unpack_from('<I', new, 64)[0], mtxf - 20 + 405)
self.assertEqual(new[mtxf + 405:], root[mtxf:])
def test_explicit_format0_compact_ocean_preserves_payload(self):
root, lod = fixture()
at = 20
size = struct.unpack_from('<I', root, at - 4)[0]
liquid = bytearray(root[at:at + size])
vertex = len(liquid)
liquid.extend(bytes(20 * 4) + bytes(range(20)))
struct.pack_into('<H', liquid, 3072 + 2, 0)
struct.pack_into('<4B', liquid, 3072 + 12, 2, 1, 4, 3)
struct.pack_into('<I', liquid, 3072 + 20, vertex)
root = root[:12] + chunk('MH2O', liquid) + root[at + size:]
lod = bytearray(lod)
patch = lod.index(b'PLAM') + 8
struct.pack_into('<H', lod, patch + 6, 0)
new, nl, audit = normalize_flat_terrain_ocean_material(root, bytes(lod))
expected = bytearray(root)
struct.pack_into('<H', expected, at + 3072, 1250)
self.assertEqual(new, expected)
self.assertEqual(audit['explicit_format0_instances'], 1)
self.assertEqual(audit['expanded_depth_only_instances'], 0)
self.assertEqual(struct.unpack_from('<HH', nl, patch + 4), (1250, 0))
# A nonzero used vertex fails qualification despite misleading zero bounds.
bad = bytearray(root)
struct.pack_into('<f', bad, at + vertex, 2.0)
unchanged, _, audit = normalize_flat_terrain_ocean_material(bytes(bad))
self.assertEqual(unchanged, bad)
self.assertEqual(audit['held_nonzero_ocean_instances'], 1)
def test_explicit_format2_preserves_depth_only_layout(self):
root, lod = fixture()
root, lod = bytearray(root), bytearray(lod)
struct.pack_into('<H', root, 20 + 3072 + 2, 2)
patch = lod.index(b'PLAM') + 8
struct.pack_into('<H', lod, patch + 6, 2)
new, nl, audit = normalize_flat_terrain_ocean_material(bytes(root), bytes(lod))
struct.pack_into('<H', root, 20 + 3072, 1250)
self.assertEqual(new, root)
self.assertEqual(audit['explicit_format2_instances'], 1)
self.assertEqual(audit['expanded_depth_only_instances'], 0)
self.assertEqual(struct.unpack_from('<HH', nl, patch + 4), (1250, 2))
def test_variable_explicit_heights_are_preserved(self):
root, lod = fixture()
at = 20
size = struct.unpack_from('<I', root, at - 4)[0]
liquid = bytearray(root[at:at + size])
vertex = len(liquid)
liquid.extend(struct.pack('<81f', *([2.0] + [0.0] * 80)) + bytes(range(81)))
struct.pack_into('<Hff', liquid, 3072 + 2, 0, -2.0, 3.0)
struct.pack_into('<I', liquid, 3072 + 20, vertex)
root = root[:12] + chunk('MH2O', liquid) + root[at + size:]
lod = bytearray(lod)
patch = lod.index(b'PLAM') + 8
struct.pack_into('<H', lod, patch + 6, 0)
struct.pack_into('<ff', lod, patch + 16, -2.0, 3.0)
new, nl, audit = normalize_flat_terrain_ocean_material(root, bytes(lod))
expected = bytearray(root)
struct.pack_into('<H', expected, at + 3072, 1250)
self.assertEqual(new, expected)
struct.pack_into('<H', lod, patch + 4, 1250)
self.assertEqual(nl, lod)
self.assertEqual(audit['explicit_format0_instances'], 1)
self.assertEqual(audit['held_nonzero_ocean_instances'], 0)
def test_native_and_nonzero_profiles_preserved(self):
for typ, level, held in ((1250, 0, 0), (2, 24, 1)):
root, lod = fixture(typ, level)
new, nl, audit = normalize_flat_terrain_ocean_material(root, lod)
self.assertEqual((new, nl), (root, lod))
self.assertEqual(audit['held_nonzero_ocean_instances'], held)
def test_previously_proved_zero_surface_flt_min_sentinel(self):
root, lod = fixture()
root, lod = bytearray(root), bytearray(lod)
struct.pack_into('<I', root, 20 + 3072 + 8, 0x00800000)
# The existing LOD generator uses the upper bound for both levels.
patch = lod.index(b'PLAM') + 8
struct.pack_into('<II', lod, patch + 16, 0x00800000, 0x00800000)
new, nl, audit = normalize_flat_terrain_ocean_material(bytes(root), bytes(lod))
self.assertEqual((audit['root_instances'], audit['lod_patches']), (1, 1))
self.assertEqual(new[20 + 3072 + 4:20 + 3072 + 12], root[20 + 3072 + 4:20 + 3072 + 12])
self.assertEqual(nl[patch + 16:patch + 24], lod[patch + 16:patch + 24])
def test_no_lod_and_malformed_bounds(self):
root, _ = fixture()
self.assertIsNone(normalize_flat_terrain_ocean_material(root)[1])
bad = bytearray(root)
struct.pack_into('<I', bad, 20, 0xffffffff)
with self.assertRaises(ValueError):
normalize_flat_terrain_ocean_material(bytes(bad))
if __name__ == '__main__':
unittest.main()