From 2d971ca7d8a423cd3e0e818c054828f1bba0b9fe Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 02:18:08 +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=E6=95=B0=E6=8D=AE=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E5=96=84=E5=90=8E=EF=BC=883663=E2=86=923632?= =?UTF-8?q?=EF=BC=8C-31=20=E8=A1=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit handle_validation_failure_cleanup:步骤2 数据验证失败时关闭详情页 + 写日志 + 清除任务数量标志(与 detail_is_none 共享同一套清理动作)。 新增 2 个单元测试覆盖正常路径与 close 异常分支,1071 → 1073 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 35 +------------ .../core/fudai_modules/fudai_detail_blocks.py | 49 +++++++++++++++++++ tests/test_fudai_detail_blocks.py | 28 +++++++++++ 3 files changed, 79 insertions(+), 33 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 6264bc6..ee6e796 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -2571,39 +2571,8 @@ class fudai_analyse: ) except Exception: pass - # 关闭详情页并返回 - try: - douyin_fudai_detail.close_fudai_detail(self) - except Exception: - pass - - # 步骤2结束计时 - step2_elapsed = (time.time() - step2_start) * 1000 - self._log_kv( - "详情处理", - "步骤2[读取福袋详情]-完成", - 总耗时毫秒=step2_elapsed, - ) - - # 清除任务数量标志 - 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完成(数据验证失败)", - elapsed_seconds=round(elapsed, 2), + fudai_detail_blocks.handle_validation_failure_cleanup( + self, start_time, step2_start ) return except Exception: diff --git a/douyin/core/fudai_modules/fudai_detail_blocks.py b/douyin/core/fudai_modules/fudai_detail_blocks.py index 60506fc..5ba334c 100644 --- a/douyin/core/fudai_modules/fudai_detail_blocks.py +++ b/douyin/core/fudai_modules/fudai_detail_blocks.py @@ -104,3 +104,52 @@ def handle_detail_is_none(analyser: Any, start_time: float) -> bool: except Exception: pass return True + + +def handle_validation_failure_cleanup( + analyser: Any, start_time: float, step2_start: float +) -> None: + """步骤2 数据验证失败的善后:关闭详情页 + 写日志 + 清除任务数量。 + + 与上面 handle_* 系列不同:本函数不返回布尔值,调用方在 if not is_valid + 分支中调用本函数后立即 return。这是因为该分支已经在外层的 try 中。 + """ + try: + from douyin.fudai import douyin_fudai_detail + + douyin_fudai_detail.close_fudai_detail(analyser) + except Exception: + pass + + try: + step2_elapsed = (time.time() - step2_start) * 1000 + analyser._log_kv( + "详情处理", + "步骤2[读取福袋详情]-完成", + 总耗时毫秒=step2_elapsed, + ) + 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完成(数据验证失败)", + elapsed_seconds=round(elapsed, 2), + ) + except Exception: + pass diff --git a/tests/test_fudai_detail_blocks.py b/tests/test_fudai_detail_blocks.py index a19400c..33e8baa 100644 --- a/tests/test_fudai_detail_blocks.py +++ b/tests/test_fudai_detail_blocks.py @@ -93,3 +93,31 @@ def test_detail_is_none_swallows_log_exceptions(): # 应当不抛错 result = fudai_detail_blocks.handle_detail_is_none(a, _time.time()) assert result is True + + +# ---- handle_validation_failure_cleanup ---- # + +def test_validation_cleanup_closes_detail_and_logs(): + a = MagicMock() + a._log_kv = MagicMock() + a._fudai_detail_task_count_from_stage = 7 + with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail") as cd: + fudai_detail_blocks.handle_validation_failure_cleanup( + a, _time.time() - 0.5, _time.time() - 0.3 + ) + cd.assert_called_once_with(a) + assert a._fudai_detail_task_count_from_stage is None + # 至少 3 次日志:步骤2完成 / 清除标志 / 完成(数据验证失败) + assert a._log_kv.call_count >= 2 + + +def test_validation_cleanup_close_failure_does_not_break(): + a = MagicMock() + with patch( + "douyin.fudai.douyin_fudai_detail.close_fudai_detail", + side_effect=RuntimeError("boom"), + ): + fudai_detail_blocks.handle_validation_failure_cleanup( + a, _time.time(), _time.time() + ) + assert a._fudai_detail_task_count_from_stage is None