把 attend_window 中"回退校验失败 → 通过映射表二次回退"的 343 行复杂逻辑抽到 attend_window_helpers: - _resolve_target_pos_via_mapping:通过映射表查找目标位置 - _resolve_current_pos_via_mapping:查找当前位置(必要时重读标题) - _decide_retry_seek:根据 mapped_pos_diff 决定是否需要纠偏 + 方向提示 - handle_verify_failure_retry_via_mapping:组合调用 → 调用 seek_callback → 根据结果设置/清理锁定期标志 + 写状态文件 新增 8 个单元测试覆盖:seek 成功/失败/异常/无回调 / 各种 mapped_pos_diff 分支。 1330 → 1338 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
859 lines
28 KiB
Python
859 lines
28 KiB
Python
"""单元测试:douyin/reservation/reservation_logic/attend_window_helpers.py"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import time
|
||
from unittest.mock import MagicMock
|
||
|
||
from douyin.reservation.reservation_logic import attend_window_helpers as h
|
||
|
||
|
||
def _make_analyser(**overrides):
|
||
a = MagicMock()
|
||
a.high_value_fudai_join_ahead_seconds = 100
|
||
a.high_value_fudai_max_swipe_back = 60
|
||
for k, v in overrides.items():
|
||
setattr(a, k, v)
|
||
return a
|
||
|
||
|
||
# ---- compute_dynamic_ahead ---- #
|
||
|
||
def test_compute_dynamic_ahead_zero_back_steps():
|
||
"""target_pos == room_pos → 不需要回退,提前量保持 join_ahead"""
|
||
a = _make_analyser()
|
||
target = {"pos_seen": 5}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=5, reservation_back_swipe_delay_s=0.6)
|
||
assert est == 0
|
||
assert da == 100 # join_ahead 默认值
|
||
|
||
|
||
def test_compute_dynamic_ahead_with_back_steps():
|
||
"""需要回退 50 步,每步 0.6s → 50*0.6+18 = 48s,仍小于 join_ahead=100,所以保持 100"""
|
||
a = _make_analyser()
|
||
target = {"pos_seen": 0}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=50, reservation_back_swipe_delay_s=0.6)
|
||
assert est == 50
|
||
assert da == 100
|
||
|
||
|
||
def test_compute_dynamic_ahead_back_steps_dominate():
|
||
"""需要回退 200 步但被 max_back=60 截断 → 60*0.6+18 = 54,仍 < 100"""
|
||
a = _make_analyser()
|
||
target = {"pos_seen": 0}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=200, reservation_back_swipe_delay_s=0.6)
|
||
assert est == 60 # 截断到 max_back
|
||
|
||
|
||
def test_compute_dynamic_ahead_huge_delay_dominates():
|
||
"""delay=5s 时 60*5+18=318 > join_ahead,dynamic_ahead 取大值"""
|
||
a = _make_analyser()
|
||
target = {"pos_seen": 0}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=60, reservation_back_swipe_delay_s=5.0)
|
||
assert est == 60
|
||
assert da == 318 # 60*5+18
|
||
|
||
|
||
def test_compute_dynamic_ahead_no_max_back_uncapped():
|
||
"""high_value_fudai_max_swipe_back=0 时,由于 `or 60` 会被兜底成 60"""
|
||
a = _make_analyser(high_value_fudai_max_swipe_back=0)
|
||
target = {"pos_seen": 0}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=100, reservation_back_swipe_delay_s=2.0)
|
||
assert est == 60 # 0 被 `or 60` 兜底
|
||
assert da == 138 # 60*2+18
|
||
|
||
|
||
def test_compute_dynamic_ahead_handles_bad_target_pos():
|
||
"""target.pos_seen 缺失时按 0 处理"""
|
||
a = _make_analyser()
|
||
target = {}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=10, reservation_back_swipe_delay_s=0.6)
|
||
assert est == 10
|
||
|
||
|
||
def test_compute_dynamic_ahead_negative_back_steps_clamped():
|
||
"""target_pos > room_pos(不正常)时 est_back_times 被钳到 0"""
|
||
a = _make_analyser()
|
||
target = {"pos_seen": 100}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=10, reservation_back_swipe_delay_s=0.6)
|
||
assert est == 0
|
||
|
||
|
||
def test_compute_dynamic_ahead_handles_bad_join_ahead():
|
||
"""getattr 抛错时回退到 100.0"""
|
||
a = MagicMock()
|
||
type(a).high_value_fudai_join_ahead_seconds = property(
|
||
lambda self: (_ for _ in ()).throw(RuntimeError("boom"))
|
||
)
|
||
a.high_value_fudai_max_swipe_back = 60
|
||
target = {"pos_seen": 5}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=5, reservation_back_swipe_delay_s=0.6)
|
||
# join_ahead 兜底为 100.0
|
||
assert da == 100
|
||
|
||
|
||
# ---- _ts_to_str ---- #
|
||
|
||
def test_ts_to_str_invalid():
|
||
assert h._ts_to_str(0) == "无效"
|
||
assert h._ts_to_str(-100) == "无效"
|
||
|
||
|
||
def test_ts_to_str_valid():
|
||
s = h._ts_to_str(1700000000) # 2023-11-14 22:13:20 UTC, 本地时区
|
||
assert len(s) == 19 # YYYY-MM-DD HH:MM:SS
|
||
|
||
|
||
# ---- log_attend_window_check_details ---- #
|
||
|
||
def test_log_attend_window_check_details_writes_one_line():
|
||
a = MagicMock()
|
||
h.log_attend_window_check_details(
|
||
a,
|
||
now_ts=1700000000,
|
||
focus_until=0,
|
||
ft=1700000100,
|
||
dynamic_ahead=100,
|
||
join_ahead=100.0,
|
||
poll_before=10.0,
|
||
est_back_times=0,
|
||
)
|
||
assert a._log_kv.call_count == 1
|
||
args, kwargs = a._log_kv.call_args
|
||
assert args[0] == "预约"
|
||
assert "process_reservation_attend_window 详细检查" in args[1]
|
||
assert kwargs["target_future_ts"] == 1700000100
|
||
assert kwargs["dynamic_ahead"] == 100
|
||
assert kwargs["in_lock_period"] is False # focus_until=0
|
||
|
||
|
||
def test_log_attend_window_check_details_in_lock_period():
|
||
a = MagicMock()
|
||
h.log_attend_window_check_details(
|
||
a,
|
||
now_ts=1700000000,
|
||
focus_until=1700000050, # > now_ts
|
||
ft=1700000100,
|
||
dynamic_ahead=100,
|
||
join_ahead=100.0,
|
||
poll_before=10.0,
|
||
est_back_times=0,
|
||
)
|
||
args, kwargs = a._log_kv.call_args
|
||
assert kwargs["in_lock_period"] is True
|
||
|
||
|
||
def test_log_attend_window_check_details_swallows_exceptions():
|
||
bad = MagicMock()
|
||
bad._log_kv.side_effect = RuntimeError("boom")
|
||
h.log_attend_window_check_details(
|
||
bad, now_ts=0, focus_until=0, ft=0, dynamic_ahead=0,
|
||
join_ahead=0.0, poll_before=0.0, est_back_times=0,
|
||
)
|
||
|
||
|
||
# ---- check_waiting_for_reservation_lottery ---- #
|
||
|
||
def test_waiting_lottery_with_reservation_returns_true():
|
||
a = MagicMock()
|
||
a._waiting_for_lottery_result = True
|
||
a._current_attending_reservation_fudai = True
|
||
assert h.check_waiting_for_reservation_lottery(a) is True
|
||
|
||
|
||
def test_waiting_lottery_only_one_flag_returns_false():
|
||
a = MagicMock()
|
||
a._waiting_for_lottery_result = True
|
||
a._current_attending_reservation_fudai = False
|
||
assert h.check_waiting_for_reservation_lottery(a) is False
|
||
|
||
a._waiting_for_lottery_result = False
|
||
a._current_attending_reservation_fudai = True
|
||
assert h.check_waiting_for_reservation_lottery(a) is False
|
||
|
||
|
||
def test_waiting_lottery_neither_flag_returns_false():
|
||
a = MagicMock()
|
||
a._waiting_for_lottery_result = False
|
||
a._current_attending_reservation_fudai = False
|
||
assert h.check_waiting_for_reservation_lottery(a) is False
|
||
|
||
|
||
# ---- maybe_promote_locked_reservation ---- #
|
||
|
||
def test_promote_locked_no_lock_period_returns_unchanged():
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 100}]
|
||
ref = [0.0]
|
||
result = h.maybe_promote_locked_reservation(
|
||
a, reservations, ref, focus_until=0.0, now_ts=200.0, poll_before=10.0
|
||
)
|
||
assert result == 0.0
|
||
|
||
|
||
def test_promote_locked_expired_returns_unchanged():
|
||
"""now_ts >= focus_until → 锁定期已过,不修改"""
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 100}]
|
||
ref = [50.0]
|
||
result = h.maybe_promote_locked_reservation(
|
||
a, reservations, ref, focus_until=50.0, now_ts=100.0, poll_before=10.0
|
||
)
|
||
assert result == 50.0
|
||
|
||
|
||
def test_promote_locked_target_not_in_queue_clears_focus():
|
||
"""锁定期对应的预约已不在队列 → 清除锁定期"""
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 200}, {"future_ts": 300}]
|
||
# focus_until = 1000,但队列里没有 ft+10 = 1000 的预约
|
||
ref = [1000.0]
|
||
now = 500.0
|
||
result = h.maybe_promote_locked_reservation(
|
||
a, reservations, ref, focus_until=1000.0, now_ts=now, poll_before=10.0
|
||
)
|
||
assert result == 0.0
|
||
assert ref[0] == 0.0
|
||
|
||
|
||
def test_promote_locked_already_at_head_no_swap():
|
||
"""锁定预约已在队头 → 不调整顺序"""
|
||
a = MagicMock()
|
||
# focus_until = 200 + 10 = 210,正好是第一个预约的"预期 focus"
|
||
reservations = [{"future_ts": 200}, {"future_ts": 300}]
|
||
ref = [210.0]
|
||
result = h.maybe_promote_locked_reservation(
|
||
a, reservations, ref, focus_until=210.0, now_ts=100.0, poll_before=10.0
|
||
)
|
||
assert result == 210.0
|
||
# 顺序未变
|
||
assert reservations[0]["future_ts"] == 200
|
||
|
||
|
||
def test_promote_locked_swaps_to_head():
|
||
"""锁定预约不在队头 → 移到队头"""
|
||
a = MagicMock()
|
||
# focus_until = 300 + 10 = 310,对应队列中第二个预约
|
||
reservations = [
|
||
{"future_ts": 200},
|
||
{"future_ts": 300},
|
||
]
|
||
ref = [310.0]
|
||
result = h.maybe_promote_locked_reservation(
|
||
a, reservations, ref, focus_until=310.0, now_ts=100.0, poll_before=10.0
|
||
)
|
||
assert result == 310.0
|
||
# 锁定预约(300)已被移到队头
|
||
assert reservations[0]["future_ts"] == 300
|
||
|
||
|
||
# ---- check_already_participated ---- #
|
||
|
||
def test_already_participated_with_match_pops_and_returns_true():
|
||
a = MagicMock()
|
||
a._participated_fudai_future_ts = {100.0, 200.0}
|
||
reservations = [{"future_ts": 100.0}, {"future_ts": 300.0}]
|
||
target = reservations[0]
|
||
write_cb = MagicMock()
|
||
result = h.check_already_participated(a, reservations, target, 100.0, write_cb)
|
||
assert result is True
|
||
assert len(reservations) == 1 # 队头被弹出
|
||
write_cb.assert_called_once()
|
||
|
||
|
||
def test_already_participated_no_match_returns_false():
|
||
a = MagicMock()
|
||
a._participated_fudai_future_ts = {500.0}
|
||
reservations = [{"future_ts": 100.0}]
|
||
write_cb = MagicMock()
|
||
result = h.check_already_participated(a, reservations, reservations[0], 100.0, write_cb)
|
||
assert result is False
|
||
assert len(reservations) == 1
|
||
write_cb.assert_not_called()
|
||
|
||
|
||
def test_already_participated_within_5s_tolerance():
|
||
"""允许 5 秒误差 → 95-105 之间也算已参与"""
|
||
a = MagicMock()
|
||
a._participated_fudai_future_ts = {100.0}
|
||
reservations = [{"future_ts": 103.0}]
|
||
write_cb = MagicMock()
|
||
result = h.check_already_participated(a, reservations, reservations[0], 103.0, write_cb)
|
||
assert result is True
|
||
|
||
|
||
def test_already_participated_outside_5s_returns_false():
|
||
a = MagicMock()
|
||
a._participated_fudai_future_ts = {100.0}
|
||
reservations = [{"future_ts": 110.0}] # 差 10 秒,超出误差
|
||
write_cb = MagicMock()
|
||
result = h.check_already_participated(a, reservations, reservations[0], 110.0, write_cb)
|
||
assert result is False
|
||
|
||
|
||
def test_already_participated_non_set_attr_treated_as_empty():
|
||
a = MagicMock()
|
||
a._participated_fudai_future_ts = "not_a_set"
|
||
reservations = [{"future_ts": 100.0}]
|
||
write_cb = MagicMock()
|
||
result = h.check_already_participated(a, reservations, reservations[0], 100.0, write_cb)
|
||
assert result is False
|
||
|
||
|
||
# ---- discard_invalid_future_ts ---- #
|
||
|
||
def test_discard_invalid_ft_zero_returns_true():
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 0}, {"future_ts": 100}]
|
||
write_cb = MagicMock()
|
||
result = h.discard_invalid_future_ts(a, reservations, reservations[0], 0.0, write_cb)
|
||
assert result is True
|
||
assert len(reservations) == 1
|
||
write_cb.assert_called_once()
|
||
|
||
|
||
def test_discard_invalid_ft_negative_returns_true():
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": -5}]
|
||
write_cb = MagicMock()
|
||
result = h.discard_invalid_future_ts(a, reservations, reservations[0], -5.0, write_cb)
|
||
assert result is True
|
||
|
||
|
||
def test_discard_invalid_ft_positive_returns_false():
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 100}]
|
||
write_cb = MagicMock()
|
||
result = h.discard_invalid_future_ts(a, reservations, reservations[0], 100.0, write_cb)
|
||
assert result is False
|
||
assert len(reservations) == 1
|
||
write_cb.assert_not_called()
|
||
|
||
|
||
# ---- log_target_reservation_details ---- #
|
||
|
||
def test_log_target_reservation_details_writes_one_line():
|
||
a = MagicMock()
|
||
target = {"prize_value_yuan": 5.0, "pos_seen": 3, "content": "test"}
|
||
h.log_target_reservation_details(a, target, ft=1700000000.0)
|
||
a._log_kv.assert_called_once()
|
||
args, kwargs = a._log_kv.call_args
|
||
assert args[0] == "预约"
|
||
assert kwargs["target_future_ts"] == 1700000000
|
||
assert kwargs["target_value"] == 5.0
|
||
|
||
|
||
def test_log_target_reservation_details_invalid_ft():
|
||
a = MagicMock()
|
||
h.log_target_reservation_details(a, {}, ft=0.0)
|
||
a._log_kv.assert_called_once()
|
||
_, kwargs = a._log_kv.call_args
|
||
assert kwargs["target_future_str"] == "无效"
|
||
|
||
|
||
def test_log_target_reservation_details_swallows_exception():
|
||
a = MagicMock()
|
||
a._log_kv.side_effect = RuntimeError("boom")
|
||
# 不应抛错
|
||
h.log_target_reservation_details(a, {}, ft=100.0)
|
||
|
||
|
||
# ---- sort_reservations_with_logging ---- #
|
||
|
||
def test_sort_reservations_logs_before_and_after():
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 200}, {"future_ts": 100}]
|
||
sort_cb = MagicMock(side_effect=lambda lst: lst.sort(key=lambda x: x["future_ts"]))
|
||
h.sort_reservations_with_logging(a, reservations, sort_cb)
|
||
# 至少 2 次日志:开始排序 / 排序完成
|
||
assert a._log_kv.call_count >= 2
|
||
# 排序后队头是 100
|
||
assert reservations[0]["future_ts"] == 100
|
||
|
||
|
||
def test_sort_reservations_swallows_sort_exception():
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 100}]
|
||
sort_cb = MagicMock(side_effect=RuntimeError("boom"))
|
||
# 不应抛错
|
||
h.sort_reservations_with_logging(a, reservations, sort_cb)
|
||
msgs = [c.args[1] for c in a._log_kv.call_args_list if len(c.args) >= 2]
|
||
assert any("队列排序异常" in m for m in msgs)
|
||
|
||
|
||
def test_sort_reservations_empty_queue():
|
||
a = MagicMock()
|
||
sort_cb = MagicMock()
|
||
h.sort_reservations_with_logging(a, [], sort_cb)
|
||
sort_cb.assert_called_once_with([])
|
||
|
||
|
||
# ---- check_total_timeout ---- #
|
||
|
||
def test_check_timeout_not_exceeded_returns_false():
|
||
a = MagicMock()
|
||
now = time.time()
|
||
assert h.check_total_timeout(a, now, total_timeout_s=10.0, stage="测试") is False
|
||
|
||
|
||
def test_check_timeout_exceeded_returns_true():
|
||
a = MagicMock()
|
||
past = time.time() - 100
|
||
assert h.check_total_timeout(a, past, total_timeout_s=10.0, stage="测试") is True
|
||
msgs = [c.args[1] for c in a._log_kv.call_args_list if len(c.args) >= 2]
|
||
assert any("超时退出(测试)" in m for m in msgs)
|
||
|
||
|
||
def test_check_timeout_swallows_exception():
|
||
a = MagicMock()
|
||
a._log_kv.side_effect = RuntimeError("boom")
|
||
past = time.time() - 100
|
||
# 应不抛错
|
||
result = h.check_total_timeout(a, past, total_timeout_s=10.0, stage="测试")
|
||
assert result is True
|
||
|
||
|
||
# ---- check_queue_full_immediate_attend ---- #
|
||
|
||
def test_queue_full_immediate_attend_true():
|
||
a = MagicMock()
|
||
a.queue_full_waiting_at_earliest = True
|
||
assert h.check_queue_full_immediate_attend(a) is True
|
||
|
||
|
||
def test_queue_full_immediate_attend_false():
|
||
a = MagicMock()
|
||
a.queue_full_waiting_at_earliest = False
|
||
assert h.check_queue_full_immediate_attend(a) is False
|
||
|
||
|
||
def test_queue_full_immediate_swallows_attr_error():
|
||
a = MagicMock()
|
||
type(a).queue_full_waiting_at_earliest = property(
|
||
lambda _self: (_ for _ in ()).throw(RuntimeError("boom"))
|
||
)
|
||
assert h.check_queue_full_immediate_attend(a) is False
|
||
|
||
|
||
# ---- handle_time_window_early_returns ---- #
|
||
|
||
def test_time_window_not_yet_arrived_returns_false():
|
||
a = MagicMock()
|
||
cleanup = MagicMock()
|
||
write_cb = MagicMock()
|
||
now = 1700000000.0
|
||
ft = now + 500 # 500s 后
|
||
result = h.handle_time_window_early_returns(
|
||
a, [], now, ft, dynamic_ahead=100,
|
||
poll_before=10.0, is_queue_full_immediate_attend=False,
|
||
cleanup_expired_callback=cleanup, write_state_callback=write_cb,
|
||
)
|
||
assert result is False
|
||
cleanup.assert_not_called()
|
||
|
||
|
||
def test_time_window_missed_calls_cleanup_and_returns_true():
|
||
a = MagicMock()
|
||
cleanup = MagicMock()
|
||
write_cb = MagicMock()
|
||
now = 1700000000.0
|
||
ft = now - 1000 # 已过去
|
||
result = h.handle_time_window_early_returns(
|
||
a, [{"future_ts": ft}], now, ft, dynamic_ahead=100,
|
||
poll_before=10.0, is_queue_full_immediate_attend=False,
|
||
cleanup_expired_callback=cleanup, write_state_callback=write_cb,
|
||
)
|
||
assert result is True
|
||
cleanup.assert_called_once()
|
||
|
||
|
||
def test_time_window_in_range_returns_none():
|
||
"""在窗口内 → 返回 None 让主流程继续"""
|
||
a = MagicMock()
|
||
cleanup = MagicMock()
|
||
now = 1700000000.0
|
||
ft = now + 50 # 50s 内
|
||
result = h.handle_time_window_early_returns(
|
||
a, [], now, ft, dynamic_ahead=100,
|
||
poll_before=10.0, is_queue_full_immediate_attend=False,
|
||
cleanup_expired_callback=cleanup, write_state_callback=MagicMock(),
|
||
)
|
||
assert result is None
|
||
cleanup.assert_not_called()
|
||
|
||
|
||
def test_time_window_queue_full_skips_not_yet_check():
|
||
"""队列满立即参与模式 → 即使时间未到也不返回 False"""
|
||
a = MagicMock()
|
||
cleanup = MagicMock()
|
||
now = 1700000000.0
|
||
ft = now + 500 # 远未到
|
||
result = h.handle_time_window_early_returns(
|
||
a, [], now, ft, dynamic_ahead=100,
|
||
poll_before=10.0, is_queue_full_immediate_attend=True,
|
||
cleanup_expired_callback=cleanup, write_state_callback=MagicMock(),
|
||
)
|
||
# 队列满模式下时间未到也算"在窗口内"
|
||
assert result is None
|
||
|
||
|
||
# ---- handle_lock_period_same_reservation_check ---- #
|
||
|
||
def _make_lock_check_args(**overrides):
|
||
"""构造 handle_lock_period_same_reservation_check 的默认参数。"""
|
||
a = MagicMock()
|
||
a.queue_full_waiting_at_earliest = False
|
||
a.room_pos = 5
|
||
target = {"pos_seen": 5, "future_ts": 1700000100}
|
||
base = {
|
||
"analyser": a,
|
||
"target": target,
|
||
"ft": 1700000100.0,
|
||
"now_ts": 1700000000.0,
|
||
"focus_until": 0.0,
|
||
"poll_before": 10.0,
|
||
"dynamic_ahead": 100,
|
||
"room_pos": 5,
|
||
"reservation_focus_until_ts_ref": [0.0],
|
||
"reservation_after_action_pending_ref": [False],
|
||
"write_state_callback": MagicMock(),
|
||
}
|
||
base.update(overrides)
|
||
return base
|
||
|
||
|
||
def test_lock_check_no_lock_period_returns_none():
|
||
args = _make_lock_check_args(focus_until=0.0)
|
||
assert h.handle_lock_period_same_reservation_check(**args) is None
|
||
|
||
|
||
def test_lock_check_queue_full_mode_returns_none():
|
||
a = MagicMock()
|
||
a.queue_full_waiting_at_earliest = True
|
||
args = _make_lock_check_args(
|
||
focus_until=1700000050.0, # 锁定期内
|
||
analyser=a,
|
||
)
|
||
args["analyser"].room_pos = 5
|
||
assert h.handle_lock_period_same_reservation_check(**args) is None
|
||
|
||
|
||
def test_lock_check_different_reservation_returns_false():
|
||
"""锁定期内但不是当前预约 → False"""
|
||
args = _make_lock_check_args(
|
||
focus_until=1700000050.0, # 远不是 1700000100 + 10 = 1700000110
|
||
ft=1700000100.0,
|
||
poll_before=10.0,
|
||
)
|
||
assert h.handle_lock_period_same_reservation_check(**args) is False
|
||
|
||
|
||
def test_lock_check_same_reservation_at_target_in_window_returns_true():
|
||
"""同一预约 + 已在目标位置 + 时间窗口内 → True"""
|
||
a = MagicMock()
|
||
a.queue_full_waiting_at_earliest = False
|
||
a.room_pos = 5
|
||
write_cb = MagicMock()
|
||
args = _make_lock_check_args(
|
||
analyser=a,
|
||
focus_until=1700000110.0, # = ft + poll_before = 1700000100 + 10
|
||
ft=1700000100.0,
|
||
now_ts=1700000050.0, # 在窗口内 [ft-100=1700000000, ft+10=1700000110]
|
||
write_state_callback=write_cb,
|
||
)
|
||
result = h.handle_lock_period_same_reservation_check(**args)
|
||
assert result is True
|
||
write_cb.assert_called_once()
|
||
assert args["reservation_focus_until_ts_ref"][0] == 1700000110.0
|
||
assert args["reservation_after_action_pending_ref"][0] is True
|
||
|
||
|
||
def test_lock_check_same_reservation_at_target_window_not_yet_returns_false():
|
||
"""同一预约 + 已在目标位置 + 时间窗口未到 → False(清除待处理标志)"""
|
||
a = MagicMock()
|
||
a.queue_full_waiting_at_earliest = False
|
||
a.room_pos = 5
|
||
args = _make_lock_check_args(
|
||
analyser=a,
|
||
focus_until=1700000110.0,
|
||
ft=1700000100.0,
|
||
now_ts=1699999800.0, # 在窗口前 [1700000000-100, 1700000110]
|
||
reservation_after_action_pending_ref=[True],
|
||
)
|
||
result = h.handle_lock_period_same_reservation_check(**args)
|
||
assert result is False
|
||
assert args["reservation_after_action_pending_ref"][0] is False
|
||
|
||
|
||
def test_lock_check_same_reservation_not_at_target_returns_none():
|
||
"""同一预约 + 不在目标位置 → None(让主流程继续走时间窗口检查)"""
|
||
a = MagicMock()
|
||
a.queue_full_waiting_at_earliest = False
|
||
a.room_pos = 10 # 不等于 target.pos_seen=5
|
||
args = _make_lock_check_args(
|
||
analyser=a,
|
||
focus_until=1700000110.0,
|
||
ft=1700000100.0,
|
||
room_pos=10,
|
||
)
|
||
result = h.handle_lock_period_same_reservation_check(**args)
|
||
assert result is None
|
||
|
||
|
||
# ---- log_entering_attend_window ---- #
|
||
|
||
def test_log_entering_attend_window_writes_one_line():
|
||
a = MagicMock()
|
||
h.log_entering_attend_window(
|
||
a, ft=1700000100.0, now_ts=1700000050.0,
|
||
room_pos=5, queue_len=3, dynamic_ahead=100, est_back_times=2, poll_before=10.0,
|
||
)
|
||
a._log_kv.assert_called_once()
|
||
_, kwargs = a._log_kv.call_args
|
||
assert kwargs["queue_len"] == 3
|
||
assert kwargs["est_back_times"] == 2
|
||
|
||
|
||
def test_log_entering_attend_window_swallows_exception():
|
||
a = MagicMock()
|
||
a._log_kv.side_effect = RuntimeError("boom")
|
||
h.log_entering_attend_window(
|
||
a, ft=0, now_ts=0, room_pos=0, queue_len=0,
|
||
dynamic_ahead=0, est_back_times=0, poll_before=0,
|
||
)
|
||
|
||
|
||
# ---- execute_seek_reservation_callback ---- #
|
||
|
||
def test_execute_seek_callback_returns_true():
|
||
a = MagicMock()
|
||
cb = MagicMock(return_value=True)
|
||
target = {"pos_seen": 5}
|
||
result = h.execute_seek_reservation_callback(
|
||
a, target, current_room_pos=10, target_pos=5, pos_diff=5,
|
||
seek_reservation_room_callback=cb,
|
||
)
|
||
assert result is True
|
||
cb.assert_called_once_with(target)
|
||
|
||
|
||
def test_execute_seek_callback_returns_false():
|
||
a = MagicMock()
|
||
cb = MagicMock(return_value=False)
|
||
result = h.execute_seek_reservation_callback(
|
||
a, {}, 0, 0, 0, cb,
|
||
)
|
||
assert result is False
|
||
|
||
|
||
def test_execute_seek_callback_exception_returns_false():
|
||
a = MagicMock()
|
||
cb = MagicMock(side_effect=RuntimeError("boom"))
|
||
result = h.execute_seek_reservation_callback(
|
||
a, {}, 0, 0, 0, cb,
|
||
)
|
||
assert result is False
|
||
# 应记录异常日志
|
||
msgs = [c.args[1] for c in a._log_kv.call_args_list if len(c.args) >= 2]
|
||
assert any("执行回退时发生异常" in m for m in msgs)
|
||
|
||
|
||
# ---- wait_for_live_room_load_after_back ---- #
|
||
|
||
def test_wait_for_live_room_load_uses_default(monkeypatch):
|
||
a = MagicMock()
|
||
a.reservation_back_verify_wait_seconds = 5.0
|
||
monkeypatch.setattr("time.sleep", lambda _s: None)
|
||
h.wait_for_live_room_load_after_back(a)
|
||
a.operation.delay.assert_called_once_with(5.0)
|
||
|
||
|
||
def test_wait_for_live_room_load_clamps_below_min(monkeypatch):
|
||
a = MagicMock()
|
||
a.reservation_back_verify_wait_seconds = 0.05 # < 0.1
|
||
monkeypatch.setattr("time.sleep", lambda _s: None)
|
||
h.wait_for_live_room_load_after_back(a)
|
||
a.operation.delay.assert_called_once_with(0.1)
|
||
|
||
|
||
def test_wait_for_live_room_load_clamps_above_max(monkeypatch):
|
||
a = MagicMock()
|
||
a.reservation_back_verify_wait_seconds = 100.0 # > 10
|
||
monkeypatch.setattr("time.sleep", lambda _s: None)
|
||
h.wait_for_live_room_load_after_back(a)
|
||
a.operation.delay.assert_called_once_with(10.0)
|
||
|
||
|
||
def test_wait_for_live_room_load_falls_back_to_sleep(monkeypatch):
|
||
a = MagicMock()
|
||
a.reservation_back_verify_wait_seconds = 5.0
|
||
a.operation.delay.side_effect = RuntimeError("boom")
|
||
sleep_called = []
|
||
monkeypatch.setattr("time.sleep", lambda s: sleep_called.append(s))
|
||
h.wait_for_live_room_load_after_back(a)
|
||
# 异常时回退到 time.sleep
|
||
assert 5.0 in sleep_called
|
||
|
||
|
||
# ---- log_back_success_pre_verify ---- #
|
||
|
||
def test_log_back_success_pre_verify_writes_one_line():
|
||
a = MagicMock()
|
||
target = {"pos_seen": 5}
|
||
h.log_back_success_pre_verify(
|
||
a, target, "标题", "原始", "内容", 100.0, 7,
|
||
)
|
||
a._log_kv.assert_called_once()
|
||
_, kwargs = a._log_kv.call_args
|
||
assert kwargs["target_pos"] == 5
|
||
assert kwargs["target_value_yuan"] == 100.0
|
||
assert kwargs["current_room_pos"] == 7
|
||
|
||
|
||
def test_log_back_success_swallows_exception():
|
||
a = MagicMock()
|
||
a._log_kv.side_effect = RuntimeError("boom")
|
||
h.log_back_success_pre_verify(a, {}, "", "", "", 0.0, 0)
|
||
|
||
|
||
# ---- log_back_failure_and_finish ---- #
|
||
|
||
def test_log_back_failure_writes_logs_and_calls_write_state():
|
||
a = MagicMock()
|
||
write_cb = MagicMock()
|
||
h.log_back_failure_and_finish(
|
||
a, {"pos_seen": 5}, ft=time.time() + 100, now_ts=time.time(),
|
||
room_pos=3, start_time=time.time() - 1.0, write_state_callback=write_cb,
|
||
)
|
||
write_cb.assert_called_once()
|
||
msgs = [c.args[1] for c in a._log_kv.call_args_list if len(c.args) >= 2]
|
||
assert any("回退到预约直播间失败" in m for m in msgs)
|
||
assert any("返回结果" in m for m in msgs)
|
||
|
||
|
||
def test_log_back_failure_swallows_write_state_exception():
|
||
a = MagicMock()
|
||
write_cb = MagicMock(side_effect=RuntimeError("boom"))
|
||
h.log_back_failure_and_finish(
|
||
a, {}, ft=0, now_ts=0, room_pos=0,
|
||
start_time=time.time(), write_state_callback=write_cb,
|
||
)
|
||
|
||
|
||
# ---- handle_verify_failure_retry_via_mapping ---- #
|
||
|
||
def test_verify_retry_seek_callback_succeeds_returns_true():
|
||
"""二次回退 seek 成功 → 设置锁定期 + return True"""
|
||
a = MagicMock()
|
||
target = {"pos_seen": 5, "future_ts": 1700000100}
|
||
focus_ref = [0.0]
|
||
pending_ref = [False]
|
||
write_cb = MagicMock()
|
||
seek_cb = MagicMock(return_value=True)
|
||
|
||
# _resolve_*_via_mapping 都返回 None → mapped_pos_diff 不计算 → should_retry_seek=True
|
||
# 然后调用 seek_cb(updated_target) 返回 True
|
||
result = h.handle_verify_failure_retry_via_mapping(
|
||
a, target, "标题", "原始", ft=1700000100.0, poll_before=10.0,
|
||
reservation_focus_until_ts_ref=focus_ref,
|
||
reservation_after_action_pending_ref=pending_ref,
|
||
seek_reservation_room_callback=seek_cb,
|
||
write_state_callback=write_cb,
|
||
)
|
||
assert result is True
|
||
assert focus_ref[0] == 1700000110.0
|
||
assert pending_ref[0] is True
|
||
seek_cb.assert_called_once()
|
||
write_cb.assert_called_once()
|
||
|
||
|
||
def test_verify_retry_seek_callback_fails_returns_false():
|
||
"""二次回退 seek 失败 → 清理标志 + return False"""
|
||
a = MagicMock()
|
||
target = {"pos_seen": 5}
|
||
focus_ref = [12345.0] # 已设置的锁定期
|
||
pending_ref = [True]
|
||
write_cb = MagicMock()
|
||
seek_cb = MagicMock(return_value=False)
|
||
|
||
result = h.handle_verify_failure_retry_via_mapping(
|
||
a, target, "标题", "原始", ft=1700000100.0, poll_before=10.0,
|
||
reservation_focus_until_ts_ref=focus_ref,
|
||
reservation_after_action_pending_ref=pending_ref,
|
||
seek_reservation_room_callback=seek_cb,
|
||
write_state_callback=write_cb,
|
||
)
|
||
assert result is False
|
||
# 清理了标志
|
||
assert focus_ref[0] == 0.0
|
||
assert pending_ref[0] is False
|
||
write_cb.assert_called_once()
|
||
|
||
|
||
def test_verify_retry_seek_exception_returns_false():
|
||
"""seek_callback 抛错 → ok2=False → return False"""
|
||
a = MagicMock()
|
||
target = {"pos_seen": 5}
|
||
focus_ref = [0.0]
|
||
pending_ref = [False]
|
||
seek_cb = MagicMock(side_effect=RuntimeError("boom"))
|
||
result = h.handle_verify_failure_retry_via_mapping(
|
||
a, target, "", "", ft=1700000100.0, poll_before=10.0,
|
||
reservation_focus_until_ts_ref=focus_ref,
|
||
reservation_after_action_pending_ref=pending_ref,
|
||
seek_reservation_room_callback=seek_cb,
|
||
write_state_callback=MagicMock(),
|
||
)
|
||
assert result is False
|
||
|
||
|
||
def test_verify_retry_no_seek_callback_returns_false():
|
||
"""seek_callback=None → ok2=False → return False"""
|
||
a = MagicMock()
|
||
result = h.handle_verify_failure_retry_via_mapping(
|
||
a, {"pos_seen": 0}, "", "", ft=0, poll_before=0,
|
||
reservation_focus_until_ts_ref=[0.0],
|
||
reservation_after_action_pending_ref=[False],
|
||
seek_reservation_room_callback=None,
|
||
write_state_callback=MagicMock(),
|
||
)
|
||
assert result is False
|
||
|
||
|
||
# ---- _decide_retry_seek ---- #
|
||
|
||
def test_decide_retry_seek_no_mapping_returns_true():
|
||
"""mapped_current_pos=None → 默认 True"""
|
||
a = MagicMock()
|
||
assert h._decide_retry_seek(a, {}, None, 5, 0) is True
|
||
assert h._decide_retry_seek(a, {}, 5, None, 0) is True
|
||
|
||
|
||
def test_decide_retry_seek_diff_nonzero_returns_true():
|
||
a = MagicMock()
|
||
assert h._decide_retry_seek(a, {}, 3, 7, 0) is True
|
||
|
||
|
||
def test_decide_retry_seek_diff_zero_with_correction_returns_true():
|
||
"""diff=0 但回退前位置 != 目标位置 → 启用方向提示并 return True"""
|
||
a = MagicMock()
|
||
target = {}
|
||
# mapped_current=mapped_target=5,但 room_pos_before=10 != 5
|
||
result = h._decide_retry_seek(a, target, 5, 5, 10)
|
||
assert result is True
|
||
assert target["_seek_direction_hint"] == "down" # 10 > 5
|
||
|
||
|
||
def test_decide_retry_seek_diff_zero_at_correct_pos_returns_false():
|
||
"""diff=0 且回退前位置 == 目标位置 → 跳过二次回退"""
|
||
a = MagicMock()
|
||
assert h._decide_retry_seek(a, {}, 5, 5, 5) is False
|
||
|
||
|
||
def test_decide_retry_seek_correction_up_direction():
|
||
"""room_pos_before < mapped_target → direction_hint = up"""
|
||
a = MagicMock()
|
||
target = {}
|
||
h._decide_retry_seek(a, target, 5, 5, 2) # 2 < 5
|
||
assert target["_seek_direction_hint"] == "up"
|