Files
TSSBOT/tests/test_match_logs.py
T
2026-06-18 04:04:12 -07:00

70 lines
2.4 KiB
Python

import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2] / "SHARED"))
from BOT.match_logs import build_event_log, build_match_logs
def _game():
return {
"_id": "abc",
"winner": "1", "loser": "2",
"tss": {"1": {"name": "TeamWin"}, "2": {"name": "TeamLose"}},
"players": {
"1": {"name": "alice", "tag": "[WIN]", "team": "1",
"units": [{"unit": "ussr_t_34", "used": True}],
"air_kills": 0, "ground_kills": 1, "assists": 0,
"deaths": 0, "captures": 0, "score": 100},
"2": {"name": "bob", "tag": "[LOS]", "team": "2",
"units": [{"unit": "germ_pz_iv", "used": True}],
"air_kills": 0, "ground_kills": 0, "assists": 0,
"deaths": 1, "captures": 0, "score": 10},
},
"chat": [{"uid": "1", "type": "ALL", "message": "gg", "time": 65000}],
"events": {"kills": [{"time": 60000, "offender_uid": 1,
"offender_unit": "ussr_t_34",
"offended_uid": 2, "offended_unit": "germ_pz_iv",
"crashed": False}]},
}
def test_chat_log_format():
chat, _ = build_match_logs(_game())
assert chat == ["+[01:05] [ALL] [TeamWin] `alice`: gg"]
def test_battle_log_kill_prefix_and_text():
_, battle = build_match_logs(_game())
assert len(battle) == 1
line = battle[0]
assert line.startswith("+[01:00] [TeamWin]")
assert "alice" in line and "destroyed" in line and "bob" in line
def test_events_base85_zstd_compressed():
import base64
import json
import zstandard
g = _game()
raw = json.dumps(g["events"]).encode()
g["events"] = base64.b85encode(zstandard.ZstdCompressor().compress(raw)).decode()
_, battle = build_match_logs(g)
assert battle and "destroyed" in battle[0]
def test_event_log_preserves_kill_victim_unit():
event_log = build_event_log(_game())
assert event_log["kills"][0]["offended_uid"] == 2
assert event_log["kills"][0]["offended_unit"] == "germ_pz_iv"
assert event_log["chat"][0]["message"] == "gg"
if __name__ == "__main__":
test_chat_log_format()
test_battle_log_kill_prefix_and_text()
test_events_base85_zstd_compressed()
test_event_log_preserves_kill_victim_unit()
print("ALL TESTS PASSED")