Files
douyi_guaji_main/tests/test_fudai_detail_blocks.py
T
CodexandClaude Opus 4.7 b8a35656a0 refactor(fudai): 拆出额外验证 + 兜底无效详情检查(1121→1084,-37 行)
- handle_extra_validation:调用 validate_fudai_detail,失败时关闭详情 + 写日志
- handle_invalid_detail_fallback:价值=0 且内容空时判定为"无效详情"提前 return

新增 9 个单元测试,1239 → 1248 用例全过。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 04:02:01 +08:00

1014 lines
36 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.
"""单元测试: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
# ---- handle_reservation_post_attend ---- #
def test_post_attend_skips_when_pending_false():
a = MagicMock()
a.reservation_after_action_pending = False
fudai_detail_blocks.handle_reservation_post_attend(a, future_timestamp=_time.time())
# 不应改任何状态
assert a.reservation_after_action_pending is False
def test_post_attend_no_other_reservations_clears_focus():
a = MagicMock()
a.reservation_after_action_pending = True
a.reservation_focus_until_ts = 12345.0
a.reservations = []
fudai_detail_blocks.handle_reservation_post_attend(
a, future_timestamp=_time.time() + 100
)
assert a.reservation_focus_until_ts == 0.0
assert a.reservation_after_action_pending is False
def test_post_attend_with_other_reservations_sets_protection():
"""剩余预约:focus_until_ts 设为 max(原值, now+10)"""
a = MagicMock()
a.reservation_after_action_pending = True
a.reservation_focus_until_ts = 0.0
current_ts = _time.time()
a.reservations = [
{"future_ts": current_ts + 200}, # 不同预约(>5s 差距)
]
fudai_detail_blocks.handle_reservation_post_attend(
a, future_timestamp=current_ts + 100
)
# 设了保护期
assert a.reservation_focus_until_ts > current_ts
def test_post_attend_removes_completed_reservation():
a = MagicMock()
a.reservation_after_action_pending = True
a.reservation_focus_until_ts = 0.0
target_ts = 1700000100.0
a.reservations = [
{"future_ts": target_ts, "content": "完成的"}, # 应被移除(差 < 5s)
{"future_ts": target_ts + 200, "content": "未完成"}, # 保留
]
fudai_detail_blocks.handle_reservation_post_attend(a, future_timestamp=target_ts)
assert len(a.reservations) == 1
assert a.reservations[0]["content"] == "未完成"
# ---- close_detail_after_successful_attend ---- #
def test_close_detail_when_not_already_closed():
a = MagicMock()
a._detail_closed_by_attend = False
fudai_detail_blocks.close_detail_after_successful_attend(a)
a.operation.click_back.assert_called_once()
def test_close_detail_when_already_closed_skips():
a = MagicMock()
a._detail_closed_by_attend = True
fudai_detail_blocks.close_detail_after_successful_attend(a)
a.operation.click_back.assert_not_called()
def test_close_detail_swallows_exceptions():
a = MagicMock()
a._detail_closed_by_attend = False
a.operation.click_back.side_effect = RuntimeError("boom")
fudai_detail_blocks.close_detail_after_successful_attend(a)
# ---- reset_fudai_detail_state_after_attend ---- #
def test_reset_state_clears_all_flags():
a = MagicMock()
a._fudai_detail_first_read = True
a._last_fudai_detail_tuple = ("x",)
a._fudai_detail_task_count_from_stage = 3
fudai_detail_blocks.reset_fudai_detail_state_after_attend(a)
assert a._fudai_detail_first_read is False
assert a._last_fudai_detail_tuple is None
assert a._fudai_detail_task_count_from_stage is None
# ---- should_skip_search_in_lock_period ---- #
def test_skip_search_no_lock_period_returns_false():
a = MagicMock()
a.reservation_focus_until_ts = 0.0
assert fudai_detail_blocks.should_skip_search_in_lock_period(a, _time.time()) is False
def test_skip_search_lock_period_but_not_queue_full_returns_false():
a = MagicMock()
a.reservation_focus_until_ts = _time.time() + 100
a.queue_full_waiting_at_earliest = False
assert fudai_detail_blocks.should_skip_search_in_lock_period(a, _time.time()) is False
def test_skip_search_no_reservations_returns_false():
a = MagicMock()
a.reservation_focus_until_ts = _time.time() + 100
a.queue_full_waiting_at_earliest = True
a.reservations = []
assert fudai_detail_blocks.should_skip_search_in_lock_period(a, _time.time()) is False
def test_skip_search_time_window_started_returns_false():
"""时间窗口已经开始(now >= ft - join_ahead)→ 不跳过"""
now = _time.time()
a = MagicMock()
a.reservation_focus_until_ts = now + 200
a.queue_full_waiting_at_earliest = True
a.reservations = [{"future_ts": now + 50}] # ft - 100 = now - 50 < now
a.high_value_fudai_join_ahead_seconds = 100
assert fudai_detail_blocks.should_skip_search_in_lock_period(a, now) is False
def test_skip_search_time_window_not_started_returns_true():
"""时间窗口尚未开始(now < ft - join_ahead)→ 跳过"""
now = _time.time()
a = MagicMock()
a.reservation_focus_until_ts = now + 500
a.queue_full_waiting_at_earliest = True
a.reservations = [{"future_ts": now + 200}] # ft - 100 = now + 100 > now
a.high_value_fudai_join_ahead_seconds = 100
assert fudai_detail_blocks.should_skip_search_in_lock_period(a, now) is True
def test_skip_search_invalid_ft_returns_false():
now = _time.time()
a = MagicMock()
a.reservation_focus_until_ts = now + 500
a.queue_full_waiting_at_earliest = True
a.reservations = [{"future_ts": 0}] # 无效
assert fudai_detail_blocks.should_skip_search_in_lock_period(a, now) is False
# ---- handle_second_open_attend ---- #
def test_second_open_not_first_read_returns_false():
a = MagicMock()
a._fudai_detail_first_read = False
assert fudai_detail_blocks.handle_second_open_attend(a, _time.time(), _time.time()) is False
def test_second_open_no_saved_detail_returns_false_for_fallthrough():
"""没有保存的详情数据 → 函数尝试重新读取后返回 False,让主流程走第一次打开路径"""
a = MagicMock()
a._fudai_detail_first_read = True
a._last_fudai_detail_tuple = None
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=None,
):
result = fudai_detail_blocks.handle_second_open_attend(a, _time.time(), _time.time())
assert result is False
def test_second_open_with_valid_detail_calls_attend():
"""有有效详情 → 调用 attend_choujiang 后 return True"""
a = MagicMock()
a._fudai_detail_first_read = True
a._last_fudai_detail_tuple = (
"content",
"x",
"x",
"x",
5.0,
("start_ts", _time.time() + 100, 100), # countdown_tuple: 至少 3 个元素
)
with patch(
"douyin.fudai.douyin_fudai_core.attend_choujiang", return_value=True
) as fake_attend:
result = fudai_detail_blocks.handle_second_open_attend(
a, _time.time(), _time.time()
)
assert result is True
fake_attend.assert_called_once_with(a)
assert a._consecutive_attend_failures == 0
assert a._fudai_detail_first_read is False
def test_second_open_attend_failure_increments_counter():
a = MagicMock()
a._fudai_detail_first_read = True
a._last_fudai_detail_tuple = (
"content", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100),
)
a._consecutive_attend_failures = 2
with patch(
"douyin.fudai.douyin_fudai_core.attend_choujiang", return_value=False
):
result = fudai_detail_blocks.handle_second_open_attend(
a, _time.time(), _time.time()
)
assert result is True
assert a._consecutive_attend_failures == 3
def test_second_open_attend_exception_returns_false_for_fallthrough():
"""attend_choujiang 抛错 → 返回 False 让主流程继续"""
a = MagicMock()
a._fudai_detail_first_read = True
a._last_fudai_detail_tuple = (
"content", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100),
)
with patch(
"douyin.fudai.douyin_fudai_core.attend_choujiang",
side_effect=RuntimeError("boom"),
):
result = fudai_detail_blocks.handle_second_open_attend(
a, _time.time(), _time.time()
)
assert result is False
# ---- handle_filter_skip_close_and_switch ---- #
def test_filter_skip_with_switch_calls_close_and_swipe():
a = MagicMock()
a.needswitch = True
a._fudai_detail_task_count_from_stage = 5
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail") as cd:
fudai_detail_blocks.handle_filter_skip_close_and_switch(
a, _time.time(), "value_too_low"
)
cd.assert_called_once_with(a)
a.swipe_up_to_next_live.assert_called_once()
assert a._fudai_detail_task_count_from_stage is None
def test_filter_skip_no_switch_only_closes():
a = MagicMock()
a.needswitch = False
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail") as cd:
fudai_detail_blocks.handle_filter_skip_close_and_switch(
a, _time.time(), "single_room_mode"
)
cd.assert_called_once_with(a)
a.swipe_up_to_next_live.assert_not_called()
def test_filter_skip_close_failure_swallowed():
a = MagicMock()
a.needswitch = True
with patch(
"douyin.fudai.douyin_fudai_detail.close_fudai_detail",
side_effect=RuntimeError("boom"),
):
# 不应抛错
fudai_detail_blocks.handle_filter_skip_close_and_switch(
a, _time.time(), "x"
)
# 关闭失败但仍执行切换
a.swipe_up_to_next_live.assert_called_once()
# ---- handle_high_value_skip_decide ---- #
def _make_high_value_analyser(threshold=500.0, join_ahead=100):
a = MagicMock()
a._log_kv = MagicMock()
a.high_value_fudai_enabled = True
a.high_value_fudai_threshold_yuan = threshold
a.high_value_fudai_join_ahead_seconds = join_ahead
return a
def test_high_value_skip_not_high_value_keeps_skip():
"""非高价值(未启用) → should_return=False, skip=True 让外层走 close+switch"""
a = _make_high_value_analyser()
a.high_value_fudai_enabled = False
detail = ("c", "x", "x", "x", 1000.0, ("s", _time.time() + 200, 200))
should_return, new_skip = fudai_detail_blocks.handle_high_value_skip_decide(
a, detail, "countdown_gt_120", _time.time()
)
assert should_return is False
assert new_skip is True
def test_high_value_skip_value_below_threshold_keeps_skip():
a = _make_high_value_analyser(threshold=500.0)
detail = ("c", "x", "x", "x", 100.0, ("s", _time.time() + 200, 200))
should_return, new_skip = fudai_detail_blocks.handle_high_value_skip_decide(
a, detail, "countdown_gt_120", _time.time()
)
assert should_return is False
assert new_skip is True
def test_high_value_skip_not_countdown_reason_keeps_skip():
"""skip_reason 不是 countdown_gt_* 时不进入高价值决策"""
a = _make_high_value_analyser()
detail = ("c", "x", "x", "x", 1000.0, ("s", _time.time() + 200, 200))
should_return, new_skip = fudai_detail_blocks.handle_high_value_skip_decide(
a, detail, "fansclub_required", _time.time()
)
assert should_return is False
assert new_skip is True
def test_high_value_skip_within_join_ahead_falls_through_to_attend():
"""剩余秒数 <= join_ahead → should_return=False, skip=False(立即参与)"""
a = _make_high_value_analyser(threshold=500.0, join_ahead=100)
now = _time.time()
detail = ("c", "x", "x", "x", 1000.0, (now - 50, now + 50, 100)) # 剩余 50s
should_return, new_skip = fudai_detail_blocks.handle_high_value_skip_decide(
a, detail, "countdown_gt_120", now
)
assert should_return is False
assert new_skip is False
def test_high_value_skip_far_from_lottery_records_reservation():
"""剩余秒数远大于阈值 → 调用 basic_join_decision 记录预约后 should_return=True"""
a = _make_high_value_analyser(threshold=500.0, join_ahead=100)
now = _time.time()
detail = ("c", "x", "x", "x", 1000.0, (now - 50, now + 500, 550)) # 剩余 500s
with patch(
"douyin.fudai.douyin_fudai_core.basic_join_decision", return_value=False
) as fake_join:
should_return, new_skip = fudai_detail_blocks.handle_high_value_skip_decide(
a, detail, "countdown_gt_120", now
)
assert should_return is True
fake_join.assert_called_once()
def test_high_value_skip_join_decision_exception_still_returns_true():
"""basic_join_decision 抛错时仍 should_return=True 并写完成日志"""
a = _make_high_value_analyser()
now = _time.time()
detail = ("c", "x", "x", "x", 1000.0, (now - 50, now + 500, 550))
with patch(
"douyin.fudai.douyin_fudai_core.basic_join_decision",
side_effect=RuntimeError("boom"),
):
should_return, new_skip = fudai_detail_blocks.handle_high_value_skip_decide(
a, detail, "countdown_gt_120", now
)
assert should_return is True
# 应记录失败日志
failure = [
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "记录预约时出错" in c.args[1]
]
assert len(failure) == 1
def test_high_value_skip_outer_exception_does_not_crash():
"""外层异常被吞噬,返回 (False, True) 让外层走默认路径"""
a = MagicMock()
a._log_kv = MagicMock()
# 让 getattr 抛错
type(a).high_value_fudai_enabled = property(
lambda self: (_ for _ in ()).throw(RuntimeError("boom"))
)
detail = ("c",)
should_return, new_skip = fudai_detail_blocks.handle_high_value_skip_decide(
a, detail, "countdown_gt_120", _time.time()
)
assert should_return is False
assert new_skip is True
# ---- handle_attend_success_post_processing ---- #
def test_attend_success_join_result_false_returns_false():
"""join_result=False → 不触发善后路径"""
a = MagicMock()
a._last_successful_attend_ts = _time.time()
detail = ("c", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
result = fudai_detail_blocks.handle_attend_success_post_processing(
a, detail, attend_before_ts=0.0, start_time=_time.time(),
step4_start=_time.time(), join_result=False,
)
assert result is False
def test_attend_success_no_attend_ts_change_returns_false():
"""_last_successful_attend_ts 没变化 → 不触发善后"""
now = _time.time()
a = MagicMock()
a._last_successful_attend_ts = now # 与 attend_before_ts 相同
detail = ("c", "x", "x", "x", 5.0, ("s", now + 100, 100))
result = fudai_detail_blocks.handle_attend_success_post_processing(
a, detail, attend_before_ts=now, start_time=now,
step4_start=now, join_result=True,
)
assert result is False
def test_attend_success_with_remaining_seconds_sets_waiting_flag():
"""剩余秒数 > 0 → 设置等待开奖标志,return True"""
now = _time.time()
a = MagicMock()
a._last_successful_attend_ts = now + 1
a.reservation_after_action_pending = False
a._detail_closed_by_attend = False
a._fudai_detail_first_read = True
a._last_fudai_detail_tuple = ("x",)
a._fudai_detail_task_count_from_stage = 5
detail = ("c", "x", "x", "x", 5.0, (now - 50, now + 60, 110)) # 剩余 60s
result = fudai_detail_blocks.handle_attend_success_post_processing(
a, detail, attend_before_ts=now, start_time=now,
step4_start=now, join_result=True,
)
assert result is True
assert a._waiting_for_lottery_result is True
assert a._lottery_result_expected_ts > 0
# 重置详情状态
assert a._fudai_detail_first_read is False
assert a._last_fudai_detail_tuple is None
def test_attend_success_no_remaining_seconds_returns_false():
"""remain_seconds == 0 时只记录日志、不设置标志,返回 False 让主流程继续"""
now = _time.time()
a = MagicMock()
a._last_successful_attend_ts = now + 1
detail = ("c", "x", "x", "x", 5.0, (now - 200, now - 100, 100)) # 已过期
result = fudai_detail_blocks.handle_attend_success_post_processing(
a, detail, attend_before_ts=now, start_time=now,
step4_start=now, join_result=True,
)
assert result is False
def test_attend_success_swallows_exception():
"""detail 处理异常被吞噬"""
now = _time.time()
a = MagicMock()
a._last_successful_attend_ts = now + 1
detail = "not a tuple" # 让 isinstance 检查不通过,countdown_tuple 保持 None
result = fudai_detail_blocks.handle_attend_success_post_processing(
a, detail, attend_before_ts=now, start_time=now,
step4_start=now, join_result=True,
)
# remain_seconds=0,应该返回 False
assert result is False
# ---- log_entry_flags ---- #
def test_log_entry_flags_writes_one_line():
a = MagicMock()
a.reservation_after_action_pending = True
a.reservation_focus_until_ts = 12345.6
a._waiting_for_lottery_result = False
a.room_pos = 7
fudai_detail_blocks.log_entry_flags(a)
assert a._log_kv.call_count == 1
args, kwargs = a._log_kv.call_args
assert args[0] == "诊断"
assert kwargs["room_pos"] == 7
assert kwargs["reservation_after_action_pending"] is True
def test_log_entry_flags_swallows_exception():
a = MagicMock()
a._log_kv.side_effect = RuntimeError("boom")
fudai_detail_blocks.log_entry_flags(a)
# ---- save_first_read_detail_success ---- #
def test_save_first_read_sets_flags_and_content():
a = MagicMock()
detail = ("content_x", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
fudai_detail_blocks.save_first_read_detail_success(a, detail)
assert a._last_fudai_detail_tuple == detail
assert a._fudai_detail_first_read is True
assert a._current_fudai_content == "content_x"
def test_save_first_read_logs_success():
a = MagicMock()
detail = ("c", "x", "x", "x", 10.0, ("s", _time.time() + 100, 100))
fudai_detail_blocks.save_first_read_detail_success(a, detail)
success_calls = [
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "福袋详情识别成功" in c.args[1]
]
assert len(success_calls) == 1
def test_save_first_read_swallows_exception():
a = MagicMock()
a._log_kv.side_effect = RuntimeError("boom")
fudai_detail_blocks.save_first_read_detail_success(a, ("c",))
# ---- extract_future_ts_from_detail ---- #
def test_extract_future_ts_with_valid_detail():
a = MagicMock()
a._current_fudai_future_ts = 0.0
target_ts = _time.time() + 100
detail = ("c", "x", "x", "x", 5.0, ("s", target_ts, 100))
fudai_detail_blocks.extract_future_ts_from_detail(a, detail)
assert a._current_fudai_future_ts == target_ts
def test_extract_future_ts_short_tuple_no_change():
a = MagicMock()
a._current_fudai_future_ts = 999.0
detail = ("only_one_elem",)
fudai_detail_blocks.extract_future_ts_from_detail(a, detail)
assert a._current_fudai_future_ts == 999.0
def test_extract_future_ts_invalid_countdown_no_change():
a = MagicMock()
a._current_fudai_future_ts = 999.0
detail = ("c", "x", "x", "x", 5.0, "not_a_tuple")
fudai_detail_blocks.extract_future_ts_from_detail(a, detail)
assert a._current_fudai_future_ts == 999.0
def test_extract_future_ts_zero_value_no_change():
a = MagicMock()
a._current_fudai_future_ts = 999.0
detail = ("c", "x", "x", "x", 5.0, ("s", 0, 0))
fudai_detail_blocks.extract_future_ts_from_detail(a, detail)
assert a._current_fudai_future_ts == 999.0
# ---- reread_detail_for_second_open_fallthrough ---- #
def test_reread_detail_returns_existing_if_valid():
"""已有有效 detail 时直接返回,不重读"""
a = MagicMock()
valid = ("c", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
a._last_fudai_detail_tuple = valid
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries"
) as fake:
result = fudai_detail_blocks.reread_detail_for_second_open_fallthrough(a)
assert result == valid
fake.assert_not_called()
def test_reread_detail_rereads_when_invalid():
a = MagicMock()
a._last_fudai_detail_tuple = None
new_detail = ("new_content", "x", "x", "x", 10.0, ("s", _time.time() + 100, 100))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=new_detail,
):
result = fudai_detail_blocks.reread_detail_for_second_open_fallthrough(a)
assert result == new_detail
assert a._last_fudai_detail_tuple == new_detail
assert a._current_fudai_content == "new_content"
def test_reread_detail_returns_none_when_reread_fails():
a = MagicMock()
a._last_fudai_detail_tuple = None
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=None,
):
result = fudai_detail_blocks.reread_detail_for_second_open_fallthrough(a)
assert result is None
# ---- perform_first_open_read_validate_filter ---- #
def test_first_open_invalid_detail_returns_should_return_true():
a = MagicMock()
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=None,
):
detail, should_return, skip, skip_reason = (
fudai_detail_blocks.perform_first_open_read_validate_filter(
a, _time.time()
)
)
assert should_return is True
assert detail is None
def test_first_open_validation_failed_triggers_cleanup():
a = MagicMock()
bad_detail = ("c", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=bad_detail,
), patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(False, "test_reason"),
), patch(
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
):
detail, should_return, _, _ = (
fudai_detail_blocks.perform_first_open_read_validate_filter(
a, _time.time()
)
)
assert should_return is True
def test_first_open_filter_passes_returns_should_return_false():
"""筛选通过 → should_return=False,主流程继续"""
a = MagicMock()
good_detail = ("content", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=good_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, ""),
):
detail, should_return, skip, skip_reason = (
fudai_detail_blocks.perform_first_open_read_validate_filter(
a, _time.time()
)
)
assert should_return is False
assert skip is False
# detail 已被保存
assert a._last_fudai_detail_tuple == good_detail
def test_first_open_filter_skip_high_value_records_returns_true():
"""筛选不通过 + 高价值决策返回 should_return=True"""
a = MagicMock()
a.high_value_fudai_enabled = True
a.high_value_fudai_threshold_yuan = 500.0
a.high_value_fudai_join_ahead_seconds = 100
now = _time.time()
detail = ("c", "x", "x", "x", 1000.0, (now - 50, now + 500, 550)) # 远超阈值
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_120"),
), patch(
"douyin.fudai.douyin_fudai_core.basic_join_decision",
return_value=False,
):
_, should_return, _, _ = (
fudai_detail_blocks.perform_first_open_read_validate_filter(a, now)
)
assert should_return is True
def test_first_open_filter_skip_normal_triggers_close_switch():
"""筛选不通过 + 非高价值 → handle_filter_skip_close_and_switch + should_return=True"""
a = MagicMock()
a.high_value_fudai_enabled = False
a.needswitch = True
detail = ("c", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
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_too_low"),
), patch(
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
):
_, should_return, _, _ = (
fudai_detail_blocks.perform_first_open_read_validate_filter(
a, _time.time()
)
)
assert should_return is True
# ---- handle_extra_validation ---- #
def test_extra_validation_detail_none_returns_false():
a = MagicMock()
assert fudai_detail_blocks.handle_extra_validation(a, None, _time.time()) is False
def test_extra_validation_valid_returns_false():
a = MagicMock()
detail = ("c", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
with patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(True, ""),
):
assert fudai_detail_blocks.handle_extra_validation(a, detail, _time.time()) is False
def test_extra_validation_invalid_returns_true_and_closes():
a = MagicMock()
detail = ("c", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
with patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(False, "bad"),
), patch(
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
) as cd:
assert fudai_detail_blocks.handle_extra_validation(a, detail, _time.time()) is True
cd.assert_called_once_with(a)
def test_extra_validation_swallows_exception():
a = MagicMock()
with patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
side_effect=RuntimeError("boom"),
):
assert fudai_detail_blocks.handle_extra_validation(a, ("x",), _time.time()) is False
# ---- handle_invalid_detail_fallback ---- #
def test_invalid_detail_fallback_zero_value_empty_content_returns_true():
a = MagicMock()
detail = ("", "x", "x", "x", 0.0)
assert fudai_detail_blocks.handle_invalid_detail_fallback(a, detail, _time.time()) is True
def test_invalid_detail_fallback_with_content_returns_false():
a = MagicMock()
detail = ("有内容", "x", "x", "x", 0.0)
assert fudai_detail_blocks.handle_invalid_detail_fallback(a, detail, _time.time()) is False
def test_invalid_detail_fallback_with_value_returns_false():
a = MagicMock()
detail = ("", "x", "x", "x", 5.0)
assert fudai_detail_blocks.handle_invalid_detail_fallback(a, detail, _time.time()) is False
def test_invalid_detail_fallback_short_tuple_returns_false():
a = MagicMock()
detail = ("only_one",)
assert fudai_detail_blocks.handle_invalid_detail_fallback(a, detail, _time.time()) is False
def test_invalid_detail_fallback_non_tuple_returns_false():
a = MagicMock()
assert fudai_detail_blocks.handle_invalid_detail_fallback(a, None, _time.time()) is False