Files
WoW-Hybrid-gateway/tests/item_build_base_test.py
2026-11-23 03:28:32 +02:00

99 lines
8.8 KiB
Python

#!/usr/bin/env python3
"""Factory builds select one portable pinned base and cannot fall back."""
import copy,hashlib,json,subprocess,sys,tempfile,unittest
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT/'ops'))
from isolated_item_render_profile import load_build_base,stage_build_base,resolve_base_package
from item_render_profile import header
def sha(data):return hashlib.sha256(data).hexdigest()
class ItemBuildBaseTest(unittest.TestCase):
def fixture(self,folder):
bundle=folder/'portable';bundle.mkdir();inputs={}
def add(key,path,data):
target=bundle/path;target.parent.mkdir(parents=True,exist_ok=True);target.write_bytes(data)
inputs[key]={'path':path,'sha256':sha(data),'bytes':len(data)}
profile={'definitions':[{'source_display':20081,'inventory_type':13,'native_display':20081,'render_item':8190,'modified_appearance':159166}],
'item_appearances':[{'item_id':90053,'modified_appearance':324144},{'item_id':93223,'modified_appearance':324145}]}
add('render_profile','profile.json',json.dumps(profile).encode());add('render_header','render.hpp',header(profile).encode())
add('metadata_header','include/gateway/item_sparse_compat.hpp',b'#define BASE_METADATA_SENTINEL 97\n')
add('random_property_header','include/gateway/item_random_property_profile.hpp',b'#pragma once\n')
for key in ['working_registry','working_codec','working_delta']:add(key,'proofs/'+key,b'fixture '+key.encode())
package={'rows':[{'id':90053,'wire_hex':'0102'}],
'working_wire_baseline':{'path':'/retired/registry.json','sha256':inputs['working_registry']['sha256'],'codec_path':'/retired/codec.so','codec_sha256':inputs['working_codec']['sha256']},
'working_wire_delta':{'path':'/retired/delta.json','sha256':inputs['working_delta']['sha256']}}
add('item_package','package.json',json.dumps(package).encode())
generator=ROOT/'ops/item_render_profile.py';source_pin={'path':'ops/item_render_profile.py','sha256':sha(generator.read_bytes()),'bytes':generator.stat().st_size}
manifest=folder/'base.json';manifest.write_text(json.dumps({'schema':1,'kind':'accepted-item-build-base','base_id':'test-base','metadata_encoding':'compact-v1','inputs':inputs,'source_pins':[source_pin]}))
return bundle,manifest,inputs
def test_portable_selection_actual_compiler_and_context_relocation(self):
with tempfile.TemporaryDirectory(dir=ROOT/'build',prefix='item-base-test-')as directory:
folder=Path(directory);bundle,manifest,inputs=self.fixture(folder);base=load_build_base(manifest,bundle)
output=folder/'compiled';selected=stage_build_base(manifest,bundle,output,ROOT/'build')
cpp=folder/'probe.cpp';cpp.write_text('#include "staged-item-render-profile.hpp"\n#include "gateway/item_sparse_compat.hpp"\nint main(){return BASE_METADATA_SENTINEL!=97 || gateway::item_render_profile::modified_appearance(90053)!=324144 || gateway::item_render_profile::modified_appearance(93223)!=324145;}\n')
result=subprocess.run(['c++','-std=c++20','-I'+str(output),'-I'+selected['compiled_include'],str(cpp),'-o',str(folder/'probe')],capture_output=True,text=True);self.assertEqual(result.returncode,0,result.stderr)
self.assertEqual(subprocess.run([str(folder/'probe')]).returncode,0)
raw=(bundle/'package.json').read_bytes();receipt=resolve_base_package(base,output);resolved=json.loads(Path(receipt['resolved_path']).read_text());original=json.loads(raw)
for section,key,name in [('working_wire_baseline','path','working_registry'),('working_wire_baseline','codec_path','working_codec'),('working_wire_delta','path','working_delta')]:
self.assertEqual(resolved[section][key],str(bundle/inputs[name]['path']));resolved[section][key]=original[section][key]
self.assertEqual(resolved,original);self.assertEqual((bundle/'package.json').read_bytes(),raw)
cli=subprocess.run([sys.executable,str(ROOT/'ops/build_forever_create.py'),'--item-build-base-manifest',str(manifest),'--item-build-base-root',str(bundle)],capture_output=True,text=True,timeout=10)
self.assertNotEqual(cli.returncode,0);self.assertIn('Accepted base builds require a separate',cli.stderr)
moved=folder/'relocated';bundle.rename(moved);self.assertEqual(load_build_base(manifest,moved)['inputs']['render_header']['sha256'],inputs['render_header']['sha256'])
def test_missing_drift_escape_and_generator_rejection(self):
with tempfile.TemporaryDirectory(dir=ROOT/'build',prefix='item-base-reject-')as directory:
folder=Path(directory);bundle,manifest,inputs=self.fixture(folder);original=json.loads(manifest.read_text())
for key,pin in inputs.items():
path=bundle/pin['path'];saved=path.read_bytes();path.unlink()
with self.assertRaises(ValueError,msg=key):load_build_base(manifest,bundle)
path.write_bytes(saved+b'changed')
with self.assertRaises(ValueError,msg=key):load_build_base(manifest,bundle)
path.write_bytes(saved)
for value in ['../outside','/absolute','include/../render.hpp']:
altered=copy.deepcopy(original);altered['inputs']['render_header']['path']=value;manifest.write_text(json.dumps(altered))
with self.assertRaises(ValueError):load_build_base(manifest,bundle)
altered=copy.deepcopy(original);altered['source_pins'][0]['sha256']='0'*64;manifest.write_text(json.dumps(altered))
with self.assertRaises(ValueError):load_build_base(manifest,bundle)
bad=copy.deepcopy(original);profile_path=bundle/inputs['render_profile']['path'];raw=profile_path.read_bytes();changed=json.loads(raw);changed['item_appearances'][0]['modified_appearance']=324146;profile_path.write_text(json.dumps(changed));bad['inputs']['render_profile'].update(bytes=profile_path.stat().st_size,sha256=sha(profile_path.read_bytes()));manifest.write_text(json.dumps(bad))
with self.assertRaisesRegex(ValueError,'reproduced'):load_build_base(manifest,bundle)
profile_path.write_bytes(raw)
manifest.write_text(json.dumps(original));(bundle/'alias').symlink_to(bundle/'render.hpp');altered=copy.deepcopy(original);altered['inputs']['render_header']['path']='alias';manifest.write_text(json.dumps(altered))
with self.assertRaises(ValueError):load_build_base(manifest,bundle)
def test_cmake_factory_selects_both_inputs_and_missing_base_is_fatal(self):
with tempfile.TemporaryDirectory(dir=ROOT/'build',prefix='item-base-cmake-')as directory:
folder=Path(directory);bundle,manifest,_=self.fixture(folder);output=folder/'cmake'
command=['cmake','-S',str(ROOT),'-B',str(output),'-DGW_ITEM_BUILD_BASE_MANIFEST='+str(manifest),'-DGW_ITEM_BUILD_BASE_ROOT='+str(bundle),'-DCMAKE_EXPORT_COMPILE_COMMANDS=ON']
result=subprocess.run(command,capture_output=True,text=True,timeout=60);self.assertEqual(result.returncode,0,result.stderr)
commands=json.loads((output/'compile_commands.json').read_text())
for source in ['src/roster_translation.cpp','native/inventory_translation.cpp','native/world_translation.cpp']:
found=[r for r in commands if Path(r['file'])==ROOT/source];self.assertTrue(found,source)
for row in found:self.assertIn('staged-item-render-profile.hpp',row['command']);self.assertIn(str(output/'base-include'),row['command'])
result=subprocess.run(['cmake','-S',str(ROOT),'-B',str(folder/'missing'),'-DGW_ITEM_BUILD_BASE_ROOT='+str(folder/'absent')],capture_output=True,text=True,timeout=60)
self.assertNotEqual(result.returncode,0);self.assertIn('Accepted item build input admission failed',result.stderr)
def test_default_build_stages_factory_base_and_rejects_explicit_override(self):
with tempfile.TemporaryDirectory(dir=ROOT/'build', prefix='item-base-default-') as directory:
source = Path(directory)
bundle, manifest, _ = self.fixture(source)
data = source/'ops/data'
data.mkdir(parents=True)
manifest.rename(data/'item-build-base-20261118.json')
(source/'ops/isolated_item_render_profile.py').symlink_to(ROOT/'ops/isolated_item_render_profile.py')
(source/'probe.cpp').write_text('void probe() {}\n')
(source/'CMakeLists.txt').write_text(
'cmake_minimum_required(VERSION 3.20)\nproject(item_base LANGUAGES CXX)\n'
'add_library(probe STATIC probe.cpp)\n'
f'include("{ROOT}/cmake/ItemBuildBase.cmake")\ngw_admit_item_build_base(probe)\n')
output = source/'build'
command = ['cmake', '-S', str(source), '-B', str(output), '-DGW_ITEM_BUILD_BASE_ROOT='+str(bundle)]
result = subprocess.run(command, capture_output=True, text=True, timeout=60)
self.assertEqual(result.returncode, 0, result.stderr)
staged = output/'accepted-item-base/staged-item-render-profile.hpp'
self.assertEqual(staged.read_bytes(), (bundle/'render.hpp').read_bytes())
self.assertFalse((output/'staged-item-render-profile.hpp').exists())
result = subprocess.run(command+['-DGW_ITEM_RENDER_PROFILE_HEADER='+str(bundle/'render.hpp')],
capture_output=True, text=True, timeout=60)
self.assertNotEqual(result.returncode, 0)
self.assertIn('Render overrides require a separate output directory', result.stderr)
if __name__=='__main__':unittest.main()