diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 9f0f7a6..4593217 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1320,40 +1320,9 @@ class fudai_analyse: pass # 检查是否所有预约已完成,需要重置到第一个直播间 - if self.queue_full_waiting_at_earliest and not self.reservations: - try: - self._log_kv( - "预约", - "[预约] 所有预约已完成,重置到第一个直播间", - ) - except Exception: - pass - self.queue_full_waiting_at_earliest = False - self.reservation_focus_until_ts = 0.0 - # 回退到第一个直播间:需要回退 room_pos 次 - try: - current_pos = int(getattr(self, "room_pos", 0) or 0) - back_times = abs(current_pos) - # 防止无限循环:最大回退次数限制为100 - if back_times > 100: - back_times = 100 - if back_times > 0: - for i in range(back_times): - _swipe_down_once() - self.operation.delay(0.6) - self.room_pos = 0 - if hasattr(self, "_swipe_times"): - self._swipe_times = 0 - try: - self._log_kv( - "预约", - "[预约] 已回退到第一个直播间", - back_times=back_times, - ) - except Exception: - pass - except Exception: - pass + if fudai_run_blocks.handle_all_reservations_completed_reset( + self, _swipe_down_once + ): return # 单间模式(needswitch=False)下不启用预约回退逻辑 diff --git a/douyin/core/fudai_modules/fudai_run_blocks.py b/douyin/core/fudai_modules/fudai_run_blocks.py index d61b842..5c64950 100644 --- a/douyin/core/fudai_modules/fudai_run_blocks.py +++ b/douyin/core/fudai_modules/fudai_run_blocks.py @@ -655,6 +655,58 @@ def handle_live_closed_stage(analyser: Any, start_time: float) -> None: ) +def handle_all_reservations_completed_reset( + analyser: Any, swipe_down_back_callback: Any +) -> bool: + """所有预约已完成且队列满等待状态:清除 focus + 回退到第一个直播间。 + + 返回 True 表示触发了重置分支且主流程应 return。 + """ + if not ( + getattr(analyser, "queue_full_waiting_at_earliest", False) + and not getattr(analyser, "reservations", None) + ): + return False + + try: + analyser._log_kv( + "预约", + "[预约] 所有预约已完成,重置到第一个直播间", + ) + except Exception: + pass + try: + analyser.queue_full_waiting_at_earliest = False + analyser.reservation_focus_until_ts = 0.0 + except Exception: + pass + + try: + current_pos = int(getattr(analyser, "room_pos", 0) or 0) + back_times = min(abs(current_pos), 100) # 最多 100 次防死循环 + if back_times > 0: + for _ in range(back_times): + try: + swipe_down_back_callback() + analyser.operation.delay(0.6) + except Exception: + pass + analyser.room_pos = 0 + if hasattr(analyser, "_swipe_times"): + analyser._swipe_times = 0 + try: + analyser._log_kv( + "预约", + "[预约] 已回退到第一个直播间", + back_times=back_times, + ) + except Exception: + pass + except Exception: + pass + return True + + def write_reservations_state_throttled(analyser: Any) -> None: """把当前预约队列写到状态文件,1 秒节流避免高频写盘。 diff --git a/tests/test_fudai_run_blocks.py b/tests/test_fudai_run_blocks.py index 97aa35f..a15a534 100644 --- a/tests/test_fudai_run_blocks.py +++ b/tests/test_fudai_run_blocks.py @@ -639,3 +639,61 @@ def test_write_state_swallows_io_error(): 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()