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:
CodexandClaude Opus 4.7 committed 2026-05-22 19:20:29 +08:00
1 parent 4c1d95d4b5
commit a92cec4ff9
5 files changed
+309 -269

No files matched your search

+156
View File
@@ -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
+5 -3
View File
@@ -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()