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:
1 parent
c7c6c51d20
commit
18c6f7b025
4 files changed
+137
-47
No files matched your search
@@ -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()
|
||||
Reference in new issue
Block a user