refactor(fudai): 拆出锁定期跳过搜索块(3181→3135,-46 行)

把 _handle_live_room_has_or_not_fudai 中锁定期内时间窗口未到的早返回块抽到
fudai_detail_blocks.should_skip_search_in_lock_period:
- 检查 focus_until + queue_full_waiting + 队头预约时间窗口三者
- 若都满足跳过条件,记录日志后返回 True

新增 6 个单元测试覆盖各种边界(无锁定期 / 队列未满 / 队列空 / 窗口已开始 /
窗口未开始 / 无效 ft)。1096 → 1102 用例全过。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 02:35:39 +08:00
1 parent 9c823f4874
commit 878a4c2fd9
3 files changed
+122 -48

No files matched your search

+2 -48
View File
@@ -3079,54 +3079,8 @@ class fudai_analyse:
pass
# 检查是否在锁定期内,且时间窗口未到,避免重复打开福袋详情
try:
now_ts = time.time()
focus_until = float(getattr(self, "reservation_focus_until_ts", 0.0) or 0.0)
queue_full_waiting = bool(
getattr(self, "queue_full_waiting_at_earliest", False) or False
)
if focus_until > 0 and now_ts < focus_until and queue_full_waiting:
# 在锁定期内,且队列满等待中,检查时间窗口
if self.reservations and len(self.reservations) > 0:
earliest = self.reservations[0]
try:
ft = float(earliest.get("future_ts") or 0.0)
if ft > 0:
join_ahead = float(
getattr(
self, "high_value_fudai_join_ahead_seconds", 100
)
or 120
)
time_window_start_ts = ft - join_ahead
# 如果时间窗口未到,避免重复打开福袋详情
if now_ts < time_window_start_ts:
try:
self._log_kv(
"预约",
"[预约] 锁定期内时间窗口未到,避免重复打开福袋详情",
focus_until=int(focus_until),
now_ts=int(now_ts),
time_window_start=int(time_window_start_ts),
remain_seconds=int(
time_window_start_ts - now_ts
),
)
except Exception:
pass
elapsed = time.time() - start_time
self._log_kv(
"流程",
"_handle_live_room_has_or_not_fudai完成(锁定期内时间窗口未到)",
elapsed_seconds=round(elapsed, 2),
)
return
except Exception:
pass
except Exception:
pass
if fudai_detail_blocks.should_skip_search_in_lock_period(self, start_time):
return
room_pos_ref: List[int] = [0]
opened = douyin_flow_control.find_and_open_fudai_detail(self, room_pos_ref)
@@ -532,6 +532,72 @@ def reset_fudai_detail_state_after_attend(analyser: Any) -> None:
except Exception:
pass
def should_skip_search_in_lock_period(analyser: Any, start_time: float) -> bool:
"""检查是否处于锁定期且时间窗口未到,若是则跳过本次福袋搜索。
返回 True 表示应当跳过搜索(主流程提前 return)。
锁定期 + 队列满等待 + 时间窗口未到三者同时成立才会跳过;其他情况返回 False。
"""
try:
now_ts = time.time()
focus_until = float(getattr(analyser, "reservation_focus_until_ts", 0.0) or 0.0)
queue_full_waiting = bool(
getattr(analyser, "queue_full_waiting_at_earliest", False) or False
)
if not (focus_until > 0 and now_ts < focus_until and queue_full_waiting):
return False
reservations = getattr(analyser, "reservations", None)
if not reservations or len(reservations) == 0:
return False
earliest = reservations[0]
try:
ft = float(earliest.get("future_ts") or 0.0)
except Exception:
return False
if ft <= 0:
return False
try:
join_ahead = float(
getattr(analyser, "high_value_fudai_join_ahead_seconds", 100) or 120
)
except Exception:
join_ahead = 120.0
time_window_start_ts = ft - join_ahead
if now_ts >= time_window_start_ts:
return False
try:
analyser._log_kv(
"预约",
"[预约] 锁定期内时间窗口未到,避免重复打开福袋详情",
focus_until=int(focus_until),
now_ts=int(now_ts),
time_window_start=int(time_window_start_ts),
remain_seconds=int(time_window_start_ts - now_ts),
)
except Exception:
pass
try:
elapsed = time.time() - start_time
analyser._log_kv(
"流程",
"_handle_live_room_has_or_not_fudai完成(锁定期内时间窗口未到)",
elapsed_seconds=round(elapsed, 2),
)
except Exception:
pass
return True
except Exception:
return False
"""last_status == "error" 且 reason == "task_count_zero" 的处理。
返回 True 表示已处理(连续失败 3 次切换直播间,或单次失败跳过),
+54
View File
@@ -342,3 +342,57 @@ def test_reset_state_clears_all_flags():
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