把 _handle_in_fudai_detail 中 'last_status==error 且 reason==task_count_zero' 的 165 行处理逻辑抽到 fudai_detail_blocks.handle_task_count_zero_error: - 累加 _fudai_detail_consecutive_failures 计数 - 第 3 次连续失败:关闭详情 + 强制切换直播间(含 ADB swipe 兜底)+ 重置计数 - 单次失败:清除任务数量标志后跳过本次 新增 6 个单元测试覆盖:第 1/3 次失败 / needswitch 开关 / status 不匹配 / extra 不是 dict 等边界。1082 → 1088 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
257 lines
8.9 KiB
Python
257 lines
8.9 KiB
Python
"""单元测试: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()
|
|
|
|
|
|
# ---- handle_detail_is_none ---- #
|
|
|
|
import time as _time
|
|
|
|
|
|
def test_detail_is_none_returns_true_and_clears_task_count():
|
|
a = MagicMock()
|
|
a._log_kv = MagicMock()
|
|
a._fudai_detail_task_count_from_stage = 5
|
|
result = fudai_detail_blocks.handle_detail_is_none(a, _time.time() - 1.0)
|
|
assert result is True
|
|
assert a._fudai_detail_task_count_from_stage is None
|
|
|
|
|
|
def test_detail_is_none_logs_completion():
|
|
a = MagicMock()
|
|
a._log_kv = MagicMock()
|
|
fudai_detail_blocks.handle_detail_is_none(a, _time.time() - 0.5)
|
|
# 至少调用了 3 次:详情为None / 清除标志 / 完成日志
|
|
assert a._log_kv.call_count >= 2
|
|
|
|
|
|
def test_detail_is_none_swallows_log_exceptions():
|
|
"""_log_kv 抛错也不应传播"""
|
|
a = MagicMock()
|
|
a._log_kv.side_effect = RuntimeError("boom")
|
|
a._fudai_detail_task_count_from_stage = 5
|
|
# 应当不抛错
|
|
result = fudai_detail_blocks.handle_detail_is_none(a, _time.time())
|
|
assert result is True
|
|
|
|
|
|
# ---- handle_validation_failure_cleanup ---- #
|
|
|
|
def test_validation_cleanup_closes_detail_and_logs():
|
|
a = MagicMock()
|
|
a._log_kv = MagicMock()
|
|
a._fudai_detail_task_count_from_stage = 7
|
|
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail") as cd:
|
|
fudai_detail_blocks.handle_validation_failure_cleanup(
|
|
a, _time.time() - 0.5, _time.time() - 0.3
|
|
)
|
|
cd.assert_called_once_with(a)
|
|
assert a._fudai_detail_task_count_from_stage is None
|
|
# 至少 3 次日志:步骤2完成 / 清除标志 / 完成(数据验证失败)
|
|
assert a._log_kv.call_count >= 2
|
|
|
|
|
|
def test_validation_cleanup_close_failure_does_not_break():
|
|
a = MagicMock()
|
|
with patch(
|
|
"douyin.fudai.douyin_fudai_detail.close_fudai_detail",
|
|
side_effect=RuntimeError("boom"),
|
|
):
|
|
fudai_detail_blocks.handle_validation_failure_cleanup(
|
|
a, _time.time(), _time.time()
|
|
)
|
|
assert a._fudai_detail_task_count_from_stage is None
|
|
|
|
|
|
# ---- cleanup_and_log_completion ---- #
|
|
|
|
def test_cleanup_and_log_completion_basic():
|
|
a = MagicMock()
|
|
a._fudai_detail_task_count_from_stage = 5
|
|
fudai_detail_blocks.cleanup_and_log_completion(a, _time.time(), "测试原因")
|
|
assert a._fudai_detail_task_count_from_stage is None
|
|
|
|
|
|
def test_cleanup_and_log_completion_with_pre_log():
|
|
a = MagicMock()
|
|
fudai_detail_blocks.cleanup_and_log_completion(
|
|
a,
|
|
_time.time(),
|
|
"无效详情",
|
|
pre_log_msg="检测到无效",
|
|
pre_log_fields={"content": "x", "value": 0},
|
|
)
|
|
# 至少调用了 pre_log + 完成日志
|
|
assert a._log_kv.call_count >= 2
|
|
|
|
|
|
def test_cleanup_and_log_completion_swallows_exceptions():
|
|
a = MagicMock()
|
|
a._log_kv.side_effect = RuntimeError("boom")
|
|
fudai_detail_blocks.cleanup_and_log_completion(a, _time.time(), "异常")
|
|
|
|
|
|
# ---- handle_status_based_early_return ---- #
|
|
|
|
def test_status_coupon_popup_returns_true():
|
|
a = MagicMock()
|
|
a._last_fudai_detail_status = "coupon_popup"
|
|
assert fudai_detail_blocks.handle_status_based_early_return(a, _time.time()) is True
|
|
|
|
|
|
def test_status_watch_task_returns_true():
|
|
a = MagicMock()
|
|
a._last_fudai_detail_status = "watch_task"
|
|
assert fudai_detail_blocks.handle_status_based_early_return(a, _time.time()) is True
|
|
|
|
|
|
def test_status_validation_failed_returns_true():
|
|
a = MagicMock()
|
|
a._last_fudai_detail_status = "validation_failed"
|
|
assert fudai_detail_blocks.handle_status_based_early_return(a, _time.time()) is True
|
|
|
|
|
|
def test_status_unknown_returns_false():
|
|
a = MagicMock()
|
|
a._last_fudai_detail_status = "ok"
|
|
assert fudai_detail_blocks.handle_status_based_early_return(a, _time.time()) is False
|
|
|
|
|
|
def test_status_error_returns_false_for_external_handling():
|
|
"""error 状态留给主流程处理(含 task_count_zero 复杂分支)"""
|
|
a = MagicMock()
|
|
a._last_fudai_detail_status = "error"
|
|
assert fudai_detail_blocks.handle_status_based_early_return(a, _time.time()) is False
|
|
|
|
|
|
def test_status_attribute_missing_returns_false():
|
|
a = MagicMock(spec=[])
|
|
assert fudai_detail_blocks.handle_status_based_early_return(a, _time.time()) is False
|
|
|
|
|
|
# ---- handle_task_count_zero_error ---- #
|
|
|
|
def test_task_count_zero_first_failure_skips():
|
|
"""第一次 task_count_zero:增加计数器、跳过本次,主流程 return"""
|
|
a = MagicMock()
|
|
a._last_fudai_detail_status = "error"
|
|
a._last_fudai_detail_extra = {"reason": "task_count_zero"}
|
|
a._fudai_detail_consecutive_failures = 0
|
|
|
|
result = fudai_detail_blocks.handle_task_count_zero_error(a, _time.time())
|
|
assert result is True
|
|
assert a._fudai_detail_consecutive_failures == 1
|
|
|
|
|
|
def test_task_count_zero_third_failure_force_switches():
|
|
"""第3次连续失败:强制关闭详情 + 切换直播间 + 重置计数器"""
|
|
a = MagicMock()
|
|
a._last_fudai_detail_status = "error"
|
|
a._last_fudai_detail_extra = {"reason": "task_count_zero"}
|
|
a._fudai_detail_consecutive_failures = 2 # 第3次会触发
|
|
a.needswitch = True
|
|
|
|
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail") as cd:
|
|
result = fudai_detail_blocks.handle_task_count_zero_error(a, _time.time())
|
|
assert result is True
|
|
cd.assert_called_once_with(a)
|
|
a.swipe_up_to_next_live.assert_called_once()
|
|
assert a._fudai_detail_consecutive_failures == 0 # 重置
|
|
|
|
|
|
def test_task_count_zero_no_switch_when_disabled():
|
|
"""needswitch=False 时不切换"""
|
|
a = MagicMock()
|
|
a._last_fudai_detail_status = "error"
|
|
a._last_fudai_detail_extra = {"reason": "task_count_zero"}
|
|
a._fudai_detail_consecutive_failures = 2
|
|
a.needswitch = False
|
|
|
|
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail"):
|
|
result = fudai_detail_blocks.handle_task_count_zero_error(a, _time.time())
|
|
assert result is True
|
|
a.swipe_up_to_next_live.assert_not_called()
|
|
|
|
|
|
def test_task_count_zero_status_not_error_returns_false():
|
|
"""status != error 时返回 False"""
|
|
a = MagicMock()
|
|
a._last_fudai_detail_status = "ok"
|
|
a._last_fudai_detail_extra = {"reason": "task_count_zero"}
|
|
assert fudai_detail_blocks.handle_task_count_zero_error(a, _time.time()) is False
|
|
|
|
|
|
def test_task_count_zero_wrong_reason_returns_false():
|
|
"""status=error 但 reason 不是 task_count_zero 返回 False(留给主流程其他分支处理)"""
|
|
a = MagicMock()
|
|
a._last_fudai_detail_status = "error"
|
|
a._last_fudai_detail_extra = {"reason": "other_error"}
|
|
assert fudai_detail_blocks.handle_task_count_zero_error(a, _time.time()) is False
|
|
|
|
|
|
def test_task_count_zero_extra_not_dict_returns_false():
|
|
a = MagicMock()
|
|
a._last_fudai_detail_status = "error"
|
|
a._last_fudai_detail_extra = None
|
|
assert fudai_detail_blocks.handle_task_count_zero_error(a, _time.time()) is False
|