Files
douyi_guaji_main/tests/test_fudai_run_blocks.py
T
CodexandClaude Opus 4.7 28f91b2aaf refactor(fudai): 拆出 needswitch 诊断日志(2429→2390,-39 行)
把 run_once_basic 中"needswitch 配置 + 第一个预约详情"的诊断日志(48 行)
抽到 fudai_run_blocks.log_needswitch_check_details。纯日志辅助函数。

新增 2 个单元测试覆盖正常队列与空队列。1146 → 1148 用例全过。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 03:08:07 +08:00

738 lines
25 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
# ---- handle_coupon_popup_stage ---- #
def test_coupon_popup_clicks_back_and_logs_completion():
a = _make_analyser()
fudai_run_blocks.handle_coupon_popup_stage(a, time.time())
a.operation.click_back.assert_called_once_with("test")
completion_msgs = [
c.args[1] for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "run_once_basic完成(处理优惠券弹窗)" in c.args[1]
]
assert len(completion_msgs) == 1
def test_coupon_popup_swallows_click_back_failure():
a = _make_analyser()
a.operation.click_back.side_effect = RuntimeError("boom")
fudai_run_blocks.handle_coupon_popup_stage(a, time.time())
# 应当记录失败日志且仍记录完成
failure_calls = [
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "按返回键关闭优惠券弹窗失败" in c.args[1]
]
assert len(failure_calls) == 1
# ---- handle_shopping_redpacket_popup_stage ---- #
def test_shopping_redpacket_clicks_close_button():
a = _make_analyser()
a.resolution_ratio_x = 1080
a.resolution_ratio_y = 2400
fudai_run_blocks.handle_shopping_redpacket_popup_stage(a, time.time())
a.operation.click.assert_called_once_with("test", 530, 1647)
def test_shopping_redpacket_swallows_click_failure():
a = _make_analyser()
a.resolution_ratio_x = 1080
a.resolution_ratio_y = 2400
a.operation.click.side_effect = RuntimeError("boom")
fudai_run_blocks.handle_shopping_redpacket_popup_stage(a, time.time())
# ---- handle_lottery_result_stage ---- #
def test_lottery_result_with_ocr_match_calls_handler():
a = _make_analyser()
a._check_lottery_result_by_ocr.return_value = "won"
fudai_run_blocks.handle_lottery_result_stage(a, time.time())
a._handle_lottery_result_by_ocr.assert_called_once_with("won")
assert a._waiting_for_lottery_result is False
assert a._current_attending_reservation_fudai is False
def test_lottery_result_no_ocr_falls_back_to_back_and_swipe():
a = _make_analyser()
a._check_lottery_result_by_ocr.return_value = None
fudai_run_blocks.handle_lottery_result_stage(a, time.time())
a.operation.click_back.assert_called_once()
a.swipe_up_to_next_live.assert_called_once()
a._handle_lottery_result_by_ocr.assert_not_called()
# ---- handle_live_closed_stage ---- #
def test_live_closed_swipes_to_next_and_logs_completion():
a = _make_analyser()
fudai_run_blocks.handle_live_closed_stage(a, time.time())
a.swipe_up_to_next_live.assert_called_once()
completion = [
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "run_once_basic完成(直播结束)" in c.args[1]
]
assert len(completion) == 1
# ---- handle_recommend_page_stage ---- #
def _make_recommend_analyser():
a = _make_analyser()
a.resolution_ratio_x = 1080
a.resolution_ratio_y = 2400
a.FOLLOW_TAB_CLICK_DELAY = 0.5
a.ENTER_LIVE_CLICK_DELAY = 0.6
a.ENTER_LIVE_FINAL_DELAY = 1.0
a.RETURN_STEP_DELAY_SECONDS = 0.8
return a
def test_recommend_page_clicks_live_tab_and_enter_live_twice():
a = _make_recommend_analyser()
fudai_run_blocks.handle_recommend_page_stage(a, "RECOMMEND_PAGE", "")
# 点击次数:直播页 1 + 直播间 2 = 3
assert a.operation.click.call_count == 3
def test_recommend_page_handles_click_failure():
a = _make_recommend_analyser()
a.operation.click.side_effect = RuntimeError("boom")
fudai_run_blocks.handle_recommend_page_stage(a, "RECOMMEND_PAGE", "")
# 异常时 fallback 调用 click_back
a.operation.click_back.assert_called_once()
# ---- handle_in_live_room_stage ---- #
def test_in_live_room_no_just_switched_calls_handler():
a = _make_analyser()
a._just_switched_room = False
a._handle_live_room_has_or_not_fudai = MagicMock()
fudai_run_blocks.handle_in_live_room_stage(a, "IN_LIVE_ROOM", time.time())
a._handle_live_room_has_or_not_fudai.assert_called_once()
# 不应该调用 delay(没切换不需要等加载)
a.operation.delay.assert_not_called()
def test_in_live_room_just_switched_waits_for_load():
a = _make_analyser()
a._just_switched_room = True
a.switch_room_wait_after_stage_diag_seconds = 5.0
a._handle_live_room_has_or_not_fudai = MagicMock()
fudai_run_blocks.handle_in_live_room_stage(a, "IN_LIVE_ROOM", time.time())
# 应当 delay(5.0)
delays = [c.args[0] for c in a.operation.delay.call_args_list]
assert 5.0 in delays
# 标志已清除
assert a._just_switched_room is False
a._handle_live_room_has_or_not_fudai.assert_called_once()
def test_in_live_room_logs_completion():
a = _make_analyser()
a._just_switched_room = False
a._handle_live_room_has_or_not_fudai = MagicMock()
fudai_run_blocks.handle_in_live_room_stage(a, "LIVE_OR_UNKNOWN", time.time())
completion = [
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "run_once_basic完成(处理直播间)" in c.args[1]
]
assert len(completion) == 1
# ---- take_screenshot_with_retries ---- #
def test_screenshot_success_first_try():
a = _make_analyser()
a.operation.get_screenshot.return_value = True
ok, elapsed = fudai_run_blocks.take_screenshot_with_retries(a, time.time())
assert ok is True
assert elapsed >= 0
def test_screenshot_success_second_try():
a = _make_analyser()
a.operation.get_screenshot.side_effect = [False, True]
ok, _ = fudai_run_blocks.take_screenshot_with_retries(a, time.time(), max_retries=3)
assert ok is True
# 重试一次,所以 delay 被调用一次
assert a.operation.delay.call_count == 1
def test_screenshot_all_retries_fail_logs_error():
a = _make_analyser()
a.operation.get_screenshot.return_value = False
ok, _ = fudai_run_blocks.take_screenshot_with_retries(a, time.time(), max_retries=3)
assert ok is False
# 应记录"获取截图失败(多次重试后仍失败)"
failure_logs = [
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "获取截图失败" in c.args[1]
]
assert len(failure_logs) == 1
def test_screenshot_get_screenshot_exception_treated_as_false():
a = _make_analyser()
a.operation.get_screenshot.side_effect = RuntimeError("adb fail")
ok, _ = fudai_run_blocks.take_screenshot_with_retries(a, time.time(), max_retries=2)
assert ok is False
# ---- maybe_clear_reservation_queue_on_first_run ---- #
def test_clear_queue_first_run_clears_reservations():
a = MagicMock()
a._first_run_once_basic = True
a.reservations = [{"future_ts": 100}, {"future_ts": 200}]
a._reservations_state_path = None # 跳过 write 状态文件
fudai_run_blocks.maybe_clear_reservation_queue_on_first_run(a)
assert a.reservations == []
assert a._first_run_once_basic is False
def test_clear_queue_non_first_run_keeps_reservations():
a = MagicMock()
a._first_run_once_basic = False
original = [{"future_ts": 100}]
a.reservations = original
fudai_run_blocks.maybe_clear_reservation_queue_on_first_run(a)
# 队列保持
assert a.reservations is original
def test_clear_queue_first_run_no_state_path_still_clears():
a = MagicMock()
a._first_run_once_basic = True
a.reservations = [{"future_ts": 100}]
a._reservations_state_path = None
fudai_run_blocks.maybe_clear_reservation_queue_on_first_run(a)
assert a.reservations == []
def test_clear_queue_swallows_log_exceptions():
a = MagicMock()
a._first_run_once_basic = True
a.reservations = [{"future_ts": 100}]
a._log_kv.side_effect = RuntimeError("boom")
a._reservations_state_path = None
# 应不抛错
fudai_run_blocks.maybe_clear_reservation_queue_on_first_run(a)
assert a.reservations == []
# ---- handle_just_recorded_reservation ---- #
def test_just_recorded_not_set_returns_false():
a = _make_analyser()
a._just_recorded_reservation = False
assert fudai_run_blocks.handle_just_recorded_reservation(a, time.time()) is False
def test_just_recorded_with_switch_swipes_up():
a = _make_analyser()
a._just_recorded_reservation = True
a.needswitch = True
assert fudai_run_blocks.handle_just_recorded_reservation(a, time.time()) is True
a.swipe_up_to_next_live.assert_called_once()
assert a._just_recorded_reservation is False
def test_just_recorded_no_switch_skips_swipe():
a = _make_analyser()
a._just_recorded_reservation = True
a.needswitch = False
assert fudai_run_blocks.handle_just_recorded_reservation(a, time.time()) is True
a.swipe_up_to_next_live.assert_not_called()
def test_just_recorded_swipe_failure_still_returns_true():
a = _make_analyser()
a._just_recorded_reservation = True
a.needswitch = True
a.swipe_up_to_next_live.side_effect = RuntimeError("boom")
assert fudai_run_blocks.handle_just_recorded_reservation(a, time.time()) is True
# ---- handle_skip_stage_diagnosis ---- #
def test_skip_stage_diag_not_set_returns_false():
a = _make_analyser()
a._skip_stage_diagnosis_and_find_fudai = False
assert fudai_run_blocks.handle_skip_stage_diagnosis(a, time.time()) is False
def test_skip_stage_diag_set_calls_handler_and_clears_flag():
a = _make_analyser()
a._skip_stage_diagnosis_and_find_fudai = True
a._handle_live_room_has_or_not_fudai = MagicMock()
assert fudai_run_blocks.handle_skip_stage_diagnosis(a, time.time()) is True
a._handle_live_room_has_or_not_fudai.assert_called_once()
assert a._skip_stage_diagnosis_and_find_fudai is False
def test_skip_stage_diag_handler_failure_still_returns_true():
a = _make_analyser()
a._skip_stage_diagnosis_and_find_fudai = True
a._handle_live_room_has_or_not_fudai = MagicMock(side_effect=RuntimeError("boom"))
assert fudai_run_blocks.handle_skip_stage_diagnosis(a, time.time()) is True
# ---- log_reservation_trigger_check ---- #
def test_log_trigger_check_disabled_logs_skip():
a = MagicMock()
fudai_run_blocks.log_reservation_trigger_check(a, False, None, 0)
msgs = [c.args[1] for c in a._log_kv.call_args_list if len(c.args) >= 2]
assert any("预约功能未启用" in m for m in msgs)
def test_log_trigger_check_empty_queue_logs_skip():
a = MagicMock()
fudai_run_blocks.log_reservation_trigger_check(a, True, [], 0)
msgs = [c.args[1] for c in a._log_kv.call_args_list if len(c.args) >= 2]
assert any("预约队列为空" in m for m in msgs)
def test_log_trigger_check_with_reservations_logs_overview():
a = MagicMock()
reservations = [
{"future_ts": 1700000100, "prize_value_yuan": 5.0, "pos_seen": 3},
{"future_ts": 1700000200, "prize_value_yuan": 10.0, "pos_seen": 5},
]
fudai_run_blocks.log_reservation_trigger_check(a, True, reservations, 2)
# 触发条件日志应被调用
overview_calls = [
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "检查预约回退触发条件" in c.args[1]
]
assert len(overview_calls) == 1
# 不应记录跳过日志
skip_calls = [
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and ("预约功能未启用" in c.args[1] or "预约队列为空" in c.args[1])
]
assert len(skip_calls) == 0
def test_log_trigger_check_truncates_to_first_three():
"""队列超过 3 项时,详情只记录前 3 项"""
a = MagicMock()
reservations = [
{"future_ts": 1700000000 + i * 100, "prize_value_yuan": float(i), "pos_seen": i}
for i in range(5)
]
fudai_run_blocks.log_reservation_trigger_check(a, True, reservations, 5)
overview_call = next(
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "检查预约回退触发条件" in c.args[1]
)
detail = overview_call.kwargs.get("reservations_detail", [])
assert len(detail) == 3
# ---- write_reservations_state_throttled ---- #
def test_write_state_throttled_skips_within_1s(tmp_path):
a = MagicMock()
state_path = tmp_path / "state.json"
a._reservations_state_path = str(state_path)
a._reservations_state_last_write_ts = time.time() - 0.5 # 0.5s 前刚写过
a.reservations = [{"future_ts": 100}]
fudai_run_blocks.write_reservations_state_throttled(a)
# 节流,文件不应被写
assert not state_path.exists()
def test_write_state_writes_when_throttle_passed(tmp_path):
a = MagicMock()
state_path = tmp_path / "state.json"
a._reservations_state_path = str(state_path)
a._reservations_state_last_write_ts = time.time() - 5 # 5s 前
a.reservations = [
{
"future_ts": 1700000000.0,
"prize_value_yuan": 5.0,
"content": "test",
"pos_seen": 3,
"room_title": "房间A",
"room_title_raw": "房间A原始",
"created_ts": 1699999999,
}
]
a.current_room_title = "当前"
a.current_room_title_raw = "当前原始"
a.room_pos = 5
fudai_run_blocks.write_reservations_state_throttled(a)
import json as _json
assert state_path.exists()
data = _json.loads(state_path.read_text(encoding="utf-8"))
assert data["reason"] == "process_reservation"
assert data["room_pos"] == 5
assert len(data["reservations"]) == 1
assert data["reservations"][0]["prize_value_yuan"] == 5.0
def test_write_state_swallows_io_error():
"""state_path 不可写时不应抛错"""
a = MagicMock()
# Windows: 用一个明确无效的路径(包含非法字符)
a._reservations_state_path = "Z:\\nonexistent\\path\\with\x00null\\state.json"
a._reservations_state_last_write_ts = 0.0
a.reservations = []
a.current_room_title = ""
a.current_room_title_raw = ""
a.room_pos = 0
# 不应抛错
fudai_run_blocks.write_reservations_state_throttled(a)
# 应记录失败日志
failure = [
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "写入预约队列状态文件失败" in c.args[1]
]
assert len(failure) == 1
# ---- handle_all_reservations_completed_reset ---- #
def test_completed_reset_no_queue_full_returns_false():
a = MagicMock()
a.queue_full_waiting_at_earliest = False
a.reservations = []
swipe = MagicMock()
assert fudai_run_blocks.handle_all_reservations_completed_reset(a, swipe) is False
swipe.assert_not_called()
def test_completed_reset_with_pending_reservations_returns_false():
a = MagicMock()
a.queue_full_waiting_at_earliest = True
a.reservations = [{"future_ts": 100}] # 有未完成
swipe = MagicMock()
assert fudai_run_blocks.handle_all_reservations_completed_reset(a, swipe) is False
swipe.assert_not_called()
def test_completed_reset_clears_focus_and_swipes_back():
a = MagicMock()
a.queue_full_waiting_at_earliest = True
a.reservations = []
a.reservation_focus_until_ts = 12345.0
a.room_pos = 5
a._swipe_times = 99
swipe = MagicMock()
assert fudai_run_blocks.handle_all_reservations_completed_reset(a, swipe) is True
assert a.queue_full_waiting_at_earliest is False
assert a.reservation_focus_until_ts == 0.0
assert a.room_pos == 0
assert a._swipe_times == 0
# 应回退 5 次
assert swipe.call_count == 5
def test_completed_reset_caps_back_times_at_100():
"""room_pos > 100 时被钳到 100"""
a = MagicMock()
a.queue_full_waiting_at_earliest = True
a.reservations = []
a.room_pos = 500
swipe = MagicMock()
fudai_run_blocks.handle_all_reservations_completed_reset(a, swipe)
assert swipe.call_count == 100
def test_completed_reset_zero_room_pos_no_swipes():
a = MagicMock()
a.queue_full_waiting_at_earliest = True
a.reservations = []
a.room_pos = 0
swipe = MagicMock()
assert fudai_run_blocks.handle_all_reservations_completed_reset(a, swipe) is True
swipe.assert_not_called()
# ---- log_needswitch_check_details ---- #
def test_log_needswitch_with_reservation_logs_first_detail():
a = MagicMock()
a.reservations = [
{
"future_ts": time.time() + 100,
"prize_value_yuan": 5.0,
"pos_seen": 3,
}
]
a.room_pos = 7
fudai_run_blocks.log_needswitch_check_details(a, True)
call = next(
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "检查预约回退条件" in c.args[1]
)
assert call.kwargs["needswitch_cfg"] is True
assert call.kwargs["reservations_count"] == 1
info = call.kwargs["first_reservation"]
assert info["pos_seen"] == 3
assert info["current_room_pos"] == 7
assert info["value"] == 5.0
def test_log_needswitch_empty_queue_logs_zero_count():
a = MagicMock()
a.reservations = []
fudai_run_blocks.log_needswitch_check_details(a, False)
call = next(
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "检查预约回退条件" in c.args[1]
)
assert call.kwargs["needswitch_cfg"] is False
assert call.kwargs["reservations_count"] == 0
assert call.kwargs["first_reservation"] == {}