refactor(recovery): 抽 3 处桌面自救重复逻辑到 douyin/core/recovery 共享模块
新增 douyin/core/recovery.py(113 行): - is_douyin_foreground(top_pkg): 统一判断前台是否抖音相关包 - recover_to_douyin(analyser, log=...): 桌面自救主入口(先 launch_package,失败回退点击) - fallback_click_recovery(analyser, log=...): 旧的点击坐标自救兜底 3 处调用点收敛: - fudai_modules/run_once_basic_helpers.handle_desktop_recovery:80 行 → 18 行 - douyin/other/douyin_yanghao.py: 3 段重复块(原 ~80 行 each)→ 单行调用 总计净减 -269 行重复代码。新增 13 个 recovery 单元测试 + 修测试 mock。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
4c1d95d4b5
commit
a92cec4ff9
5 files changed
+309
-269
No files matched your search
@@ -258,6 +258,8 @@ def handle_desktop_recovery(self: "fudai_analyse") -> bool:
|
||||
返回 True 表示已触发自救并递归调用过 run_once_basic,caller 应当 `return`;
|
||||
返回 False 表示不需要自救,继续走后续步骤。
|
||||
"""
|
||||
from douyin.core import recovery as _recovery
|
||||
|
||||
try:
|
||||
top_pkg = ""
|
||||
try:
|
||||
@@ -267,14 +269,7 @@ def handle_desktop_recovery(self: "fudai_analyse") -> bool:
|
||||
top_pkg = ""
|
||||
self._log_kv("自救", "桌面检测:获取前台包名失败", error=str(exc))
|
||||
|
||||
# 如果获取到包名且不是抖音相关包,说明可能在桌面或其他应用
|
||||
is_douyin_pkg = top_pkg and (
|
||||
"aweme" in top_pkg
|
||||
or "douyin" in top_pkg
|
||||
or "com.ss.android.ugc" in top_pkg
|
||||
)
|
||||
|
||||
if top_pkg and not is_douyin_pkg:
|
||||
if top_pkg and not _recovery.is_douyin_foreground(top_pkg):
|
||||
self._log_kv(
|
||||
"自救",
|
||||
"桌面检测:检测到不在抖音前台",
|
||||
@@ -287,53 +282,10 @@ def handle_desktop_recovery(self: "fudai_analyse") -> bool:
|
||||
"发现前台包不在抖音,尝试通过包名启动抖音",
|
||||
top_pkg=top_pkg,
|
||||
)
|
||||
# 优先:通过包名启动抖音(monkey + am start 兜底)
|
||||
launched_ok = False
|
||||
used_pkg = ""
|
||||
try:
|
||||
launched_ok, used_pkg = self.operation.launch_package(
|
||||
self.device_id, ""
|
||||
)
|
||||
except Exception as exc:
|
||||
self._log_kv(
|
||||
"自救",
|
||||
"包名启动调用异常,回退点击坐标",
|
||||
error=str(exc),
|
||||
)
|
||||
if launched_ok:
|
||||
self._log_kv(
|
||||
"自救", "包名启动抖音成功", used_package=used_pkg
|
||||
)
|
||||
self.operation.delay(2.5)
|
||||
else:
|
||||
# 兜底:保留旧的点击坐标自救
|
||||
self._log_kv(
|
||||
"自救", "包名启动失败,回退点击桌面图标 + 备用坐标"
|
||||
)
|
||||
try:
|
||||
self.click(int(self.desktop_icon_x), int(self.desktop_icon_y))
|
||||
except Exception:
|
||||
# 退化为默认硬编码位置
|
||||
self.click(153, 260)
|
||||
self.operation.delay(2)
|
||||
try:
|
||||
lx = int(self.yanghao_fallback_live_x)
|
||||
ly = int(self.yanghao_fallback_live_y)
|
||||
self._log_kv("自救", "点击直播按钮", x=lx, y=ly)
|
||||
self.click(lx, ly)
|
||||
self.operation.delay(1.5)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
ex = int(self.yanghao_fallback_enter_live_x)
|
||||
ey = int(self.yanghao_fallback_enter_live_y)
|
||||
self._log_kv("自救", "点击进入直播间", x=ex, y=ey, 次数=2)
|
||||
self.click(ex, ey)
|
||||
self.operation.delay(0.6)
|
||||
self.click(ex, ey)
|
||||
self.operation.delay(2.0)
|
||||
except Exception:
|
||||
pass
|
||||
_recovery.recover_to_douyin(
|
||||
self,
|
||||
log=lambda msg: self._log_kv("自救", msg),
|
||||
)
|
||||
# 自救完成后立即重新执行一次主流程
|
||||
try:
|
||||
self._log_kv(
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
"""桌面自救共享逻辑
|
||||
|
||||
把 fudai_modules.run_once_basic_helpers 和 douyin.other.douyin_yanghao
|
||||
中重复的"前台不在抖音 → 包名启动 → 失败兜底点击坐标"逻辑抽到这里。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
|
||||
def is_douyin_foreground(top_pkg: str) -> bool:
|
||||
"""判断给定的前台包名是否属于抖音相关包。"""
|
||||
return bool(top_pkg) and (
|
||||
"aweme" in top_pkg
|
||||
or "douyin" in top_pkg
|
||||
or "com.ss.android.ugc" in top_pkg
|
||||
)
|
||||
|
||||
|
||||
def fallback_click_recovery(
|
||||
analyser: Any,
|
||||
log: Optional[Callable[[str], None]] = None,
|
||||
) -> None:
|
||||
"""旧的点击坐标自救:桌面图标 → 直播按钮 → 进入直播间×2。
|
||||
|
||||
保留作为 launch_package 失败时的兜底。所有坐标读 analyser 上的属性,
|
||||
取不到时使用默认值(适配 1080x2400 的位置)。
|
||||
"""
|
||||
op = analyser.operation
|
||||
device_id = analyser.device_id
|
||||
|
||||
desktop_x = int(getattr(analyser, "desktop_icon_x", 153) or 153)
|
||||
desktop_y = int(getattr(analyser, "desktop_icon_y", 260) or 260)
|
||||
try:
|
||||
op.click(device_id, desktop_x, desktop_y)
|
||||
except Exception:
|
||||
# 退化为默认硬编码位置
|
||||
try:
|
||||
op.click(device_id, 153, 260)
|
||||
except Exception:
|
||||
pass
|
||||
op.delay(2)
|
||||
|
||||
fallback_x = int(getattr(analyser, "yanghao_fallback_live_x", 540) or 540)
|
||||
fallback_y = int(getattr(analyser, "yanghao_fallback_live_y", 2000) or 2000)
|
||||
try:
|
||||
if log:
|
||||
log(f"点击直播按钮 x={fallback_x} y={fallback_y}")
|
||||
op.click(device_id, fallback_x, fallback_y)
|
||||
op.delay(1.5)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
enter_x = int(getattr(analyser, "yanghao_fallback_enter_live_x", 540) or 540)
|
||||
enter_y = int(getattr(analyser, "yanghao_fallback_enter_live_y", 1200) or 1200)
|
||||
try:
|
||||
if log:
|
||||
log(f"点击进入直播间 x={enter_x} y={enter_y} 次数=2")
|
||||
op.click(device_id, enter_x, enter_y)
|
||||
op.delay(0.6)
|
||||
op.click(device_id, enter_x, enter_y)
|
||||
op.delay(2.0)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def recover_to_douyin(
|
||||
analyser: Any,
|
||||
log: Optional[Callable[[str], None]] = None,
|
||||
) -> bool:
|
||||
"""统一的桌面自救入口。
|
||||
|
||||
流程:
|
||||
1. 优先 op.launch_package(device_id, ""),按抖音常用包名顺序尝试
|
||||
2. 包名启动失败或抛异常时,回退到点击坐标自救(fallback_click_recovery)
|
||||
|
||||
参数:
|
||||
analyser: 提供 .operation / .device_id / 自救坐标属性的对象
|
||||
log: 可选的日志回调(接受单字符串)
|
||||
|
||||
返回:
|
||||
True 表示已尝试某种自救(不保证一定成功,但已执行动作);
|
||||
False 仅在调用环境本身出现严重异常时返回。
|
||||
"""
|
||||
try:
|
||||
op = analyser.operation
|
||||
device_id = analyser.device_id
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
launched_ok = False
|
||||
used_pkg = ""
|
||||
try:
|
||||
launched_ok, used_pkg = op.launch_package(device_id, "")
|
||||
except Exception as exc:
|
||||
if log:
|
||||
log(f"包名启动调用异常 {exc},回退点击坐标")
|
||||
|
||||
if launched_ok:
|
||||
if log:
|
||||
log(f"包名启动抖音成功 pkg={used_pkg}")
|
||||
try:
|
||||
op.delay(2.5)
|
||||
except Exception:
|
||||
time.sleep(2.5)
|
||||
return True
|
||||
|
||||
# 兜底:保留旧的点击坐标自救
|
||||
if log:
|
||||
log("包名启动失败,回退点击桌面图标 + 备用坐标")
|
||||
fallback_click_recovery(analyser, log=log)
|
||||
return True
|
||||
+28
-211
@@ -299,95 +299,24 @@ def yanghao_idle(analyser, total_seconds: int) -> None:
|
||||
if now - last_desktop_check_ts >= desktop_check_interval:
|
||||
last_desktop_check_ts = now
|
||||
try:
|
||||
from douyin.core import recovery as _recovery
|
||||
|
||||
top_pkg = (
|
||||
analyser.operation.get_top_package(analyser.device_id)
|
||||
or ""
|
||||
)
|
||||
is_douyin_pkg = top_pkg and (
|
||||
"aweme" in top_pkg
|
||||
or "douyin" in top_pkg
|
||||
or "com.ss.android.ugc" in top_pkg
|
||||
)
|
||||
if top_pkg and not is_douyin_pkg:
|
||||
_yanghao_log(analyser,
|
||||
f"[养号] 自救: 检测到不在抖音前台,前台包名={top_pkg}"
|
||||
if top_pkg and not _recovery.is_douyin_foreground(top_pkg):
|
||||
_yanghao_log(
|
||||
analyser,
|
||||
f"[养号] 自救: 检测到不在抖音前台,前台包名={top_pkg}",
|
||||
)
|
||||
try:
|
||||
# 优先:通过包名启动抖音
|
||||
launched_ok = False
|
||||
used_pkg = ""
|
||||
try:
|
||||
launched_ok, used_pkg = (
|
||||
analyser.operation.launch_package(
|
||||
analyser.device_id, ""
|
||||
)
|
||||
)
|
||||
except Exception as exc:
|
||||
_yanghao_log(
|
||||
analyser,
|
||||
f"[养号] 自救: 包名启动调用异常 {exc},回退点击坐标",
|
||||
)
|
||||
if launched_ok:
|
||||
_yanghao_log(
|
||||
analyser,
|
||||
f"[养号] 自救: 包名启动抖音成功,pkg={used_pkg}",
|
||||
)
|
||||
analyser.operation.delay(2.5)
|
||||
else:
|
||||
# 兜底:保留旧的点击坐标自救
|
||||
desktop_x = int(
|
||||
getattr(analyser, "desktop_icon_x", 153) or 153
|
||||
)
|
||||
desktop_y = int(
|
||||
getattr(analyser, "desktop_icon_y", 260) or 260
|
||||
)
|
||||
analyser.operation.click(
|
||||
analyser.device_id, desktop_x, desktop_y
|
||||
)
|
||||
analyser.operation.delay(2)
|
||||
|
||||
fallback_x = int(
|
||||
getattr(
|
||||
analyser, "yanghao_fallback_live_x", 540
|
||||
)
|
||||
or 540
|
||||
)
|
||||
fallback_y = int(
|
||||
getattr(
|
||||
analyser, "yanghao_fallback_live_y", 2000
|
||||
)
|
||||
or 2000
|
||||
)
|
||||
analyser.operation.click(
|
||||
analyser.device_id, fallback_x, fallback_y
|
||||
)
|
||||
analyser.operation.delay(1.5)
|
||||
|
||||
enter_x = int(
|
||||
getattr(
|
||||
analyser,
|
||||
"yanghao_fallback_enter_live_x",
|
||||
540,
|
||||
)
|
||||
or 540
|
||||
)
|
||||
enter_y = int(
|
||||
getattr(
|
||||
analyser,
|
||||
"yanghao_fallback_enter_live_y",
|
||||
1200,
|
||||
)
|
||||
or 1200
|
||||
)
|
||||
analyser.operation.click(
|
||||
analyser.device_id, enter_x, enter_y
|
||||
)
|
||||
analyser.operation.delay(0.6)
|
||||
analyser.operation.click(
|
||||
analyser.device_id, enter_x, enter_y
|
||||
)
|
||||
analyser.operation.delay(2.0)
|
||||
|
||||
_recovery.recover_to_douyin(
|
||||
analyser,
|
||||
log=lambda msg: _yanghao_log(
|
||||
analyser, f"[养号] 自救: {msg}"
|
||||
),
|
||||
)
|
||||
_yanghao_log(analyser, "[养号] 自救: 恢复操作完成")
|
||||
except Exception as e:
|
||||
_yanghao_log(analyser, f"[养号] 自救: 恢复操作失败: {e}")
|
||||
@@ -507,75 +436,22 @@ def yanghao_idle(analyser, total_seconds: int) -> None:
|
||||
def _yanghao_check_and_recover_from_desktop(analyser) -> bool:
|
||||
"""养号模式下的桌面检测和自救恢复(提前定义以供循环使用)。"""
|
||||
try:
|
||||
# 获取前台包名
|
||||
from douyin.core import recovery as _recovery
|
||||
|
||||
top_pkg = analyser.operation.get_top_package(analyser.device_id) or ""
|
||||
|
||||
# 检查是否在抖音前台
|
||||
is_douyin_pkg = top_pkg and (
|
||||
"aweme" in top_pkg
|
||||
or "douyin" in top_pkg
|
||||
or "com.ss.android.ugc" in top_pkg
|
||||
)
|
||||
|
||||
if top_pkg and not is_douyin_pkg:
|
||||
if top_pkg and not _recovery.is_douyin_foreground(top_pkg):
|
||||
_yanghao_log(analyser, f"[养号] 自救: 检测到不在抖音前台,前台包名={top_pkg}")
|
||||
|
||||
# 执行自救操作
|
||||
try:
|
||||
# 优先:通过包名启动抖音
|
||||
launched_ok = False
|
||||
used_pkg = ""
|
||||
try:
|
||||
launched_ok, used_pkg = analyser.operation.launch_package(
|
||||
analyser.device_id, ""
|
||||
)
|
||||
except Exception as exc:
|
||||
_yanghao_log(
|
||||
analyser,
|
||||
f"[养号] 自救: 包名启动调用异常 {exc},回退点击坐标",
|
||||
)
|
||||
if launched_ok:
|
||||
_yanghao_log(
|
||||
analyser,
|
||||
f"[养号] 自救: 包名启动抖音成功,pkg={used_pkg}",
|
||||
)
|
||||
analyser.operation.delay(2.5)
|
||||
else:
|
||||
# 兜底:保留旧的点击坐标自救
|
||||
desktop_x = int(getattr(analyser, "desktop_icon_x", 153) or 153)
|
||||
desktop_y = int(getattr(analyser, "desktop_icon_y", 260) or 260)
|
||||
analyser.operation.click(analyser.device_id, desktop_x, desktop_y)
|
||||
analyser.operation.delay(2)
|
||||
|
||||
fallback_x = int(
|
||||
getattr(analyser, "yanghao_fallback_live_x", 540) or 540
|
||||
)
|
||||
fallback_y = int(
|
||||
getattr(analyser, "yanghao_fallback_live_y", 2000) or 2000
|
||||
)
|
||||
analyser.operation.click(analyser.device_id, fallback_x, fallback_y)
|
||||
analyser.operation.delay(1.5)
|
||||
|
||||
enter_x = int(
|
||||
getattr(analyser, "yanghao_fallback_enter_live_x", 540) or 540
|
||||
)
|
||||
enter_y = int(
|
||||
getattr(analyser, "yanghao_fallback_enter_live_y", 1200) or 1200
|
||||
)
|
||||
analyser.operation.click(analyser.device_id, enter_x, enter_y)
|
||||
analyser.operation.delay(0.6)
|
||||
analyser.operation.click(analyser.device_id, enter_x, enter_y)
|
||||
analyser.operation.delay(2.0)
|
||||
|
||||
_recovery.recover_to_douyin(
|
||||
analyser,
|
||||
log=lambda msg: _yanghao_log(analyser, f"[养号] 自救: {msg}"),
|
||||
)
|
||||
_yanghao_log(analyser, "[养号] 自救: 恢复操作完成")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
_yanghao_log(analyser, f"[养号] 自救: 恢复操作失败: {e}")
|
||||
return False
|
||||
else:
|
||||
return False
|
||||
|
||||
return False
|
||||
except Exception as e:
|
||||
_yanghao_log(analyser, f"[养号] 自救: 桌面检测异常: {e}")
|
||||
return False
|
||||
@@ -653,81 +529,22 @@ def yanghao_idle(analyser, total_seconds: int) -> None:
|
||||
def _yanghao_check_and_recover_from_desktop(analyser) -> bool:
|
||||
"""养号模式下的桌面检测和自救恢复"""
|
||||
try:
|
||||
# 获取前台包名
|
||||
from douyin.core import recovery as _recovery
|
||||
|
||||
top_pkg = analyser.operation.get_top_package(analyser.device_id) or ""
|
||||
|
||||
# 检查是否在抖音前台
|
||||
is_douyin_pkg = top_pkg and (
|
||||
"aweme" in top_pkg
|
||||
or "douyin" in top_pkg
|
||||
or "com.ss.android.ugc" in top_pkg
|
||||
)
|
||||
|
||||
if top_pkg and not is_douyin_pkg:
|
||||
if top_pkg and not _recovery.is_douyin_foreground(top_pkg):
|
||||
_yanghao_log(analyser, f"[养号] 自救: 检测到不在抖音前台,前台包名={top_pkg}")
|
||||
|
||||
# 执行自救操作
|
||||
try:
|
||||
# 优先:通过包名启动抖音
|
||||
launched_ok = False
|
||||
used_pkg = ""
|
||||
try:
|
||||
launched_ok, used_pkg = analyser.operation.launch_package(
|
||||
analyser.device_id, ""
|
||||
)
|
||||
except Exception as exc:
|
||||
_yanghao_log(
|
||||
analyser,
|
||||
f"[养号] 自救: 包名启动调用异常 {exc},回退点击坐标",
|
||||
)
|
||||
if launched_ok:
|
||||
_yanghao_log(
|
||||
analyser,
|
||||
f"[养号] 自救: 包名启动抖音成功,pkg={used_pkg}",
|
||||
)
|
||||
analyser.operation.delay(2.5)
|
||||
else:
|
||||
# 兜底:保留旧的点击坐标自救
|
||||
desktop_x = int(getattr(analyser, "desktop_icon_x", 153) or 153)
|
||||
desktop_y = int(getattr(analyser, "desktop_icon_y", 260) or 260)
|
||||
analyser.operation.click(
|
||||
analyser.device_id, desktop_x, desktop_y
|
||||
)
|
||||
analyser.operation.delay(2)
|
||||
|
||||
fallback_x = int(
|
||||
getattr(analyser, "yanghao_fallback_live_x", 540) or 540
|
||||
)
|
||||
fallback_y = int(
|
||||
getattr(analyser, "yanghao_fallback_live_y", 2000) or 2000
|
||||
)
|
||||
analyser.operation.click(
|
||||
analyser.device_id, fallback_x, fallback_y
|
||||
)
|
||||
analyser.operation.delay(1.5)
|
||||
|
||||
enter_x = int(
|
||||
getattr(analyser, "yanghao_fallback_enter_live_x", 540)
|
||||
or 540
|
||||
)
|
||||
enter_y = int(
|
||||
getattr(analyser, "yanghao_fallback_enter_live_y", 1200)
|
||||
or 1200
|
||||
)
|
||||
analyser.operation.click(analyser.device_id, enter_x, enter_y)
|
||||
analyser.operation.delay(0.6)
|
||||
analyser.operation.click(analyser.device_id, enter_x, enter_y)
|
||||
analyser.operation.delay(2.0)
|
||||
|
||||
_recovery.recover_to_douyin(
|
||||
analyser,
|
||||
log=lambda msg: _yanghao_log(analyser, f"[养号] 自救: {msg}"),
|
||||
)
|
||||
_yanghao_log(analyser, "[养号] 自救: 恢复操作完成")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
_yanghao_log(analyser, f"[养号] 自救: 恢复操作失败: {e}")
|
||||
return False
|
||||
else:
|
||||
return False
|
||||
|
||||
return False
|
||||
except Exception as e:
|
||||
_yanghao_log(analyser, f"[养号] 自救: 桌面检测异常: {e}")
|
||||
return False
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
"""douyin/core/recovery.py 单元测试。
|
||||
|
||||
覆盖:
|
||||
- is_douyin_foreground:抖音 / 极速版 / 桌面 / 空
|
||||
- recover_to_douyin:launch_package 成功 / 失败回退点击 / 异常回退点击
|
||||
- fallback_click_recovery:使用配置坐标 / 使用默认值 / op.click 抛异常
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from douyin.core import recovery
|
||||
|
||||
|
||||
# ============================================================
|
||||
# is_douyin_foreground
|
||||
# ============================================================
|
||||
|
||||
class TestIsDouyinForeground:
|
||||
def test_aweme_main(self):
|
||||
assert recovery.is_douyin_foreground("com.ss.android.ugc.aweme") is True
|
||||
|
||||
def test_aweme_lite(self):
|
||||
assert recovery.is_douyin_foreground("com.ss.android.ugc.aweme.lite") is True
|
||||
|
||||
def test_douyin_in_pkg(self):
|
||||
assert recovery.is_douyin_foreground("com.bytedance.douyin.something") is True
|
||||
|
||||
def test_launcher_returns_false(self):
|
||||
assert recovery.is_douyin_foreground("com.android.launcher") is False
|
||||
|
||||
def test_empty_returns_false(self):
|
||||
assert recovery.is_douyin_foreground("") is False
|
||||
|
||||
|
||||
# ============================================================
|
||||
# fallback_click_recovery
|
||||
# ============================================================
|
||||
|
||||
class TestFallbackClickRecovery:
|
||||
def _mk_analyser(self):
|
||||
a = MagicMock()
|
||||
a.device_id = "MOCK"
|
||||
a.operation = MagicMock()
|
||||
a.operation.click = MagicMock(return_value=None)
|
||||
a.operation.delay = MagicMock(return_value=None)
|
||||
a.desktop_icon_x = 200
|
||||
a.desktop_icon_y = 300
|
||||
a.yanghao_fallback_live_x = 540
|
||||
a.yanghao_fallback_live_y = 2000
|
||||
a.yanghao_fallback_enter_live_x = 540
|
||||
a.yanghao_fallback_enter_live_y = 1200
|
||||
return a
|
||||
|
||||
def test_clicks_in_order(self):
|
||||
a = self._mk_analyser()
|
||||
recovery.fallback_click_recovery(a)
|
||||
# 至少 4 次 click:桌面图标 + 直播按钮 + 进入直播间 ×2
|
||||
assert a.operation.click.call_count == 4
|
||||
# 第一次点击是桌面图标
|
||||
first_call = a.operation.click.call_args_list[0]
|
||||
assert first_call.args == ("MOCK", 200, 300)
|
||||
|
||||
def test_uses_defaults_when_attrs_missing(self):
|
||||
"""属性缺失时使用默认坐标。"""
|
||||
a = MagicMock()
|
||||
a.device_id = "MOCK"
|
||||
a.operation = MagicMock()
|
||||
a.operation.click = MagicMock(return_value=None)
|
||||
a.operation.delay = MagicMock(return_value=None)
|
||||
# 不设 desktop_icon_x 等属性 → MagicMock 自动生成属性,需明确删除
|
||||
# 用 spec=[] 创造空的对象
|
||||
for attr in (
|
||||
"desktop_icon_x",
|
||||
"desktop_icon_y",
|
||||
"yanghao_fallback_live_x",
|
||||
"yanghao_fallback_live_y",
|
||||
"yanghao_fallback_enter_live_x",
|
||||
"yanghao_fallback_enter_live_y",
|
||||
):
|
||||
# 设置为 0 触发 `or 默认` 逻辑
|
||||
setattr(a, attr, 0)
|
||||
recovery.fallback_click_recovery(a)
|
||||
# 第一次点击应使用默认 (153, 260)
|
||||
first_call = a.operation.click.call_args_list[0]
|
||||
assert first_call.args == ("MOCK", 153, 260)
|
||||
|
||||
def test_op_click_exception_does_not_propagate(self):
|
||||
a = self._mk_analyser()
|
||||
a.operation.click = MagicMock(side_effect=RuntimeError("adb fail"))
|
||||
# 不应抛出
|
||||
recovery.fallback_click_recovery(a)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# recover_to_douyin
|
||||
# ============================================================
|
||||
|
||||
class TestRecoverToDouyin:
|
||||
def _mk_analyser(self):
|
||||
a = MagicMock()
|
||||
a.device_id = "MOCK"
|
||||
a.operation = MagicMock()
|
||||
a.operation.click = MagicMock(return_value=None)
|
||||
a.operation.delay = MagicMock(return_value=None)
|
||||
a.desktop_icon_x = 200
|
||||
a.desktop_icon_y = 300
|
||||
a.yanghao_fallback_live_x = 540
|
||||
a.yanghao_fallback_live_y = 2000
|
||||
a.yanghao_fallback_enter_live_x = 540
|
||||
a.yanghao_fallback_enter_live_y = 1200
|
||||
return a
|
||||
|
||||
def test_launch_success_no_fallback_click(self):
|
||||
a = self._mk_analyser()
|
||||
a.operation.launch_package = MagicMock(
|
||||
return_value=(True, "com.ss.android.ugc.aweme")
|
||||
)
|
||||
result = recovery.recover_to_douyin(a)
|
||||
assert result is True
|
||||
a.operation.launch_package.assert_called_once_with("MOCK", "")
|
||||
# 成功时不应再点击桌面坐标
|
||||
a.operation.click.assert_not_called()
|
||||
|
||||
def test_launch_failure_falls_back_to_clicks(self):
|
||||
a = self._mk_analyser()
|
||||
a.operation.launch_package = MagicMock(return_value=(False, ""))
|
||||
result = recovery.recover_to_douyin(a)
|
||||
assert result is True
|
||||
# 失败时回退点击 4 次
|
||||
assert a.operation.click.call_count == 4
|
||||
|
||||
def test_launch_exception_falls_back_to_clicks(self):
|
||||
a = self._mk_analyser()
|
||||
a.operation.launch_package = MagicMock(side_effect=RuntimeError("adb dead"))
|
||||
result = recovery.recover_to_douyin(a)
|
||||
assert result is True
|
||||
a.operation.click.assert_called()
|
||||
|
||||
def test_log_callback_invoked_on_success(self):
|
||||
a = self._mk_analyser()
|
||||
a.operation.launch_package = MagicMock(
|
||||
return_value=(True, "com.ss.android.ugc.aweme.lite")
|
||||
)
|
||||
msgs = []
|
||||
recovery.recover_to_douyin(a, log=msgs.append)
|
||||
# 成功消息应包含 used_pkg
|
||||
assert any("aweme.lite" in m for m in msgs)
|
||||
|
||||
def test_missing_operation_returns_false(self):
|
||||
"""analyser 没有 operation 属性时不抛、返回 False。"""
|
||||
a = MagicMock(spec=[]) # 空 spec → 访问任何属性都抛 AttributeError
|
||||
result = recovery.recover_to_douyin(a)
|
||||
assert result is False
|
||||
@@ -132,6 +132,7 @@ class TestHandleDesktopRecovery:
|
||||
s.operation = MagicMock()
|
||||
s.operation.get_top_package = MagicMock(return_value="com.android.launcher")
|
||||
s.operation.launch_package = MagicMock(return_value=(False, ""))
|
||||
s.operation.click = MagicMock(return_value=None)
|
||||
s.click = MagicMock(return_value=None)
|
||||
s.run_once_basic = MagicMock(return_value=None)
|
||||
s.desktop_icon_x = 153
|
||||
@@ -143,8 +144,8 @@ class TestHandleDesktopRecovery:
|
||||
|
||||
result = ronce.handle_desktop_recovery(s)
|
||||
assert result is True
|
||||
# 失败时应回退到点击坐标(桌面图标 + 直播 + 进入直播间×2 = 4 次)
|
||||
assert s.click.call_count >= 3
|
||||
# 失败时 fallback_click_recovery 通过 op.click 点击桌面图标 + 直播 + 进入直播间×2
|
||||
assert s.operation.click.call_count >= 3
|
||||
s.run_once_basic.assert_called_once()
|
||||
|
||||
def test_launch_package_exception_falls_back_to_clicks(self):
|
||||
@@ -153,6 +154,7 @@ class TestHandleDesktopRecovery:
|
||||
s.operation = MagicMock()
|
||||
s.operation.get_top_package = MagicMock(return_value="com.android.launcher")
|
||||
s.operation.launch_package = MagicMock(side_effect=RuntimeError("adb dead"))
|
||||
s.operation.click = MagicMock(return_value=None)
|
||||
s.click = MagicMock(return_value=None)
|
||||
s.run_once_basic = MagicMock(return_value=None)
|
||||
s.desktop_icon_x = 153
|
||||
@@ -164,7 +166,7 @@ class TestHandleDesktopRecovery:
|
||||
|
||||
result = ronce.handle_desktop_recovery(s)
|
||||
assert result is True
|
||||
s.click.assert_called()
|
||||
s.operation.click.assert_called()
|
||||
|
||||
def test_get_top_package_failed_returns_false(self):
|
||||
s = _mk_self()
|
||||
|
||||
Reference in new issue
Block a user