Files
douyi_guaji_main/tests/test_stats_formatter.py
T
CodexandClaude Opus 4.7 230c0d4049 refactor(gui): 抽 refresh_stats 字符串格式化到 stats_formatter (6339 → 5634)
gui_app.py 的 refresh_stats 闭包是 705 行纯字符串构建:把 runner.get_stats()
的字典格式化成多个分段(基本信息/快速汇总/核心统计/跳过/OCR/详情/福袋查找/
性能/粉丝团/切换/预约/错误/价值/最近福袋/中奖记录)。

抽到 gui/components/stats_formatter.py:
- format_stats(stats: Dict) -> List[str] 主入口
- 14 个 _append_* 子段构建器(每段一个,按需追加)
- _section_lines(title, body) 标题包装

闭包改为 16 行委托:调 format_stats + 写入 ScrolledText。

新增 32 个单元测试覆盖:
- 空 dict / 缺字段时的兜底
- 基本运行状态(uptime / pid / exit_code 各种格式)
- 各分段的"有数据 / 无数据"条件
- 关键率计算(打开成功率/点击率/参与成功率/中奖率/跳过率/错误频率)
- 边界情况(attend_ok > clicks 检测到已参与,结果未开奖警告,wins 截断到 10 条)

1393 passed (+32)。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 03:23:53 +08:00

