883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
100 lines
6.0 KiB
Python
100 lines
6.0 KiB
Python
"""Offline ordering tests; never imports real input or connects to a client."""
|
|
from collections import deque
|
|
from pathlib import Path
|
|
import sys,types
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
|
|
events=[]
|
|
ui=types.ModuleType('client_ui')
|
|
ui.key=lambda display,key:events.append(('key',key))
|
|
sys.modules['client_ui']=ui
|
|
from native_posture import establish_standing
|
|
|
|
def word(sequence,state,guid=4509,field=138):
|
|
return dict(legacy_guid=str(guid),sequence=sequence,source_packet=sequence,opcode=0x1f6,kind=0,field=field,value=1175040|state)
|
|
def frame(ack,state,total=1,rows=None,**changes):
|
|
if rows is None:rows=[word(total,state)] if total else []
|
|
result=dict(error=0,closed=False,cast_result=dict(stand_received=ack,stand_state=state),source_unit_fields=dict(total=total,rows=rows));result.update(changes);return result
|
|
class Controller:
|
|
def __init__(self,*frames):self.frames=deque(frames)
|
|
def rpc(self,operation,**fields):
|
|
assert operation=='inspect-engine-pipe' and fields==dict(unit_fields_only=True);events.append(('read',))
|
|
if not self.frames:raise AssertionError('Unexpected inspection after readiness')
|
|
return self.frames.popleft()
|
|
def wait(self,predicate,timeout):
|
|
assert timeout==4;events.append(('wait',))
|
|
for _ in range(8):
|
|
value=predicate()
|
|
if value:return value
|
|
raise RuntimeError('Bounded mock readiness wait expired')
|
|
class Session:
|
|
d=None
|
|
def macro(self,text):events.append(('macro',text))
|
|
def run(*frames,**options):
|
|
events.clear();controller=Controller(*frames);result=establish_standing(controller,Session(),self_guid=4509,**options)
|
|
assert not controller.frames
|
|
return result
|
|
|
|
def inputs():return [e for e in events if e[0] in ('key','macro')]
|
|
result=run(frame(8,0,total=5))
|
|
assert result[0]['ack']==8 and result[0]['reused'] and result[0]['source_field_verified']
|
|
assert result[0]['source_field']['sequence']==5 and events==[('read',)]
|
|
# ACK0 alone is insufficient while its actual self stand word is still seated.
|
|
result=run(frame(8,1,total=5),frame(8,0,total=5,rows=[word(5,1)]),frame(9,1,total=5),frame(10,0,total=5,rows=[word(5,1)]),frame(10,0,total=6))
|
|
assert result[0]['ack']==10 and result[0]['source_field']['sequence']==6
|
|
assert inputs()==[('macro','/stand')]
|
|
assert result[0]['source_field_baseline']==5
|
|
# Unknown initial state uses one X; its first fresh standing ACK and word suffice.
|
|
result=run(frame(0,0,total=0),frame(0,0,total=0),frame(1,0,total=1))
|
|
assert result[0]['ack']==1 and result[0]['source_field_verified']
|
|
assert inputs()==[('key','x')]
|
|
# The first seated ACK must also receive its real field update before /stand.
|
|
result=run(frame(0,0,total=0),frame(1,1,total=0),frame(1,1,total=1),frame(1,0,total=1,rows=[word(1,1)]),frame(2,1,total=1),frame(3,0,total=1,rows=[word(1,1)]),frame(3,0,total=2))
|
|
assert [r['state'] for r in result]==[1,0] and [r['source_field']['sequence'] for r in result]==[1,2]
|
|
assert inputs()==[('key','x'),('macro','/stand')]
|
|
assert events.index(('macro','/stand'))>events.index(('wait',))
|
|
# If an old self word left the ring, generate a real fresh cycle, never accept
|
|
# an unrelated GUID/field or the old ACK as proof of its result.
|
|
result=run(frame(8,0,total=100,rows=[word(99,0,guid=4510),word(100,0,field=164)]),frame(8,1,total=100,rows=[]),frame(9,1,total=101),frame(10,0,total=101,rows=[word(101,1)]),frame(10,0,total=102))
|
|
assert inputs()==[('key','x'),('macro','/stand')]
|
|
assert [r['ack'] for r in result]==[9,10]
|
|
assert [r['source_field_baseline'] for r in result]==[100,101]
|
|
# Reuse a known ACK only after a contradictory recent self word catches up.
|
|
result=run(frame(8,0,total=4,rows=[word(4,1)]),frame(8,0,total=5))
|
|
assert result[0]['reused'] and result[0]['source_field']['sequence']==5 and inputs()==[]
|
|
# Busy optional snapshots are polled without guessing a baseline or input.
|
|
result=run(frame(8,0,source_unit_fields=dict(available=False,retryable=True,rows=[])),frame(8,0,total=5))
|
|
assert result[0]['reused'] and inputs()==[]
|
|
# Source-field unsupported ABI requires an explicit legacy opt-out, labelled.
|
|
legacy=frame(8,0);legacy.pop('source_unit_fields')
|
|
events.clear()
|
|
try:establish_standing(Controller(legacy),Session(),self_guid=4509)
|
|
except RuntimeError as error:assert 'unverified' in str(error)
|
|
else:raise AssertionError('Native readiness must not silently fall back to ACK-only')
|
|
assert inputs()==[]
|
|
result=run(legacy,require_source_field=False)
|
|
assert not result[0]['source_field_verified'] and 'unverified' in result[0]['source_field_limit']
|
|
for frames in ((frame(0,0,closed=True),),(frame(3,1),frame(3,1,error=1)),(frame(3,2),),(frame(0,0,total=0),frame(1,2))):
|
|
events.clear()
|
|
try:establish_standing(Controller(*frames),Session(),self_guid=4509)
|
|
except RuntimeError:pass
|
|
else:raise AssertionError('Closed/error/unexpected posture must fail')
|
|
if len(frames)==1:assert events==[('read',)]
|
|
events.clear()
|
|
try:establish_standing(Controller(frame(3,1),*[frame(3,0)]*8),Session(),self_guid=4509)
|
|
except RuntimeError as error:assert 'expired' in str(error)
|
|
else:raise AssertionError('Stale zero-state ACK must not count as a fresh stand')
|
|
assert inputs()==[('macro','/stand')]
|
|
# A fresh ACK cannot pass with a stale/foreign self field, even when lowbyte0.
|
|
events.clear()
|
|
try:establish_standing(Controller(frame(3,1,total=10),frame(4,0,total=10,rows=[word(10,0)]),*[frame(4,0,total=11,rows=[word(11,0,guid=4510)])]*8),Session(),self_guid=4509)
|
|
except RuntimeError as error:assert 'expired' in str(error)
|
|
else:raise AssertionError('Stale/foreign source words cannot establish self standing')
|
|
assert inputs()==[('macro','/stand')]
|
|
for invalid in (None,True,0,-1,2**64,'4509'):
|
|
events.clear()
|
|
try:establish_standing(Controller(),Session(),self_guid=invalid)
|
|
except ValueError:pass
|
|
else:raise AssertionError('Unverified self identity accepted')
|
|
assert events==[]
|
|
print('Original posture actions require fresh ACKs and actual matching self stand words; known state reuse, bounded fresh-cycle recovery, busy polls, stale/foreign words, session loss and explicit ABI2 opt-out verified. No UI/client invoked.')
|