7279b3c207
Build / Build source distribution (push) Failing after 13s
Build / Execute unit tests (push) Failing after 8s
Build / Verify generated game data is current (push) Failing after 7s
Build / Upload Release Asset (push) Has been skipped
Build / upload_pypi (push) Has been skipped
194 lines
6.2 KiB
Python
194 lines
6.2 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
|
|
|
|
|
|
def test_summarize_breaks_damage_down_per_player():
|
|
"""A combined total next to "main player" reads as that player's damage.
|
|
|
|
Two players damaged a mob; the report must attribute each share rather
|
|
than printing one ambiguous number.
|
|
"""
|
|
events = [
|
|
{'name': 'MainPlayerAppeared', 'value': {'id': 0, 'name': 'Alice'}},
|
|
{'name': 'PlayerAppeared', 'value': {'id': 1, 'name': 'Bob'}},
|
|
{'name': 'DamageDone', 'value': {'source': 1, 'value': 1309.0}},
|
|
{'name': 'DamageDone', 'value': {'source': 1, 'value': 100.0}},
|
|
{'name': 'DamageDone', 'value': {'source': 0, 'value': 232.0}},
|
|
{'name': 'DamageTaken', 'value': {'source': 1, 'value': 244.0}},
|
|
]
|
|
|
|
report = summarize(events)
|
|
|
|
assert 'Bob: 1409.00' in report
|
|
assert 'Alice: 232.00' in report
|
|
# the party-wide figure is still reported, but labelled as such
|
|
assert 'damage done: 1641.00 (all players)' in report
|
|
assert 'damage taken by player:' in report
|
|
assert 'Bob: 244.00' in report
|
|
|
|
|
|
def test_summarize_labels_unknown_player_ids():
|
|
events = [
|
|
{'name': 'MainPlayerAppeared', 'value': {'id': 0, 'name': 'Alice'}},
|
|
{'name': 'DamageDone', 'value': {'source': 7, 'value': 50.0}},
|
|
]
|
|
|
|
report = summarize(events)
|
|
|
|
# no appearance event for id 7, so it must not be silently attributed
|
|
assert 'player #7: 50.00' in report
|
|
|
|
|
|
def test_summarize_reports_empty_damage_sections():
|
|
report = summarize([
|
|
{'name': 'MainPlayerAppeared', 'value': {'id': 0, 'name': 'Alice'}},
|
|
])
|
|
|
|
assert 'damage done by player:' in report
|
|
assert 'damage taken by player:' in report
|
|
assert '(none)' 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
|