新增 2 个 helper: - handle_just_recorded_reservation:刚记录预约后强制切换直播间 - handle_skip_stage_diagnosis:0.5 跳过阶段检测分支 新增 5 个单元测试。行为保持不变。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
247 lines
9.4 KiB
Python
247 lines
9.4 KiB
Python
"""run_once_basic_helpers 单元测试。
|
|
|
|
覆盖:
|
|
- handle_waiting_for_lottery_result:标志未设 / 已超时分支
|
|
- handle_no_attend_timeout:无超时 / 从未参与超时 / 已参与后超时
|
|
- handle_desktop_recovery:抖音前台 / 非抖音前台触发自救
|
|
- handle_high_value_room_revisit:未启用直接 False
|
|
- screenshot_and_diagnose:截图失败返回 None
|
|
- dispatch_stage_branch:弹窗 / 直播结束 / 未匹配阶段
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
from douyin.core.fudai_modules import run_once_basic_helpers as ronce
|
|
|
|
|
|
def _mk_self():
|
|
"""构造一个尽量贴近 fudai_analyse 的 mock,包含本模块用到的属性和方法。"""
|
|
s = MagicMock()
|
|
# 数值属性
|
|
s._lottery_result_expected_ts = 0.0
|
|
s._no_attend_timeout_minutes = 30.0
|
|
s._last_successful_attend_ts = 0.0
|
|
s._run_start_ts = 0.0
|
|
s._waiting_for_lottery_result = False
|
|
s._current_attending_reservation_fudai = False
|
|
s.high_value_room_revisit_enabled = False
|
|
s.resolution_ratio_x = 1080
|
|
s.resolution_ratio_y = 2400
|
|
s.device_id = "MOCK"
|
|
# _log_kv 不抛
|
|
s._log_kv = MagicMock(return_value=None)
|
|
return s
|
|
|
|
|
|
# ============================================================
|
|
# handle_waiting_for_lottery_result
|
|
# ============================================================
|
|
|
|
class TestHandleWaitingForLotteryResult:
|
|
def test_flag_not_set_returns_false(self):
|
|
s = _mk_self()
|
|
s._waiting_for_lottery_result = False
|
|
assert ronce.handle_waiting_for_lottery_result(s, 0.0) is False
|
|
|
|
def test_already_timed_out_clears_flags_and_returns_false(self):
|
|
"""超时分支不 return(继续走后续步骤)。"""
|
|
s = _mk_self()
|
|
s._waiting_for_lottery_result = True
|
|
s._lottery_result_expected_ts = 1.0 # 远古时间,肯定超时
|
|
result = ronce.handle_waiting_for_lottery_result(s, 0.0)
|
|
assert result is False
|
|
assert s._waiting_for_lottery_result is False
|
|
assert s._current_attending_reservation_fudai is False
|
|
|
|
|
|
# ============================================================
|
|
# handle_no_attend_timeout
|
|
# ============================================================
|
|
|
|
class TestHandleNoAttendTimeout:
|
|
def test_no_timeout_returns_false(self):
|
|
s = _mk_self()
|
|
# 刚跑起来,没超时
|
|
import time
|
|
s._run_start_ts = time.time()
|
|
s._last_successful_attend_ts = 0.0
|
|
s._no_attend_timeout_minutes = 30.0
|
|
assert ronce.handle_no_attend_timeout(s, 0.0) is False
|
|
|
|
def test_never_attended_timeout_triggers(self):
|
|
"""从未成功参与过且超时:触发回到推荐页 + return True。"""
|
|
s = _mk_self()
|
|
s._run_start_ts = 1.0 # 远古非零,避免 or current_ts 的短路
|
|
s._last_successful_attend_ts = 0.0
|
|
s._no_attend_timeout_minutes = 30.0
|
|
# 替换 _return_to_recommend_and_reenter_live 为 mock
|
|
s._return_to_recommend_and_reenter_live = MagicMock(return_value=None)
|
|
assert ronce.handle_no_attend_timeout(s, 0.0) is True
|
|
s._return_to_recommend_and_reenter_live.assert_called_once()
|
|
|
|
def test_attended_long_ago_triggers(self):
|
|
s = _mk_self()
|
|
s._run_start_ts = 0.0
|
|
s._last_successful_attend_ts = 1.0 # 极远古
|
|
s._no_attend_timeout_minutes = 1.0
|
|
s._return_to_recommend_and_reenter_live = MagicMock(return_value=None)
|
|
assert ronce.handle_no_attend_timeout(s, 0.0) is True
|
|
|
|
|
|
# ============================================================
|
|
# handle_desktop_recovery
|
|
# ============================================================
|
|
|
|
class TestHandleDesktopRecovery:
|
|
def test_in_douyin_returns_false(self):
|
|
s = _mk_self()
|
|
s.operation = MagicMock()
|
|
s.operation.get_top_package = MagicMock(return_value="com.ss.android.ugc.aweme")
|
|
assert ronce.handle_desktop_recovery(s) is False
|
|
|
|
def test_outside_douyin_triggers_recovery(self):
|
|
s = _mk_self()
|
|
s.operation = MagicMock()
|
|
s.operation.get_top_package = MagicMock(return_value="com.android.launcher")
|
|
s.click = MagicMock(return_value=None)
|
|
s.run_once_basic = MagicMock(return_value=None)
|
|
s.desktop_icon_x = 153
|
|
s.desktop_icon_y = 260
|
|
s.yanghao_fallback_live_x = 100
|
|
s.yanghao_fallback_live_y = 100
|
|
s.yanghao_fallback_enter_live_x = 100
|
|
s.yanghao_fallback_enter_live_y = 100
|
|
|
|
result = ronce.handle_desktop_recovery(s)
|
|
assert result is True
|
|
s.run_once_basic.assert_called_once()
|
|
|
|
def test_get_top_package_failed_returns_false(self):
|
|
s = _mk_self()
|
|
s.operation = MagicMock()
|
|
s.operation.get_top_package = MagicMock(side_effect=RuntimeError("adb fail"))
|
|
# 异常时 top_pkg='' 不进入自救分支
|
|
assert ronce.handle_desktop_recovery(s) is False
|
|
|
|
|
|
# ============================================================
|
|
# handle_high_value_room_revisit
|
|
# ============================================================
|
|
|
|
class TestHighValueRoomRevisit:
|
|
def test_disabled_returns_false(self):
|
|
s = _mk_self()
|
|
s.high_value_room_revisit_enabled = False
|
|
assert ronce.handle_high_value_room_revisit(s, 0.0) is False
|
|
|
|
|
|
# ============================================================
|
|
# screenshot_and_diagnose
|
|
# ============================================================
|
|
|
|
class TestScreenshotAndDiagnose:
|
|
def test_screenshot_failed_returns_none(self):
|
|
s = _mk_self()
|
|
s.operation = MagicMock()
|
|
s.operation.get_screenshot = MagicMock(return_value=False)
|
|
s.operation.delay = MagicMock(return_value=None)
|
|
result = ronce.screenshot_and_diagnose(s, 0.0)
|
|
assert result is None
|
|
|
|
def test_screenshot_ok_returns_stage_seen(self):
|
|
s = _mk_self()
|
|
s.operation = MagicMock()
|
|
s.operation.get_screenshot = MagicMock(return_value=True)
|
|
s._diagnose_stage_from_current_screen = MagicMock(
|
|
return_value=("IN_LIVE_ROOM", {"detail_seen": False})
|
|
)
|
|
result = ronce.screenshot_and_diagnose(s, 0.0)
|
|
assert result == ("IN_LIVE_ROOM", {"detail_seen": False})
|
|
# 应该已写入缓存
|
|
assert s._last_diag_stage == "IN_LIVE_ROOM"
|
|
assert s._last_diag_seen == {"detail_seen": False}
|
|
|
|
|
|
# ============================================================
|
|
# dispatch_stage_branch
|
|
# ============================================================
|
|
|
|
class TestDispatchStageBranch:
|
|
def test_coupon_popup_clicks_back(self):
|
|
s = _mk_self()
|
|
s.operation = MagicMock()
|
|
s.operation.click_back = MagicMock(return_value=None)
|
|
ronce.dispatch_stage_branch(s, "COUPON_POPUP", {}, 0.0)
|
|
s.operation.click_back.assert_called_once_with(s.device_id)
|
|
|
|
def test_live_closed_swipes_up(self):
|
|
s = _mk_self()
|
|
s.swipe_up_to_next_live = MagicMock(return_value=None)
|
|
ronce.dispatch_stage_branch(s, "LIVE_CLOSED", {}, 0.0)
|
|
s.swipe_up_to_next_live.assert_called_once()
|
|
|
|
def test_unknown_stage_only_logs(self):
|
|
s = _mk_self()
|
|
# 不应抛
|
|
ronce.dispatch_stage_branch(s, "TOTALLY_UNKNOWN", {}, 0.0)
|
|
# 至少调用过 _log_kv
|
|
assert s._log_kv.called
|
|
|
|
def test_fudai_detail_calls_handle_in_fudai_detail(self):
|
|
s = _mk_self()
|
|
s._handle_in_fudai_detail = MagicMock(return_value=None)
|
|
ronce.dispatch_stage_branch(s, "FUDai_DETAIL", {}, 0.0)
|
|
s._handle_in_fudai_detail.assert_called_once()
|
|
|
|
|
|
# ============================================================
|
|
# handle_just_recorded_reservation
|
|
# ============================================================
|
|
|
|
class TestHandleJustRecordedReservation:
|
|
def test_flag_not_set_returns_false(self):
|
|
s = _mk_self()
|
|
s._just_recorded_reservation = False
|
|
assert ronce.handle_just_recorded_reservation(s, 0.0) is False
|
|
|
|
def test_flag_set_with_needswitch_swipes(self):
|
|
s = _mk_self()
|
|
s._just_recorded_reservation = True
|
|
s.needswitch = True
|
|
s.swipe_up_to_next_live = MagicMock(return_value=None)
|
|
assert ronce.handle_just_recorded_reservation(s, 0.0) is True
|
|
# 标志应被清除
|
|
assert s._just_recorded_reservation is False
|
|
s.swipe_up_to_next_live.assert_called_once()
|
|
|
|
def test_flag_set_without_needswitch_no_swipe(self):
|
|
s = _mk_self()
|
|
s._just_recorded_reservation = True
|
|
s.needswitch = False
|
|
s.swipe_up_to_next_live = MagicMock(return_value=None)
|
|
assert ronce.handle_just_recorded_reservation(s, 0.0) is True
|
|
s.swipe_up_to_next_live.assert_not_called()
|
|
|
|
|
|
# ============================================================
|
|
# handle_skip_stage_diagnosis
|
|
# ============================================================
|
|
|
|
class TestHandleSkipStageDiagnosis:
|
|
def test_flag_not_set_returns_false(self):
|
|
s = _mk_self()
|
|
s._skip_stage_diagnosis_and_find_fudai = False
|
|
assert ronce.handle_skip_stage_diagnosis(s, 0.0) is False
|
|
|
|
def test_flag_set_finds_fudai_directly(self):
|
|
s = _mk_self()
|
|
s._skip_stage_diagnosis_and_find_fudai = True
|
|
s._handle_live_room_has_or_not_fudai = MagicMock(return_value=None)
|
|
assert ronce.handle_skip_stage_diagnosis(s, 0.0) is True
|
|
# 标志应被清除
|
|
assert s._skip_stage_diagnosis_and_find_fudai is False
|
|
s._handle_live_room_has_or_not_fudai.assert_called_once()
|