From 992db1e1613f4312dd3905657db0718469ee1506 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 02:43:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8B=86=E5=87=BA=204=20?= =?UTF-8?q?=E4=B8=AA=20stage=20=E5=BC=B9=E7=AA=97/=E7=BB=93=E6=9D=9F?= =?UTF-8?q?=E5=88=86=E6=94=AF=EF=BC=882975=E2=86=922915=EF=BC=8C-60=20?= =?UTF-8?q?=E8=A1=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把 run_once_basic 中 4 个 stage 分支抽到 fudai_run_blocks: - handle_coupon_popup_stage:优惠券弹窗按返回键 - handle_shopping_redpacket_popup_stage:购物红包弹窗点击关闭 - handle_lottery_result_stage:开奖结果 OCR 识别 + 处理或回退 - handle_live_closed_stage:直播结束下滑切换 新增 7 个单元测试覆盖每个分支主路径 + 异常吞噬。1107 → 1114 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 68 +---------------- douyin/core/fudai_modules/fudai_run_blocks.py | 75 ++++++++++++++++++ tests/test_fudai_run_blocks.py | 76 +++++++++++++++++++ 3 files changed, 155 insertions(+), 64 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 6abf53b..5714578 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1916,38 +1916,10 @@ class fudai_analyse: # 处理弹窗阶段 if stage == "COUPON_POPUP": - # 优惠券弹窗:按返回键关闭 - self._log_kv("弹窗处理", "检测到优惠券弹窗,按返回键关闭") - try: - self.operation.click_back(self.device_id) - self.operation.delay(0.5) - self._log_kv("弹窗处理", "已按返回键关闭优惠券弹窗") - except Exception as e: - self._log_kv("弹窗处理", "按返回键关闭优惠券弹窗失败", error=str(e)) - elapsed = time.time() - start_time - self._log_kv( - "流程", - "run_once_basic完成(处理优惠券弹窗)", - elapsed_seconds=round(elapsed, 2), - ) + fudai_run_blocks.handle_coupon_popup_stage(self, start_time) return elif stage == "SHOPPING_REDPACKET_POPUP": - # 购物红包弹窗:点击关闭按钮 - self._log_kv("弹窗处理", "检测到购物红包弹窗,点击关闭按钮") - try: - close_x = int(530 * self.resolution_ratio_x // 1080) - close_y = int(1647 * self.resolution_ratio_y // 2400) - self.operation.click(self.device_id, close_x, close_y) - self.operation.delay(0.5) - self._log_kv("弹窗处理", "已点击购物红包关闭按钮", x=close_x, y=close_y) - except Exception as e: - self._log_kv("弹窗处理", "点击购物红包关闭按钮失败", error=str(e)) - elapsed = time.time() - start_time - self._log_kv( - "流程", - "run_once_basic完成(处理购物红包弹窗)", - elapsed_seconds=round(elapsed, 2), - ) + fudai_run_blocks.handle_shopping_redpacket_popup_stage(self, start_time) return elif stage == "FANSCLUB_JOIN": # 粉丝团加入阶段:先点击参与按钮,再点击两次预设位置,最后关闭详情页 @@ -2075,43 +2047,11 @@ class fudai_analyse: douyin_live_room.reflash_zhibo(self) elif stage == "FUDai_RESULT": # 抽奖结果弹窗:处理开奖结果(使用OCR识别,不使用阶段诊断) - # 清除等待开奖标志 - self._waiting_for_lottery_result = False - self._current_attending_reservation_fudai = False - - self._log_kv("开奖结果", "检测到抽奖结果弹窗,使用OCR识别处理") - # 直接OCR识别并处理 - lottery_ocr_start = time.time() - result = self._check_lottery_result_by_ocr() - if result: - self._handle_lottery_result_by_ocr(result) - else: - # OCR识别失败,使用返回键关闭 - try: - self.operation.click_back(self.device_id) - self.operation.delay(0.5) - self.swipe_up_to_next_live() - except Exception: - pass - lottery_ocr_elapsed = time.time() - lottery_ocr_start - self._log_kv("开奖结果", f"OCR处理完成,耗时: {lottery_ocr_elapsed:.3f}s") - elapsed = time.time() - start_time - self._log_kv( - "流程", - "run_once_basic完成(处理开奖结果)", - elapsed_seconds=round(elapsed, 2), - ) + fudai_run_blocks.handle_lottery_result_stage(self, start_time) return elif stage in ("LIVE_CLOSED",): # 直播结束:下滑/切下一个 - self._log_kv("阶段", "当前直播已结束,尝试切换下一个") - self.swipe_up_to_next_live() - elapsed = time.time() - start_time - self._log_kv( - "流程", - "run_once_basic完成(直播结束)", - elapsed_seconds=round(elapsed, 2), - ) + fudai_run_blocks.handle_live_closed_stage(self, start_time) else: # 未识别出特殊阶段:尽量保持原地,交给上层循环 elapsed = time.time() - start_time diff --git a/douyin/core/fudai_modules/fudai_run_blocks.py b/douyin/core/fudai_modules/fudai_run_blocks.py index 5ac801a..1142faa 100644 --- a/douyin/core/fudai_modules/fudai_run_blocks.py +++ b/douyin/core/fudai_modules/fudai_run_blocks.py @@ -578,3 +578,78 @@ def handle_fansclub_join_stage(analyser: Any, start_time: float) -> None: "run_once_basic完成(处理粉丝团)", elapsed_seconds=round(elapsed, 2), ) + + +def handle_coupon_popup_stage(analyser: Any, start_time: float) -> None: + """stage == "COUPON_POPUP":按返回键关闭优惠券弹窗。""" + analyser._log_kv("弹窗处理", "检测到优惠券弹窗,按返回键关闭") + try: + analyser.operation.click_back(analyser.device_id) + analyser.operation.delay(0.5) + analyser._log_kv("弹窗处理", "已按返回键关闭优惠券弹窗") + except Exception as e: + analyser._log_kv("弹窗处理", "按返回键关闭优惠券弹窗失败", error=str(e)) + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "run_once_basic完成(处理优惠券弹窗)", + elapsed_seconds=round(elapsed, 2), + ) + + +def handle_shopping_redpacket_popup_stage(analyser: Any, start_time: float) -> None: + """stage == "SHOPPING_REDPACKET_POPUP":点击关闭按钮关闭购物红包弹窗。""" + analyser._log_kv("弹窗处理", "检测到购物红包弹窗,点击关闭按钮") + try: + close_x = int(530 * analyser.resolution_ratio_x // 1080) + close_y = int(1647 * analyser.resolution_ratio_y // 2400) + analyser.operation.click(analyser.device_id, close_x, close_y) + analyser.operation.delay(0.5) + analyser._log_kv("弹窗处理", "已点击购物红包关闭按钮", x=close_x, y=close_y) + except Exception as e: + analyser._log_kv("弹窗处理", "点击购物红包关闭按钮失败", error=str(e)) + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "run_once_basic完成(处理购物红包弹窗)", + elapsed_seconds=round(elapsed, 2), + ) + + +def handle_lottery_result_stage(analyser: Any, start_time: float) -> None: + """stage == "FUDai_RESULT":清除等待标志 + OCR 识别开奖结果 + 处理或回退。""" + analyser._waiting_for_lottery_result = False + analyser._current_attending_reservation_fudai = False + + analyser._log_kv("开奖结果", "检测到抽奖结果弹窗,使用OCR识别处理") + lottery_ocr_start = time.time() + result = analyser._check_lottery_result_by_ocr() + if result: + analyser._handle_lottery_result_by_ocr(result) + else: + try: + analyser.operation.click_back(analyser.device_id) + analyser.operation.delay(0.5) + analyser.swipe_up_to_next_live() + except Exception: + pass + lottery_ocr_elapsed = time.time() - lottery_ocr_start + analyser._log_kv("开奖结果", f"OCR处理完成,耗时: {lottery_ocr_elapsed:.3f}s") + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "run_once_basic完成(处理开奖结果)", + elapsed_seconds=round(elapsed, 2), + ) + + +def handle_live_closed_stage(analyser: Any, start_time: float) -> None: + """stage == "LIVE_CLOSED":下滑切换下一个直播间。""" + analyser._log_kv("阶段", "当前直播已结束,尝试切换下一个") + analyser.swipe_up_to_next_live() + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "run_once_basic完成(直播结束)", + elapsed_seconds=round(elapsed, 2), + ) diff --git a/tests/test_fudai_run_blocks.py b/tests/test_fudai_run_blocks.py index 7efa112..091c921 100644 --- a/tests/test_fudai_run_blocks.py +++ b/tests/test_fudai_run_blocks.py @@ -242,3 +242,79 @@ def test_fansclub_join_logs_completion(): or (len(c.args) >= 2 and c.args[1] == "run_once_basic完成(处理粉丝团)") ] assert len(completion_calls) >= 1 + + +# ---- handle_coupon_popup_stage ---- # + +def test_coupon_popup_clicks_back_and_logs_completion(): + a = _make_analyser() + fudai_run_blocks.handle_coupon_popup_stage(a, time.time()) + a.operation.click_back.assert_called_once_with("test") + completion_msgs = [ + c.args[1] for c in a._log_kv.call_args_list + if len(c.args) >= 2 and "run_once_basic完成(处理优惠券弹窗)" in c.args[1] + ] + assert len(completion_msgs) == 1 + + +def test_coupon_popup_swallows_click_back_failure(): + a = _make_analyser() + a.operation.click_back.side_effect = RuntimeError("boom") + fudai_run_blocks.handle_coupon_popup_stage(a, time.time()) + # 应当记录失败日志且仍记录完成 + failure_calls = [ + c for c in a._log_kv.call_args_list + if len(c.args) >= 2 and "按返回键关闭优惠券弹窗失败" in c.args[1] + ] + assert len(failure_calls) == 1 + + +# ---- handle_shopping_redpacket_popup_stage ---- # + +def test_shopping_redpacket_clicks_close_button(): + a = _make_analyser() + a.resolution_ratio_x = 1080 + a.resolution_ratio_y = 2400 + fudai_run_blocks.handle_shopping_redpacket_popup_stage(a, time.time()) + a.operation.click.assert_called_once_with("test", 530, 1647) + + +def test_shopping_redpacket_swallows_click_failure(): + a = _make_analyser() + a.resolution_ratio_x = 1080 + a.resolution_ratio_y = 2400 + a.operation.click.side_effect = RuntimeError("boom") + fudai_run_blocks.handle_shopping_redpacket_popup_stage(a, time.time()) + + +# ---- handle_lottery_result_stage ---- # + +def test_lottery_result_with_ocr_match_calls_handler(): + a = _make_analyser() + a._check_lottery_result_by_ocr.return_value = "won" + fudai_run_blocks.handle_lottery_result_stage(a, time.time()) + a._handle_lottery_result_by_ocr.assert_called_once_with("won") + assert a._waiting_for_lottery_result is False + assert a._current_attending_reservation_fudai is False + + +def test_lottery_result_no_ocr_falls_back_to_back_and_swipe(): + a = _make_analyser() + a._check_lottery_result_by_ocr.return_value = None + fudai_run_blocks.handle_lottery_result_stage(a, time.time()) + a.operation.click_back.assert_called_once() + a.swipe_up_to_next_live.assert_called_once() + a._handle_lottery_result_by_ocr.assert_not_called() + + +# ---- handle_live_closed_stage ---- # + +def test_live_closed_swipes_to_next_and_logs_completion(): + a = _make_analyser() + fudai_run_blocks.handle_live_closed_stage(a, time.time()) + a.swipe_up_to_next_live.assert_called_once() + completion = [ + c for c in a._log_kv.call_args_list + if len(c.args) >= 2 and "run_once_basic完成(直播结束)" in c.args[1] + ] + assert len(completion) == 1