refactor(fudai): 拆出 4 个 stage 弹窗/结束分支(2975→2915,-60 行)

把 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) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 02:43:10 +08:00
1 parent 1604ad1904
commit 992db1e161
3 files changed
+155 -64

No files matched your search

+4 -64
View File
@@ -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
@@ -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),
)
+76
View File
@@ -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