Files
douyi_guaji_main/tests/test_yanghao_pkg.py
T
CodexandClaude Opus 4.7 e417acfb7c 为 10 个已拆出的子包补单元测试(+226 用例)
新增 tests/ 下 10 个测试文件,把已拆出的子包从"被外部测试间接覆盖"提升到
"独立可测":

- test_web_gui_pkg (52)
  覆盖 logger / ansi / adb_compat / nested / stats_helpers
  含 _wg_log/_LOGGER 日志、ANSI 清理、点号路径、_parse_countdown_seconds 等
- test_underlying_operations_pkg (35)
  覆盖 adb_helpers / bounds / countdown / ocr_content / pic_path
  含 _adb_path 环境变量覆盖、check_countdown 多种格式、OCR 字符纠错
- test_reservation_logic_helpers (21)
  覆盖 helpers.py 5 个纯函数(_now_ts / _normalize_room_title /
  _calculate_string_similarity / _extract_content_core /
  _sort_reservations_inplace 排序稳定性)
- test_screenshot_manager_pkg (47)
  覆盖 paths.get_category_for_picname(5 类关键词分类)+
  get_latest_attempt_path / get_last_success_path(按 operation_type 路径推断)
  + 异步计数器
- test_gui_core_pkg (13)
  覆盖 RunParams dataclass 28 字段构造 + adb 函数 + read_config / write_config
  roundtrip
- test_fudai_core_pkg (17)
  覆盖 _FudaiOcrExecutorState 单例 + _get_fudai_ocr_executor 缓存复用 + 11
  个 thin_wrappers 与原实现等价行为
- test_handler_shells (8)
  覆盖 ReservationHandler / StageHandler / LoggingWrapper 三个薄壳类的
  staticmethod forwarding 完整性
- test_yanghao_pkg (14)
  覆盖 _yanghao_log(_log_kv 优先 + logger fallback + broken analyser
  兜底 + 未知 level 不抛)+ yanghao_get_recommend_seconds 默认值
- test_pixel_common (6)
  覆盖 _safe_open_screenshot_rgb 用 tmp_path 构造合法/损坏/截断/RGBA PNG
- test_fudai_finder_pkg (13)
  覆盖 5 个类(FudaiImageAcquirer / FudaiDetector / FudaiClickExecutor /
  FudaiDetailVerifier / FudaiFinder)实例化与组合关系,verify
  detect_by_template 类方法签名仍兼容(拆出后)

回归:
- 全套测试 355 → 581(+226 用例)
- 全部 581 passed / 1 skipped in 27s
- 新拆出的 12 个子包现在都有专门的单元测试覆盖

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 20:54:35 +08:00

93 lines
3.2 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.
"""douyin/other/douyin_yanghao_pkg/ 子包单元测试。
覆盖 logger(_yanghao_log)、entrypoints(yanghao_get_recommend_seconds 等)。
yanghao_idle 仍在原文件,不在本子包测试范围。
"""
from __future__ import annotations
import logging
import pytest
from douyin.other.douyin_yanghao_pkg import (
_LOGGER,
_yanghao_log,
yanghao_get_recommend_seconds,
)
# ============================================================
# logger.py — _yanghao_log
# ============================================================
class TestYanghaoLog:
def test_logger_name(self):
assert _LOGGER.name == "douyin.other.douyin_yanghao"
def test_with_log_kv_analyser_uses_it(self):
"""analyser 提供 _log_kv 时应优先调用,不走 logger。"""
captured = []
class A:
def _log_kv(self, *args, **kwargs):
captured.append((args, kwargs))
_yanghao_log(A(), "[test] hello", level="INFO")
assert len(captured) == 1, "_log_kv 应被调用 1 次"
def test_without_log_kv_uses_logger(self, caplog):
"""analyser 无 _log_kv 时走 logger。"""
class A: pass
with caplog.at_level(logging.INFO, logger="douyin.other.douyin_yanghao"):
_yanghao_log(A(), "[test] fallback", level="INFO")
# 至少捕获到一条记录
assert any("[test] fallback" in r.getMessage() for r in caplog.records)
def test_none_analyser_does_not_raise(self):
# 传 None 应兜底到 logger
_yanghao_log(None, "[test] none", level="WARNING")
def test_broken_log_kv_does_not_raise(self):
"""analyser._log_kv 抛异常应被吞掉,不影响调用方。"""
class Broken:
def _log_kv(self, *args, **kwargs):
raise RuntimeError("broken")
_yanghao_log(Broken(), "[test] broken", level="ERROR")
@pytest.mark.parametrize("level", ["INFO", "WARNING", "ERROR", "DEBUG"])
def test_known_levels(self, level):
# 已知级别应不抛
_yanghao_log(None, f"[test] {level}", level=level)
def test_unknown_level_does_not_raise(self):
_yanghao_log(None, "[test] unknown", level="FOOBAR")
# ============================================================
# entrypoints.py — yanghao_get_recommend_seconds
# ============================================================
class TestYanghaoGetRecommendSeconds:
def test_default_attribute(self):
class A:
yanghao_recommend_duration_seconds = 100
assert yanghao_get_recommend_seconds(A()) == 100
def test_missing_attribute_uses_default(self):
class A: pass # 没 yanghao_recommend_duration_seconds
result = yanghao_get_recommend_seconds(A())
# 默认应为 60
assert result == 60
def test_zero_or_negative_uses_default(self):
class A:
yanghao_recommend_duration_seconds = 0
# 0 应该被替换为默认 60
result = yanghao_get_recommend_seconds(A())
assert result == 60
def test_returns_int(self):
class A:
yanghao_recommend_duration_seconds = 200
assert isinstance(yanghao_get_recommend_seconds(A()), int)