refactor(fudai): 抽 run_once_basic 0.1 高价值预约检查到 helper (2079 → 1541)
新增 5 个 helper 到 run_once_basic_helpers.py: - handle_reservation_check 0.1 预约检查主入口 - _maybe_clear_reservations_on_first_run 首次运行清空队列 + 状态文件 - _log_reservation_check_trigger 调试日志:检查触发条件 - _run_reservation_attend_window 队列满处理 / 抢占逻辑 - _log_reservation_attend_check 调试日志:第一个预约信息 净减 538 行重复嵌套样板。新增 7 个单元测试覆盖: - 高价值未启用 / 队列空 → return False - 首次运行清空队列 - 正常路径调 process_reservation_attend_window - 队列满 → return True - needswitch=False 走单间模式 + cleanup - 等待开奖标志不阻断流程 至此 run_once_basic 总长 612 → 88 行(-86%),douyin_fudai.py 累计 4121 → 1541(-2580 行 / -63%),不再是项目最大文件。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
61cdf7b445
commit
ffd048ffa5
3 files changed
+672
-541
No files matched your search
@@ -10,7 +10,7 @@
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -293,3 +293,125 @@ class TestHandleSkipStageDiagnosis:
|
||||
# 标志应被清除
|
||||
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_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()
|
||||
Reference in new issue
Block a user