refactor(fudai_attend): 抽 1057 行粉丝团门槛分支到 fansclub_handler (1629 → 573)
attend_choujiang 中"加入粉丝团/粉丝团"按钮分支占 1057 行,含一个 751 行的
内嵌闭包 _recover_to_fudai_detail_after_fansclub 处理"加入页"恢复。该分支
是文件中最大的单一逻辑块,独立性高(没有共享主流程的局部变量,全靠 analyser
属性传递状态)。
新建 fudai_attend_pkg/fansclub_handler.py(1070 行)作为整段抽出后的目的地。
导出顶层函数 handle_fansclub_branch(analyser, attend_button_text) -> bool。
attend_choujiang 中原 elif 块替换为 4 行委托:
elif attend_button_text and (
"加入粉丝团" in attend_button_text or "粉丝团" in attend_button_text
):
return handle_fansclub_branch(analyser, attend_button_text)
行为完全保留:
- 设置 analyser.last_attend_fail_reason = "fansclub_required"
- 第一步点参与按钮 (543, 2070)
- 第二步 OCR 找加入粉丝团按钮 + bbox 中心点击(fallback 区域中心)
- 第三步关闭详情页 (500, 470)
- 第四步根据 auto_join_fansclub 等待
- 第五步 _recover_to_fudai_detail_after_fansclub 在加入页再点击 + 返回直播间
新增 9 个单元测试覆盖:
- 总返回 True
- 设置 fail_reason = "fansclub_required"
- 必经的两次坐标点击 (543,2070) / (500,470)
- 截图缺失/OCR 抛异常时的兜底
- auto_join 启用/禁用分支
1313 passed (+9)。
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
2f0eb90d98
commit
f718dfe2e4
4 files changed
+1188
-1053
No files matched your search
File diff suppressed because it is too large.
Load diff
Whitespace-only changes.
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,115 @@
|
||||
"""douyin/fudai/core/fudai_attend_pkg/fansclub_handler 单元测试。
|
||||
|
||||
主要验证:
|
||||
- handle_fansclub_branch 总是返回 True
|
||||
- 设置 analyser.last_attend_fail_reason = "fansclub_required"
|
||||
- 在路径上点击参与按钮 (543, 2070)
|
||||
- 在路径上点击关闭详情 (500, 470)
|
||||
- 异常路径不抛
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from douyin.fudai.core.fudai_attend_pkg.fansclub_handler import (
|
||||
handle_fansclub_branch,
|
||||
)
|
||||
|
||||
|
||||
def _mk_analyser(*, auto_join_fansclub: bool = False):
|
||||
"""构造一个 attend 流程能跑通的 mock analyser。"""
|
||||
a = MagicMock()
|
||||
a.last_attend_fail_reason = ""
|
||||
a.auto_join_fansclub = bool(auto_join_fansclub)
|
||||
a.resolution_ratio_x = 1080
|
||||
a.resolution_ratio_y = 2400
|
||||
a.device_id = "MOCK"
|
||||
a.fansclub_click_delay = 0.0 # 单测里跳过实际延迟
|
||||
a.fansclub_auto_join_wait_seconds = 0.0
|
||||
a.fansclub_manual_wait_seconds = 0.0
|
||||
|
||||
# operation 的方法都返回非 None / "" 让流程顺利
|
||||
a.operation = MagicMock()
|
||||
a.operation.delay = MagicMock(return_value=None)
|
||||
a.operation.click = MagicMock(return_value=None)
|
||||
a.operation.click_back = MagicMock(return_value=None)
|
||||
a.operation.analyse_pic_word = MagicMock(return_value="") # OCR 不识别 → 走坐标兜底
|
||||
a.operation.analyse_pic_digits = MagicMock(return_value="")
|
||||
a.operation.ocr = MagicMock()
|
||||
a.operation.ocr.ocr = MagicMock(return_value=[])
|
||||
a.operation.ocr.readtext = MagicMock(return_value=[])
|
||||
a.operation._find_pic_path = MagicMock(return_value="")
|
||||
a.cut_pic = MagicMock(return_value=True)
|
||||
a.click = MagicMock(return_value=None)
|
||||
a._log_kv = MagicMock(return_value=None)
|
||||
a._shot_and_diagnose = MagicMock(return_value=None)
|
||||
return a
|
||||
|
||||
|
||||
class TestHandleFansclubBranch:
|
||||
def test_returns_true(self):
|
||||
a = _mk_analyser()
|
||||
result = handle_fansclub_branch(a, "加入粉丝团")
|
||||
assert result is True
|
||||
|
||||
def test_sets_fail_reason(self):
|
||||
a = _mk_analyser()
|
||||
handle_fansclub_branch(a, "加入粉丝团")
|
||||
assert a.last_attend_fail_reason == "fansclub_required"
|
||||
|
||||
def test_clicks_attend_button_at_543_2070(self):
|
||||
"""第一步必须点参与按钮 (543, 2070)。"""
|
||||
a = _mk_analyser()
|
||||
handle_fansclub_branch(a, "加入粉丝团")
|
||||
# 找 analyser.click(543, 2070) 这一调用
|
||||
attend_calls = [
|
||||
c for c in a.click.call_args_list if c.args == (543, 2070)
|
||||
]
|
||||
assert len(attend_calls) >= 1
|
||||
|
||||
def test_closes_detail_at_500_470(self):
|
||||
"""第三步必须点 (500, 470) 关闭详情。"""
|
||||
a = _mk_analyser()
|
||||
handle_fansclub_branch(a, "加入粉丝团")
|
||||
close_calls = [
|
||||
c for c in a.click.call_args_list if c.args == (500, 470)
|
||||
]
|
||||
assert len(close_calls) >= 1
|
||||
|
||||
def test_handles_粉丝团_text_only(self):
|
||||
"""attend_button_text 含 "粉丝团" 但不含 "加入" 也走该分支。"""
|
||||
a = _mk_analyser()
|
||||
result = handle_fansclub_branch(a, "粉丝团门槛")
|
||||
assert result is True
|
||||
|
||||
def test_does_not_raise_when_screenshot_missing(self):
|
||||
"""path_config.get_screenshot_path 不存在时不应抛。"""
|
||||
a = _mk_analyser()
|
||||
with patch("os.path.exists", return_value=False):
|
||||
result = handle_fansclub_branch(a, "加入粉丝团")
|
||||
assert result is True
|
||||
|
||||
def test_does_not_raise_when_ocr_throws(self):
|
||||
"""OCR 全部抛异常时仍正常返回 True(不传播)。"""
|
||||
a = _mk_analyser()
|
||||
a.operation.analyse_pic_word = MagicMock(side_effect=RuntimeError("ocr fail"))
|
||||
a.operation.ocr.ocr = MagicMock(side_effect=RuntimeError("ocr fail"))
|
||||
a.operation.ocr.readtext = MagicMock(side_effect=RuntimeError("ocr fail"))
|
||||
result = handle_fansclub_branch(a, "加入粉丝团")
|
||||
assert result is True
|
||||
|
||||
def test_auto_join_disabled_does_not_wait_long(self):
|
||||
"""未启用自动加入时也走完整流程,返回 True。"""
|
||||
a = _mk_analyser(auto_join_fansclub=False)
|
||||
result = handle_fansclub_branch(a, "加入粉丝团")
|
||||
assert result is True
|
||||
# auto_join 关闭时应当走"等待手动确认"日志
|
||||
log_msgs = [str(c) for c in a._log_kv.call_args_list]
|
||||
assert any("未启用自动加粉丝团" in m for m in log_msgs)
|
||||
|
||||
def test_auto_join_enabled(self):
|
||||
a = _mk_analyser(auto_join_fansclub=True)
|
||||
result = handle_fansclub_branch(a, "加入粉丝团")
|
||||
assert result is True
|
||||
Reference in new issue
Block a user