"""Exercise both classic and CH5 extended layout interception without rewriting packed scripts.""" import copy from pathlib import Path import sys import types import unittest from unittest.mock import patch ROOT=Path(__file__).resolve().parents[2] class InventoryPageTests(unittest.TestCase): def load(self): mods={n:types.ModuleType(n) for n in ('constInfo','player','ui','uiInventory','uiselectitem','item')} mods['player'].INVENTORY_PAGE_COUNT=5 mods['player'].EQUIPMENT_SLOT_START=225 raw=(ROOT/'src/platform/ScriptLib/InventoryPagesScript.inc').read_text() code=raw.split('R"PY(',1)[1].rsplit(')PY";',1)[0].rsplit('__Install()',1)[0] ns={'xrange':range} with patch.dict(sys.modules,mods):exec(code,ns) return ns,mods def test_classic_and_extended_loaders_both_generate_five_tabs(self): for name,count,slot in [('InventoryWindow.py',2,90),('InventoryWindowEx.py',4,225)]: with self.subTest(name=name): ns,mods=self.load() tabs=[dict(name='Inventory_Tab_%02d'%(i+1),y=224) for i in range(count)] template=dict(name='InventoryWindow',children=[dict(name='board',children=[ dict(name='EquipmentSlot',slot=[dict(index=slot)]),*tabs])]) mods['ui'].execfile=lambda file,d:d.update(window=copy.deepcopy(template)) ns['_InstallScriptPatch']() out={};mods['ui'].execfile('UIScript/'+name,out) ns['_PatchInventoryWindow'](out['window']) # idempotent on repeated patch board=out['window']['children'][0] self.assertEqual([x['name'] for x in board['children'] if x['name'].startswith('Inventory_Tab_')], ['Inventory_Tab_%02d'%(i+1) for i in range(5)]) self.assertEqual(ns['_Find'](out['window'],'EquipmentSlot')['slot'][0]['index'],225) def test_unrelated_layout_not_changed(self): ns,mods=self.load() template=dict(name='other',children=[]) mods['ui'].execfile=lambda file,d:d.update(window=copy.deepcopy(template)) ns['_InstallScriptPatch']() out={};mods['ui'].execfile('UIScript/ShopWindow.py',out) self.assertEqual(out['window'],template) if __name__=='__main__':unittest.main()