Files
douyi_guaji_main/tests/test_handle_detail_helpers.py
T

793 lines
32 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""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_invalid_detail_fallback
# ============================================================
class TestCheckInvalidDetailFallback:
def test_invalid_detail_clears_first_read_and_cached_tuple(self):
s = _mk_self()
s._fudai_detail_first_read = True
s._last_fudai_detail_tuple = ("stale",)
detail = ("", "", 0, 0, 0.0, (0, 0, 0))
assert hd.check_invalid_detail_fallback(s, detail, 0.0) is True
assert s._fudai_detail_first_read is False
assert s._last_fudai_detail_tuple is None
# ============================================================
# 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
assert s._last_fudai_detail_status == ""
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
assert s._last_fudai_detail_status == ""
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
assert s._last_fudai_detail_status == ""
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()
assert s._last_fudai_detail_status == ""
assert s._last_fudai_detail_extra == {}
def test_error_not_in_fudai_detail_first_failure(self):
"""read_once 返回的 not_in_fudai_detail 也应走同一条连续失败保护。"""
s = self._mk_for_status("error", last_extra={"reason": "not_in_fudai_detail"})
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()
assert s._last_fudai_detail_status == ""
assert s._last_fudai_detail_extra == {}
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
# ============================================================
# handle_first_open_branch
# ============================================================
class TestHandleFirstOpenBranch:
def _mk_for_first_open(self):
s = _mk_self()
s.device_id = "MOCK"
s.operation = MagicMock()
s.operation.delay = MagicMock(return_value=None)
s.operation.swipe = MagicMock(return_value=None)
s._fudai_detail_task_count_from_stage = 0
s._last_fudai_detail_tuple = None
s._fudai_detail_first_read = False
s._current_fudai_content = ""
s.swipe_up_to_next_live = MagicMock(return_value=None)
s.high_value_fudai_enabled = False
return s
def test_invalid_detail_returns_true(self):
"""read_fudai_detail_with_retries 返回 None / 短 tuple → return True 提前结束。"""
s = self._mk_for_first_open()
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=None,
):
assert hd.handle_first_open_branch(s, 0.0) is True
def test_short_tuple_returns_true(self):
s = self._mk_for_first_open()
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=("only_one_field",),
):
assert hd.handle_first_open_branch(s, 0.0) is True
def test_validation_failed_returns_true_closes_detail(self):
"""validate_fudai_detail 返回 (False, reason) → 关详情 + return True。"""
import time
s = self._mk_for_first_open()
detail = ("X", 0, 0, 0, 0, (0, time.time() + 60, 60))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=detail,
), patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(False, "bad_data"),
), patch(
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
) as mock_close:
result = hd.handle_first_open_branch(s, 0.0)
assert result is True
mock_close.assert_called_once()
assert s._fudai_detail_first_read is False
assert s._last_fudai_detail_tuple is None
def test_filter_pass_saves_data_returns_false(self):
"""筛选通过 → 保存 detail + 标记 first_read,return False 让 caller 继续。"""
import time
s = self._mk_for_first_open()
detail = ("有效福袋", 0, 0, 0, 100.0, (0, time.time() + 60, 60))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=detail,
), patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(True, ""),
), patch(
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
return_value=(False, ""),
):
result = hd.handle_first_open_branch(s, 0.0)
assert result is False
assert s._last_fudai_detail_tuple == detail
assert s._fudai_detail_first_read is True
assert s._current_fudai_content == "有效福袋"
def test_special_status_does_not_mark_first_read(self):
"""若 read_with_retries 已产出终止类状态,不应把详情误标成 second-open。"""
import time
s = self._mk_for_first_open()
s._last_fudai_detail_status = "watch_task"
detail = ("X", 0, 0, 0, 100.0, (0, time.time() + 60, 60))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=detail,
), patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(True, ""),
), patch(
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
return_value=(False, ""),
):
result = hd.handle_first_open_branch(s, 0.0)
assert result is False
assert s._fudai_detail_first_read is False
def test_filter_skip_normal_closes_and_swipes(self):
"""普通筛选不通过(非高价值)→ 关详情 + 切直播间 + return True。"""
import time
s = self._mk_for_first_open()
s.needswitch = True
detail = ("X", 0, 0, 0, 100.0, (0, time.time() + 60, 60))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=detail,
), patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(True, ""),
), patch(
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
return_value=(True, "value_lt_threshold"),
), patch(
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
) as mock_close:
result = hd.handle_first_open_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_first_read is False
assert s._last_fudai_detail_tuple is None
def test_filter_skip_no_switch_when_needswitch_false(self):
"""needswitch=False 筛选不通过时不切直播间。"""
import time
s = self._mk_for_first_open()
s.needswitch = False
detail = ("X", 0, 0, 0, 100.0, (0, time.time() + 60, 60))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=detail,
), patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(True, ""),
), patch(
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
return_value=(True, "value_lt_threshold"),
), patch(
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
):
result = hd.handle_first_open_branch(s, 0.0)
assert result is True
s.swipe_up_to_next_live.assert_not_called()
def test_high_value_within_threshold_continues(self):
"""高价值倒计时阈值内 → skip=False,保存数据 return False。"""
import time
s = self._mk_for_first_open()
s.high_value_fudai_enabled = True
s.high_value_fudai_threshold_yuan = 500.0
s.high_value_fudai_join_ahead_seconds = 100
# 倒计时只剩 60 秒(< 100),价值 1000(> 500) → 立即参与
future_ts = time.time() + 60
detail = ("高价值福袋", 0, 0, 0, 1000.0, (time.time(), future_ts, 60))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=detail,
), patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(True, ""),
), patch(
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
return_value=(True, "countdown_gt_30"),
):
result = hd.handle_first_open_branch(s, 0.0)
assert result is False
assert s._fudai_detail_first_read is True
def test_high_value_outside_threshold_records_reservation(self):
"""高价值倒计时阈值外 → 调 basic_join_decision 记预约 + return True。"""
import time
s = self._mk_for_first_open()
s.high_value_fudai_enabled = True
s.high_value_fudai_threshold_yuan = 500.0
s.high_value_fudai_join_ahead_seconds = 100
# 倒计时还剩 600 秒(> 100) → 记预约
future_ts = time.time() + 600
detail = ("高价值福袋", 0, 0, 0, 1000.0, (time.time(), future_ts, 600))
join_called = MagicMock()
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=detail,
), patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(True, ""),
), patch(
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
return_value=(True, "countdown_gt_30"),
), patch(
"douyin.fudai.douyin_fudai_core.basic_join_decision",
side_effect=join_called,
):
result = hd.handle_first_open_branch(s, 0.0)
assert result is True
join_called.assert_called_once()
# ============================================================
# handle_second_open_branch
# ============================================================
class TestHandleSecondOpenBranch:
def _mk_for_second_open(self):
s = _mk_self()
s.device_id = "MOCK"
s.operation = MagicMock()
s.operation.delay = MagicMock(return_value=None)
s._fudai_detail_first_read = True
s._consecutive_attend_failures = 0
s._fudai_detail_task_count_from_stage = 0
return s
def test_no_cached_detail_refetches_and_returns_false(self):
"""没有缓存的 detail → 重新读取后保存,return False 让 caller 继续走正常流程。"""
import time
s = self._mk_for_second_open()
s._last_fudai_detail_tuple = None
new_detail = ("X", 0, 0, 0, 100.0, (time.time(), time.time() + 60, 60))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=new_detail,
):
result = hd.handle_second_open_branch(s, 0.0, 0.0)
assert result is False
# 重读成功应保存到 self
assert s._last_fudai_detail_tuple == new_detail
assert s._current_fudai_content == "X"
def test_refetch_special_status_clears_first_read_flag(self):
"""第二次打开重读后若状态已终止,不应继续保留 second-open 标志。"""
s = self._mk_for_second_open()
s._last_fudai_detail_tuple = None
s._last_fudai_detail_status = "watch_task"
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=("X", 0, 0, 0, 100.0, (0, 60.0, 60)),
):
result = hd.handle_second_open_branch(s, 0.0, 0.0)
assert result is False
assert s._fudai_detail_first_read is False
def test_short_tuple_triggers_refetch(self):
"""缓存 tuple 长度 < 6 → 触发重新读取。"""
s = self._mk_for_second_open()
s._last_fudai_detail_tuple = ("a", "b") # 太短
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=None,
) as mock_read:
result = hd.handle_second_open_branch(s, 0.0, 0.0)
assert result is False
mock_read.assert_called_once()
assert s._fudai_detail_first_read is False
def test_refetch_invalid_detail_clears_first_read_flag(self):
"""第二次打开重读后仍拿不到有效 detail,不应无限保留 second-open 状态。"""
s = self._mk_for_second_open()
s._last_fudai_detail_tuple = None
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=None,
):
result = hd.handle_second_open_branch(s, 0.0, 0.0)
assert result is False
assert s._fudai_detail_first_read is False
def test_attend_success_returns_true_clears_failures(self):
"""attend_choujiang 成功 → 清标志 + 清失败计数 + return True。"""
import time
s = self._mk_for_second_open()
s._consecutive_attend_failures = 3
future_ts = time.time() + 60
s._last_fudai_detail_tuple = ("X", 0, 0, 0, 100.0, (time.time(), future_ts, 60))
with patch(
"douyin.fudai.douyin_fudai_core.attend_choujiang",
return_value=True,
):
result = hd.handle_second_open_branch(s, 0.0, 0.0)
assert result is True
assert s._consecutive_attend_failures == 0
# 清标志:first_read 重置、tuple 清掉
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
def test_attend_failure_returns_true_increments_counter(self):
"""attend_choujiang 失败 → 增加连续失败计数 + return True。"""
import time
s = self._mk_for_second_open()
s._consecutive_attend_failures = 2
future_ts = time.time() + 60
s._last_fudai_detail_tuple = ("X", 0, 0, 0, 100.0, (time.time(), future_ts, 60))
with patch(
"douyin.fudai.douyin_fudai_core.attend_choujiang",
return_value=False,
):
result = hd.handle_second_open_branch(s, 0.0, 0.0)
assert result is True
assert s._consecutive_attend_failures == 3
def test_attend_exception_returns_false(self):
"""attend_choujiang 抛异常 → log + return False(fall through 到正常流程)。"""
import time
s = self._mk_for_second_open()
future_ts = time.time() + 60
s._last_fudai_detail_tuple = ("X", 0, 0, 0, 100.0, (time.time(), future_ts, 60))
with patch(
"douyin.fudai.douyin_fudai_core.attend_choujiang",
side_effect=RuntimeError("boom"),
):
result = hd.handle_second_open_branch(s, 0.0, 0.0)
assert result is False
def test_sets_current_fudai_future_ts_from_cache(self):
"""缓存 detail 有效时设置 self._current_fudai_future_ts。"""
import time
s = self._mk_for_second_open()
future_ts = time.time() + 60
s._last_fudai_detail_tuple = ("X", 0, 0, 0, 100.0, (time.time(), future_ts, 60))
with patch(
"douyin.fudai.douyin_fudai_core.attend_choujiang",
return_value=True,
):
hd.handle_second_open_branch(s, 0.0, 0.0)
assert s._current_fudai_future_ts == future_ts