From b6cd3ac8a3eba0fa6fbe497150fb47ca8d84c8d0 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 02:15:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8B=86=E5=87=BA=20=5Fhand?= =?UTF-8?q?le=5Fin=5Ffudai=5Fdetail=20=E8=AF=A6=E6=83=85=E4=B8=BANone?= =?UTF-8?q?=E6=97=A9=E8=BF=94=E5=9B=9E=EF=BC=883687=E2=86=923663=EF=BC=8C-?= =?UTF-8?q?24=20=E8=A1=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit handle_detail_is_none:detail==None 时清除任务数量标志 + 写完成日志 + 早返回。 新增 3 个单元测试,1068 → 1071 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 28 ++------------- .../core/fudai_modules/fudai_detail_blocks.py | 36 +++++++++++++++++++ tests/test_fudai_detail_blocks.py | 32 +++++++++++++++++ 3 files changed, 70 insertions(+), 26 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 55ed328..6264bc6 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -3304,32 +3304,8 @@ class fudai_analyse: step4_start = time.time() # 执行参与决策 if detail is None: - try: - self._log_kv("参与", "详情数据为 None,无法执行参与决策") - except Exception: - pass - - # 清除任务数量标志 - try: - self._fudai_detail_task_count_from_stage = None - try: - self._log_kv( - "福袋详情", - "_handle_in_fudai_detail:清除任务数量标志", - log_type="DEBUG", - ) - except Exception: - pass - except Exception: - pass - - elapsed = time.time() - start_time - self._log_kv( - "流程", - "_handle_in_fudai_detail完成(详情为None)", - elapsed_seconds=round(elapsed, 2), - ) - return + if fudai_detail_blocks.handle_detail_is_none(self, start_time): + return join_result = douyin_fudai_core.basic_join_decision( self, detail, (skip, skip_reason) ) diff --git a/douyin/core/fudai_modules/fudai_detail_blocks.py b/douyin/core/fudai_modules/fudai_detail_blocks.py index 0375823..60506fc 100644 --- a/douyin/core/fudai_modules/fudai_detail_blocks.py +++ b/douyin/core/fudai_modules/fudai_detail_blocks.py @@ -6,6 +6,7 @@ from __future__ import annotations +import time from typing import Any @@ -68,3 +69,38 @@ def handle_too_many_consecutive_failures( except Exception: pass return True + + +def handle_detail_is_none(analyser: Any, start_time: float) -> bool: + """步骤4 入口:detail 为 None 时清除任务数量标志并提前 return。 + + 返回 True 表示已处理 None 分支,主流程应 return。 + """ + try: + analyser._log_kv("参与", "详情数据为 None,无法执行参与决策") + except Exception: + pass + + try: + analyser._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + + try: + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "_handle_in_fudai_detail完成(详情为None)", + elapsed_seconds=round(elapsed, 2), + ) + except Exception: + pass + return True diff --git a/tests/test_fudai_detail_blocks.py b/tests/test_fudai_detail_blocks.py index a432d9c..a19400c 100644 --- a/tests/test_fudai_detail_blocks.py +++ b/tests/test_fudai_detail_blocks.py @@ -61,3 +61,35 @@ def test_close_detail_failure_does_not_break(): result = fudai_detail_blocks.handle_too_many_consecutive_failures(a) assert result is True a.swipe_up_to_next_live.assert_called_once() + + +# ---- handle_detail_is_none ---- # + +import time as _time + + +def test_detail_is_none_returns_true_and_clears_task_count(): + a = MagicMock() + a._log_kv = MagicMock() + a._fudai_detail_task_count_from_stage = 5 + result = fudai_detail_blocks.handle_detail_is_none(a, _time.time() - 1.0) + assert result is True + assert a._fudai_detail_task_count_from_stage is None + + +def test_detail_is_none_logs_completion(): + a = MagicMock() + a._log_kv = MagicMock() + fudai_detail_blocks.handle_detail_is_none(a, _time.time() - 0.5) + # 至少调用了 3 次:详情为None / 清除标志 / 完成日志 + assert a._log_kv.call_count >= 2 + + +def test_detail_is_none_swallows_log_exceptions(): + """_log_kv 抛错也不应传播""" + a = MagicMock() + a._log_kv.side_effect = RuntimeError("boom") + a._fudai_detail_task_count_from_stage = 5 + # 应当不抛错 + result = fudai_detail_blocks.handle_detail_is_none(a, _time.time()) + assert result is True