147 lines
4.6 KiB
Python
147 lines
4.6 KiB
Python
import json
|
|
import os
|
|
import re
|
|
import time
|
|
from collections import Counter
|
|
|
|
import pytest
|
|
|
|
from src.decode.pipeline import Pipeline
|
|
from src.net.replay import ReplayEngine, load_payloads
|
|
from src.replay_cli import summarize
|
|
|
|
RAW_LOG_PATH = os.path.join(
|
|
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'aoaddons.log')
|
|
|
|
|
|
def _write_jsonl(path, payloads):
|
|
with open(path, 'w', encoding='utf-8') as out:
|
|
out.write(json.dumps({'type': 'session_start'}) + '\n')
|
|
for i, payload in enumerate(payloads):
|
|
out.write(json.dumps({
|
|
'type': 'udp_payload',
|
|
'timestamp': f'2026-10-10T00:00:{i:02d}.000+00:00',
|
|
'payload_hex': payload.hex(),
|
|
}) + '\n')
|
|
out.write(json.dumps({'type': 'game_message', 'name': 'UpdateFame'}) + '\n')
|
|
|
|
|
|
def test_load_payloads_reads_jsonl_and_ignores_other_records(tmp_path):
|
|
path = tmp_path / 'capture.jsonl'
|
|
_write_jsonl(str(path), [b'\x01\x02', b'\x03\x04\x05'])
|
|
|
|
assert load_payloads(str(path)) == [b'\x01\x02', b'\x03\x04\x05']
|
|
|
|
|
|
def test_load_payloads_supports_legacy_raw_format(tmp_path):
|
|
path = tmp_path / 'aoaddons.log'
|
|
path.write_text(
|
|
'some log line\n'
|
|
'Raw payload: [1, 2, 3]\n'
|
|
'unrelated\n'
|
|
'Raw payload: [4, 5]\n',
|
|
encoding='utf-8')
|
|
|
|
assert load_payloads(str(path)) == [b'\x01\x02\x03', b'\x04\x05']
|
|
|
|
|
|
def test_replay_engine_start_fails_for_missing_file():
|
|
engine = ReplayEngine(lambda payload: None, path='/does/not/exist.jsonl')
|
|
assert engine.start() is False
|
|
|
|
|
|
def test_replay_engine_feeds_payloads_in_order(tmp_path):
|
|
path = tmp_path / 'capture.jsonl'
|
|
_write_jsonl(str(path), [b'\xaa', b'\xbb', b'\xcc'])
|
|
|
|
received = []
|
|
engine = ReplayEngine(received.append, path=str(path), speed=0.0)
|
|
assert engine.start() is True
|
|
engine.wait(timeout=5)
|
|
|
|
assert received == [b'\xaa', b'\xbb', b'\xcc']
|
|
assert engine.packet_count == 3
|
|
|
|
|
|
def test_replay_engine_paces_with_timestamps(tmp_path):
|
|
path = tmp_path / 'capture.jsonl'
|
|
with open(path, 'w', encoding='utf-8') as out:
|
|
out.write(json.dumps({'type': 'session_start'}) + '\n')
|
|
for i in range(3):
|
|
out.write(json.dumps({
|
|
'type': 'udp_payload',
|
|
'timestamp': f'2026-10-10T00:00:00.{i * 100:03d}+00:00',
|
|
'payload_hex': 'aa',
|
|
}) + '\n')
|
|
|
|
started = time.monotonic()
|
|
engine = ReplayEngine(lambda payload: None, path=str(path), speed=1.0)
|
|
engine.start()
|
|
engine.wait(timeout=5)
|
|
elapsed = time.monotonic() - started
|
|
|
|
assert elapsed >= 0.15
|
|
|
|
|
|
def test_replay_engine_ignores_duplicate_start(tmp_path):
|
|
path = tmp_path / 'capture.jsonl'
|
|
_write_jsonl(str(path), [b'\xaa'])
|
|
|
|
engine = ReplayEngine(lambda payload: None, path=str(path))
|
|
assert engine.start() is True
|
|
assert engine.start() is True
|
|
engine.wait(timeout=5)
|
|
assert engine.packet_count == 1
|
|
|
|
|
|
def test_summarize_reports_fame_silver_and_player():
|
|
events = [
|
|
{'name': 'MainPlayerAppeared', 'value': {'id': 0, 'name': 'Alice'}},
|
|
{'name': 'ZoneChange'},
|
|
{'name': 'UpdateFame', 'value': {'value': 857.472}},
|
|
{'name': 'SilverGained', 'value': {
|
|
'value': 64.8351, 'silver_type': 'looted'}},
|
|
{'name': 'DamageDone', 'value': {'source': 1, 'value': 120.0}},
|
|
]
|
|
|
|
report = summarize(events, path='/tmp/capture.jsonl', packet_count=42)
|
|
|
|
assert 'Alice' in report
|
|
assert '857.47' in report
|
|
assert '64.84' in report
|
|
assert '120.00' in report
|
|
assert 'packets: 42' in report
|
|
|
|
|
|
pytestmark = pytest.mark.skipif(
|
|
not os.path.exists(RAW_LOG_PATH),
|
|
reason='aoaddons.log capture fixture is not available')
|
|
|
|
|
|
def _raw_payloads():
|
|
payloads = []
|
|
pattern = re.compile(r'Raw payload: \[([0-9, ]+)\]')
|
|
with open(RAW_LOG_PATH, encoding='utf-8', errors='replace') as log:
|
|
for line in log:
|
|
match = pattern.search(line)
|
|
if match:
|
|
payloads.append(
|
|
bytes(int(part) for part in match.group(1).split(',')))
|
|
return payloads
|
|
|
|
|
|
def test_replay_engine_integration_produces_events(tmp_path):
|
|
path = tmp_path / 'capture.jsonl'
|
|
_write_jsonl(str(path), _raw_payloads())
|
|
|
|
events = []
|
|
pipeline = Pipeline(events.append)
|
|
engine = ReplayEngine(pipeline.on_udp, path=str(path), speed=0.0)
|
|
assert engine.start() is True
|
|
engine.wait(timeout=10)
|
|
|
|
counts = Counter(event['name'] for event in events)
|
|
assert counts['MainPlayerAppeared'] >= 1
|
|
assert counts['PlayerAppeared'] == 2
|
|
assert counts['ZoneChange'] == 3
|