抽出 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>
71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
"""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
|