360 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""gui/components/stats_formatter 单元测试。
format_stats 是纯函数:dict → list[str]。覆盖:
- 空 stats / 缺少字段:兜底输出占位
- 基本运行状态格式化
- 各分段(核心/跳过/OCR/详情/福袋查找/粉丝团/切换/预约/错误/价值/最近福袋/中奖)
"""
from __future__ import annotations
import pytest
from gui.components.stats_formatter import format_stats
# ============================================================
# 边界
# ============================================================
class TestEmptyStats:
def test_empty_dict_returns_basic_info(self):
lines = format_stats({})
# 至少有"运行状态信息"标题
joined = "\n".join(lines)
assert "运行状态信息" in joined
assert "未运行" in joined # running 缺失等价于 False
def test_no_counters_no_extra_sections(self):
lines = format_stats({"running": False, "counters": {}})
joined = "\n".join(lines)
assert "暂无统计数据" not in joined # 至少有运行状态段
# 不应出现具体业务统计标题
assert "OCR识别统计" not in joined
assert "粉丝团相关统计" not in joined
class TestBasicInfo:
def test_running_true(self):
lines = format_stats({"running": True, "pid": 1234})
joined = "\n".join(lines)
assert "运行中" in joined
assert "PID: 1234" in joined
def test_uptime_format(self):
# 1 小时 23 分 45 秒 = 5025 秒
lines = format_stats({"uptime_seconds": 5025})
joined = "\n".join(lines)
assert "运行时长: 01:23:45" in joined
def test_uptime_zero(self):
lines = format_stats({"uptime_seconds": 0})
assert any("运行时长: -" == line for line in lines)
def test_exit_code_present(self):
lines = format_stats({"exit_code": 137})
assert any("退出码: 137" in line for line in lines)
def test_exit_code_none(self):
lines = format_stats({})
assert any("退出码: -" == line for line in lines)
# ============================================================
# 快速汇总段
# ============================================================
class TestQuickSummary:
def test_quick_summary_appears_with_data(self):
lines = format_stats({
"uptime_seconds": 7200, # 2 小时
"counters": {
"fudai_seen_unique": 10,
"fudai_attend_ok_unique": 5,
"fudai_result_win": 1,
"fudai_order_success": 1,
},
})
joined = "\n".join(lines)
assert "快速汇总" in joined
assert "识别福袋: 10 个" in joined
assert "运行效率: 2.5 次参与/小时" in joined # 5/2 hours
def test_quick_summary_skipped_when_all_zero(self):
lines = format_stats({"counters": {}})
joined = "\n".join(lines)
assert "快速汇总" not in joined
# ============================================================
# 核心统计段
# ============================================================
class TestCoreStats:
def test_open_rate_calculated(self):
lines = format_stats({
"counters": {
"fudai_seen_unique": 100,
"fudai_open_detail_unique": 80,
},
})
joined = "\n".join(lines)
assert "打开成功率: 80.0%" in joined
def test_click_rate_calculated(self):
lines = format_stats({
"counters": {
"fudai_open_detail_unique": 50,
"fudai_click_attend_unique": 25,
},
})
joined = "\n".join(lines)
assert "点击率: 50.0%" in joined
def test_attend_success_rate_via_clicks(self):
lines = format_stats({
"counters": {
"fudai_click_attend_unique": 10,
"fudai_attend_ok_unique": 8,
},
})
joined = "\n".join(lines)
assert "参与成功率: 80.0%" in joined
def test_attend_ok_exceeds_clicks_shows_detected(self):
"""attend_ok > click_attend → 显示"检测到已参与"提示。"""
lines = format_stats({
"counters": {
"fudai_click_attend_unique": 5,
"fudai_attend_ok_unique": 8,
},
})
joined = "\n".join(lines)
assert "其中 3 次为检测到已参与" in joined
def test_pending_results_warning(self):
"""attend_ok > result_win + result_no_win → 显示"未开奖"提示。"""
lines = format_stats({
"counters": {
"fudai_attend_ok_unique": 10,
"fudai_result_win": 3,
"fudai_result_no_win": 5,
},
})
joined = "\n".join(lines)
assert "有 2 次参与成功尚未开奖" in joined
# ============================================================
# 各业务分段
# ============================================================
class TestSkipStats:
def test_skip_stats_with_data(self):
lines = format_stats({
"counters": {
"fudai_seen_unique": 100,
"fudai_skip_value_low": 30,
"fudai_skip_content": 20,
},
})
joined = "\n".join(lines)
assert "跳过原因统计" in joined
assert "总跳过次数: 50" in joined
assert "跳过率: 50.0%" in joined
def test_skip_stats_skipped_when_no_data(self):
lines = format_stats({"counters": {}})
joined = "\n".join(lines)
assert "跳过原因统计" not in joined
class TestStageDiagStats:
def test_appears_with_data(self):
lines = format_stats({
"counters": {
"stage_diagnosis_complete": 90,
"stage_diagnosis_failed": 10,
},
})
joined = "\n".join(lines)
assert "阶段诊断统计" in joined
assert "成功率: 90.0%" in joined
class TestOcrStats:
def test_appears_with_data(self):
lines = format_stats({
"counters": {
"ocr_attempt": 100,
"ocr_success": 75,
"ocr_failed": 20,
},
})
joined = "\n".join(lines)
assert "OCR识别统计" in joined
assert "OCR成功率: 75.0%" in joined
# 100 - (75+20) = 5 → 显示"未记录结果"
assert "5 次尝试未记录结果" in joined
class TestFansclubStats:
def test_appears_with_data(self):
lines = format_stats({
"counters": {
"fansclub_required": 5,
"fansclub_manual_needed": 2,
},
})
joined = "\n".join(lines)
assert "粉丝团相关统计" in joined
# 总数 7,required 5 → 71.4%
assert "需要粉丝团: 5" in joined
class TestSwitchStats:
def test_with_uptime_shows_frequency(self):
lines = format_stats({
"uptime_seconds": 3600, # 1 小时
"counters": {
"switch_room": 30,
"switch_room_no_fudai": 20,
},
})
joined = "\n".join(lines)
assert "直播间切换统计" in joined
assert "切换频率: 30.0 次/小时" in joined
class TestReservationStats:
def test_recorded_only(self):
lines = format_stats({
"counters": {
"reservation_recorded": 5,
},
})
joined = "\n".join(lines)
assert "预约相关统计(详细)" in joined
assert "记录预约总数: 5" in joined
def test_back_rate_calculated(self):
lines = format_stats({
"counters": {
"reservation_recorded": 10,
"reservation_back_success": 8,
},
})
joined = "\n".join(lines)
assert "预约回退成功率: 80.0%" in joined
class TestErrorStats:
def test_error_total(self):
lines = format_stats({
"counters": {"error_total": 15},
})
joined = "\n".join(lines)
assert "错误统计(详细)" in joined
assert "总错误数: 15" in joined
def test_python_errors_grouped(self):
lines = format_stats({
"counters": {
"error_unbound_local": 1,
"error_attribute": 2,
"error_key": 3,
},
})
joined = "\n".join(lines)
assert "Python异常类型:" in joined
assert "AttributeError: 2" in joined
def test_module_errors_grouped(self):
lines = format_stats({
"counters": {
"error_ocr": 5,
"error_screenshot": 3,
},
})
joined = "\n".join(lines)
assert "功能模块错误:" in joined
assert "OCR识别错误: 5" in joined
def test_error_rate_per_hour(self):
lines = format_stats({
"uptime_seconds": 3600,
"counters": {"error_total": 10},
})
joined = "\n".join(lines)
assert "错误频率: 10.00 次/小时" in joined
# ============================================================
# 价值统计 + 最近福袋 + 中奖记录
# ============================================================
class TestValueStats:
def test_appears_with_events(self):
lines = format_stats({
"events": [
{"meta": {"prize_value_yuan": 600}},
{"meta": {"prize_value_yuan": 200}},
{"meta": {"prize_value_yuan": 50}},
],
})
joined = "\n".join(lines)
assert "价值统计" in joined
assert "总价值: 850.00 元" in joined
# 高/中/低各 1 个 = 33.3%
assert "高价值(≥500元): 1 (33.3%)" in joined
def test_skipped_when_no_events(self):
lines = format_stats({"events": []})
joined = "\n".join(lines)
assert "价值统计" not in joined
class TestFudaiLast:
def test_appears_with_content(self):
lines = format_stats({
"fudai_last": {
"content": "iPhone 15",
"countdown": "05:30",
"participants": 1000,
"copies": 10,
},
})
joined = "\n".join(lines)
assert "最近一次福袋信息" in joined
assert "内容: iPhone 15" in joined
assert "倒计时: 05:30" in joined
# 1000/10 = 100,中奖概率 = 1/100 = 1.00%
assert "中奖概率: 1.00%" in joined
def test_skipped_when_no_content_no_countdown(self):
lines = format_stats({"fudai_last": {}})
joined = "\n".join(lines)
assert "最近一次福袋信息" not in joined
class TestWins:
def test_appears_with_data(self):
lines = format_stats({
"wins": [
{"ts": 1700000000, "content": "iPhone 15", "prize_value_yuan": 5999},
],
})
joined = "\n".join(lines)
assert "最近中奖记录" in joined
assert "iPhone 15" in joined
assert "5999" in joined
def test_truncates_to_10(self):
wins = [{"ts": 1700000000 + i, "content": f"奖品{i}"} for i in range(20)]
lines = format_stats({"wins": wins})
joined = "\n".join(lines)
# 最后 10 条应该是 10..19
assert "奖品10" in joined
assert "奖品19" in joined
# 前 10 条不该出现
assert "奖品0\n" not in joined # tricky, but 奖品0 不该作为单独条目