新增 3 个 helper: - step4_make_join_decision:参与决策入口(detail=None 短路 + 调 basic_join_decision) - _handle_reservation_completion:参与成功是预约福袋时的锁定期处理 + 队列清理 - _close_detail_after_attend:根据 _detail_closed_by_attend 决定是否点 click_back 净减 251 行重复代码。新增 7 个 step4 单元测试覆盖: - detail=None 短路 - join_decision 失败不设标志 - countdown 有效设标志 + 关详情 - attend 已关详情时跳过 click_back - 预约福袋无其他预约 → 清锁定期 - 预约福袋有其他预约 → 设保护期 - countdown 已过期不设标志 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
255 lines
9.4 KiB
Python
255 lines
9.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
|
||
|
||
|
||
# ============================================================
|
||
# 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
|
||
|