From 03e509ac254a982580d473505b4039c3c2011f21 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 03:11:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8B=86=E5=87=BA=E9=A2=84?= =?UTF-8?q?=E7=BA=A6=E6=8A=A2=E5=8D=A0=E8=B0=83=E5=BA=A6=E5=9D=97=EF=BC=88?= =?UTF-8?q?2429=E2=86=922318=EF=BC=8C-111=20=E8=A1=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit execute_reservation_attend_or_cleanup:根据 needswitch 分支处理: - 多间模式:调用 process_reservation_attend_window 抢占回退 - 单间模式:仅清理过期预约 新增 4 个单元测试覆盖正/负回报值、ref 同步、cleanup 异常吞噬。 1148 → 1152 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 92 ++------------- douyin/core/fudai_modules/fudai_run_blocks.py | 93 +++++++++++++++ tests/test_fudai_run_blocks.py | 110 +++++++++++++++++- 3 files changed, 212 insertions(+), 83 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 39695bc..badc1b3 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1335,88 +1335,16 @@ class fudai_analyse: # 添加详细的调试日志,确认 needswitch 的值和预约信息 fudai_run_blocks.log_needswitch_check_details(self, needswitch_cfg) - if needswitch_cfg: - # 多间模式:需要回退到预约直播间 - try: - self._log_kv( - "预约", - "[预约] 多间模式:开始调用 process_reservation_attend_window", - reservations_count=( - len(self.reservations) if self.reservations else 0 - ), - room_pos=int(getattr(self, "room_pos", 0) or 0), - ) - except Exception: - pass - - preempted = ( - douyin_reservation_logic.process_reservation_attend_window( - analyser=self, - reservations=self.reservations, - room_pos=int(getattr(self, "room_pos", 0) or 0), - reservation_focus_until_ts_ref=focus_ref, - reservation_after_action_pending_ref=after_action_ref, - reservation_back_swipe_delay_s=float( - self.reservation_back_swipe_delay or 0.6 - ), - swipe_down_back_callback=_swipe_down_once, - seek_reservation_room_callback=_seek_reservation_room, - sort_reservations_callback=_sort_reservations, - write_state_callback=_write_state, - ) - ) - - try: - self._log_kv( - "预约", - "[预约] process_reservation_attend_window 返回结果", - preempted=preempted, - focus_until=( - int(focus_ref[0]) - if focus_ref and focus_ref[0] > 0 - else 0 - ), - ) - except Exception: - pass - # 同步回写 ref 值 - if focus_ref: - self.reservation_focus_until_ts = float(focus_ref[0] or 0.0) - if after_action_ref: - self.reservation_after_action_pending = bool( - after_action_ref[0] - ) - - # 如果抢占成功,设置当前参与的福袋类型为预约福袋 - if preempted: - self._current_attending_reservation_fudai = True - - # 抢占逻辑已删除,直接继续处理 - else: - # 单间模式:不需要回退,预约会在当前直播间自动识别和参与 - # 只需要清理过期的预约 - try: - self._log_kv( - "预约", - "[预约] 单间模式(needswitch=False):不执行预约回退,预约会在当前直播间自动识别和参与", - reservations_count=( - len(self.reservations) if self.reservations else 0 - ), - ) - - # use module-level `douyin_reservation_logic` - douyin_reservation_logic.cleanup_expired_reservations( - self, self.reservations, _write_state - ) - except Exception as exc: - try: - self._log_kv( - "预约", - "[预约] 单间模式下清理过期预约时出错", - error=str(exc), - ) - except Exception: - pass + 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, + ) except Exception: pass diff --git a/douyin/core/fudai_modules/fudai_run_blocks.py b/douyin/core/fudai_modules/fudai_run_blocks.py index 661b5fa..5991756 100644 --- a/douyin/core/fudai_modules/fudai_run_blocks.py +++ b/douyin/core/fudai_modules/fudai_run_blocks.py @@ -655,6 +655,99 @@ def handle_live_closed_stage(analyser: Any, start_time: float) -> None: ) +def execute_reservation_attend_or_cleanup( + analyser: Any, + needswitch_cfg: bool, + focus_ref: list, + after_action_ref: list, + swipe_down_back_callback: Any, + seek_reservation_room_callback: Any, + sort_reservations_callback: Any, + write_state_callback: Any, +) -> None: + """根据 needswitch_cfg 分支处理: + - True:调用 process_reservation_attend_window 抢占回退到目标直播间 + - False(单间模式):仅清理过期预约 + """ + from douyin.reservation import douyin_reservation_logic + + if needswitch_cfg: + try: + analyser._log_kv( + "预约", + "[预约] 多间模式:开始调用 process_reservation_attend_window", + reservations_count=( + len(analyser.reservations) if analyser.reservations else 0 + ), + room_pos=int(getattr(analyser, "room_pos", 0) or 0), + ) + except Exception: + pass + + preempted = douyin_reservation_logic.process_reservation_attend_window( + analyser=analyser, + reservations=analyser.reservations, + room_pos=int(getattr(analyser, "room_pos", 0) or 0), + reservation_focus_until_ts_ref=focus_ref, + reservation_after_action_pending_ref=after_action_ref, + reservation_back_swipe_delay_s=float( + analyser.reservation_back_swipe_delay or 0.6 + ), + swipe_down_back_callback=swipe_down_back_callback, + seek_reservation_room_callback=seek_reservation_room_callback, + sort_reservations_callback=sort_reservations_callback, + write_state_callback=write_state_callback, + ) + + try: + analyser._log_kv( + "预约", + "[预约] process_reservation_attend_window 返回结果", + preempted=preempted, + focus_until=( + int(focus_ref[0]) + if focus_ref and focus_ref[0] > 0 + else 0 + ), + ) + except Exception: + pass + + # 同步回写 ref 值 + if focus_ref: + analyser.reservation_focus_until_ts = float(focus_ref[0] or 0.0) + if after_action_ref: + analyser.reservation_after_action_pending = bool(after_action_ref[0]) + + # 如果抢占成功,设置当前参与的福袋类型为预约福袋 + if preempted: + analyser._current_attending_reservation_fudai = True + return + + # 单间模式:仅清理过期预约 + try: + analyser._log_kv( + "预约", + "[预约] 单间模式(needswitch=False):不执行预约回退,预约会在当前直播间自动识别和参与", + reservations_count=( + len(analyser.reservations) if analyser.reservations else 0 + ), + ) + + douyin_reservation_logic.cleanup_expired_reservations( + analyser, analyser.reservations, write_state_callback + ) + except Exception as exc: + try: + analyser._log_kv( + "预约", + "[预约] 单间模式下清理过期预约时出错", + error=str(exc), + ) + except Exception: + pass + + def log_needswitch_check_details(analyser: Any, needswitch_cfg: bool) -> None: """记录 needswitch 配置 + 第一个预约详情的诊断日志(纯日志辅助)。""" try: diff --git a/tests/test_fudai_run_blocks.py b/tests/test_fudai_run_blocks.py index 4c6a9bb..458202f 100644 --- a/tests/test_fudai_run_blocks.py +++ b/tests/test_fudai_run_blocks.py @@ -10,7 +10,7 @@ from __future__ import annotations import time -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch import pytest @@ -735,3 +735,111 @@ def test_log_needswitch_empty_queue_logs_zero_count(): assert call.kwargs["needswitch_cfg"] is False assert call.kwargs["reservations_count"] == 0 assert call.kwargs["first_reservation"] == {} + + +# ---- execute_reservation_attend_or_cleanup ---- # + +def _make_reservation_analyser(): + a = MagicMock() + a._log_kv = MagicMock() + a.reservations = [{"future_ts": time.time() + 100, "prize_value_yuan": 5.0}] + a.room_pos = 5 + a.reservation_back_swipe_delay = 0.6 + a.reservation_focus_until_ts = 0.0 + a.reservation_after_action_pending = False + a._current_attending_reservation_fudai = False + return a + + +def test_execute_reservation_multi_room_calls_attend_window(): + """needswitch=True 时调用 process_reservation_attend_window""" + a = _make_reservation_analyser() + from douyin.reservation import douyin_reservation_logic as drl + + with patch.object( + drl, "process_reservation_attend_window", return_value=True + ) as fake_attend: + fudai_run_blocks.execute_reservation_attend_or_cleanup( + a, + needswitch_cfg=True, + focus_ref=[123.0], + after_action_ref=[True], + swipe_down_back_callback=MagicMock(), + seek_reservation_room_callback=MagicMock(), + sort_reservations_callback=MagicMock(), + write_state_callback=MagicMock(), + ) + fake_attend.assert_called_once() + # ref 值同步回写 + assert a.reservation_focus_until_ts == 123.0 + assert a.reservation_after_action_pending is True + # preempted=True 时设置标志 + assert a._current_attending_reservation_fudai is True + + +def test_execute_reservation_single_room_calls_cleanup(): + """needswitch=False 时只调用 cleanup_expired_reservations""" + a = _make_reservation_analyser() + from douyin.reservation import douyin_reservation_logic as drl + + write_cb = MagicMock() + with patch.object(drl, "cleanup_expired_reservations") as fake_cleanup, patch.object( + drl, "process_reservation_attend_window" + ) as fake_attend: + fudai_run_blocks.execute_reservation_attend_or_cleanup( + a, + needswitch_cfg=False, + focus_ref=[0.0], + after_action_ref=[False], + swipe_down_back_callback=MagicMock(), + seek_reservation_room_callback=MagicMock(), + sort_reservations_callback=MagicMock(), + write_state_callback=write_cb, + ) + fake_cleanup.assert_called_once_with(a, a.reservations, write_cb) + fake_attend.assert_not_called() + + +def test_execute_reservation_preempted_false_does_not_set_flag(): + """preempted=False 时不应设置 _current_attending_reservation_fudai=True""" + a = _make_reservation_analyser() + from douyin.reservation import douyin_reservation_logic as drl + + with patch.object(drl, "process_reservation_attend_window", return_value=False): + fudai_run_blocks.execute_reservation_attend_or_cleanup( + a, + needswitch_cfg=True, + focus_ref=[0.0], + after_action_ref=[False], + swipe_down_back_callback=MagicMock(), + seek_reservation_room_callback=MagicMock(), + sort_reservations_callback=MagicMock(), + write_state_callback=MagicMock(), + ) + assert a._current_attending_reservation_fudai is False + + +def test_execute_reservation_single_room_swallows_cleanup_exception(): + """单间模式 cleanup 抛错也不应崩""" + a = _make_reservation_analyser() + from douyin.reservation import douyin_reservation_logic as drl + + with patch.object( + drl, "cleanup_expired_reservations", side_effect=RuntimeError("boom") + ): + fudai_run_blocks.execute_reservation_attend_or_cleanup( + a, + needswitch_cfg=False, + focus_ref=[0.0], + after_action_ref=[False], + swipe_down_back_callback=MagicMock(), + seek_reservation_room_callback=MagicMock(), + sort_reservations_callback=MagicMock(), + write_state_callback=MagicMock(), + ) + # 应记录失败日志 + 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