refactor(fudai): 抽 _handle_in_fudai_detail 入口检查到 handle_detail_helpers (3294 → 3233)

抽出 2 个独立函数到 fudai_modules/handle_detail_helpers.py:
- log_entry_flags:进入方法时记录关键 flags
- handle_consecutive_failures_check:连续参与失败次数检查

新增 5 个单元测试覆盖阈值、needswitch 分支。行为保持不变。

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 18:31:43 +08:00
1 parent 5802586b20
commit 695a84d8b5
3 files changed
+167 -64

No files matched your search

+3 -64
View File
@@ -1921,74 +1921,13 @@ class fudai_analyse:
# pylint: disable=too-many-nested-blocks
def _handle_in_fudai_detail(self) -> None:
"""已经在福袋详情页:只跑详情识别 + 是否参与的基础逻辑。"""
from douyin.core.fudai_modules import handle_detail_helpers as _hd_helpers
start_time = time.time()
try:
self._log_kv(
"诊断",
"进入 _handle_in_fudai_detail:记录当前关键 flags",
reservation_after_action_pending=bool(
getattr(self, "reservation_after_action_pending", False)
),
reservation_focus_until_ts=float(
getattr(self, "reservation_focus_until_ts", 0.0) or 0.0
),
waiting_for_lottery=bool(
getattr(self, "_waiting_for_lottery_result", False)
),
room_pos=int(getattr(self, "room_pos", 0) or 0),
)
except Exception:
pass
_hd_helpers.log_entry_flags(self)
# 注意:点击参与抽奖后会自动关闭详情页,下次循环会跳过阶段检测直接查找福袋
# 检查连续参与失败次数,避免卡死
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 _hd_helpers.handle_consecutive_failures_check(self):
return
# 步骤1:检查是否是第二次打开福袋详情
@@ -0,0 +1,94 @@
"""_handle_in_fudai_detail 辅助逻辑
把 _handle_in_fudai_detail 的若干独立段落抽出来,保持原语义和日志不变。
helper 返回 True 表示主流程应当结束本轮(caller `return`)。
"""
from __future__ import annotations
import time
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from ..douyin_fudai import fudai_analyse
def log_entry_flags(self: "fudai_analyse") -> None:
"""记录进入 _handle_in_fudai_detail 时的关键 flags。"""
try:
self._log_kv(
"诊断",
"进入 _handle_in_fudai_detail:记录当前关键 flags",
reservation_after_action_pending=bool(
getattr(self, "reservation_after_action_pending", False)
),
reservation_focus_until_ts=float(
getattr(self, "reservation_focus_until_ts", 0.0) or 0.0
),
waiting_for_lottery=bool(
getattr(self, "_waiting_for_lottery_result", False)
),
room_pos=int(getattr(self, "room_pos", 0) or 0),
)
except Exception:
pass
def handle_consecutive_failures_check(self: "fudai_analyse") -> bool:
"""检查连续参与失败次数。
超过阈值则关闭详情页 + 按需切换直播间 + 重置计数器,并返回 True
表示 caller 应当 `return`。
返回 False 表示尚未达到阈值,继续后续步骤。
"""
consecutive_failures = getattr(self, "_consecutive_attend_failures", 0)
max_consecutive_failures = 5
if consecutive_failures < max_consecutive_failures:
return False
from douyin.fudai import douyin_fudai_detail
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
return True
+70
View File
@@ -0,0 +1,70 @@
"""handle_detail_helpers 单元测试。
覆盖:
- log_entry_flags:永不抛
- handle_consecutive_failures_check:阈值未达 / 达到(needswitch=True/False)
"""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import pytest
from douyin.core.fudai_modules import handle_detail_helpers as hd
def _mk_self():
s = MagicMock()
s._consecutive_attend_failures = 0
s.needswitch = True
s.reservation_after_action_pending = False
s.reservation_focus_until_ts = 0.0
s._waiting_for_lottery_result = False
s.room_pos = 0
s._log_kv = MagicMock(return_value=None)
return s
class TestLogEntryFlags:
def test_does_not_raise(self):
s = _mk_self()
hd.log_entry_flags(s)
assert s._log_kv.called
class TestHandleConsecutiveFailuresCheck:
def test_below_threshold_returns_false(self):
s = _mk_self()
s._consecutive_attend_failures = 2
assert hd.handle_consecutive_failures_check(s) is False
def test_at_threshold_with_needswitch_swipes(self):
s = _mk_self()
s._consecutive_attend_failures = 5
s.needswitch = True
s.swipe_up_to_next_live = MagicMock(return_value=None)
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail") as mock_close:
mock_close.return_value = None
assert hd.handle_consecutive_failures_check(s) is True
s.swipe_up_to_next_live.assert_called_once()
# 计数器应被重置
assert s._consecutive_attend_failures == 0
def test_at_threshold_without_needswitch_no_swipe(self):
s = _mk_self()
s._consecutive_attend_failures = 5
s.needswitch = False
s.swipe_up_to_next_live = MagicMock(return_value=None)
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail") as mock_close:
mock_close.return_value = None
assert hd.handle_consecutive_failures_check(s) is True
s.swipe_up_to_next_live.assert_not_called()
assert s._consecutive_attend_failures == 0
def test_above_threshold_also_triggers(self):
s = _mk_self()
s._consecutive_attend_failures = 10
s.needswitch = True
s.swipe_up_to_next_live = MagicMock(return_value=None)
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail"):
assert hd.handle_consecutive_failures_check(s) is True