"""剩余 _pkg 子包的综合单元测试。 覆盖: - douyin.detection.detection_pkg 检测入口的可调用性 + _ensure_detection_cache - douyin.fudai.core.fudai_detail_orchestrator_pkg read_fudai_detail_once / _with_retries 可调用性 - douyin.logging.douyin_logging_wrapper_pkg.misc 5 个小型日志辅助方法 - douyin.core.douyin_flow_control_pkg.filter filter_fudai_detail 可调用性 - douyin.fudai.douyin_fudai_finder_pkg.detect_by_template module-level 函数 业务函数依赖大量 analyser 状态 + 真实截图,单测只覆盖: - API 兼容(callable / 签名 / 返回类型) - 不抛异常的轻量调用 """ from __future__ import annotations import logging import time import pytest # ============================================================ # detection_pkg # ============================================================ class TestDetectionPkg: def test_imports(self): from douyin.detection.detection_pkg import ( _ensure_detection_cache, check_detail_height, check_have_fudai_with_ready_screenshot, check_in_fudai_detail, ) for fn in [_ensure_detection_cache, check_detail_height, check_have_fudai_with_ready_screenshot, check_in_fudai_detail]: assert callable(fn) def test_ensure_detection_cache_creates_attrs(self): from douyin.detection.detection_pkg import _ensure_detection_cache class A: pass a = A() _ensure_detection_cache(a) # 应注入 _detection_cache + _detection_cache_ttl assert hasattr(a, "_detection_cache") assert isinstance(a._detection_cache, dict) assert hasattr(a, "_detection_cache_ttl") assert isinstance(a._detection_cache_ttl, (int, float)) def test_ensure_detection_cache_idempotent(self): """第二次调用应保留首次设置的 cache 内容(不被覆盖)。""" from douyin.detection.detection_pkg import _ensure_detection_cache class A: pass a = A() _ensure_detection_cache(a) a._detection_cache["key"] = "value" _ensure_detection_cache(a) assert a._detection_cache.get("key") == "value" # ============================================================ # fudai_detail_orchestrator_pkg # ============================================================ class TestFudaiDetailOrchestratorPkg: def test_imports_and_callable(self): from douyin.fudai.core.fudai_detail_orchestrator_pkg import ( read_fudai_detail_once, read_fudai_detail_with_retries, ) assert callable(read_fudai_detail_once) assert callable(read_fudai_detail_with_retries) def test_module_level_matches_re_export(self): from douyin.fudai.core.fudai_detail_orchestrator import ( read_fudai_detail_once as v1, read_fudai_detail_with_retries as v2, ) from douyin.fudai.core.fudai_detail_orchestrator_pkg import ( read_fudai_detail_once as p1, read_fudai_detail_with_retries as p2, ) # 兼容 shim 与子包应指向同一函数对象 assert v1 is p1 assert v2 is p2 # ============================================================ # logging_wrapper_pkg.misc # ============================================================ class TestLoggingWrapperMisc: def test_get_performance_stats_returns_dict(self): from douyin.logging.douyin_logging_wrapper_pkg.misc import get_performance_stats result = get_performance_stats() assert isinstance(result, dict) def test_log_step_time_does_not_raise(self): from douyin.logging.douyin_logging_wrapper_pkg.misc import log_step_time class A: def _log_kv(self, *a, **kw): pass log_step_time(A(), "step1", time.time() - 1.0) def test_log_error_with_context_does_not_raise(self): from douyin.logging.douyin_logging_wrapper_pkg.misc import log_error_with_context class A: def _log_kv(self, *a, **kw): pass try: raise ValueError("test") except ValueError as e: log_error_with_context(A(), e, context={"step": "test"}) def test_log_performance_step_does_not_raise(self): from douyin.logging.douyin_logging_wrapper_pkg.misc import log_performance_step class A: def _log_kv(self, *a, **kw): pass log_performance_step(A(), "step1", elapsed_seconds=0.5, details={"items": 100}) def test_log_debug_info_does_not_raise(self): from douyin.logging.douyin_logging_wrapper_pkg.misc import log_debug_info class A: def _log_kv(self, *a, **kw): pass log_debug_info(A(), "debug msg", key1="value1", key2=42) # ============================================================ # flow_control_pkg.filter(可调用性 + 4 个对外 API 都是 callable) # ============================================================ class TestFlowControlPkg: def test_all_4_functions_callable(self): from douyin.core.douyin_flow_control_pkg import ( filter_fudai_detail, find_and_open_fudai_detail, handle_no_fudai_situation, handle_stage_after_diagnosis, ) for fn in [filter_fudai_detail, find_and_open_fudai_detail, handle_no_fudai_situation, handle_stage_after_diagnosis]: assert callable(fn) def test_module_level_matches_re_export(self): from douyin.core.douyin_flow_control import find_and_open_fudai_detail as v1 from douyin.core.douyin_flow_control_pkg import find_and_open_fudai_detail as v2 assert v1 is v2 # ============================================================ # detect_by_template(module-level) # ============================================================ class TestDetectByTemplateModuleLevel: """已从 FudaiDetector 类拆出为 module-level;类方法变成 forwarding。""" def test_signature(self): import inspect from douyin.fudai.douyin_fudai_finder_pkg.detect_by_template import ( detect_by_template, ) sig = inspect.signature(detect_by_template) params = list(sig.parameters.keys()) # 第一个参数应是 analyser,第二个 rx,第三个 pic,第四个 region assert params[0] == "analyser" assert params[1] == "rx" assert "pic" in params assert "region" in params def test_class_method_still_works(self): """FudaiDetector.detect_by_template 类方法应仍可调用(forwarding)。""" from douyin.fudai.douyin_fudai_finder import FudaiDetector class FakeAnalyser: resolution_ratio_x = 1080 resolution_ratio_y = 2400 y_pianyi = 0 class _Op: pass operation = _Op() d = FudaiDetector(FakeAnalyser()) # 不真的传 PIL Image(因为会触发完整识别),只看类方法存在 assert callable(d.detect_by_template) # ============================================================ # underlying_operations_pkg 第二轮拆分新增(save_reward_pic 等) # ============================================================ class TestUnderlyingOpsR2: def test_save_reward_pic_callable(self): from douyin.core.underlying_operations_pkg import save_reward_pic assert callable(save_reward_pic) def test_set_clipboard_text_callable(self): from douyin.core.underlying_operations_pkg import set_clipboard_text assert callable(set_clipboard_text) def test_set_clipboard_text_empty_returns_false(self): from douyin.core.underlying_operations_pkg import set_clipboard_text # 空文本应直接返回 False,不调用 ADB assert set_clipboard_text("any_device", "") is False assert set_clipboard_text("any_device", " ") is False def test_swipe_with_sendevent_callable(self): from douyin.core.underlying_operations_pkg import _swipe_with_sendevent_improved assert callable(_swipe_with_sendevent_improved) def test_select_device_callable(self): from douyin.core.underlying_operations_pkg import select_device assert callable(select_device) def test_analyse_pic_digits_callable(self): from douyin.core.underlying_operations_pkg import analyse_pic_digits assert callable(analyse_pic_digits)