新增 3 个 helper: - check_last_status_branch:根据 _last_fudai_detail_status 路由 4 个状态 (coupon_popup / watch_task / validation_failed / error) - _clear_task_count_flag:清除 _fudai_detail_task_count_from_stage(多分支共用) - _handle_error_status:error + reason=task_count_zero 时连续 3 次切直播间 净减 260 行重复样板代码。新增 9 个单元测试覆盖: - 4 种已知 status 各自分支 - 未知/空 status 不触发 - task_count_zero 第 1 次仅累计、第 3 次强制切房 / needswitch=False 不切 至此 _handle_in_fudai_detail 总长 1010 → 752 行, douyin_fudai.py 累计 4121 → 2614(-1507 行 / -37%)。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
420 lines
16 KiB
Python
420 lines
16 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
|
||
|
||
|
||
# ============================================================
|
||
# step4_make_join_decision
|
||
# ============================================================
|
||
|
||
class TestStep4MakeJoinDecision:
|
||
"""步骤4:参与决策 + 等待开奖标志 + 预约清理。"""
|
||
|
||
def _mk_for_step4(self):
|
||
s = _mk_self()
|
||
s.operation = MagicMock()
|
||
s.operation.click_back = MagicMock(return_value=None)
|
||
s.operation.delay = MagicMock(return_value=None)
|
||
s.device_id = "MOCK"
|
||
s._last_successful_attend_ts = 0.0
|
||
s._waiting_for_lottery_result = False
|
||
s._lottery_result_expected_ts = 0.0
|
||
s._lottery_wait_start_ts = 0.0
|
||
s._fudai_detail_first_read = False
|
||
s._last_fudai_detail_tuple = None
|
||
s._fudai_detail_task_count_from_stage = 5
|
||
s._detail_closed_by_attend = False
|
||
s.reservations = []
|
||
s.reservation_after_action_pending = False
|
||
s.reservation_focus_until_ts = 0.0
|
||
return s
|
||
|
||
def test_detail_none_logs_and_returns(self):
|
||
s = self._mk_for_step4()
|
||
# detail=None 分支:不应触发 basic_join_decision
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core.basic_join_decision"
|
||
) as mock_join:
|
||
hd.step4_make_join_decision(s, None, False, "", 0.0, 0.0)
|
||
mock_join.assert_not_called()
|
||
# task_count_from_stage 应被清除
|
||
assert s._fudai_detail_task_count_from_stage is None
|
||
|
||
def test_join_decision_failed_returns_silently(self):
|
||
"""basic_join_decision 返回 False → 不设等待开奖标志。"""
|
||
s = self._mk_for_step4()
|
||
s._last_successful_attend_ts = 100.0 # 不变
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||
return_value=False,
|
||
):
|
||
hd.step4_make_join_decision(s, ("X", 0, 0, 0, 0, (0, 0, 0)),
|
||
False, "", 100.0, 0.0)
|
||
assert s._waiting_for_lottery_result is False
|
||
|
||
def test_join_success_sets_lottery_flag(self):
|
||
"""参与成功 + countdown 有效 → 设置等待开奖标志 + 关闭详情页。"""
|
||
import time
|
||
|
||
s = self._mk_for_step4()
|
||
future_ts = time.time() + 30 # 30 秒后开奖
|
||
# detail tuple: (content, ?, ?, ?, ?, countdown_tuple)
|
||
# countdown_tuple = (countdown_start, countdown_end, original_seconds)
|
||
detail = ("test_content", 0, 0, 0, 0, (0, future_ts, 30))
|
||
|
||
# basic_join_decision 返回 True 且更新 _last_successful_attend_ts
|
||
def fake_join(*args, **kwargs):
|
||
s._last_successful_attend_ts = time.time()
|
||
return True
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||
side_effect=fake_join,
|
||
):
|
||
hd.step4_make_join_decision(s, detail, False, "", 0.0, 0.0)
|
||
|
||
assert s._waiting_for_lottery_result is True
|
||
assert s._lottery_result_expected_ts == future_ts
|
||
assert s._fudai_detail_first_read is False
|
||
assert s._last_fudai_detail_tuple is None
|
||
assert s._fudai_detail_task_count_from_stage is None
|
||
# attend 还没关详情 → 应该点 click_back
|
||
s.operation.click_back.assert_called_once()
|
||
|
||
def test_join_success_skips_click_back_when_already_closed(self):
|
||
"""attend_choujiang 已关详情 → 跳过 click_back。"""
|
||
import time
|
||
|
||
s = self._mk_for_step4()
|
||
s._detail_closed_by_attend = True
|
||
future_ts = time.time() + 30
|
||
detail = ("test_content", 0, 0, 0, 0, (0, future_ts, 30))
|
||
|
||
def fake_join(*args, **kwargs):
|
||
s._last_successful_attend_ts = time.time()
|
||
return True
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||
side_effect=fake_join,
|
||
):
|
||
hd.step4_make_join_decision(s, detail, False, "", 0.0, 0.0)
|
||
|
||
s.operation.click_back.assert_not_called()
|
||
|
||
def test_join_success_clears_focus_when_no_other_reservations(self):
|
||
"""预约福袋参与成功且无其他预约 → 清除 reservation_focus_until_ts。"""
|
||
import time
|
||
|
||
s = self._mk_for_step4()
|
||
s.reservation_after_action_pending = True
|
||
s.reservation_focus_until_ts = 9999.0
|
||
# 只有一条预约,且就是当前正在参与的
|
||
future_ts = time.time() + 30
|
||
s.reservations = [{"future_ts": future_ts, "content": "X"}]
|
||
detail = ("test_content", 0, 0, 0, 0, (0, future_ts, 30))
|
||
|
||
def fake_join(*args, **kwargs):
|
||
s._last_successful_attend_ts = time.time()
|
||
return True
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||
side_effect=fake_join,
|
||
):
|
||
hd.step4_make_join_decision(s, detail, False, "", 0.0, 0.0)
|
||
|
||
assert s.reservation_focus_until_ts == 0.0
|
||
assert s.reservation_after_action_pending is False
|
||
# 已完成的预约应被移除
|
||
assert s.reservations == []
|
||
|
||
def test_join_success_sets_protection_when_other_reservations_exist(self):
|
||
"""预约福袋参与成功但还有其他预约 → 设置 10 秒保护期。"""
|
||
import time
|
||
|
||
s = self._mk_for_step4()
|
||
s.reservation_after_action_pending = True
|
||
s.reservation_focus_until_ts = 0.0 # 当前没有锁定
|
||
future_ts = time.time() + 30
|
||
# 当前刚参加的 + 另外一条未来的预约
|
||
s.reservations = [
|
||
{"future_ts": future_ts, "content": "current"},
|
||
{"future_ts": future_ts + 60, "content": "next"},
|
||
]
|
||
detail = ("test_content", 0, 0, 0, 0, (0, future_ts, 30))
|
||
|
||
def fake_join(*args, **kwargs):
|
||
s._last_successful_attend_ts = time.time()
|
||
return True
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||
side_effect=fake_join,
|
||
):
|
||
with patch(
|
||
"douyin.reservation.douyin_reservation_logic._sort_reservations_inplace",
|
||
return_value=None,
|
||
):
|
||
hd.step4_make_join_decision(s, detail, False, "", 0.0, 0.0)
|
||
|
||
# 设置了保护期(约 10 秒后)
|
||
assert s.reservation_focus_until_ts > time.time() + 5
|
||
assert s.reservation_after_action_pending is False
|
||
# 当前预约被移除,下一个保留
|
||
assert len(s.reservations) == 1
|
||
assert s.reservations[0]["content"] == "next"
|
||
|
||
def test_no_remain_seconds_skips_lottery_flag(self):
|
||
"""countdown 已过期(remain<=0)→ 不设等待开奖标志。"""
|
||
import time
|
||
|
||
s = self._mk_for_step4()
|
||
# future_ts 在过去 → remain_seconds == 0
|
||
detail = ("X", 0, 0, 0, 0, (0, time.time() - 10, 30))
|
||
|
||
def fake_join(*args, **kwargs):
|
||
s._last_successful_attend_ts = time.time()
|
||
return True
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||
side_effect=fake_join,
|
||
):
|
||
hd.step4_make_join_decision(s, detail, False, "", 0.0, 0.0)
|
||
|
||
assert s._waiting_for_lottery_result is False
|
||
|
||
|
||
# ============================================================
|
||
# check_invalid_detail_fallback
|
||
# ============================================================
|
||
|
||
class TestCheckInvalidDetailFallback:
|
||
def test_empty_content_zero_value_returns_true(self):
|
||
s = _mk_self()
|
||
s._fudai_detail_task_count_from_stage = 5
|
||
# detail[0]="", detail[4]=0.0 → 视为无效
|
||
detail = ("", 0, 0, 0, 0.0, (0, 0, 0))
|
||
assert hd.check_invalid_detail_fallback(s, detail, 0.0) is True
|
||
assert s._fudai_detail_task_count_from_stage is None
|
||
|
||
def test_normal_content_returns_false(self):
|
||
s = _mk_self()
|
||
detail = ("有效福袋", 0, 0, 0, 100.0, (0, 9999, 30))
|
||
assert hd.check_invalid_detail_fallback(s, detail, 0.0) is False
|
||
|
||
def test_value_zero_but_has_content_returns_false(self):
|
||
s = _mk_self()
|
||
detail = ("某某福袋", 0, 0, 0, 0.0, (0, 0, 0))
|
||
assert hd.check_invalid_detail_fallback(s, detail, 0.0) is False
|
||
|
||
def test_short_tuple_returns_false(self):
|
||
"""detail tuple 长度 < 5 时不应触发。"""
|
||
s = _mk_self()
|
||
assert hd.check_invalid_detail_fallback(s, ("a", "b"), 0.0) is False
|
||
|
||
def test_non_tuple_returns_false(self):
|
||
s = _mk_self()
|
||
assert hd.check_invalid_detail_fallback(s, None, 0.0) is False
|
||
assert hd.check_invalid_detail_fallback(s, "not a tuple", 0.0) is False
|
||
|
||
|
||
# ============================================================
|
||
# prepare_for_attend
|
||
# ============================================================
|
||
|
||
class TestPrepareForAttend:
|
||
def test_returns_attend_before_ts(self):
|
||
s = _mk_self()
|
||
s._last_successful_attend_ts = 12345.6
|
||
# detail 缺 countdown 部分
|
||
result = hd.prepare_for_attend(s, ("X",))
|
||
assert result == 12345.6
|
||
assert s._detail_closed_by_attend is False
|
||
|
||
def test_extracts_future_ts(self):
|
||
import time
|
||
|
||
s = _mk_self()
|
||
future_ts = time.time() + 60
|
||
detail = ("X", 0, 0, 0, 0, (0, future_ts, 60))
|
||
hd.prepare_for_attend(s, detail)
|
||
assert s._current_fudai_future_ts == future_ts
|
||
|
||
def test_none_detail_does_not_raise(self):
|
||
s = _mk_self()
|
||
s._last_successful_attend_ts = 0.0
|
||
# 不应抛
|
||
result = hd.prepare_for_attend(s, None)
|
||
assert result == 0.0
|
||
|
||
def test_attend_before_ts_default_zero(self):
|
||
s = _mk_self()
|
||
# _last_successful_attend_ts 默认在 _mk_self 没设
|
||
if hasattr(s, "_last_successful_attend_ts"):
|
||
del s._last_successful_attend_ts.__dict__ # noqa
|
||
# MagicMock 的属性永远存在 → getattr 返回 MagicMock;用真对象避免歧义
|
||
class Plain:
|
||
pass
|
||
p = Plain()
|
||
p._log_kv = lambda *a, **k: None
|
||
result = hd.prepare_for_attend(p, ("a", "b", "c", "d", 0, (0, 0, 0)))
|
||
assert result == 0.0
|
||
|
||
|
||
# ============================================================
|
||
# check_last_status_branch
|
||
# ============================================================
|
||
|
||
class TestCheckLastStatusBranch:
|
||
def _mk_for_status(self, last_status: str = "", last_extra=None):
|
||
s = _mk_self()
|
||
s._last_fudai_detail_status = last_status
|
||
if last_extra is not None:
|
||
s._last_fudai_detail_extra = last_extra
|
||
s._fudai_detail_task_count_from_stage = 5
|
||
s._fudai_detail_consecutive_failures = 0
|
||
s.device_id = "MOCK"
|
||
s.operation = MagicMock()
|
||
s.operation.delay = MagicMock(return_value=None)
|
||
s.operation.swipe = MagicMock(return_value=None)
|
||
return s
|
||
|
||
def test_empty_status_returns_false(self):
|
||
s = self._mk_for_status("")
|
||
assert hd.check_last_status_branch(s, 0.0) is False
|
||
|
||
def test_unknown_status_returns_false(self):
|
||
s = self._mk_for_status("some_other_status")
|
||
assert hd.check_last_status_branch(s, 0.0) is False
|
||
|
||
def test_coupon_popup_returns_true_clears_task_count(self):
|
||
s = self._mk_for_status("coupon_popup")
|
||
assert hd.check_last_status_branch(s, 0.0) is True
|
||
assert s._fudai_detail_task_count_from_stage is None
|
||
|
||
def test_watch_task_returns_true_clears_task_count(self):
|
||
s = self._mk_for_status("watch_task")
|
||
assert hd.check_last_status_branch(s, 0.0) is True
|
||
assert s._fudai_detail_task_count_from_stage is None
|
||
|
||
def test_validation_failed_returns_true_clears_task_count(self):
|
||
s = self._mk_for_status("validation_failed")
|
||
assert hd.check_last_status_branch(s, 0.0) is True
|
||
assert s._fudai_detail_task_count_from_stage is None
|
||
|
||
def test_error_without_task_count_zero_reason_returns_false(self):
|
||
"""error 但 reason 不是 task_count_zero → 不在此处理。"""
|
||
s = self._mk_for_status("error", last_extra={"reason": "other_error"})
|
||
assert hd.check_last_status_branch(s, 0.0) is False
|
||
|
||
def test_error_task_count_zero_first_failure(self):
|
||
"""task_count_zero 第 1 次:仅累加计数 + 清标志,不切直播间。"""
|
||
s = self._mk_for_status("error", last_extra={"reason": "task_count_zero"})
|
||
s._fudai_detail_consecutive_failures = 0
|
||
s.swipe_up_to_next_live = MagicMock(return_value=None)
|
||
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail"):
|
||
result = hd.check_last_status_branch(s, 0.0)
|
||
assert result is True
|
||
assert s._fudai_detail_consecutive_failures == 1
|
||
s.swipe_up_to_next_live.assert_not_called()
|
||
|
||
def test_error_task_count_zero_third_failure_swipes(self):
|
||
"""task_count_zero 第 3 次:强制关详情 + 切直播间 + 重置计数。"""
|
||
s = self._mk_for_status("error", last_extra={"reason": "task_count_zero"})
|
||
s._fudai_detail_consecutive_failures = 2 # 加 1 后达到 3
|
||
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:
|
||
result = hd.check_last_status_branch(s, 0.0)
|
||
assert result is True
|
||
mock_close.assert_called_once()
|
||
s.swipe_up_to_next_live.assert_called_once()
|
||
# 计数应被重置
|
||
assert s._fudai_detail_consecutive_failures == 0
|
||
# 任务数量标志被清除
|
||
assert s._fudai_detail_task_count_from_stage is None
|
||
|
||
def test_error_task_count_zero_third_failure_no_switch_when_needswitch_false(self):
|
||
"""needswitch=False 时第 3 次也不切直播间。"""
|
||
s = self._mk_for_status("error", last_extra={"reason": "task_count_zero"})
|
||
s._fudai_detail_consecutive_failures = 2
|
||
s.needswitch = False
|
||
s.swipe_up_to_next_live = MagicMock(return_value=None)
|
||
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail"):
|
||
result = hd.check_last_status_branch(s, 0.0)
|
||
assert result is True
|
||
s.swipe_up_to_next_live.assert_not_called()
|
||
assert s._fudai_detail_consecutive_failures == 0
|
||
|