refactor(fudai): 拆出高价值预约总调度(1273→1212,-61 行)

把 run_once_basic 中"高价值预约 + 闭包构造 + 6 个子块串联"的 67 行块抽到
fudai_run_blocks.process_high_value_reservation_check:
- 内部构造 _swipe_down_once / _sort_reservations / _write_state /
  _seek_reservation_room 4 个闭包
- 串联 handle_queue_full_wait_at_earliest / log_skip_reservation_check_for_state /
  handle_all_reservations_completed_reset / log_needswitch_check_details /
  execute_reservation_attend_or_cleanup

新增 4 个单元测试,1227 → 1231 用例全过。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 03:53:33 +08:00
1 parent e567948ec9
commit df8424b706
3 files changed
+144 -65

No files matched your search

+4 -65
View File
@@ -797,71 +797,10 @@ class fudai_analyse:
self, high_value_enabled, reservations_list, reservations_count
)
if high_value_enabled and reservations_list:
def _swipe_down_once() -> None:
self.swipe_down_back_from_live()
def _sort_reservations() -> None:
douyin_reservation_logic._sort_reservations_inplace(self.reservations) # type: ignore[attr-defined]
def _write_state() -> None:
fudai_run_blocks.write_reservations_state_throttled(self)
focus_ref = [float(self.reservation_focus_until_ts or 0.0)]
after_action_ref = [
bool(self.reservation_after_action_pending or False)
]
def _seek_reservation_room(item: dict) -> bool:
try:
return bool(
self._seek_reservation_room(item, operation_type="预约")
)
except Exception:
return False
# 检查队列是否已满且需要等待
if fudai_run_blocks.handle_queue_full_wait_at_earliest(
self,
start_time=start_time,
focus_ref=focus_ref,
swipe_down_back_callback=_swipe_down_once,
seek_reservation_room_callback=_seek_reservation_room,
sort_reservations_callback=_sort_reservations,
write_state_callback=_write_state,
):
return
# 等待开奖 / 正在参与预约 / 队列满等待 三种状态下记录跳过日志
fudai_run_blocks.log_skip_reservation_check_for_state(self)
# 检查是否所有预约已完成,需要重置到第一个直播间
if fudai_run_blocks.handle_all_reservations_completed_reset(
self, _swipe_down_once
):
return
# 单间模式(needswitch=False)下不启用预约回退逻辑
# 因为单间模式不会切换直播间,不需要回退,预约会在当前直播间自动识别和参与
try:
needswitch_cfg = bool(getattr(self, "needswitch", True))
except Exception:
needswitch_cfg = True
# 添加详细的调试日志,确认 needswitch 的值和预约信息
fudai_run_blocks.log_needswitch_check_details(self, needswitch_cfg)
fudai_run_blocks.execute_reservation_attend_or_cleanup(
self,
needswitch_cfg=needswitch_cfg,
focus_ref=focus_ref,
after_action_ref=after_action_ref,
swipe_down_back_callback=_swipe_down_once,
seek_reservation_room_callback=_seek_reservation_room,
sort_reservations_callback=_sort_reservations,
write_state_callback=_write_state,
)
if fudai_run_blocks.process_high_value_reservation_check(
self, start_time, high_value_enabled, reservations_list
):
return
except Exception:
pass
@@ -1466,6 +1466,78 @@ def handle_recommend_page_stage(analyser: Any, stage: str, seen: Any) -> None:
pass
def process_high_value_reservation_check(
analyser: Any,
start_time: float,
high_value_enabled: bool,
reservations_list: Any,
) -> bool:
"""高价值预约检查的主调度:构造闭包 → 队列满等待 → 全部完成重置 → 调度 attend/cleanup。
返回 True 表示主流程应当 ``return``(已触发队列满等待或全部完成重置)。
返回 False 表示继续后续流程。
"""
if not (high_value_enabled and reservations_list):
return False
from douyin.reservation import douyin_reservation_logic
def _swipe_down_once() -> None:
analyser.swipe_down_back_from_live()
def _sort_reservations() -> None:
douyin_reservation_logic._sort_reservations_inplace(analyser.reservations) # type: ignore[attr-defined]
def _write_state() -> None:
write_reservations_state_throttled(analyser)
focus_ref = [float(analyser.reservation_focus_until_ts or 0.0)]
after_action_ref = [bool(analyser.reservation_after_action_pending or False)]
def _seek_reservation_room(item: dict) -> bool:
try:
return bool(
analyser._seek_reservation_room(item, operation_type="预约")
)
except Exception:
return False
if handle_queue_full_wait_at_earliest(
analyser,
start_time=start_time,
focus_ref=focus_ref,
swipe_down_back_callback=_swipe_down_once,
seek_reservation_room_callback=_seek_reservation_room,
sort_reservations_callback=_sort_reservations,
write_state_callback=_write_state,
):
return True
log_skip_reservation_check_for_state(analyser)
if handle_all_reservations_completed_reset(analyser, _swipe_down_once):
return True
try:
needswitch_cfg = bool(getattr(analyser, "needswitch", True))
except Exception:
needswitch_cfg = True
log_needswitch_check_details(analyser, needswitch_cfg)
execute_reservation_attend_or_cleanup(
analyser,
needswitch_cfg=needswitch_cfg,
focus_ref=focus_ref,
after_action_ref=after_action_ref,
swipe_down_back_callback=_swipe_down_once,
seek_reservation_room_callback=_seek_reservation_room,
sort_reservations_callback=_sort_reservations,
write_state_callback=_write_state,
)
return False
def diagnose_stage_with_logging(
analyser: Any, start_time: float, screenshot_elapsed: float
) -> tuple[str, str]:
+68
View File
@@ -1063,6 +1063,74 @@ def test_dispatch_unknown_stage_logs_and_returns_false():
assert any("未匹配到特定阶段" in m for m in msgs)
# ---- process_high_value_reservation_check ---- #
def test_process_high_value_disabled_returns_false():
a = _make_analyser()
assert (
fudai_run_blocks.process_high_value_reservation_check(
a, time.time(), False, [{"future_ts": 100}]
)
is False
)
def test_process_high_value_no_reservations_returns_false():
a = _make_analyser()
assert (
fudai_run_blocks.process_high_value_reservation_check(
a, time.time(), True, []
)
is False
)
def test_process_high_value_queue_full_triggers_return():
"""队列已满时 process 返回 True 让主流程 return"""
a = _make_analyser()
a.reservations = [{"future_ts": 100}, {"future_ts": 200}, {"future_ts": 300}]
a.high_value_fudai_max_reservations = 2
a.queue_full_waiting_at_earliest = False
a.reservation_focus_until_ts = 0.0
a.reservation_back_swipe_delay = 0.6
a.reservation_after_action_pending = False
a._seek_reservation_room = MagicMock(return_value=True)
a.swipe_down_back_from_live = MagicMock()
from douyin.reservation import douyin_reservation_logic as drl
with patch.object(drl, "handle_queue_full_wait_at_earliest", return_value=True):
result = fudai_run_blocks.process_high_value_reservation_check(
a, time.time(), True, a.reservations
)
assert result is True
def test_process_high_value_queue_not_full_runs_normal_path():
"""队列未满 → 进入 attend/cleanup 调度,返回 False"""
a = _make_analyser()
a.reservations = [{"future_ts": time.time() + 100}]
a.high_value_fudai_max_reservations = 5
a.queue_full_waiting_at_earliest = False
a.reservation_focus_until_ts = 0.0
a.reservation_back_swipe_delay = 0.6
a.reservation_after_action_pending = False
a._seek_reservation_room = MagicMock(return_value=True)
a.swipe_down_back_from_live = MagicMock()
a._waiting_for_lottery_result = False
a._current_attending_reservation_fudai = False
a.needswitch = False # 走单间模式 cleanup 路径,避免触发外部依赖
from douyin.reservation import douyin_reservation_logic as drl
with patch.object(drl, "cleanup_expired_reservations") as fake_cleanup:
result = fudai_run_blocks.process_high_value_reservation_check(
a, time.time(), True, a.reservations
)
assert result is False
fake_cleanup.assert_called_once()
# ---- execute_reservation_attend_or_cleanup ---- #
def _make_reservation_analyser():