diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 4593217..39695bc 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -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: # 多间模式:需要回退到预约直播间 diff --git a/douyin/core/fudai_modules/fudai_run_blocks.py b/douyin/core/fudai_modules/fudai_run_blocks.py index 5c64950..661b5fa 100644 --- a/douyin/core/fudai_modules/fudai_run_blocks.py +++ b/douyin/core/fudai_modules/fudai_run_blocks.py @@ -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: diff --git a/tests/test_fudai_run_blocks.py b/tests/test_fudai_run_blocks.py index a15a534..4c6a9bb 100644 --- a/tests/test_fudai_run_blocks.py +++ b/tests/test_fudai_run_blocks.py @@ -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"] == {}