883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
72 lines
5.6 KiB
Python
72 lines
5.6 KiB
Python
import ast,importlib.util,json,os,runpy,sys,tempfile,types,unittest
|
|
from collections import Counter
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
SCRIPT=ROOT/"runtime/continuation-20261023/private-packet-capture/observe-owned-numeric-gameplay.py"
|
|
sys.path.insert(0,str(ROOT/"ops"))
|
|
from native_world_trace import WorldTrace
|
|
|
|
def collector():
|
|
tree=ast.parse(SCRIPT.read_text());node=next(n for n in tree.body if isinstance(n,ast.FunctionDef)and n.name=="capture_packet_headers")
|
|
scope={"os":os,"json":json};exec(compile(ast.Module(body=[node],type_ignores=[]),str(SCRIPT),"exec"),scope)
|
|
return scope["capture_packet_headers"]
|
|
|
|
class PacketObserverTest(unittest.TestCase):
|
|
def test_actual_observer_same_snapshot_resume_and_private_headers(self):
|
|
# Header-only synthetic snapshots use the existing WorldTrace owning-fixture schema.
|
|
# They are not evidence of native or server acceptance.
|
|
snapshots=[{"world":{"trace_count":2,"received":1,"trace":[{"direction":1,"opcode":150,"bytes":20},{"direction":3,"opcode":0x4c0001,"bytes":100,"body":"PRIVATE_SENTINEL"}]},"motion":{"actions":[{"sequence":1,"kind":2}]}},
|
|
{"world":{"trace_count":4,"received":2,"trace":[{"direction":1,"opcode":0x3f,"bytes":20},{"direction":3,"opcode":0x4c0001,"bytes":101,"body":"PRIVATE_SENTINEL"}]},"motion":{"actions":[{"sequence":1,"kind":2},{"sequence":2,"kind":2}]}}]
|
|
for snapshot in snapshots:snapshot["world_auth"]=True;snapshot["world"].update(login_sent=1,self_create=True)
|
|
calls=[];auth_calls=[]
|
|
class Controller:
|
|
def __init__(self,*args,**kwargs):pass
|
|
def rpc(self,operation,**kwargs):
|
|
calls.append((operation,kwargs))
|
|
if operation=="inspect-engine-pipe":return snapshots[min(sum(x[0]==operation for x in calls)-1,1)]
|
|
return {"state":1}
|
|
controller_module=types.ModuleType("native_engine_character_test");controller_module.Controller=Controller
|
|
auth_module=types.ModuleType("native_manual_login")
|
|
def ready(*args):auth_calls.append(args);return True
|
|
auth_module.authenticated_world_ready=ready
|
|
with tempfile.TemporaryDirectory()as directory:
|
|
base=Path(directory);bundle=base/"bundle";bundle.mkdir();capture=base/"capture";capture.mkdir(mode=0o700)
|
|
pointer=base/"pointer.json";pointer.write_text(json.dumps({"capture_root":str(capture),"current_packet_baseline_bytes":0}))
|
|
existing=capture/"numeric-session-99999999";existing.mkdir(mode=0o700);marker=existing/"preserved-action-log";marker.write_text("unchanged previous window")
|
|
resume=base/"resume.json";resume.write_text(json.dumps({"client_pid":99999999,"observer_pid":123,"last_sequence":1,"packet_cursor":2,"current_window":4}))
|
|
probes=[0]
|
|
original_exists=Path.exists
|
|
def alive(path):
|
|
if str(path)=="/proc/99999999":probes[0]+=1;return probes[0]<=5
|
|
return original_exists(path)
|
|
argv=[str(SCRIPT),"--bundle",str(bundle),"--pid","99999999","--pointer",str(pointer),"--resume-state",str(resume)]
|
|
with mock.patch.dict(sys.modules,{"native_engine_character_test":controller_module,"native_manual_login":auth_module}),mock.patch.object(sys,"argv",argv),mock.patch.object(Path,"exists",new=alive),mock.patch("time.sleep"):
|
|
runpy.run_path(str(SCRIPT),run_name="__main__")
|
|
output=capture/"numeric-session-99999999"
|
|
headers=[json.loads(x)for x in (output/"packets-0005.jsonl").read_text().splitlines()]
|
|
self.assertEqual([x["index"]for x in headers],[2,3]);self.assertEqual(headers[-1]["stage"],"pe_submit_returned_successfully")
|
|
self.assertNotIn("PRIVATE_SENTINEL",(output/"packets-0005.jsonl").read_text())
|
|
self.assertEqual((output/"packets-0005.jsonl").stat().st_mode&0o777,0o600)
|
|
actions=[json.loads(x)for x in (output/"actions-0005.jsonl").read_text().splitlines()]
|
|
self.assertEqual([x["sequence"]for x in actions],[2])
|
|
state=json.loads((output/"observer-state.json").read_text());self.assertEqual(state["packet_cursor"],4);self.assertEqual(state["last_sequence"],2)
|
|
self.assertEqual(marker.read_text(),"unchanged previous window")
|
|
self.assertEqual(len(auth_calls),1)
|
|
# Initial readiness pipe + two existing loop pipe snapshots; no packet-specific RPC.
|
|
self.assertEqual(Counter(x[0]for x in calls)["inspect-engine-pipe"],3)
|
|
self.assertTrue(all(kwargs.get("unit_fields_only")is True for name,kwargs in calls if name=="inspect-engine-pipe"))
|
|
def test_schema_gap_and_counter_reset_are_metadata_only(self):
|
|
capture=collector();trace=WorldTrace(object(),collect_fields=False);state={}
|
|
with tempfile.TemporaryDirectory()as directory:
|
|
path=Path(directory)/"packets.jsonl"
|
|
capture(trace,{},path,state);self.assertFalse(state["packet_trace"]["available"]);self.assertFalse(path.exists())
|
|
trace.last=1
|
|
capture(trace,{"trace_count":10,"trace":[{"direction":3,"opcode":0x4c0001,"bytes":80,"body":"PRIVATE_SENTINEL"}]},path,state)
|
|
rows=[json.loads(x)for x in path.read_text().splitlines()]
|
|
self.assertEqual(rows[0]["record"],"packet_trace_gap");self.assertEqual(rows[0]["missing"],8);self.assertEqual(rows[1]["index"],9)
|
|
capture(trace,{"trace_count":1,"trace":[{"direction":3,"opcode":0x4c0001,"bytes":80}]},path,state)
|
|
self.assertEqual(state["packet_cursor"],1);self.assertNotIn("PRIVATE_SENTINEL",path.read_text())
|
|
|
|
if __name__=="__main__":unittest.main()
|