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

163 lines
8.8 KiB
Python

#!/usr/bin/env python3
"""One grouped adapter check: real native patch + Source OBJ and native reuse.
Source M2/WMO transformation adapters are not exercised here. The original
backend owner checks remain authoritative for those unchanged implementations.
"""
from pathlib import Path
import copy
import hashlib
import json
import struct
import sys
import tempfile
import unittest
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'ops'))
import map_conversion_batch as batch
from map_adt_height_test import synthetic
def chunk(tag, body):
return tag[::-1].encode() + struct.pack('<I', len(body)) + body
def adt_fixture(native=False, bad_holes=False):
"""Reuse the established 256-cell fixture, adding the admitted MCNR shape."""
parts = batch.terrain._adt_split_records(synthetic())
raw = chunk('MVER', struct.pack('<I', 18)) + chunk('MHDR', bytes(64))
raw += chunk('MMDX', b'world/test.m2\0') + chunk('MMID', struct.pack('<I', 0))
placement = bytearray(36)
struct.pack_into('<I', placement, 4, 123)
struct.pack_into('<H', placement, 32, 1024)
raw += chunk('MDDF', bytes(placement))
for tag, body, _ in parts:
if tag != 'MCNK':
continue
header = bytearray(body[:128])
struct.pack_into('<I', header, 0, 0x18040 if native else 0)
if native:
# High-resolution native holes occupy these former legacy offsets.
header[20:28] = bytes(8)
else:
struct.pack_into('<I', header, 24, 724)
if bad_holes:
struct.pack_into('<I', header, 60, 1)
if not native:
struct.pack_into('<f', header, 112, 3.0)
heights = bytearray(body[136:716])
if not native:
struct.pack_into('<f', heights, 0, 2.0)
normals = bytes(435) + (b'P' * 13 if native else b'')
legacy_padding = bytes(13) if not native else b''
raw += chunk('MCNK', bytes(header) + chunk('MCVT', bytes(heights)) +
chunk('MCNR', normals) + legacy_padding)
return raw
class BatchBoundary(unittest.TestCase):
def test_one_mixed_batch_and_plan(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
def reference(name, raw):
path = root / name
path.write_bytes(raw)
return {'path': str(path), 'sha256': hashlib.sha256(raw).hexdigest(), 'bytes': len(raw)}
source = adt_fixture()
native = adt_fixture(native=True)
profile = batch.terrain.NATIVE_NEAR_TERRAIN_PROFILE_V1
payloads = {
'source': reference('source.adt', source),
'native': reference('native.adt', native),
'holes': reference('holes.adt', adt_fixture(bad_holes=True)),
'v19': reference('v19.adt', source[:8] + struct.pack('<I', 19) + source[12:]),
'model_source': reference('source.m2', b'unrecognized Source model'),
'model_native': reference('native.m2', b'established native model'),
}
payloads['native_alias'] = dict(payloads['native'])
payloads['bad_hash'] = dict(payloads['source'], sha256='0' * 64)
native_receipt = reference('native-profile.json', batch.encoded(profile))
source_receipt = reference('source-contract.json', batch.encoded({
'status': 'PASS_SOURCE_OBJ_STRUCTURAL_FIXTURE', 'operation': 'source-adt-split'}))
provenance = reference('original-consumer-fixture.json', batch.encoded({
'kind': 'synthetic-original-consumer-contract',
'scope': 'fixture provenance pinning, not original client REA acceptance'}))
contracts = {
'native': {'operation': 'native-patch', 'admission': 'private-candidate',
'api': 'adt_near_terrain_profile_candidate', 'receipt': native_receipt,
'accepted_kind': profile['kind'], 'provenance': [provenance]},
'source': {'operation': 'source-adt-split', 'admission': 'private-candidate',
'api': 'adt_split_candidate', 'receipt': source_receipt,
'accepted_status': 'PASS_SOURCE_OBJ_STRUCTURAL_FIXTURE',
'dependencies_complete': True, 'provenance': [provenance]},
}
common = {'kind': 'adt', 'map_id': 1, 'tile': [2, 3], 'source': 'source'}
patch = dict(common, id='patch', native='native', native_file_data_id=12345,
contracts={'native-patch': 'native'})
request = {'kind': 'map-conversion-batch-v1', 'payloads': payloads,
'contracts': contracts, 'jobs': [
dict(common, id='identical-reuse', source='native_alias', native='native'),
patch,
dict(patch, id='same-operation'),
dict(patch, id='holes-refused', source='holes'),
dict(common, id='source-OBJ', contracts={'source-adt-split': 'source'},
options={'roles': ['obj0'], 'file_data_ids': {'world/test.m2': 123456}}),
dict(common, id='bad-domain', tile=[64, 3]),
dict(common, id='unsupported-version', source='v19'),
dict(common, id='native-no-contract', native='native',
contracts={'source-adt-split': 'source'}),
{'id': 'native-asset-retained', 'kind': 'm2', 'source': 'model_source', 'native': 'model_native'},
{'id': 'native-asset-patch-held', 'kind': 'm2', 'source': 'model_source',
'native': 'model_native', 'requires_native_patch': True},
dict(common, id='integrity-refused', source='bad_hash'),
dict(patch, id='reserved-option-refused', options={'map_id': 9}),
]}
original_request = copy.deepcopy(request)
plan_request = copy.deepcopy(request)
del plan_request['contracts']['native']['provenance']
with mock.patch.object(batch.terrain, 'adt_near_terrain_profile_candidate',
side_effect=AssertionError('plan invoked transform')):
# Preserve signature for option preflight while proving no transform call.
batch.terrain.adt_near_terrain_profile_candidate.__signature__ = __import__('inspect').signature(
batch.terrain.adt_near_terrain_candidate)
plan = batch.plan_batch(plan_request)
self.assertEqual(plan['resolved'], 0)
self.assertGreater(plan['counts']['PLANNED'], 0)
self.assertIn('original REA', next(row for row in plan['rows'] if row['id'] == 'patch')['reason'])
output = root / 'converted'
report = batch.convert_batch(request, output)
rows = {r['id']: r for r in report['rows']}
self.assertEqual(rows['identical-reuse']['status'], 'REUSED')
self.assertEqual(rows['patch']['status'], 'CONVERTED', rows['patch'])
self.assertTrue(rows['same-operation']['conversion_reused'])
self.assertEqual(rows['holes-refused']['status'], 'HELD')
self.assertEqual(rows['source-OBJ']['status'], 'CONVERTED', rows['source-OBJ'])
for name in ['bad-domain', 'unsupported-version', 'native-no-contract',
'native-asset-patch-held', 'integrity-refused', 'reserved-option-refused']:
self.assertEqual(rows[name]['status'], 'HELD', name)
self.assertEqual(rows['native-asset-retained']['status'], 'REUSED')
retained = rows['native-asset-retained']['outputs'][0]
self.assertEqual((output / retained['file']).read_bytes(), b'established native model')
obj = (output / rows['source-OBJ']['outputs'][0]['file']).read_bytes()
chunks = {tag: body for tag, body, _ in batch.terrain._adt_split_records(obj)}
self.assertEqual(struct.unpack_from('<I', chunks['MDDF'])[0], 123456)
self.assertEqual(report['requested'], 12)
self.assertEqual(report['resolved'], 5)
self.assertEqual(report['held'], 7)
self.assertEqual(report['unique_input_files_read'], 9)
self.assertEqual(report['unique_operations_executed'], 5)
self.assertEqual(request, original_request)
self.assertFalse(report['installed'])
self.assertFalse(report['runtime_verified'])
self.assertFalse(report['candidate_content_complete'])
self.assertEqual(rows['patch']['contract']['provenance'], [provenance])
self.assertTrue((output / 'report.json').is_file())
with self.assertRaisesRegex(ValueError, 'new immutable output'):
batch.convert_batch(request, output)
if __name__ == '__main__':
unittest.main()