Files
douyi_guaji_main/tests/test_fudai_finder_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

152 lines
5.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/fudai/douyin_fudai_finder_pkg/ 子包单元测试。
覆盖 5 个类(FudaiImageAcquirer / FudaiDetector / FudaiClickExecutor /
FudaiDetailVerifier / FudaiFinder)的实例化与组合关系,以及顶层函数
check_have_fudai_with_ready_screenshot_optimized。
业务行为(实际识别 / 点击)依赖真实截图与 ADB,留给端到端测试。
"""
from __future__ import annotations
import pytest
from douyin.fudai.douyin_fudai_finder import (
FudaiClickExecutor,
FudaiDetailVerifier,
FudaiDetector,
FudaiFinder,
FudaiImageAcquirer,
check_have_fudai_with_ready_screenshot_optimized,
)
# ============================================================
# Fake analyser 提供构造时需要的最小 attribute 集合
# ============================================================
class FakeAnalyser:
"""构造 FudaiFinder 链路所需的最小 attribute。"""
def __init__(self):
self.device_id = "test_device"
# FudaiDetector._init_search_regions 用到这些
self.resolution_ratio_x = 1080
self.resolution_ratio_y = 2400
self.y_pianyi = 0
# FudaiClickExecutor 可能用 operation
class _Op: pass
self.operation = _Op()
# ============================================================
# FudaiImageAcquirer
# ============================================================
class TestFudaiImageAcquirer:
def test_construct(self):
acquirer = FudaiImageAcquirer(FakeAnalyser())
assert acquirer is not None
def test_acquire_screenshot_callable(self):
acquirer = FudaiImageAcquirer(FakeAnalyser())
assert callable(getattr(acquirer, "acquire_screenshot", None))
# ============================================================
# FudaiDetector
# ============================================================
class TestFudaiDetector:
def test_construct(self):
d = FudaiDetector(FakeAnalyser())
assert d.analyser is not None
assert d.rx == 1080
assert d.ry == 2400
def test_search_regions_initialized(self):
d = FudaiDetector(FakeAnalyser())
assert hasattr(d, "search_regions")
assert isinstance(d.search_regions, list)
def test_detect_methods_callable(self):
d = FudaiDetector(FakeAnalyser())
for name in ("detect", "detect_by_pixel", "detect_by_template"):
assert callable(getattr(d, name, None)), f"{name} 不可调用"
# ============================================================
# FudaiClickExecutor
# ============================================================
class TestFudaiClickExecutor:
def test_construct(self):
executor = FudaiClickExecutor(FakeAnalyser())
assert executor is not None
# ============================================================
# FudaiDetailVerifier
# ============================================================
class TestFudaiDetailVerifier:
def test_construct(self):
verifier = FudaiDetailVerifier(FakeAnalyser())
assert verifier is not None
# ============================================================
# FudaiFinder(组合 4 个子组件)
# ============================================================
class TestFudaiFinder:
def test_construct_includes_all_components(self):
finder = FudaiFinder(FakeAnalyser())
assert isinstance(finder.acquirer, FudaiImageAcquirer)
assert isinstance(finder.detector, FudaiDetector)
assert isinstance(finder.executor, FudaiClickExecutor)
assert isinstance(finder.verifier, FudaiDetailVerifier)
def test_components_share_analyser(self):
analyser = FakeAnalyser()
finder = FudaiFinder(analyser)
# 4 个子组件都应该持有同一个 analyser 引用
assert finder.acquirer.analyser is analyser
assert finder.detector.analyser is analyser
# ============================================================
# 顶层函数
# ============================================================
class TestCheckHaveFudaiWithReadyScreenshotOptimized:
def test_callable(self):
assert callable(check_have_fudai_with_ready_screenshot_optimized)
# ============================================================
# detect_by_template 已拆出为 module-level 函数
# ============================================================
class TestDetectByTemplateAsModuleLevel:
"""detect_by_template 现在是 module-level 函数,类方法 1 行 forwarding。"""
def test_module_level_callable(self):
from douyin.fudai.douyin_fudai_finder_pkg.detect_by_template import (
detect_by_template as module_fn,
)
assert callable(module_fn)
def test_class_method_callable(self):
# 类方法仍可访问
d = FudaiDetector(FakeAnalyser())
assert callable(d.detect_by_template)
def test_class_method_signature_unchanged(self):
"""类方法签名应仍是 (self, pic, region),对外 API 不变。"""
import inspect
sig = inspect.signature(FudaiDetector.detect_by_template)
params = list(sig.parameters.keys())
# 第一个是 self(unbound),后两个 pic, region
assert "pic" in params
assert "region" in params