From 970e9043cb3bf4f1b471a63e930e73c5cdae0b31 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 03:05:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8B=86=E5=87=BA"=E6=89=80?= =?UTF-8?q?=E6=9C=89=E9=A2=84=E7=BA=A6=E5=AE=8C=E6=88=90=E5=90=8E=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E5=88=B0=E7=AC=AC=E4=B8=80=E4=B8=AA=E7=9B=B4=E6=92=AD?= =?UTF-8?q?=E9=97=B4"=E5=9D=97=EF=BC=882460=E2=86=922429=EF=BC=8C-31=20?= =?UTF-8?q?=E8=A1=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit handle_all_reservations_completed_reset:当队列满等待标志开启且队列已空时, 清除 focus + 按 room_pos 次数下滑回退到第一个直播间(最多 100 次防死循环)。 新增 5 个单元测试,1141 → 1146 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 37 +----------- douyin/core/fudai_modules/fudai_run_blocks.py | 52 +++++++++++++++++ tests/test_fudai_run_blocks.py | 58 +++++++++++++++++++ 3 files changed, 113 insertions(+), 34 deletions(-) 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()