refactor(fudai): 拆出 _handle_in_fudai_detail 失败计数子块(3732→3687,-45 行)

把 _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) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 02:11:44 +08:00
1 parent c7c6c51d20
commit 18c6f7b025
4 files changed
+137 -47

No files matched your search

+2 -47
View File
@@ -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:检查是否是第二次打开福袋详情
+2
View File
@@ -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",
]
@@ -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
+63
View File
@@ -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()