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

131 lines
11 KiB
Python

"""Actual source override is a blocker; empty-override cases are offline fixtures."""
import copy,hashlib,json,sys,unittest
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];sys.path[:0]=[str(ROOT/'ops')]
import spell_item_proc_compat as m,spell_passive_compat as codec,content_package_stage as stage
from spell_definition_common import review_proc_override_snapshot
from item_sparse_compat import package_payloads
class ExistingRestorativeProcTest(unittest.TestCase):
def fixture(self,empty_override=False):
v=json.loads((ROOT/'tests/fixtures/existing-restorative-proc-source.json').read_text())['inputs']
if empty_override:v['proc_overrides']['rows']=[]
schemas={t:codec.fields(ROOT.parent/'tools/WoWDBDefs/definitions'/(t+'.dbd'),t)for t in('Spell','SpellName','SpellMisc','SpellEffect','SpellLevels','SpellAuraOptions')}
return v,schemas
def component(self,v):return m.existing_restoration_component(**v[0],schemas=v[1])
def test_real_source_three_ppm_override_blocks_staging(self):
v=self.fixture();before=copy.deepcopy(v);proof=review_proc_override_snapshot(v[0]['proc_overrides'],[27418,27419])
self.assertFalse(proof['overrides_absent']);self.assertEqual(proof['rows'][0]['ppmRate'],'3')
with self.assertRaisesRegex(ValueError,'execution review: 27419'):self.component(v)
self.assertEqual(v,before)
def test_empty_override_fixture_corrects_metadata_only_and_preserves_trigger(self):
v=self.fixture(True);before=copy.deepcopy(v);r=self.component(v);self.assertEqual(v,before)
self.assertEqual(len(r['records']),3);self.assertEqual(r['validated_trigger_ids'],[27418])
self.assertEqual({row['table_name']for row in r['records']},{'Spell','SpellEffect','SpellLevels'})
self.assertEqual({row['source_spell_id']for row in r['records']},{27419})
self.assertEqual(next(row['record']['BaseLevel']for row in r['records']if row['table_name']=='SpellLevels'),50)
self.assertIn('$/10;27418s2',next(row['record']['Description_lang']for row in r['records']if row['table_name']=='Spell'))
for row in r['records']:self.assertEqual(bytes.fromhex(row['wire_hex']),codec.encode(row['record'],v[1][row['table_name']]))
self.assertFalse(r['installed']);self.assertFalse(r['engine_accepted']);self.assertFalse(r['gameplay_verified']);self.assertFalse(r['installable'])
def test_reapplying_effective_native_metadata_is_noop(self):
v=self.fixture(True);r=self.component(v)
for row in r['records']:
table=v[0]['native'][row['table_name']]
for i,old in enumerate(table):
if old['ID']==row['id']:table[i]=copy.deepcopy(row['record']);break
else:self.fail('Unexpected new child allocation')
self.assertEqual(self.component(v)['records'],[])
def test_default_new_parent_proc_policy_remains_strict(self):
v=self.fixture(True);s=next(s for s in v[0]['sources']if s['entry']==27419)
with self.assertRaises(ValueError):m.validate_source(s,v[0]['proc_proof'])
def test_missing_parent_levels_are_neutral_only_for_zero_source_requirements(self):
v=self.fixture(True);parent=next(s for s in v[0]['sources']if s['entry']==27419)
v[0]['native']['SpellLevels'][:]=[r for r in v[0]['native']['SpellLevels']if r['SpellID']!=27419]
with self.assertRaisesRegex(ValueError,'proc levels'):self.component(v)
parent['levels']={k:0 for k in parent['levels']}
r=self.component(v);self.assertFalse(any(row['table_name']=='SpellLevels'for row in r['records']))
self.assertFalse(r['gameplay_verified'])
def test_real_override_presentation_scope_changes_only_text(self):
v=self.fixture();v[0]['presentation_only']=True;before=copy.deepcopy(v);r=self.component(v)
self.assertEqual(v,before);self.assertEqual(len(r['records']),1);self.assertEqual(r['records'][0]['table_name'],'Spell')
self.assertEqual(r['records'][0]['changed_fields'],['Description_lang']);self.assertEqual(r['definition_policy'],m.EXISTING_RESTORE_TEXT_POLICY)
self.assertEqual(r['proc_override_review']['rows'][0]['ppmRate'],'3');self.assertFalse(r['proc_execution_rules_converted'])
self.assertEqual({row['table_name']for row in r['deferred_metadata_corrections']},{'SpellEffect','SpellLevels'})
self.assertIn('$/10;27418s2',r['records'][0]['record']['Description_lang'])
def test_presentation_policy_stager_cannot_smuggle_in_execution_changes(self):
v=self.fixture();v[0]['presentation_only']=True;r=self.component(v);raw=b'preserved\0';row=dict(table_name='SpellName',table_hash=hex(codec.TABLES['SpellName'][0]),id=9911,wire_hex=raw.hex(),sha256=hashlib.sha256(raw).hexdigest());pkg=dict(build=codec.BUILD,schema=codec.SCHEMA,id_remapping=False,rows=[row],push_id=package_payloads([row])[0])
staged=stage.compose_existing_proc_corrections(pkg,r,v[1]);self.assertEqual(staged['added_count'],1)
full=self.fixture(True);injected=next(row for row in self.component(full)['records']if row['table_name']=='SpellEffect');r['records'].append(injected)
with self.assertRaisesRegex(ValueError,'derivation'):stage.compose_existing_proc_corrections(pkg,r,v[1])
r=self.component(v);r['inputs']['presentation_only']=False
with self.assertRaisesRegex(ValueError,'execution review'):stage.compose_existing_proc_corrections(pkg,r,v[1])
def test_presentation_flag_is_typed_and_never_changes_default_guard(self):
v=self.fixture();v[0]['presentation_only']=1
with self.assertRaisesRegex(ValueError,'Typed'):self.component(v)
def test_source_and_positive_native_contract_failures_reject(self):
for mode in('trigger','trigger-amount','target','radius','ppm','category','cooldown','school','extra-effect','token'):
v=self.fixture(True);src=next(s for s in v[0]['sources']if s['entry']==27419);n=v[0]['native']
if mode=='trigger':v[0]['sources']=[src]
elif mode=='trigger-amount':next(r for r in n['SpellEffect']if r['SpellID']==27418)['EffectBasePointsF']=999
elif mode=='target':next(r for r in n['SpellEffect']if r['SpellID']==27419)['ImplicitTarget']=[6,0]
elif mode=='radius':next(r for r in n['SpellEffect']if r['SpellID']==27419)['EffectRadiusIndex']=[1,0]
elif mode=='ppm':next(r for r in n['SpellAuraOptions']if r['SpellID']==27419)['SpellProcsPerMinuteID']=1
elif mode=='category':n['SpellCategories']=[]
elif mode=='cooldown':src['timing']['recoveryTime']=1
elif mode=='school':next(r for r in n['SpellMisc']if r['SpellID']==27419)['SchoolMask']=1
elif mode=='extra-effect':n['SpellEffect'].append(dict(next(r for r in n['SpellEffect']if r['SpellID']==27419),ID=999999,EffectIndex=1))
else:src['description']='Has $h1% chance.'
with self.assertRaises(ValueError,msg=mode):self.component(v)
def test_common_stager_rebuilds_preserves_and_deduplicates(self):
v=self.fixture(True);r=self.component(v);raw=b'preserved\0';row=dict(table_name='SpellName',table_hash=hex(codec.TABLES['SpellName'][0]),id=9911,wire_hex=raw.hex(),sha256=hashlib.sha256(raw).hexdigest());pkg=dict(build=codec.BUILD,schema=codec.SCHEMA,id_remapping=False,rows=[row],push_id=package_payloads([row])[0])
staged=stage.compose_existing_proc_corrections(pkg,r,v[1]);self.assertEqual(staged['added_count'],3);self.assertEqual(staged['rows'][0],row)
pkg.update(rows=staged['rows'],push_id=staged['push_id']);self.assertEqual(stage.compose_existing_proc_corrections(pkg,r,v[1])['added_count'],0)
r['inputs']['proc_overrides']=self.fixture()[0]['proc_overrides']
with self.assertRaisesRegex(ValueError,'execution review'):stage.compose_existing_proc_corrections(pkg,r,v[1])
def test_server_ids_are_not_allowlisted(self):
v=self.fixture(True);mapping={27418:600130,27419:600131}
for s in v[0]['sources']:
s['entry']=mapping[s['entry']]
for e in s['effects']:
if e[10]in mapping:e[10]=mapping[e[10]]
v[0]['proc_overrides']['source_spell_ids']=list(mapping.values())
for table,rows in v[0]['native'].items():
for row in rows:
if table in('Spell','SpellName')and row['ID']in mapping:row['ID']=mapping[row['ID']]
elif row.get('SpellID')in mapping:row['SpellID']=mapping[row['SpellID']]
if row.get('EffectTriggerSpell')in mapping:row['EffectTriggerSpell']=mapping[row['EffectTriggerSpell']]
r=self.component(v);self.assertEqual(r['validated_trigger_ids'],[600130]);self.assertEqual({row['source_spell_id']for row in r['records']},{600131})
class ProcOverrideSnapshotTest(unittest.TestCase):
def fixture(self):return json.loads((ROOT/'tests/fixtures/owned-spell-proc-overrides.json').read_text())
def test_batch_retains_all_seven_execution_overrides_and_wide_masks(self):
s=self.fixture();before=copy.deepcopy(s);r=review_proc_override_snapshot(s,s['source_spell_ids'])
self.assertEqual(s,before);self.assertEqual(r['override_row_ids'],[17670,21890,23547,23548,23689,25669,27419])
self.assertEqual(next(row['ppmRate']for row in r['rows']if row['entry']=='27419'),'3')
self.assertEqual(next(row['SpellFamilyMask0']for row in r['rows']if row['entry']=='21890'),'3763103747823')
self.assertFalse(r['source_rules_converted']);self.assertTrue(r['server_authoritative'])
self.assertEqual(r,review_proc_override_snapshot(s,list(reversed(s['source_spell_ids']))))
def test_subset_absence_requires_actual_query_coverage(self):
s=self.fixture();r=review_proc_override_snapshot(s,[15600,15601]);self.assertTrue(r['overrides_absent']);self.assertEqual(r['rows'],[])
with self.assertRaisesRegex(ValueError,'coverage'):review_proc_override_snapshot(s,[600130])
def test_unverified_or_wrong_origin_snapshot_rejects(self):
for key,value in(('kind','other'),('table','tw_logon.account'),('two_reads_equal',False),('database_modified',True),('columns',[]),('source_spell_ids',[])):
s=self.fixture();s[key]=value
with self.assertRaises(ValueError,msg=key):review_proc_override_snapshot(s,[27419])
def test_duplicate_unknown_nonfinite_or_incomplete_rows_reject(self):
for mode in('duplicate','unknown','field','mask','nan','huge','bool','text','oversized','fraction'):
s=self.fixture();row=s['rows'][0]
if mode=='duplicate':s['rows'].append(copy.deepcopy(row))
elif mode=='unknown':row['entry']='600130'
elif mode=='field':del row['Cooldown']
elif mode=='mask':row['procFlags']='-1'
elif mode=='nan':row['ppmRate']='NaN'
elif mode=='huge':row['SpellFamilyMask0']=str(1<<64)
elif mode=='bool':row['Cooldown']=False
elif mode=='text':row['CustomChance']='not a number'
elif mode=='oversized':row['ppmRate']='0'*129
else:row['procEx']='0.5'
with self.assertRaises(ValueError,msg=mode):review_proc_override_snapshot(s,[27419])
if __name__=='__main__':unittest.main()