"""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