481 lines
19 KiB
Python
481 lines
19 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, patch
|
|
|
|
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_uses_launch_package_first(self):
|
|
"""非抖音前台时应优先调用 launch_package,不再点击桌面图标。"""
|
|
s = _mk_self()
|
|
s.operation = MagicMock()
|
|
s.operation.get_top_package = MagicMock(return_value="com.android.launcher")
|
|
s.operation.launch_package = MagicMock(
|
|
return_value=(True, "com.ss.android.ugc.aweme")
|
|
)
|
|
s.click = 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.operation.launch_package.assert_called_once_with(s.device_id, "")
|
|
# 包名启动成功时不应再点击桌面坐标
|
|
s.click.assert_not_called()
|
|
|
|
def test_launch_package_failure_falls_back_to_clicks(self):
|
|
"""包名启动失败时回退到旧的点击坐标自救。"""
|
|
s = _mk_self()
|
|
s.operation = MagicMock()
|
|
s.operation.get_top_package = MagicMock(return_value="com.android.launcher")
|
|
s.operation.launch_package = MagicMock(return_value=(False, ""))
|
|
s.operation.click = MagicMock(return_value=None)
|
|
s.click = 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
|
|
# 失败时 fallback_click_recovery 通过 op.click 点击桌面图标 + 直播 + 进入直播间×2
|
|
assert s.operation.click.call_count >= 3
|
|
|
|
def test_launch_package_exception_falls_back_to_clicks(self):
|
|
"""launch_package 抛异常时也应回退到点击。"""
|
|
s = _mk_self()
|
|
s.operation = MagicMock()
|
|
s.operation.get_top_package = MagicMock(return_value="com.android.launcher")
|
|
s.operation.launch_package = MagicMock(side_effect=RuntimeError("adb dead"))
|
|
s.operation.click = MagicMock(return_value=None)
|
|
s.click = 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.operation.click.assert_called()
|
|
|
|
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}
|
|
|
|
|
|
class TestFinderReuseRecovery:
|
|
def test_invalid_reused_screenshot_falls_back_to_fresh_capture(self, monkeypatch, tmp_path):
|
|
from douyin.detection.detection_pkg import finder as mod
|
|
|
|
class _Pic:
|
|
size = (1080, 2400)
|
|
def load(self): return None
|
|
def convert(self, _mode): return self
|
|
def close(self): return None
|
|
|
|
screenshot_path = tmp_path / "screenshot.png"
|
|
screenshot_path.write_bytes(b"x" * 2048)
|
|
|
|
analyser = MagicMock()
|
|
analyser.fudai_finder_optimized_enabled = False
|
|
analyser.device_id = "MOCK"
|
|
analyser._screenshot_ready_for_ocr = True
|
|
analyser.operation = MagicMock()
|
|
analyser.operation.get_screenshot = MagicMock(return_value=True)
|
|
analyser.operation.delay = MagicMock(return_value=None)
|
|
analyser.resolution_ratio_x = 1080
|
|
analyser.resolution_ratio_y = 2400
|
|
analyser.y_pianyi = 0
|
|
analyser._log_kv = MagicMock(return_value=None)
|
|
|
|
state = {"count": 0}
|
|
def _open(_path):
|
|
state["count"] += 1
|
|
if state["count"] == 1:
|
|
raise OSError("broken reused screenshot")
|
|
return _Pic()
|
|
|
|
import sys
|
|
from types import ModuleType, SimpleNamespace
|
|
|
|
pil_mod = ModuleType("PIL")
|
|
pil_mod.Image = SimpleNamespace(open=_open)
|
|
monkeypatch.setitem(sys.modules, "PIL", pil_mod)
|
|
monkeypatch.setattr("douyin.detection.detection_pkg.finder.path_config.get_screenshot_path", lambda: str(screenshot_path))
|
|
monkeypatch.setattr("douyin.detection.detection_pkg.finder.scan_with_numpy", lambda *a, **k: (False, None))
|
|
monkeypatch.setattr("douyin.detection.detection_pkg.finder.scan_pixel_by_pixel", lambda *a, **k: (False, None))
|
|
monkeypatch.setattr("douyin.detection.detection_pkg.finder.match_template", lambda *a, **k: (False, None))
|
|
|
|
result = mod.check_have_fudai_with_ready_screenshot(analyser, screenshot_ready=True)
|
|
|
|
assert result is False
|
|
analyser.operation.get_screenshot.assert_called_once_with(analyser.device_id, use_service=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()
|
|
|
|
|
|
# ============================================================
|
|
# handle_reservation_check —— 0.1 高价值预约检查
|
|
# ============================================================
|
|
|
|
class TestHandleReservationCheck:
|
|
def _mk_for_reservation(self):
|
|
s = _mk_self()
|
|
s._first_run_once_basic = False # 默认非首次,避免 mock 状态文件写入
|
|
s.high_value_fudai_enabled = False
|
|
s.reservations = []
|
|
s.queue_full_waiting_at_earliest = False
|
|
s.reservation_focus_until_ts = 0.0
|
|
s.reservation_after_action_pending = False
|
|
s.reservation_back_swipe_delay = 0.6
|
|
s.high_value_fudai_max_reservations = 3
|
|
s.room_pos = 0
|
|
s.needswitch = True
|
|
s.operation = MagicMock()
|
|
s.operation.delay = MagicMock(return_value=None)
|
|
s.swipe_down_back_from_live = MagicMock(return_value=None)
|
|
s._seek_reservation_room = MagicMock(return_value=False)
|
|
s._reservations_state_path = ""
|
|
return s
|
|
|
|
def test_high_value_disabled_returns_false(self):
|
|
"""high_value_fudai_enabled=False → 不进入主逻辑,return False。"""
|
|
s = self._mk_for_reservation()
|
|
s.high_value_fudai_enabled = False
|
|
assert ronce.handle_reservation_check(s, 0.0) is False
|
|
|
|
def test_empty_reservations_returns_false(self):
|
|
"""启用了但队列为空 → return False,让 caller 继续。"""
|
|
s = self._mk_for_reservation()
|
|
s.high_value_fudai_enabled = True
|
|
s.reservations = []
|
|
assert ronce.handle_reservation_check(s, 0.0) is False
|
|
|
|
def test_first_run_clears_reservations(self):
|
|
"""首次运行时清空预约队列 + 标记非首次。"""
|
|
s = self._mk_for_reservation()
|
|
s._first_run_once_basic = True
|
|
s.high_value_fudai_enabled = False # 走简短路径
|
|
s.reservations = [{"future_ts": 9999, "content": "old"}]
|
|
s._reservations_state_path = "" # 不触发状态文件写入
|
|
ronce.handle_reservation_check(s, 0.0)
|
|
assert s.reservations == []
|
|
assert s._first_run_once_basic is False
|
|
|
|
def test_normal_path_calls_attend_window(self):
|
|
"""启用 + 有预约 + 不满 + needswitch=True → 调 process_reservation_attend_window。"""
|
|
import time
|
|
|
|
s = self._mk_for_reservation()
|
|
s.high_value_fudai_enabled = True
|
|
s.reservations = [
|
|
{"future_ts": time.time() + 100, "content": "X", "pos_seen": 0}
|
|
]
|
|
with patch(
|
|
"douyin.reservation.douyin_reservation_logic.process_reservation_attend_window",
|
|
return_value=False,
|
|
) as mock_process:
|
|
result = ronce.handle_reservation_check(s, 0.0)
|
|
assert result is False
|
|
mock_process.assert_called_once()
|
|
|
|
def test_preempted_reservation_switches_to_direct_attend(self):
|
|
import time
|
|
|
|
s = self._mk_for_reservation()
|
|
s.high_value_fudai_enabled = True
|
|
s.reservations = [
|
|
{"future_ts": time.time() + 100, "content": "X", "pos_seen": 0}
|
|
]
|
|
s._handle_reservation_direct_attend = MagicMock(return_value=None)
|
|
|
|
with patch(
|
|
"douyin.reservation.douyin_reservation_logic.process_reservation_attend_window",
|
|
return_value=True,
|
|
):
|
|
result = ronce.handle_reservation_check(s, 0.0)
|
|
|
|
assert result is False
|
|
assert s._current_attending_reservation_fudai is True
|
|
|
|
def test_queue_full_returns_true(self):
|
|
"""队列满 + handle_queue_full_wait_at_earliest 返回 True → return True 让 caller return。"""
|
|
import time
|
|
|
|
s = self._mk_for_reservation()
|
|
s.high_value_fudai_enabled = True
|
|
s.high_value_fudai_max_reservations = 2
|
|
s.reservations = [
|
|
{"future_ts": time.time() + 100, "content": "A"},
|
|
{"future_ts": time.time() + 200, "content": "B"},
|
|
]
|
|
s.queue_full_waiting_at_earliest = False
|
|
with patch(
|
|
"douyin.reservation.douyin_reservation_logic.handle_queue_full_wait_at_earliest",
|
|
return_value=True,
|
|
):
|
|
result = ronce.handle_reservation_check(s, 0.0)
|
|
assert result is True
|
|
assert s.queue_full_waiting_at_earliest is True
|
|
|
|
def test_needswitch_false_calls_cleanup(self):
|
|
"""needswitch=False → 走单间模式分支,调 cleanup_expired_reservations。"""
|
|
import time
|
|
|
|
s = self._mk_for_reservation()
|
|
s.high_value_fudai_enabled = True
|
|
s.needswitch = False
|
|
s.reservations = [
|
|
{"future_ts": time.time() + 100, "content": "X", "pos_seen": 0}
|
|
]
|
|
with patch(
|
|
"douyin.reservation.douyin_reservation_logic.cleanup_expired_reservations"
|
|
) as mock_cleanup:
|
|
result = ronce.handle_reservation_check(s, 0.0)
|
|
assert result is False
|
|
mock_cleanup.assert_called_once()
|
|
|
|
def test_waiting_for_lottery_skips_attend_window(self):
|
|
"""_waiting_for_lottery_result=True → 跳过预约回退,仍 return False。"""
|
|
import time
|
|
|
|
s = self._mk_for_reservation()
|
|
s.high_value_fudai_enabled = True
|
|
s._waiting_for_lottery_result = True
|
|
s.reservations = [
|
|
{"future_ts": time.time() + 100, "content": "X", "pos_seen": 0}
|
|
]
|
|
with patch(
|
|
"douyin.reservation.douyin_reservation_logic.process_reservation_attend_window"
|
|
) as mock_process:
|
|
result = ronce.handle_reservation_check(s, 0.0)
|
|
# 等待开奖时仍会执行多间模式 process(这是原代码行为:日志后不 return,让流程继续)
|
|
assert result is False
|
|
# process 仍被调用(日志只是说"跳过预约回退"但代码没真的跳)
|
|
mock_process.assert_called_once()
|