Files
douyi_guaji_main/tests/test_logging_simple_mode.py
T

265 lines
7.6 KiB
Python

from __future__ import annotations
import json
from types import SimpleNamespace
from douyin.logging.douyin_logging_wrapper_pkg.enhanced import enhanced_log_kv
from douyin.logging.douyin_logging_wrapper_pkg.simple import _enhanced_log_kv_simple
def _analyser() -> SimpleNamespace:
return SimpleNamespace(_last_log_tag=None, _log_seq=0)
def test_simple_suppresses_debug_perf_and_screenshot_noise(capsys):
analyser = _analyser()
_enhanced_log_kv_simple(
analyser,
"性能",
"福袋详情关键耗时(ms)",
"INFO",
["[2026-06-03 11:09:05.450]", "[INFO]", "[性能]", "福袋详情关键耗时(ms)"],
"2026-06-03 11:09:05.450",
{"_log_type": "PERF", "total_ms": 123.4},
False,
"",
)
_enhanced_log_kv_simple(
analyser,
"调试图像",
"福袋详情综合标注图已保存",
"INFO",
["[2026-06-03 11:09:08.342]", "[INFO]", "[调试图像]", "福袋详情综合标注图已保存"],
"2026-06-03 11:09:08.342",
{"_log_type": "SCREENSHOT"},
False,
"",
)
assert capsys.readouterr().out == ""
def test_simple_suppresses_flow_and_ocr_raw_debug(capsys):
analyser = _analyser()
_enhanced_log_kv_simple(
analyser,
"流程标志",
"[FLOW_FLAG] fudai_contain:start",
"INFO",
["[2026-06-03 11:09:05.462]", "[INFO]", "[流程标志]", "[FLOW_FLAG] fudai_contain:start"],
"2026-06-03 11:09:05.462",
{"_log_type": "OP"},
False,
"",
)
_enhanced_log_kv_simple(
analyser,
"OCR",
"OCR调试(福袋内容): result[0] keys=['input_path', 'dt_polys', 'rec_texts']",
"INFO",
[
"[2026-06-03 11:09:28.000]",
"[INFO]",
"[OCR]",
"OCR调试(福袋内容): result[0] keys=['input_path', 'dt_polys', 'rec_texts']",
],
"2026-06-03 11:09:28.000",
{"_log_type": "DEBUG"},
False,
"",
)
assert capsys.readouterr().out == ""
def test_simple_keeps_business_summary_and_warnings(capsys):
analyser = _analyser()
enhanced_log_kv(
analyser,
"福袋详情",
"read_fudai_detail_once完成(成功)",
elapsed_seconds=1.2,
value_yuan=198,
)
enhanced_log_kv(
analyser,
"验证",
"倒计时OCR结果完全为空",
level="WARNING",
log_type="DEBUG",
)
out = capsys.readouterr().out
assert "[福袋详情]" in out
assert "read_fudai_detail_once完成(成功)" in out
assert "倒计时OCR结果完全为空" not in out
def test_simple_keeps_non_noise_warning(capsys):
analyser = _analyser()
enhanced_log_kv(
analyser,
"设备",
"ADB设备连接异常",
level="WARNING",
)
out = capsys.readouterr().out
assert "[WARNING]" in out
assert "ADB设备连接异常" in out
def test_simple_stdout_uses_short_daily_line(capsys):
analyser = _analyser()
_enhanced_log_kv_simple(
analyser,
"福袋详情",
"read_fudai_detail_once完成(成功)",
"INFO",
[
"[2026-06-03 11:09:30.208]",
"[INFO]",
"[福袋详情]",
"read_fudai_detail_once完成(成功)",
"elapsed_seconds=25.1 value_yuan=198",
],
"2026-06-03 11:09:30.208",
{"elapsed_seconds": 25.1, "value_yuan": 198},
False,
"",
)
out = capsys.readouterr().out.strip()
assert out.startswith("[11:09:30] [福袋详情]")
assert "[INFO]" not in out
assert "2026-06-03" not in out
assert "价值=198" in out
assert len(out) <= 150
def test_simple_suppresses_ocr_summary_and_validation_noise(capsys):
analyser = _analyser()
_enhanced_log_kv_simple(
analyser,
"福袋详情",
"OCR摘要",
"INFO",
["[2026-06-03 11:09:30.178]", "[INFO]", "[福袋详情]", "OCR摘要"],
"2026-06-03 11:09:30.178",
{"_log_type": "OCR_SUMMARY"},
False,
"",
)
_enhanced_log_kv_simple(
analyser,
"福袋倒计时",
"分割识别失败(分钟或秒为空)",
"INFO",
["[2026-06-03 11:09:30.181]", "[INFO]", "[福袋倒计时]", "分割识别失败"],
"2026-06-03 11:09:30.181",
{},
False,
"",
)
assert capsys.readouterr().out == ""
def test_simple_actions_log_skips_fudai_probe_noise(monkeypatch, tmp_path, capsys):
import douyin.core.path_config as path_config
monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(tmp_path))
monkeypatch.setattr(
path_config, "get_actions_log_path", lambda: str(tmp_path / "actions.log")
)
analyser = _analyser()
_enhanced_log_kv_simple(
analyser,
"[福袋查找] 检测完成",
"[福袋查找] 检测完成",
"INFO",
["[2026-06-18 04:35:40.000]", "[INFO]", "[福袋查找]", "[福袋查找] 检测完成"],
"2026-06-18 04:35:40.000",
{"elapsed_ms": 10.0},
False,
"",
)
capsys.readouterr()
assert not (tmp_path / "actions.log").exists()
def test_simple_actions_log_keeps_real_attend_result(monkeypatch, tmp_path, capsys):
import douyin.core.path_config as path_config
monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(tmp_path))
monkeypatch.setattr(
path_config, "get_actions_log_path", lambda: str(tmp_path / "actions.log")
)
analyser = _analyser()
_enhanced_log_kv_simple(
analyser,
"参与",
"参与成功,设置等待开奖标志",
"INFO",
["[2026-06-18 04:35:40.000]", "[INFO]", "[参与]", "参与成功,设置等待开奖标志"],
"2026-06-18 04:35:40.000",
{},
False,
"",
)
capsys.readouterr()
actions_text = (tmp_path / "actions.log").read_text(encoding="utf-8-sig")
assert "参与成功,设置等待开奖标志" in actions_text
def test_enhanced_log_preserves_simple_and_detail_files(monkeypatch, tmp_path):
import douyin.core.path_config as path_config
config_path = tmp_path / "config.json"
config_path.write_text(
json.dumps({"logging": {"log_format_mode": "simple"}}),
encoding="utf-8",
)
monkeypatch.setattr(path_config, "get_config_path", lambda: str(config_path))
monkeypatch.setattr(
path_config, "get_logs_dir", lambda: str(tmp_path)
)
monkeypatch.setattr(
path_config, "get_actions_log_path", lambda: str(tmp_path / "actions.log")
)
monkeypatch.setattr(
path_config, "get_simple_log_path", lambda: str(tmp_path / "simple.log")
)
monkeypatch.setattr(
path_config, "get_detail_log_path", lambda: str(tmp_path / "detail.log")
)
analyser = _analyser()
enhanced_log_kv(analyser, "福袋详情", "OCR摘要", log_type="OCR_SUMMARY", total_ms=1234.5)
config_path.write_text(
json.dumps({"logging": {"log_format_mode": "detailed"}}),
encoding="utf-8",
)
enhanced_log_kv(analyser, "福袋详情", "OCR摘要2", log_type="OCR_SUMMARY", total_ms=2345.6)
simple_log = next(tmp_path.glob("simple_*.log"))
detail_log = next(tmp_path.glob("detail_*.log"))
assert simple_log.exists()
assert detail_log.exists()
simple_text = simple_log.read_text(encoding="utf-8-sig")
detail_text = detail_log.read_text(encoding="utf-8-sig")
assert "OCR摘要" in simple_text
assert "OCR摘要2" in simple_text
assert "total_ms=1234.50" in detail_text
assert "total_ms=2345.60" in detail_text