"""单元测试:douyin/reservation/reservation_logic/attend_window_helpers.py""" from __future__ import annotations 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