refactor(fudai): 拆出 FANSCLUB_JOIN stage 处理(3135→2975,-160 行)
把 run_once_basic 中 FANSCLUB_JOIN 分支(166 行)抽到 fudai_run_blocks.handle_fansclub_join_stage: - 主路径:点参与按钮 + 2 次预设位置 + 关详情 - 异常路径:按 auto_join_fansclub 决定重试或等待 15s + 按 fansclub_fail_action 决定切换 douyin_fudai.py 终于跌破 3000 行(2975)。 新增 5 个单元测试覆盖:正常路径 / 自动加入重试 / 等待手动 / 失败切换 / 完成日志。 1102 → 1107 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
878a4c2fd9
commit
1604ad1904
3 files changed
+233
-161
No files matched your search
+1
-161
@@ -1951,167 +1951,7 @@ class fudai_analyse:
|
||||
return
|
||||
elif stage == "FANSCLUB_JOIN":
|
||||
# 粉丝团加入阶段:先点击参与按钮,再点击两次预设位置,最后关闭详情页
|
||||
self._log_kv("粉丝团处理", "检测到粉丝团加入按钮,开始处理")
|
||||
try:
|
||||
# 第一步:先点击参与按钮(基于截图优化坐标:OCR区域(306,2030)-(780,2110),区域中心(543, 2070))
|
||||
attend_x = int(543 * self.resolution_ratio_x // 1080)
|
||||
attend_y = int(2070 * self.resolution_ratio_y // 2400)
|
||||
self.operation.click(self.device_id, attend_x, attend_y)
|
||||
self._log_kv(
|
||||
"粉丝团处理",
|
||||
"先点击参与按钮",
|
||||
x=attend_x,
|
||||
y=attend_y,
|
||||
base_x=543,
|
||||
base_y=2070,
|
||||
region="(306,2030)-(780,2110)",
|
||||
)
|
||||
self.operation.delay(0.5) # 等待参与按钮响应
|
||||
|
||||
# 第二步:点击两次预设位置(527, 567)
|
||||
preset_x = int(527 * self.resolution_ratio_x // 1080)
|
||||
preset_y = int(567 * self.resolution_ratio_y // 2400)
|
||||
|
||||
# 第一次点击预设位置
|
||||
self.operation.click(self.device_id, preset_x, preset_y)
|
||||
self._log_kv(
|
||||
"粉丝团处理",
|
||||
"第一次点击预设位置",
|
||||
x=preset_x,
|
||||
y=preset_y,
|
||||
base_x=527,
|
||||
base_y=567,
|
||||
)
|
||||
self.operation.delay(0.5) # 短暂等待后立即第二次点击
|
||||
|
||||
# 第二次点击预设位置
|
||||
self.operation.click(self.device_id, preset_x, preset_y)
|
||||
self._log_kv(
|
||||
"粉丝团处理",
|
||||
"第二次点击预设位置",
|
||||
x=preset_x,
|
||||
y=preset_y,
|
||||
base_x=527,
|
||||
base_y=567,
|
||||
)
|
||||
self.operation.delay(1.0) # 等待点击完成
|
||||
|
||||
# 第三步:关闭详情页(点击详情页旁边位置)
|
||||
close_x = int(500 * self.resolution_ratio_x // 1080)
|
||||
close_y = int(470 * self.resolution_ratio_y // 2400)
|
||||
self.operation.click(self.device_id, close_x, close_y)
|
||||
self._log_kv(
|
||||
"粉丝团处理",
|
||||
"关闭详情页",
|
||||
x=close_x,
|
||||
y=close_y,
|
||||
base_x=500,
|
||||
base_y=470,
|
||||
)
|
||||
self.operation.delay(1.0) # 等待关闭完成
|
||||
except Exception as exc:
|
||||
self._log_kv("粉丝团处理", "处理粉丝团加入失败", error=str(exc))
|
||||
|
||||
# 如果启用了自动加入,等待页面加载后再次点击(如果需要)
|
||||
if getattr(self, "auto_join_fansclub", False):
|
||||
self._log_kv("粉丝团处理", "已启用自动加入粉丝团,等待页面加载")
|
||||
self.operation.delay(1.0)
|
||||
# 再次诊断阶段,看是否还在粉丝团加入页
|
||||
try:
|
||||
stage_after, _ = self._diagnose_stage_from_current_screen()
|
||||
if stage_after == "FANSCLUB_JOIN":
|
||||
# 仍在粉丝团加入页,再次点击(连续点击两次)
|
||||
# 使用预设位置坐标(527, 567)
|
||||
preset_x = int(527 * self.resolution_ratio_x // 1080)
|
||||
preset_y = int(567 * self.resolution_ratio_y // 2400)
|
||||
self.operation.click(self.device_id, preset_x, preset_y)
|
||||
self._log_kv(
|
||||
"粉丝团处理",
|
||||
"仍在粉丝团加入页,再次第一次点击",
|
||||
x=preset_x,
|
||||
y=preset_y,
|
||||
base_x=527,
|
||||
base_y=567,
|
||||
)
|
||||
self.operation.delay(0.5)
|
||||
self.operation.click(self.device_id, preset_x, preset_y)
|
||||
self._log_kv(
|
||||
"粉丝团处理",
|
||||
"仍在粉丝团加入页,再次第二次点击",
|
||||
x=preset_x,
|
||||
y=preset_y,
|
||||
base_x=527,
|
||||
base_y=567,
|
||||
)
|
||||
self.operation.delay(2.0)
|
||||
|
||||
# 加入粉丝团成功后,点击 (527, 567) 返回直播间
|
||||
try:
|
||||
return_x = int(527 * self.resolution_ratio_x // 1080)
|
||||
return_y = int(567 * self.resolution_ratio_y // 2400)
|
||||
self.operation.click(self.device_id, return_x, return_y)
|
||||
self._log_kv(
|
||||
"粉丝团处理",
|
||||
"加入粉丝团成功后,点击返回直播间",
|
||||
x=return_x,
|
||||
y=return_y,
|
||||
base_x=527,
|
||||
base_y=567,
|
||||
)
|
||||
self.operation.delay(1.0) # 等待返回完成
|
||||
except Exception as exc_inner:
|
||||
self._log_kv(
|
||||
"粉丝团处理",
|
||||
"点击返回直播间失败",
|
||||
error=str(exc_inner),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
self._log_kv(
|
||||
"粉丝团处理",
|
||||
"未启用自动加入粉丝团,等待手动确认",
|
||||
wait_seconds=15,
|
||||
)
|
||||
self.operation.delay(15.0)
|
||||
|
||||
# 未启用自动加入时,也需要在等待后点击返回
|
||||
# 加入粉丝团成功后,点击 (527, 567) 返回直播间
|
||||
try:
|
||||
return_x = int(527 * self.resolution_ratio_x // 1080)
|
||||
return_y = int(567 * self.resolution_ratio_y // 2400)
|
||||
self.operation.click(self.device_id, return_x, return_y)
|
||||
self._log_kv(
|
||||
"粉丝团处理",
|
||||
"加入粉丝团成功后,点击返回直播间",
|
||||
x=return_x,
|
||||
y=return_y,
|
||||
base_x=527,
|
||||
base_y=567,
|
||||
)
|
||||
self.operation.delay(1.0) # 等待返回完成
|
||||
except Exception as exc_inner2:
|
||||
self._log_kv(
|
||||
"粉丝团处理", "点击返回直播间失败", error=str(exc_inner2)
|
||||
)
|
||||
|
||||
# 延迟后返回,让主循环继续处理
|
||||
self.operation.delay(0.5)
|
||||
|
||||
# 如果失败,根据配置决定是否切换直播间
|
||||
fansclub_fail_action = getattr(self, "fansclub_fail_action", "stay")
|
||||
if fansclub_fail_action == "switch":
|
||||
self._log_kv("粉丝团处理", "粉丝团失败策略为switch,切换直播间")
|
||||
try:
|
||||
self.swipe_up_to_next_live()
|
||||
except Exception:
|
||||
pass
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(处理粉丝团)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
fudai_run_blocks.handle_fansclub_join_stage(self, start_time)
|
||||
return
|
||||
elif stage == "FUDai_DETAIL":
|
||||
# 已经在详情页:直接按正常详情流程处理(已移除预约回退直接参与标志)
|
||||
|
||||
@@ -416,3 +416,165 @@ def handle_high_value_room_revisit(analyser: Any, start_time: float) -> bool:
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
def handle_fansclub_join_stage(analyser: Any, start_time: float) -> None:
|
||||
"""处理 stage == "FANSCLUB_JOIN" 分支:先点参与按钮 → 点两次预设位置 → 关详情。
|
||||
|
||||
若失败,按 auto_join_fansclub / fansclub_fail_action 配置走相应回退。
|
||||
本函数不返回 bool,调用方在 elif 分支中调用后立即 return。
|
||||
"""
|
||||
analyser._log_kv("粉丝团处理", "检测到粉丝团加入按钮,开始处理")
|
||||
try:
|
||||
# 第一步:先点击参与按钮(OCR区域(306,2030)-(780,2110),区域中心(543, 2070))
|
||||
attend_x = int(543 * analyser.resolution_ratio_x // 1080)
|
||||
attend_y = int(2070 * analyser.resolution_ratio_y // 2400)
|
||||
analyser.operation.click(analyser.device_id, attend_x, attend_y)
|
||||
analyser._log_kv(
|
||||
"粉丝团处理",
|
||||
"先点击参与按钮",
|
||||
x=attend_x,
|
||||
y=attend_y,
|
||||
base_x=543,
|
||||
base_y=2070,
|
||||
region="(306,2030)-(780,2110)",
|
||||
)
|
||||
analyser.operation.delay(0.5)
|
||||
|
||||
# 第二步:点击两次预设位置(527, 567)
|
||||
preset_x = int(527 * analyser.resolution_ratio_x // 1080)
|
||||
preset_y = int(567 * analyser.resolution_ratio_y // 2400)
|
||||
|
||||
analyser.operation.click(analyser.device_id, preset_x, preset_y)
|
||||
analyser._log_kv(
|
||||
"粉丝团处理",
|
||||
"第一次点击预设位置",
|
||||
x=preset_x,
|
||||
y=preset_y,
|
||||
base_x=527,
|
||||
base_y=567,
|
||||
)
|
||||
analyser.operation.delay(0.5)
|
||||
|
||||
analyser.operation.click(analyser.device_id, preset_x, preset_y)
|
||||
analyser._log_kv(
|
||||
"粉丝团处理",
|
||||
"第二次点击预设位置",
|
||||
x=preset_x,
|
||||
y=preset_y,
|
||||
base_x=527,
|
||||
base_y=567,
|
||||
)
|
||||
analyser.operation.delay(1.0)
|
||||
|
||||
# 第三步:关闭详情页(点击详情页旁边位置)
|
||||
close_x = int(500 * analyser.resolution_ratio_x // 1080)
|
||||
close_y = int(470 * analyser.resolution_ratio_y // 2400)
|
||||
analyser.operation.click(analyser.device_id, close_x, close_y)
|
||||
analyser._log_kv(
|
||||
"粉丝团处理",
|
||||
"关闭详情页",
|
||||
x=close_x,
|
||||
y=close_y,
|
||||
base_x=500,
|
||||
base_y=470,
|
||||
)
|
||||
analyser.operation.delay(1.0)
|
||||
except Exception as exc:
|
||||
analyser._log_kv("粉丝团处理", "处理粉丝团加入失败", error=str(exc))
|
||||
|
||||
# 失败回退:根据 auto_join_fansclub 决定立即重试或等待手动
|
||||
if getattr(analyser, "auto_join_fansclub", False):
|
||||
analyser._log_kv("粉丝团处理", "已启用自动加入粉丝团,等待页面加载")
|
||||
analyser.operation.delay(1.0)
|
||||
try:
|
||||
stage_after, _ = analyser._diagnose_stage_from_current_screen()
|
||||
if stage_after == "FANSCLUB_JOIN":
|
||||
preset_x = int(527 * analyser.resolution_ratio_x // 1080)
|
||||
preset_y = int(567 * analyser.resolution_ratio_y // 2400)
|
||||
analyser.operation.click(analyser.device_id, preset_x, preset_y)
|
||||
analyser._log_kv(
|
||||
"粉丝团处理",
|
||||
"仍在粉丝团加入页,再次第一次点击",
|
||||
x=preset_x,
|
||||
y=preset_y,
|
||||
base_x=527,
|
||||
base_y=567,
|
||||
)
|
||||
analyser.operation.delay(0.5)
|
||||
analyser.operation.click(analyser.device_id, preset_x, preset_y)
|
||||
analyser._log_kv(
|
||||
"粉丝团处理",
|
||||
"仍在粉丝团加入页,再次第二次点击",
|
||||
x=preset_x,
|
||||
y=preset_y,
|
||||
base_x=527,
|
||||
base_y=567,
|
||||
)
|
||||
analyser.operation.delay(2.0)
|
||||
|
||||
try:
|
||||
return_x = int(527 * analyser.resolution_ratio_x // 1080)
|
||||
return_y = int(567 * analyser.resolution_ratio_y // 2400)
|
||||
analyser.operation.click(
|
||||
analyser.device_id, return_x, return_y
|
||||
)
|
||||
analyser._log_kv(
|
||||
"粉丝团处理",
|
||||
"加入粉丝团成功后,点击返回直播间",
|
||||
x=return_x,
|
||||
y=return_y,
|
||||
base_x=527,
|
||||
base_y=567,
|
||||
)
|
||||
analyser.operation.delay(1.0)
|
||||
except Exception as exc_inner:
|
||||
analyser._log_kv(
|
||||
"粉丝团处理",
|
||||
"点击返回直播间失败",
|
||||
error=str(exc_inner),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
analyser._log_kv(
|
||||
"粉丝团处理",
|
||||
"未启用自动加入粉丝团,等待手动确认",
|
||||
wait_seconds=15,
|
||||
)
|
||||
analyser.operation.delay(15.0)
|
||||
|
||||
try:
|
||||
return_x = int(527 * analyser.resolution_ratio_x // 1080)
|
||||
return_y = int(567 * analyser.resolution_ratio_y // 2400)
|
||||
analyser.operation.click(analyser.device_id, return_x, return_y)
|
||||
analyser._log_kv(
|
||||
"粉丝团处理",
|
||||
"加入粉丝团成功后,点击返回直播间",
|
||||
x=return_x,
|
||||
y=return_y,
|
||||
base_x=527,
|
||||
base_y=567,
|
||||
)
|
||||
analyser.operation.delay(1.0)
|
||||
except Exception as exc_inner2:
|
||||
analyser._log_kv(
|
||||
"粉丝团处理", "点击返回直播间失败", error=str(exc_inner2)
|
||||
)
|
||||
|
||||
analyser.operation.delay(0.5)
|
||||
|
||||
fansclub_fail_action = getattr(analyser, "fansclub_fail_action", "stay")
|
||||
if fansclub_fail_action == "switch":
|
||||
analyser._log_kv("粉丝团处理", "粉丝团失败策略为switch,切换直播间")
|
||||
try:
|
||||
analyser.swipe_up_to_next_live()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
analyser._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(处理粉丝团)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
@@ -172,3 +172,73 @@ def test_revisit_enabled_but_module_import_error_returns_false():
|
||||
# 我们只需保证不崩
|
||||
result = fudai_run_blocks.handle_high_value_room_revisit(a, time.time())
|
||||
assert isinstance(result, bool)
|
||||
|
||||
|
||||
# ---- handle_fansclub_join_stage ---- #
|
||||
|
||||
def _make_fansclub_analyser():
|
||||
a = _make_analyser()
|
||||
a.resolution_ratio_x = 1080
|
||||
a.resolution_ratio_y = 2400
|
||||
a.auto_join_fansclub = False
|
||||
a.fansclub_fail_action = "stay"
|
||||
a._diagnose_stage_from_current_screen = MagicMock(return_value=("OK", ""))
|
||||
return a
|
||||
|
||||
|
||||
def test_fansclub_join_happy_path_clicks_three_positions():
|
||||
"""正常路径:3 次点击(参与按钮 + 2 次预设位置 + 关闭详情)= 共 4 次 click"""
|
||||
a = _make_fansclub_analyser()
|
||||
fudai_run_blocks.handle_fansclub_join_stage(a, time.time())
|
||||
# click 调用次数:参与按钮 + 第一次预设 + 第二次预设 + 关闭详情 = 4
|
||||
assert a.operation.click.call_count == 4
|
||||
|
||||
|
||||
def test_fansclub_join_failure_with_auto_join_retries():
|
||||
"""主路径出错且 auto_join=True → 重新诊断后重试两次点击"""
|
||||
a = _make_fansclub_analyser()
|
||||
a.auto_join_fansclub = True
|
||||
# 让第一次 click 抛错触发 except 分支
|
||||
a.operation.click.side_effect = [
|
||||
RuntimeError("first click fail"), # 第一次 click 就失败
|
||||
] + [None] * 20 # 后续 click 正常
|
||||
|
||||
a._diagnose_stage_from_current_screen.return_value = ("FANSCLUB_JOIN", "")
|
||||
|
||||
fudai_run_blocks.handle_fansclub_join_stage(a, time.time())
|
||||
# 仍应完成(不抛错)
|
||||
a._log_kv.assert_any_call("粉丝团处理", "处理粉丝团加入失败", error="first click fail")
|
||||
|
||||
|
||||
def test_fansclub_join_failure_without_auto_join_waits():
|
||||
"""主路径出错且 auto_join=False → 等待 15 秒"""
|
||||
a = _make_fansclub_analyser()
|
||||
a.auto_join_fansclub = False
|
||||
a.operation.click.side_effect = [RuntimeError("boom")] + [None] * 20
|
||||
|
||||
fudai_run_blocks.handle_fansclub_join_stage(a, time.time())
|
||||
# 应当调用 delay(15.0)
|
||||
delay_calls = [c.args[0] for c in a.operation.delay.call_args_list]
|
||||
assert 15.0 in delay_calls
|
||||
|
||||
|
||||
def test_fansclub_join_failure_with_switch_action_swipes():
|
||||
"""主路径出错且 fansclub_fail_action=switch → 切换直播间"""
|
||||
a = _make_fansclub_analyser()
|
||||
a.fansclub_fail_action = "switch"
|
||||
a.auto_join_fansclub = False
|
||||
a.operation.click.side_effect = [RuntimeError("boom")] + [None] * 20
|
||||
|
||||
fudai_run_blocks.handle_fansclub_join_stage(a, time.time())
|
||||
a.swipe_up_to_next_live.assert_called_once()
|
||||
|
||||
|
||||
def test_fansclub_join_logs_completion():
|
||||
a = _make_fansclub_analyser()
|
||||
fudai_run_blocks.handle_fansclub_join_stage(a, time.time())
|
||||
completion_calls = [
|
||||
c for c in a._log_kv.call_args_list
|
||||
if c.args == ("流程", "run_once_basic完成(处理粉丝团)")
|
||||
or (len(c.args) >= 2 and c.args[1] == "run_once_basic完成(处理粉丝团)")
|
||||
]
|
||||
assert len(completion_calls) >= 1
|
||||
Reference in new issue
Block a user