把 run_once_basic 中 FANSCLUB_JOIN 分支(166 行)抽到 fudai_run_blocks.handle_fansclub_join_stage: - 主路径:点参与按钮 + 2 次预设位置 + 关详情 - 异常路径:按 auto_join_fansclub 决定重试或等待 15s + 按 fansclub_fail_action 决定切换 douyin_fudai.py 终于跌破 3000 行(2975)。 新增 5 个单元测试覆盖:正常路径 / 自动加入重试 / 等待手动 / 失败切换 / 完成日志。 1102 → 1107 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
245 lines
8.7 KiB
Python
245 lines
8.7 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)
|
|
|
|
|
|
# ---- handle_fansclub_join_stage ---- #
|
|
|
|
def _make_fansclub_analyser():
|
|
a = _make_analyser()
|
|
a.resolution_ratio_x = 1080
|
|
a.resolution_ratio_y = 2400
|
|
a.auto_join_fansclub = False
|
|
a.fansclub_fail_action = "stay"
|
|
a._diagnose_stage_from_current_screen = MagicMock(return_value=("OK", ""))
|
|
return a
|
|
|
|
|
|
def test_fansclub_join_happy_path_clicks_three_positions():
|
|
"""正常路径:3 次点击(参与按钮 + 2 次预设位置 + 关闭详情)= 共 4 次 click"""
|
|
a = _make_fansclub_analyser()
|
|
fudai_run_blocks.handle_fansclub_join_stage(a, time.time())
|
|
# click 调用次数:参与按钮 + 第一次预设 + 第二次预设 + 关闭详情 = 4
|
|
assert a.operation.click.call_count == 4
|
|
|
|
|
|
def test_fansclub_join_failure_with_auto_join_retries():
|
|
"""主路径出错且 auto_join=True → 重新诊断后重试两次点击"""
|
|
a = _make_fansclub_analyser()
|
|
a.auto_join_fansclub = True
|
|
# 让第一次 click 抛错触发 except 分支
|
|
a.operation.click.side_effect = [
|
|
RuntimeError("first click fail"), # 第一次 click 就失败
|
|
] + [None] * 20 # 后续 click 正常
|
|
|
|
a._diagnose_stage_from_current_screen.return_value = ("FANSCLUB_JOIN", "")
|
|
|
|
fudai_run_blocks.handle_fansclub_join_stage(a, time.time())
|
|
# 仍应完成(不抛错)
|
|
a._log_kv.assert_any_call("粉丝团处理", "处理粉丝团加入失败", error="first click fail")
|
|
|
|
|
|
def test_fansclub_join_failure_without_auto_join_waits():
|
|
"""主路径出错且 auto_join=False → 等待 15 秒"""
|
|
a = _make_fansclub_analyser()
|
|
a.auto_join_fansclub = False
|
|
a.operation.click.side_effect = [RuntimeError("boom")] + [None] * 20
|
|
|
|
fudai_run_blocks.handle_fansclub_join_stage(a, time.time())
|
|
# 应当调用 delay(15.0)
|
|
delay_calls = [c.args[0] for c in a.operation.delay.call_args_list]
|
|
assert 15.0 in delay_calls
|
|
|
|
|
|
def test_fansclub_join_failure_with_switch_action_swipes():
|
|
"""主路径出错且 fansclub_fail_action=switch → 切换直播间"""
|
|
a = _make_fansclub_analyser()
|
|
a.fansclub_fail_action = "switch"
|
|
a.auto_join_fansclub = False
|
|
a.operation.click.side_effect = [RuntimeError("boom")] + [None] * 20
|
|
|
|
fudai_run_blocks.handle_fansclub_join_stage(a, time.time())
|
|
a.swipe_up_to_next_live.assert_called_once()
|
|
|
|
|
|
def test_fansclub_join_logs_completion():
|
|
a = _make_fansclub_analyser()
|
|
fudai_run_blocks.handle_fansclub_join_stage(a, time.time())
|
|
completion_calls = [
|
|
c for c in a._log_kv.call_args_list
|
|
if c.args == ("流程", "run_once_basic完成(处理粉丝团)")
|
|
or (len(c.args) >= 2 and c.args[1] == "run_once_basic完成(处理粉丝团)")
|
|
]
|
|
assert len(completion_calls) >= 1
|