refactor(fudai): 拆出 needswitch 诊断日志(2429→2390,-39 行)

把 run_once_basic 中"needswitch 配置 + 第一个预约详情"的诊断日志(48 行)
抽到 fudai_run_blocks.log_needswitch_check_details。纯日志辅助函数。

新增 2 个单元测试覆盖正常队列与空队列。1146 → 1148 用例全过。

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 03:08:07 +08:00
1 parent 970e9043cb
commit 28f91b2aaf
3 files changed
+79 -40

No files matched your search

+1 -40
View File
@@ -1333,46 +1333,7 @@ class fudai_analyse:
needswitch_cfg = True
# 添加详细的调试日志,确认 needswitch 的值和预约信息
try:
# 输出第一个预约的详细信息
first_reservation_info = {}
if self.reservations and len(self.reservations) > 0:
first_r = self.reservations[0]
try:
ft = float(first_r.get("future_ts") or 0)
from datetime import datetime
now_ts = time.time()
ft_str = (
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
if ft > 0
else "无效"
)
remain_seconds = int(ft - now_ts) if ft > 0 else 0
first_reservation_info = {
"future_ts": int(ft) if ft > 0 else 0,
"future_str": ft_str,
"value": float(first_r.get("prize_value_yuan", 0)),
"pos_seen": int(first_r.get("pos_seen", 0)),
"remain_seconds": remain_seconds,
"current_room_pos": int(
getattr(self, "room_pos", 0) or 0
),
}
except Exception:
pass
self._log_kv(
"预约",
"[预约] run_once_basic:检查预约回退条件",
needswitch_cfg=needswitch_cfg,
reservations_count=(
len(self.reservations) if self.reservations else 0
),
first_reservation=first_reservation_info,
)
except Exception:
pass
fudai_run_blocks.log_needswitch_check_details(self, needswitch_cfg)
if needswitch_cfg:
# 多间模式:需要回退到预约直播间
@@ -655,6 +655,46 @@ def handle_live_closed_stage(analyser: Any, start_time: float) -> None:
)
def log_needswitch_check_details(analyser: Any, needswitch_cfg: bool) -> None:
"""记录 needswitch 配置 + 第一个预约详情的诊断日志(纯日志辅助)。"""
try:
first_reservation_info = {}
reservations = getattr(analyser, "reservations", None)
if reservations and len(reservations) > 0:
first_r = reservations[0]
try:
ft = float(first_r.get("future_ts") or 0)
now_ts = time.time()
ft_str = (
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
if ft > 0
else "无效"
)
remain_seconds = int(ft - now_ts) if ft > 0 else 0
first_reservation_info = {
"future_ts": int(ft) if ft > 0 else 0,
"future_str": ft_str,
"value": float(first_r.get("prize_value_yuan", 0)),
"pos_seen": int(first_r.get("pos_seen", 0)),
"remain_seconds": remain_seconds,
"current_room_pos": int(getattr(analyser, "room_pos", 0) or 0),
}
except Exception:
pass
analyser._log_kv(
"预约",
"[预约] run_once_basic:检查预约回退条件",
needswitch_cfg=needswitch_cfg,
reservations_count=(
len(reservations) if reservations else 0
),
first_reservation=first_reservation_info,
)
except Exception:
pass
def handle_all_reservations_completed_reset(
analyser: Any, swipe_down_back_callback: Any
) -> bool:
+38
View File
@@ -697,3 +697,41 @@ def test_completed_reset_zero_room_pos_no_swipes():
swipe = MagicMock()
assert fudai_run_blocks.handle_all_reservations_completed_reset(a, swipe) is True
swipe.assert_not_called()
# ---- log_needswitch_check_details ---- #
def test_log_needswitch_with_reservation_logs_first_detail():
a = MagicMock()
a.reservations = [
{
"future_ts": time.time() + 100,
"prize_value_yuan": 5.0,
"pos_seen": 3,
}
]
a.room_pos = 7
fudai_run_blocks.log_needswitch_check_details(a, True)
call = next(
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "检查预约回退条件" in c.args[1]
)
assert call.kwargs["needswitch_cfg"] is True
assert call.kwargs["reservations_count"] == 1
info = call.kwargs["first_reservation"]
assert info["pos_seen"] == 3
assert info["current_room_pos"] == 7
assert info["value"] == 5.0
def test_log_needswitch_empty_queue_logs_zero_count():
a = MagicMock()
a.reservations = []
fudai_run_blocks.log_needswitch_check_details(a, False)
call = next(
c for c in a._log_kv.call_args_list
if len(c.args) >= 2 and "检查预约回退条件" in c.args[1]
)
assert call.kwargs["needswitch_cfg"] is False
assert call.kwargs["reservations_count"] == 0
assert call.kwargs["first_reservation"] == {}