把 run_once_basic 中互相独立的 4 个块抽到 fudai_run_blocks: - handle_waiting_for_lottery_result(等待开奖期间的持续 OCR 检测) - handle_no_attend_timeout(30 分钟未参与超时回到推荐页) - handle_desktop_recovery(误返回桌面后的自救路径) - handle_high_value_room_revisit(高价值房间重访检查) 每个函数返回 bool:True=主流程应 return,False=继续下一阶段。 原方法只剩调用 + 早返回,可读性大幅提升。 新增 13 个单元测试覆盖 4 个块的核心分支,1013 → 1026 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
175 lines
6.0 KiB
Python
175 lines
6.0 KiB
Python
"""单元测试:douyin/core/fudai_modules/fudai_run_blocks.py
|
|
|
|
覆盖从 run_once_basic 提取出的 4 个独立子块的核心分支:
|
|
- 等待开奖结果(超时、未触发、检测到结果)
|
|
- 30 分钟未参与超时
|
|
- 桌面误返回自救
|
|
- 高价值房间重访
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
from douyin.core.fudai_modules import fudai_run_blocks
|
|
|
|
|
|
# ---- 基础桩 ---- #
|
|
|
|
def _make_analyser(**overrides):
|
|
a = MagicMock()
|
|
a._log_kv = MagicMock()
|
|
a._waiting_for_lottery_result = False
|
|
a._lottery_result_expected_ts = 0.0
|
|
a._current_attending_reservation_fudai = False
|
|
a._lottery_wait_start_ts = 0.0
|
|
a.needswitch = True
|
|
a.swipe_up_to_next_live = MagicMock()
|
|
a._handle_lottery_result_by_ocr = MagicMock()
|
|
a._check_lottery_result_by_ocr = MagicMock(return_value=None)
|
|
a.operation = MagicMock()
|
|
a.operation.get_screenshot = MagicMock()
|
|
a.operation.delay = MagicMock()
|
|
a.operation.get_top_package = MagicMock(return_value="com.ss.android.ugc.aweme")
|
|
|
|
a._last_successful_attend_ts = 0.0
|
|
a._no_attend_timeout_minutes = 30.0
|
|
a._run_start_ts = time.time()
|
|
a._return_to_recommend_and_reenter_live = MagicMock()
|
|
|
|
a.high_value_room_revisit_enabled = False
|
|
a.run_once_basic = MagicMock()
|
|
|
|
a.click = MagicMock()
|
|
a.desktop_icon_x = 153
|
|
a.desktop_icon_y = 260
|
|
a.yanghao_fallback_live_x = 100
|
|
a.yanghao_fallback_live_y = 200
|
|
a.yanghao_fallback_enter_live_x = 300
|
|
a.yanghao_fallback_enter_live_y = 400
|
|
a.device_id = "test"
|
|
|
|
for k, v in overrides.items():
|
|
setattr(a, k, v)
|
|
return a
|
|
|
|
|
|
# ---- handle_waiting_for_lottery_result ---- #
|
|
|
|
def test_lottery_not_waiting_returns_false():
|
|
a = _make_analyser(_waiting_for_lottery_result=False)
|
|
assert fudai_run_blocks.handle_waiting_for_lottery_result(a, time.time()) is False
|
|
|
|
|
|
def test_lottery_already_timed_out_clears_flag_and_continues():
|
|
"""超时太久(已经超过 future_ts + 10s):清标志后返回 False(继续主流程)"""
|
|
past_ts = time.time() - 100
|
|
a = _make_analyser(
|
|
_waiting_for_lottery_result=True,
|
|
_lottery_result_expected_ts=past_ts,
|
|
)
|
|
result = fudai_run_blocks.handle_waiting_for_lottery_result(a, time.time())
|
|
assert result is False
|
|
assert a._waiting_for_lottery_result is False
|
|
assert a._current_attending_reservation_fudai is False
|
|
|
|
|
|
def test_lottery_detected_during_wait_handles_and_returns_true():
|
|
future_ts = time.time() + 5
|
|
a = _make_analyser(
|
|
_waiting_for_lottery_result=True,
|
|
_lottery_result_expected_ts=future_ts,
|
|
_lottery_wait_start_ts=time.time(),
|
|
)
|
|
a._check_lottery_result_by_ocr.return_value = "won"
|
|
result = fudai_run_blocks.handle_waiting_for_lottery_result(a, time.time())
|
|
assert result is True
|
|
a._handle_lottery_result_by_ocr.assert_called_once_with("won")
|
|
assert a._waiting_for_lottery_result is False
|
|
|
|
|
|
# ---- handle_no_attend_timeout ---- #
|
|
|
|
def test_no_attend_timeout_with_recent_attend_returns_false():
|
|
a = _make_analyser(_last_successful_attend_ts=time.time() - 60) # 1 分钟前
|
|
assert fudai_run_blocks.handle_no_attend_timeout(a, time.time()) is False
|
|
|
|
|
|
def test_no_attend_timeout_exceeded_with_last_attend_returns_true():
|
|
a = _make_analyser(
|
|
_last_successful_attend_ts=time.time() - 31 * 60, # 31 分钟前
|
|
_no_attend_timeout_minutes=30.0,
|
|
)
|
|
assert fudai_run_blocks.handle_no_attend_timeout(a, time.time()) is True
|
|
a._return_to_recommend_and_reenter_live.assert_called_once()
|
|
|
|
|
|
def test_no_attend_timeout_exceeded_without_attend_returns_true():
|
|
a = _make_analyser(
|
|
_last_successful_attend_ts=0.0,
|
|
_run_start_ts=time.time() - 31 * 60,
|
|
_no_attend_timeout_minutes=30.0,
|
|
)
|
|
assert fudai_run_blocks.handle_no_attend_timeout(a, time.time()) is True
|
|
a._return_to_recommend_and_reenter_live.assert_called_once()
|
|
|
|
|
|
def test_no_attend_timeout_within_run_returns_false():
|
|
a = _make_analyser(
|
|
_last_successful_attend_ts=0.0,
|
|
_run_start_ts=time.time() - 5 * 60, # 5 分钟前启动
|
|
_no_attend_timeout_minutes=30.0,
|
|
)
|
|
assert fudai_run_blocks.handle_no_attend_timeout(a, time.time()) is False
|
|
|
|
|
|
# ---- handle_desktop_recovery ---- #
|
|
|
|
def test_desktop_recovery_in_douyin_returns_false():
|
|
a = _make_analyser()
|
|
a.operation.get_top_package.return_value = "com.ss.android.ugc.aweme"
|
|
assert fudai_run_blocks.handle_desktop_recovery(a) is False
|
|
# 不在桌面,不应该触发 click
|
|
a.click.assert_not_called()
|
|
|
|
|
|
def test_desktop_recovery_on_desktop_triggers_recovery():
|
|
a = _make_analyser()
|
|
a.operation.get_top_package.return_value = "com.android.launcher3"
|
|
assert fudai_run_blocks.handle_desktop_recovery(a) is True
|
|
# 触发自救后,应该 click 桌面图标 + 直播按钮 + 进入直播间(多次)
|
|
assert a.click.call_count >= 2
|
|
# 应该递归调用一次 run_once_basic
|
|
a.run_once_basic.assert_called_once()
|
|
|
|
|
|
def test_desktop_recovery_empty_top_pkg_returns_false():
|
|
a = _make_analyser()
|
|
a.operation.get_top_package.return_value = ""
|
|
assert fudai_run_blocks.handle_desktop_recovery(a) is False
|
|
|
|
|
|
def test_desktop_recovery_top_pkg_exception_returns_false():
|
|
a = _make_analyser()
|
|
a.operation.get_top_package.side_effect = Exception("adb fail")
|
|
assert fudai_run_blocks.handle_desktop_recovery(a) is False
|
|
|
|
|
|
# ---- handle_high_value_room_revisit ---- #
|
|
|
|
def test_revisit_disabled_returns_false():
|
|
a = _make_analyser(high_value_room_revisit_enabled=False)
|
|
assert fudai_run_blocks.handle_high_value_room_revisit(a, time.time()) is False
|
|
|
|
|
|
def test_revisit_enabled_but_module_import_error_returns_false():
|
|
"""import 失败的话异常被吞,返回 False(继续主流程)"""
|
|
a = _make_analyser(high_value_room_revisit_enabled=True)
|
|
# 这里实际会 import douyin.other.douyin_high_value_room,模块存在则可能往下走
|
|
# 我们只需保证不崩
|
|
result = fudai_run_blocks.handle_high_value_room_revisit(a, time.time())
|
|
assert isinstance(result, bool)
|