From 18c6f7b0254880a89922ca3dd3f1000010936723 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 02:11:44 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8B=86=E5=87=BA=20=5Fhand?= =?UTF-8?q?le=5Fin=5Ffudai=5Fdetail=20=E5=A4=B1=E8=B4=A5=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E5=AD=90=E5=9D=97=EF=BC=883732=E2=86=923687=EF=BC=8C-45=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_in_fudai_detail 中连续参与失败次数过多的早返回块抽到 fudai_detail_blocks.handle_too_many_consecutive_failures: - 检查 _consecutive_attend_failures 是否超阈值 - 关闭详情页 → 按 needswitch 决定切换 → 重置计数器 - 返回 True 让主流程 return 新增 5 个单元测试覆盖阈值边界 / 切换开关 / 异常兜底,1063 → 1068 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 49 +------------ douyin/core/fudai_modules/__init__.py | 2 + .../core/fudai_modules/fudai_detail_blocks.py | 70 +++++++++++++++++++ tests/test_fudai_detail_blocks.py | 63 +++++++++++++++++ 4 files changed, 137 insertions(+), 47 deletions(-) create mode 100644 douyin/core/fudai_modules/fudai_detail_blocks.py create mode 100644 tests/test_fudai_detail_blocks.py diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 5417031..55ed328 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -40,6 +40,7 @@ from douyin.handlers import douyin_swipe_handler from douyin.reservation import douyin_reservation_handler from douyin.stage import douyin_stage_handler from douyin.core.fudai_modules import fudai_run_blocks +from douyin.core.fudai_modules import fudai_detail_blocks class fudai_analyse: @@ -2380,53 +2381,7 @@ class fudai_analyse: # 注意:点击参与抽奖后会自动关闭详情页,下次循环会跳过阶段检测直接查找福袋 # 检查连续参与失败次数,避免卡死 - consecutive_failures = getattr(self, "_consecutive_attend_failures", 0) - max_consecutive_failures = 5 # 最多允许5次连续失败 - if consecutive_failures >= max_consecutive_failures: - try: - self._log_kv( - "参与", - "_handle_in_fudai_detail:连续参与失败次数过多,跳过此福袋", - consecutive_failures=consecutive_failures, - max_allowed=max_consecutive_failures, - ) - except Exception: - pass - # 关闭详情页,避免卡死 - try: - douyin_fudai_detail.close_fudai_detail(self) - except Exception: - pass - # 连续失败过多时,按需切换直播间 - try: - needswitch = bool(getattr(self, "needswitch", True)) - except Exception: - needswitch = True - if needswitch: - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:连续参与失败过多,切换直播间", - ) - except Exception: - pass - try: - self.swipe_up_to_next_live() - except Exception: - pass - else: - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:连续参与失败过多,单间模式不切换", - ) - except Exception: - pass - # 重置计数器,避免影响后续直播间 - try: - self._consecutive_attend_failures = 0 - except Exception: - pass + if fudai_detail_blocks.handle_too_many_consecutive_failures(self): return # 步骤1:检查是否是第二次打开福袋详情 diff --git a/douyin/core/fudai_modules/__init__.py b/douyin/core/fudai_modules/__init__.py index 02d4e81..039fcf6 100644 --- a/douyin/core/fudai_modules/__init__.py +++ b/douyin/core/fudai_modules/__init__.py @@ -11,6 +11,7 @@ from .fudai_lottery_result import LotteryResultMixin from .fudai_room_utils import RoomUtilsMixin from .fudai_main_logic import MainLogicMixin from . import fudai_run_blocks +from . import fudai_detail_blocks __all__ = [ "BaseFudaiMixin", @@ -22,4 +23,5 @@ __all__ = [ "RoomUtilsMixin", "MainLogicMixin", "fudai_run_blocks", + "fudai_detail_blocks", ] diff --git a/douyin/core/fudai_modules/fudai_detail_blocks.py b/douyin/core/fudai_modules/fudai_detail_blocks.py new file mode 100644 index 0000000..0375823 --- /dev/null +++ b/douyin/core/fudai_modules/fudai_detail_blocks.py @@ -0,0 +1,70 @@ +"""_handle_in_fudai_detail 流程的可提取子块。 + +把 ``fudai_analyse._handle_in_fudai_detail`` 中互相独立、退出条件清晰的 +自包含子块抽到此处。每个函数返回 ``True`` 表示主流程应当 ``return``。 +""" + +from __future__ import annotations + +from typing import Any + + +def handle_too_many_consecutive_failures( + analyser: Any, max_consecutive_failures: int = 5 +) -> bool: + """连续参与失败次数过多 → 关闭详情页、按需切换直播间、重置计数器。 + + 返回 True 表示触发了"过多失败"分支,主流程应 return。 + """ + consecutive_failures = getattr(analyser, "_consecutive_attend_failures", 0) + if consecutive_failures < max_consecutive_failures: + return False + + try: + analyser._log_kv( + "参与", + "_handle_in_fudai_detail:连续参与失败次数过多,跳过此福袋", + consecutive_failures=consecutive_failures, + max_allowed=max_consecutive_failures, + ) + except Exception: + pass + + try: + from douyin.fudai import douyin_fudai_detail + + douyin_fudai_detail.close_fudai_detail(analyser) + except Exception: + pass + + try: + needswitch = bool(getattr(analyser, "needswitch", True)) + except Exception: + needswitch = True + + if needswitch: + try: + analyser._log_kv( + "流程", + "_handle_in_fudai_detail:连续参与失败过多,切换直播间", + ) + except Exception: + pass + try: + analyser.swipe_up_to_next_live() + except Exception: + pass + else: + try: + analyser._log_kv( + "流程", + "_handle_in_fudai_detail:连续参与失败过多,单间模式不切换", + ) + except Exception: + pass + + try: + analyser._consecutive_attend_failures = 0 + except Exception: + pass + return True diff --git a/tests/test_fudai_detail_blocks.py b/tests/test_fudai_detail_blocks.py new file mode 100644 index 0000000..a432d9c --- /dev/null +++ b/tests/test_fudai_detail_blocks.py @@ -0,0 +1,63 @@ +"""单元测试:douyin/core/fudai_modules/fudai_detail_blocks.py + +覆盖从 _handle_in_fudai_detail 提取出的子块。 +""" + +from __future__ import annotations + +from unittest.mock import MagicMock, patch + +from douyin.core.fudai_modules import fudai_detail_blocks + + +def _make_analyser(consecutive_failures=0, needswitch=True): + a = MagicMock() + a._consecutive_attend_failures = consecutive_failures + a.needswitch = needswitch + a._log_kv = MagicMock() + a.swipe_up_to_next_live = MagicMock() + return a + + +def test_below_threshold_returns_false(): + a = _make_analyser(consecutive_failures=2) + assert fudai_detail_blocks.handle_too_many_consecutive_failures(a) is False + a.swipe_up_to_next_live.assert_not_called() + + +def test_at_threshold_triggers_recovery_with_switch(): + a = _make_analyser(consecutive_failures=5, needswitch=True) + with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail") as cd: + result = fudai_detail_blocks.handle_too_many_consecutive_failures(a) + assert result is True + cd.assert_called_once_with(a) + a.swipe_up_to_next_live.assert_called_once() + assert a._consecutive_attend_failures == 0 + + +def test_above_threshold_with_no_switch_does_not_swipe(): + a = _make_analyser(consecutive_failures=10, needswitch=False) + with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail"): + result = fudai_detail_blocks.handle_too_many_consecutive_failures(a) + assert result is True + a.swipe_up_to_next_live.assert_not_called() + assert a._consecutive_attend_failures == 0 + + +def test_custom_threshold_param(): + """传入自定义阈值""" + a = _make_analyser(consecutive_failures=3) + assert fudai_detail_blocks.handle_too_many_consecutive_failures(a, max_consecutive_failures=10) is False + assert fudai_detail_blocks.handle_too_many_consecutive_failures(a, max_consecutive_failures=2) is True + + +def test_close_detail_failure_does_not_break(): + """close_fudai_detail 抛错也不影响后续步骤""" + a = _make_analyser(consecutive_failures=5) + with patch( + "douyin.fudai.douyin_fudai_detail.close_fudai_detail", + side_effect=RuntimeError("boom"), + ): + result = fudai_detail_blocks.handle_too_many_consecutive_failures(a) + assert result is True + a.swipe_up_to_next_live.assert_called_once()