新增 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>
124 lines
4.3 KiB
Python
124 lines
4.3 KiB
Python
"""douyin/fudai/douyin_fudai_core_pkg/ 子包单元测试。
|
||
|
||
覆盖:ocr_executor(线程池复用)+ thin_wrappers(11 个兼容性 forwarding)。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from concurrent.futures import ThreadPoolExecutor
|
||
|
||
import pytest
|
||
|
||
from douyin.fudai.douyin_fudai_core_pkg import (
|
||
_FudaiOcrExecutorState,
|
||
_fudai_ocr_executor_state,
|
||
_get_fudai_ocr_executor,
|
||
attend_choujiang,
|
||
basic_join_decision,
|
||
check_have_reward_notice_confirm,
|
||
check_in_order_confirm_page,
|
||
check_lucky_draw_result,
|
||
get_reward,
|
||
is_watch_task_fudai,
|
||
should_skip_fudai_by_config,
|
||
_is_short_countdown_text,
|
||
)
|
||
|
||
|
||
# ============================================================
|
||
# ocr_executor.py
|
||
# ============================================================
|
||
|
||
class TestFudaiOcrExecutorState:
|
||
def test_default_state(self):
|
||
state = _FudaiOcrExecutorState()
|
||
assert state.executor is None
|
||
assert state.workers == 0
|
||
assert state.lock is not None # threading.Lock
|
||
|
||
def test_global_singleton_exists(self):
|
||
# 模块级单例应可访问
|
||
assert _fudai_ocr_executor_state is not None
|
||
assert isinstance(_fudai_ocr_executor_state, _FudaiOcrExecutorState)
|
||
|
||
|
||
class TestGetFudaiOcrExecutor:
|
||
def test_returns_executor(self):
|
||
ex = _get_fudai_ocr_executor(2)
|
||
assert isinstance(ex, ThreadPoolExecutor)
|
||
|
||
def test_caches_when_workers_match(self):
|
||
# 相同 workers 应返回同一对象
|
||
ex1 = _get_fudai_ocr_executor(4)
|
||
ex2 = _get_fudai_ocr_executor(4)
|
||
assert ex1 is ex2
|
||
|
||
def test_recreates_when_workers_change(self):
|
||
# 不同 workers 应重建(旧 executor 被 shutdown)
|
||
ex_a = _get_fudai_ocr_executor(2)
|
||
ex_b = _get_fudai_ocr_executor(8)
|
||
# 不强求 is not,但 max_workers 应该是新值
|
||
assert _fudai_ocr_executor_state.workers == 8
|
||
|
||
|
||
# ============================================================
|
||
# thin_wrappers.py — 11 个 forwarding 函数都应可调用
|
||
# ============================================================
|
||
|
||
class TestThinWrappersExist:
|
||
"""验证 11 个 forwarding 函数全部可调用且签名正确。"""
|
||
|
||
@pytest.mark.parametrize(
|
||
"fn",
|
||
[
|
||
attend_choujiang,
|
||
check_lucky_draw_result,
|
||
check_have_reward_notice_confirm,
|
||
check_in_order_confirm_page,
|
||
get_reward,
|
||
is_watch_task_fudai,
|
||
_is_short_countdown_text,
|
||
should_skip_fudai_by_config,
|
||
basic_join_decision,
|
||
],
|
||
)
|
||
def test_callable(self, fn):
|
||
assert callable(fn)
|
||
|
||
|
||
class TestForwardedBehavior:
|
||
"""通过薄壳调用应等价于直接调用 fudai_filter / fudai_attend / fudai_result_checker 的实现。"""
|
||
|
||
def test_is_watch_task_fudai_forwarding(self):
|
||
# is_watch_task_fudai 是 fudai_filter 中已有完整测试的函数
|
||
# 通过本子包的薄壳调用应得同结果
|
||
from douyin.fudai.core.fudai_filter import is_watch_task_fudai as direct
|
||
assert is_watch_task_fudai("观看3分钟即可参与") == direct("观看3分钟即可参与")
|
||
assert is_watch_task_fudai("现金红包") == direct("现金红包")
|
||
|
||
def test_is_short_countdown_text_forwarding(self):
|
||
from douyin.fudai.core.fudai_filter import _is_short_countdown_text as direct
|
||
for text in ["00:00", "00:30", "5分钟", "abc", ""]:
|
||
assert _is_short_countdown_text(text) == direct(text)
|
||
|
||
def test_should_skip_fudai_by_config_forwarding(self):
|
||
"""通过薄壳调用应等价于 fudai_filter 的实现。"""
|
||
import time
|
||
from douyin.fudai.core.fudai_filter import should_skip_fudai_by_config as direct
|
||
|
||
class M:
|
||
def __init__(self):
|
||
self.needswitch = True
|
||
self.min_prize_value_yuan = 0.0
|
||
self.min_participants = 0
|
||
self.max_participants = 0
|
||
self.skip_if_countdown_lt_seconds = 0
|
||
self.contains_want = []
|
||
self.contains_not_want = []
|
||
def _log_kv(self, *a, **kw): pass
|
||
|
||
detail = ('现金1000元', '2分钟', 50, 1, 1000.0, (time.time()+120, time.time()+120))
|
||
thru_thin = should_skip_fudai_by_config(M(), detail)
|
||
thru_direct = direct(M(), detail)
|
||
# 结果相同
|
||
assert thru_thin == thru_direct
|