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

171 lines
11 KiB
Python

from pathlib import Path
import sys
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
from native_world_trace import WorldTrace,summarize_source_health
trace=WorldTrace(None)
def frame(count,first):
return {'trace_count':count,'trace':[{'direction':1,'opcode':i,'bytes':8} for i in range(first,count)]}
trace.capture(frame(40,8));trace.capture(frame(43,11));trace.capture(frame(43,11))
assert [r['index'] for r in trace.records]==[40,41,42] and trace.lost==0
trace.capture(frame(90,58))
assert trace.lost==15 and trace.records[-1]['index']==89
try:trace.capture(frame(1,0))
except RuntimeError:pass
else:raise AssertionError('Session reset must not silently splice traces')
print('Metadata collection de-duplicates ring snapshots, retains request/response across wrap, reports overwritten rows and rejects session reset.')
entry=WorldTrace(None,collect_fields=False)
entry.capture_fields({'total':20});entry.capture_fields({'total':0})
assert entry.report()['source_unit_fields']['observation_enabled']is False
assert entry.report()['source_unit_fields']['available']is False
assert entry.report()['source_unit_fields']['rows']==[]
for bad in (None,0,1,'false'):
try:WorldTrace(None,collect_fields=bad)
except ValueError:pass
else:raise AssertionError('Invalid field observation policy accepted')
bookmarked=WorldTrace(None)
try:bookmarked.bookmark()
except RuntimeError:pass
else:raise AssertionError('Uninitialized trace cannot bookmark a future input')
bookmarked.capture(dict(trace_count=0,trace=[]));empty_anchor=bookmarked.bookmark()
assert empty_anchor==-1
event=dict(direction=2,opcode=0x3e011e,bytes=18)
bookmarked.capture(dict(trace_count=1,trace=[event]))
assert bookmarked.wait(2,0x3e011e,after_index=empty_anchor)['index']==0
anchor=bookmarked.bookmark();assert anchor==0
bookmarked.capture(dict(trace_count=2,trace=[event,event]))
assert bookmarked.wait(2,0x3e011e,after_index=anchor)['index']==1, \
'Immediate first new event must match, never the identical prior event'
bookmarked.error='Controlled observer failure'
try:bookmarked.bookmark()
except RuntimeError:pass
else:raise AssertionError('Failed observer cannot authorize another action bookmark')
print('Pre-action bookmarks include the first immediate new event and exclude identical prior events; empty traces start at-1 and unready/failed traces reject.')
def motion(count,rows):
return dict(received=count,sent=count,unknown=0,rejected=0,captured=len(rows),rows=rows)
def actor(guid,identity,**changes):
row=dict(legacy_guid=str(guid),modern_guid='00'*16,id=identity,duration=0,points=0,facing=1,start=[1.,2.,3.],destination=[1.,2.,3.])
row.update(changes);return row
tracked=WorldTrace(None);prior=actor(123,7);foreign=actor(124,8)
tracked.capture_motion(motion(100,[prior,foreign]));assert tracked.track_motion('123')=={7}
assert tracked.motion_rows()==[] # Initial ring contents are baseline, never new evidence.
fresh=actor(123,9)
tracked.capture_motion(motion(101,[prior,foreign,fresh]));tracked.capture_motion(motion(101,[prior,foreign,fresh]))
assert len(tracked.motion_rows())==1 and tracked.motion_rows()[0]['id']==9
assert tracked.motion_rows()[0]['seen_monotonic']>=tracked.report()['tracked_actor_motion']['armed_monotonic']
tracked.capture_motion(motion(102,[foreign]));assert tracked.motion_rows()[0]['id']==9 # Retained after global eviction.
assert tracked.report()['tracked_actor_motion']['legacy_guid']=='123'
try:tracked.capture_motion(motion(1,[]))
except RuntimeError as error:assert 'reset' in str(error)
else:raise AssertionError('Motion counter reset must fail')
try:tracked.track_motion(124)
except RuntimeError:pass
else:raise AssertionError('Tracking cannot silently change actor or baseline')
bounded=WorldTrace(None);bounded.capture_motion(motion(0,[]));bounded.track_motion(123)
for index in range(512):bounded.capture_motion(motion(index+1,[actor(123,index)]))
assert len(bounded.motion_rows())==512
try:bounded.capture_motion(motion(513,[actor(123,512)]))
except RuntimeError as error:assert 'exhausted' in str(error)
else:raise AssertionError('Actor motion observation must remain bounded')
assert len(bounded.motion_rows())==512
for invalid in (0,-1,2**64,True,'foreign'):
try:WorldTrace(None).track_motion(invalid)
except ValueError:pass
else:raise AssertionError('Invalid tracked identity accepted')
assert 'tracked_actor_motion' not in WorldTrace(None).report()
print('Exact-actor public motion rows retain new evidence across global-ring eviction, exclude prior/foreign rows, de-duplicate, and reject reset/bound overflow. No client or RPC invoked.')
def field_row(sequence,packet,field=164,value=10241):
return dict(legacy_guid='4509',sequence=sequence,source_packet=packet,opcode=0x1f6,kind=0,field=field,value=value)
def field_state(total,packets,rows):
return dict(total=total,source_packets=packets,rows=rows,overflow=False,overwritten=max(0,total-64))
def source_frame(received,rounds):
rows=[]
for _ in range(rounds):rows.extend((dict(direction=1,opcode=0x1f6,bytes=63),dict(direction=3,opcode=0x5e0000,bytes=61)))
return dict(trace_count=len(rows),trace=rows[-32:],received=received)
fields=WorldTrace(None);fields.capture_fields(None)
assert not fields.report()['source_unit_fields']['available']
fields.capture(source_frame(101,1));fields.capture_fields(field_state(10,91,[field_row(10,91)]))
assert not fields.report()['source_unit_fields']['counter_offset_confirmed']
assert fields.report()['source_unit_fields']['rows']==[] # Existing field state is baseline.
fields.capture(source_frame(102,2));fields.capture_fields(field_state(11,92,[field_row(10,91),field_row(11,92,value=10240)]))
observed=fields.report()['source_unit_fields'];assert observed['counter_offset_candidate']==10 and observed['counter_offset_confirmed']
assert observed['rows'][0]['world_trace_index']==2 and observed['rows'][0]['value']==10240
assert observed['rows'][0]['correlation']=='stable-counter-delta-and-opcode'
assert fields.records[0]['host_source_packet']==102
fields.capture_fields(field_state(11,92,[field_row(10,91),field_row(11,92,value=10240)]))
assert len(fields.report()['source_unit_fields']['rows'])==1
# A non-atomic/misaligned new counter pair cannot silently retain correlation.
fields.capture(source_frame(104,3));fields.capture_fields(field_state(12,93,[field_row(12,93)]))
assert not fields.report()['source_unit_fields']['counter_offset_confirmed']
assert fields.report()['source_unit_fields']['rows'][-1]['world_trace_index'] is None
fields.capture_fields(field_state(100,100,[field_row(i,100) for i in range(37,101)]))
assert fields.report()['source_unit_fields']['lost_rows']==24
assert fields.report()['source_unit_fields']['snapshot_overwritten']==36
try:fields.capture_fields(field_state(1,1,[field_row(1,1)]))
except RuntimeError as error:assert 'reset' in str(error)
else:raise AssertionError('Source field reset must not splice sessions')
try:fields.capture_fields(None)
except RuntimeError as error:assert 'disappeared' in str(error)
else:raise AssertionError('Source field metadata disappearance must remain visible')
uncorrelated=WorldTrace(None);uncorrelated.capture_fields(field_state(0,0,[]))
uncorrelated.capture_fields(field_state(1,1,[field_row(1,1)]))
assert uncorrelated.report()['source_unit_fields']['rows'][0]['world_trace_index'] is None
for malformed in (field_state(2,2,[field_row(1,1)]),dict(field_state(0,0,[]),overflow=True)):
try:WorldTrace(None).capture_fields(malformed)
except RuntimeError:pass
else:raise AssertionError('Discontinuous/overflowed public field ring accepted')
print('Source-field tails exclude baseline/de-duplicate, reportoverwrite gaps/reset, retainunknowncorrelation, and require stable advancing counter deltas plusmatchingopcode forhost-index association. No RPC/client invoked.')
# A busy optional field read must not discard independent opcode observations.
busy=dict(available=False,retryable=True,reason='source-field-snapshot-busy',attempts=3,rows=[],engine_application_verified=False)
recovering=WorldTrace(None);recovering.capture(source_frame(10,1));recovering.capture_fields(field_state(5,10,[field_row(5,10)]))
recovering.capture(source_frame(11,2));recovering.capture_fields(busy)
pending=recovering.report()['source_unit_fields']
assert pending['available'] and not pending['latest_available'] and pending['retryable_reads']==1
assert recovering.error is None and recovering.bookmark()==3 and len(recovering.records)==2
recovering.capture(source_frame(100,3));recovering.capture_fields(field_state(75,100,[field_row(i,100) for i in range(12,76)]))
recovered=recovering.report()['source_unit_fields']
assert recovered['latest_available'] and recovered['retryable_reads']==1 and recovered['lost_rows']==6
assert len(recovered['rows'])==64 and recovered['rows'][0]['sequence']==12
assert not recovered['counter_offset_confirmed'] and all(row['world_trace_index'] is None for row in recovered['rows'])
startup_busy=WorldTrace(None);startup_busy.capture_fields(busy)
assert not startup_busy.report()['source_unit_fields']['available'] and startup_busy.error is None
startup_busy.capture_fields(field_state(5,10,[field_row(5,10)]))
assert startup_busy.report()['source_unit_fields']['rows']==[] # First successful sample remains baseline.
try:startup_busy.capture_fields(dict(busy,rows=[field_row(1,1)]))
except RuntimeError:pass
else:raise AssertionError('Malformed retryable results must fail closed')
print('Busy optional field snapshots preserve world tracing and explicit retry counts; recovery detects overwritten field gaps without claiming correlation.')
# A regenerating training dummy can end at full health despite real damage.
# Keep observed decreases/increases separate; neither is an attribution claim.
def health_report(values):
return dict(lost_metadata_rows=0,observer_error=None,source_unit_fields=dict(
available=True,counter_offset_confirmed=True,lost_rows=0,
rows=[dict(legacy_guid='123',field=22,sequence=i+1,world_trace_index=20+i,value=v)for i,v in enumerate(values)]))
r=summarize_source_health(health_report([1000,900,1100,1000]),123,22)
assert r['observed_decreases']==200 and r['observed_increases']==200 and r['net_decrease']==0
assert r['complete_retained_capture'] and not r['total_damage_verified'] and not r['engine_application_verified']
assert not r['attacker_attribution_verified']
r=summarize_source_health(health_report([1000,900,1100]),'123',22,after_index=20)
assert r['samples']==2 and r['observed_decreases']==0 and r['observed_increases']==200 and r['net_decrease']==-200
assert summarize_source_health(health_report([1000]),'124',22)['samples']==0
missing=health_report([1000,900]);missing['source_unit_fields']['lost_rows']=1
assert not summarize_source_health(missing,123,22)['complete_retained_capture']
missing=health_report([1000,900]);missing['source_unit_fields']['rows'][1]['world_trace_index']=None
assert summarize_source_health(missing,123,22)['uncorrelated_rows']==1
assert not summarize_source_health(missing,123,22)['complete_retained_capture']
for values in ([True],[float('nan')],[-1],[2**32]):
try:summarize_source_health(health_report(values),123,22)
except ValueError:pass
else:raise AssertionError('Invalid source health accepted')
wrong=health_report([1000,900]);wrong['source_unit_fields']['rows'][1]['sequence']=1
try:summarize_source_health(wrong,123,22)
except ValueError:pass
else:raise AssertionError('Unordered source health samples accepted')
print('Source health summaries separate observed damage-sized decreases from healing/reset increases, bound identity/sequences, and retain attribution/engine verification limits.')