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

206 lines
14 KiB
Python

#!/usr/bin/env python3
"""Offline presentation graph regressions; no services or client mutation."""
import copy
import contextlib
import io
import hashlib
import json
from pathlib import Path
import struct
import sys
import tempfile
import unittest
from unittest.mock import patch
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
import turtle_data as data
import install_console_diagnostics as installer
from spell_content_compare_test import OFFSETS,inventory
class TalentPresentationBatchTest(unittest.TestCase):
def test_static_stack_target_radius_projection_requires_exact_auxiliary(self):
source=self.source('$u $i1 $a1',trigger=0);rows=[list(r) for r in source.rows]
row=next(r for r in rows if r[0]==100);row[OFFSETS['CumulativeAura']]=5;row[OFFSETS['MaxTargets']]=7;row[OFFSETS['EffectRadiusIndex']]=44
source=self.table(173,rows,source.strings)
bits=lambda value:struct.unpack('<I',struct.pack('<f',value))[0]
radius=self.table(4,[[44,bits(12.5),bits(0),bits(12.5)]])
graph=data.spell_tooltip_graph(source,[100],radius_candidates=[radius]);r=graph['records'][100]
self.assertEqual((r['stackAmount'],r['maxTargets'],r['effects'][0]['radius']),(5,7,12.5))
for other in (self.table(4,[[44,bits(20),bits(0),bits(20)]]),self.table(4,[[44,bits(12.5),bits(1),bits(30)]])):
candidates=[radius,other]if other.by_id[44][2]==0 else [other]
held=data.spell_tooltip_graph(source,[100],radius_candidates=candidates)
self.assertNotIn('radius',held['records'][100]['effects'][0])
self.assertTrue(any(i['kind']in('conflicting-radius','nonconstant-radius')for i in held['issues']))
missing=data.spell_tooltip_graph(source,[100]);self.assertNotIn('radius',missing['records'][100]['effects'][0])
with self.assertRaisesRegex(ValueError,'auxiliary schema'):data.spell_tooltip_graph(source,[100],radius_candidates=[self.table(2,[])])
def test_default_generation_publishes_same_tooltip_dependency_closure(self):
source=self.source();talent=[0]*21;talent[0]=77;talent[1]=1;talent[2]=6;talent[4]=100
tab=[0]*15;tab[0]=1;tab[1]=1;tab[12]=1
fixtures={'Spell':source,'Talent':self.table(21,[talent]),
'TalentTab':self.table(15,[tab],b'\0Tree\0'),'SpellIcon':self.table(2,[[7,1]],b'\0Interface\\Icons\\Spell\0'),
'SkillLineAbility':self.table(15,[]),'SpellDuration':self.table(4,[[32,6000,0,6000]]),'SpellRadius':self.table(4,[]),
'ChrClasses':self.table(1,[[1]]),'ChrRaces':self.table(1,[[1]])}
client=self.root/'client';client.mkdir()
for name,table in fixtures.items():(client/(name+'.dbc')).write_bytes(table.bytes)
with patch.object(data,'TABLES',tuple(fixtures)),patch.object(data,'CLIENT',client),\
patch.object(data,'SERVER',self.root/'absent-server'),patch.object(data,'ROOT',self.root),\
patch.object(data,'talent_rank_successors',return_value={100:[300]}),contextlib.redirect_stdout(io.StringIO()):
data.generate(self.root/'generated')
generated=self.root/'generated';payload=(generated/'TurtleData.lua').read_text()
report=json.loads((generated/'talents.json').read_text())
self.assertIn('["tooltipSpells"]=',payload)
self.assertIn('["durationMs"]=6000',payload)
self.assertEqual(report['tooltip_presentation']['dependency_records'],2)
self.assertEqual(report['tooltip_presentation']['missing_references'],[])
self.assertEqual([r['id']for r in report['talents'][0]['ranks']],[100])
self.assertEqual(report['rank_successors'],{'100':[300]})
self.assertEqual(report['talents'][0]['row'],6)
self.assertFalse(report['tooltip_presentation']['relationships_or_server_state_changed'])
def setUp(self):
self.temp=tempfile.TemporaryDirectory();self.root=Path(self.temp.name)
self.layout=patch.object(data,'spell_layout',return_value=OFFSETS);self.layout.start()
def tearDown(self):self.layout.stop();self.temp.cleanup()
def table(self,fields,rows,strings=b'\0'):
raw=struct.pack('<4s4I',b'WDBC',len(rows),fields,fields*4,len(strings))+b''.join(struct.pack('<'+'I'*fields,*r)for r in rows)+strings
path=self.root/(hashlib.sha256(raw).hexdigest()+'.dbc');path.write_bytes(raw)
return data.DBC(path,fields)
def source(self,description='$200s1 for $200d',trigger=200):
strings=b'\0Name\0'+description.encode()+b'\0Other\0'
rows=[]
for ident in(100,200,300):
r=[0]*173;r[0]=ident;r[OFFSETS['Name_lang']]=1;r[OFFSETS['SpellIconID']]=7
r[OFFSETS['EffectBasePoints']]=9;r[OFFSETS['EffectDieSides']]=0
r[OFFSETS['EffectAuraPeriod']]=2000
r[OFFSETS['ProcChance']]=17;r[OFFSETS['ProcCharges']]=3
r[OFFSETS['EffectAmplitude']]=struct.unpack('<I',struct.pack('<f',1.0))[0]
if ident==100:r[OFFSETS['Description_lang']]=6;r[OFFSETS['EffectTriggerSpell']]=trigger
if ident==200:r[OFFSETS['EffectTriggerSpell']]=100;r[OFFSETS['DurationIndex']]=32
rows.append(r)
return self.table(173,rows,strings)
def baseline(self):return dict(tabs=[dict(id=161,name='Arms')],talents=[dict(id=109,tab=161,row=6,column=1,prerequisite=0,ranks=[dict(id=100,name='Old',icon='old-icon')])],rankSuccessors={'100':[300]})
def test_trigger_and_text_cycles_preserve_ids_and_real_field_types(self):
source=self.source();graph=data.spell_tooltip_graph(source,[100])
self.assertEqual(list(graph['records']),[100,200]);self.assertEqual(graph['missing'],[])
effect=graph['records'][100]['effects'][0]
self.assertEqual(effect['amplitudeMs'],2000) # Not the neighboring float multiplier.
self.assertEqual(effect['dieSides'],0);self.assertEqual(effect['dicePerLevel'],0)
self.assertEqual(graph['records'][200]['procCharges'],3)
self.assertEqual(graph['records'][200]['procChance'],17)
def test_charge_only_reference_loads_source_definition_without_trigger(self):
graph=data.spell_tooltip_graph(self.source('Next $200n swings.',trigger=0),[100])
self.assertEqual(list(graph['records']),[100,200]);self.assertEqual(graph['records'][200]['procCharges'],3)
def test_auxiliary_agreement_and_conflict_are_per_record(self):
duration=self.table(4,[[32,6000,0,6000]])
icons=self.table(2,[[7,1]],b'\0Interface\\Icons\\Source\0')
graph=data.spell_tooltip_graph(self.source(),[100],duration_candidates=[duration],icon_candidates=[icons])
self.assertEqual(graph['records'][200]['durationMs'],6000)
self.assertEqual(graph['records'][100]['icon'],'Interface\\Icons\\Source')
other=self.table(4,[[32,7000,0,7000]])
badicon=self.table(2,[[7,1]],b'\0Different\0')
graph=data.spell_tooltip_graph(self.source(),[100],duration_candidates=[duration,other],icon_candidates=[icons,badicon])
self.assertNotIn('durationMs',graph['records'][200]);self.assertNotIn('icon',graph['records'][100])
self.assertIn('conflicting-duration',[r['kind']for r in graph['issues']])
def test_refresh_preserves_tree_original_data_and_trained_chains(self):
baseline=self.baseline();before=copy.deepcopy(baseline)
result,report=data.refresh_talent_data(baseline,self.source())
self.assertEqual(baseline,before);self.assertEqual(result['tabs'],baseline['tabs'])
self.assertEqual(result['rankSuccessors'],{100:[300]})
self.assertEqual(set(result['tooltipSpells']),{200,300})
self.assertEqual(result['talents'][0]['row'],6)
self.assertEqual(result['talents'][0]['ranks'][0]['icon'],'old-icon')
self.assertFalse(report['relationships_or_server_state_changed'])
self.assertEqual(data.refresh_talent_data(baseline,self.source()),(result,report))
def test_missing_dependencies_are_reported_not_invented(self):
graph=data.spell_tooltip_graph(self.source('$999s1',999),[100])
self.assertEqual(graph['missing'],[999]);self.assertNotIn(999,graph['records'])
baseline=self.baseline();baseline['talents'][0]['ranks'][0]['id']=999
with self.assertRaisesRegex(ValueError,'missing a genuine'):data.refresh_talent_data(baseline,self.source())
graph=data.spell_tooltip_graph(self.source('$/10;999s1',trigger=0),[100])
self.assertEqual(graph['missing'],[999])
def test_bad_schema_root_trigger_and_chain_rejected(self):
for roots in([True],[0],[0x80000000]):
with self.subTest(roots=roots),self.assertRaises(ValueError):data.spell_tooltip_graph(self.source(),roots)
with self.assertRaisesRegex(ValueError,'Trigger dependency'):data.spell_tooltip_graph(self.source(trigger=0xffffffff),[100])
for chain in([100],[300,300],['300'],[999]):
baseline=self.baseline();baseline['rankSuccessors']={'100':chain}
with self.subTest(chain=chain),self.assertRaises(ValueError):data.refresh_talent_data(baseline,self.source())
with self.assertRaises(ValueError):data.spell_tooltip_graph(self.table(2,[[1,0]]),[1])
def test_scaling_duration_is_not_claimed_constant(self):
duration=self.table(4,[[32,6000,1000,12000]])
graph=data.spell_tooltip_graph(self.source(),[100],duration_candidates=[duration])
self.assertNotIn('durationMs',graph['records'][200])
self.assertIn('nonconstant-or-permanent-duration',[r['kind']for r in graph['issues']])
def test_staging_revalidates_observation_and_is_deterministic(self):
source=self.source();baseline=self.root/'baseline.json';baseline.write_text(json.dumps(self.baseline()))
observed=self.root/'inventory.json';observed.write_text(json.dumps(inventory(source)))
# Test isolation uses a temporary project root, never a real runtime.
(self.root/'runtime').mkdir()
with patch.object(data,'ROOT',self.root):
a=self.root/'runtime/a';b=self.root/'runtime/b'
data.stage_talent_refresh(baseline,source.path,observed,a)
data.stage_talent_refresh(baseline,source.path,observed,b)
self.assertEqual({p.name:p.read_bytes()for p in a.iterdir()},{p.name:p.read_bytes()for p in b.iterdir()})
with self.assertRaises(ValueError):data.stage_talent_refresh(baseline,source.path,observed,a)
v=json.loads(observed.read_text());v['second_input_pass_verified']=False;observed.write_text(json.dumps(v))
with self.assertRaises(ValueError):data.stage_talent_refresh(baseline,source.path,observed,self.root/'runtime/rejected')
self.assertFalse((self.root/'runtime/rejected').exists())
def installed_fixture(self):
source=self.source();baseline=self.root/'baseline.json';baseline.write_text(json.dumps(self.baseline()))
observed=self.root/'inventory.json';observed.write_text(json.dumps(inventory(source)))
(self.root/'runtime').mkdir();(self.root/'ops').mkdir()
(self.root/'ops/turtle_data.py').write_bytes(Path(data.__file__).read_bytes())
profile=self.root/'runtime/profile'
with patch.object(data,'ROOT',self.root):data.stage_talent_refresh(baseline,source.path,observed,profile)
target=self.root/'addon';target.mkdir()
for name in ('TurtleTalentProvider.lua','TurtleTalents.lua'):(target/name).write_bytes(b'-- original module\n')
(target/'TurtleData.lua').write_text('-- Generated from fixture; not a client.\nGatewayTurtleData='+data.lua(self.baseline())+'\n')
(target/'GatewayDiagnostics.toc').write_text('TurtleData.lua\nTurtleTalentProvider.lua\nTurtleTalents.lua\n')
(target/'Unrelated.lua').write_bytes(b'-- retain unrelated working module exactly\n')
previous=dict(files={p.name:hashlib.sha256(p.read_bytes()).hexdigest()for p in target.iterdir()})
return target,previous,profile
def test_focused_install_composition_preserves_unrelated_modules_and_does_not_write(self):
target,previous,profile=self.installed_fixture()
before={p.name:p.read_bytes()for p in target.iterdir()}
with patch.object(installer,'ROOT',self.root):files,manifest=installer.talent_only_files(target,previous,profile)
self.assertEqual({p.name:p.read_bytes()for p in target.iterdir()},before)
self.assertEqual(files['Unrelated.lua'],before['Unrelated.lua'])
self.assertEqual(files['GatewayDiagnostics.toc'],before['GatewayDiagnostics.toc'])
self.assertEqual(files['TurtleData.lua'],(profile/'TurtleData.lua').read_bytes())
self.assertEqual(manifest['directory'],str(profile))
def test_focused_install_refuses_profile_tampering_and_local_changes(self):
target,previous,profile=self.installed_fixture()
with patch.object(installer,'ROOT',self.root):
original=(profile/'TurtleData.lua').read_bytes();(profile/'TurtleData.lua').write_bytes(original+b'-- tampered\n')
with self.assertRaisesRegex(ValueError,'artifact differs'):installer.talent_only_files(target,previous,profile)
(profile/'TurtleData.lua').write_bytes(original)
(target/'Unrelated.lua').write_bytes(b'-- another unfinished change\n')
with self.assertRaisesRegex(RuntimeError,'differs from recovery'):installer.talent_only_files(target,previous,profile)
def test_focused_install_refuses_different_tree_identity(self):
target,previous,profile=self.installed_fixture()
changed=self.baseline();changed['talents'][0]['row']=5
(target/'TurtleData.lua').write_text('-- Generated from different fixture.\nGatewayTurtleData='+data.lua(changed)+'\n')
previous['files']['TurtleData.lua']=hashlib.sha256((target/'TurtleData.lua').read_bytes()).hexdigest()
with patch.object(installer,'ROOT',self.root),self.assertRaisesRegex(ValueError,'tree identity differs'):
installer.talent_only_files(target,previous,profile)
if __name__=='__main__':unittest.main()