Files
douyi_guaji_main/tests/test_web_gui_stats_log_parser.py
T

83 lines
2.3 KiB
Python

from __future__ import annotations
from types import SimpleNamespace
from gui.web_gui_pkg.stats_log_parser import update_stats_from_line
def _make_runner() -> SimpleNamespace:
r = SimpleNamespace()
r._fudai_last = {
"content": "",
"countdown": "",
"participants": None,
"copies": None,
"prize_value_yuan": None,
"updated_ts": None,
}
r._recorded_fudai_seen = set()
r._current_trace_id = ""
r._mdc_context = {}
r._counters = {}
r._events = []
def _inc(key: str, n: int = 1) -> None:
r._counters[key] = int(r._counters.get(key, 0)) + int(n)
def _record_event(typ: str, meta: dict | None = None) -> None:
r._events.append({"type": typ, "meta": meta or {}})
def _snapshot_fudai() -> dict:
return {
"content": r._fudai_last.get("content") or "",
"countdown": r._fudai_last.get("countdown") or "",
"participants": r._fudai_last.get("participants"),
"copies": r._fudai_last.get("copies"),
"prize_value_yuan": r._fudai_last.get("prize_value_yuan"),
}
def _record_win() -> None:
return None
r._inc = _inc
r._record_event = _record_event
r._snapshot_fudai = _snapshot_fudai
r._record_win = _record_win
return r
def test_content_filter_log_should_not_override_fudai_content() -> None:
runner = _make_runner()
runner._fudai_last["content"] = "福袋内容A"
update_stats_from_line(
runner,
"[2026-06-01 12:00:00.000] [INFO] [筛选] 内容关键词过滤:通过",
)
assert runner._fudai_last["content"] == "福袋内容A"
def test_countdown_filter_log_should_not_override_countdown() -> None:
runner = _make_runner()
runner._fudai_last["countdown"] = "02:48"
update_stats_from_line(
runner,
"[2026-06-01 12:00:00.000] [INFO] [筛选] 倒计时过滤:通过",
)
assert runner._fudai_last["countdown"] == "02:48"
def test_fudai_content_line_with_extra_fields_extracts_only_content() -> None:
runner = _make_runner()
update_stats_from_line(
runner,
"[2026-06-01 12:00:00.000] [DEBUG] [福袋] 福袋内容:未识别到 按钮文本=直播间,否则会失去抽",
)
assert runner._fudai_last["content"] == "未识别到"