264 lines
8.5 KiB
Python
264 lines
8.5 KiB
Python
"""douyin/handlers 模块单元测试。
|
|
|
|
覆盖:
|
|
- douyin_swipe_ops: _adb_path 纯函数
|
|
- SwipeHandler 类存在性验证
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from unittest.mock import MagicMock, patch
|
|
import pytest
|
|
|
|
|
|
class TestAdbPath:
|
|
"""_adb_path 纯函数测试。"""
|
|
|
|
def test_default_returns_adb(self):
|
|
from douyin.handlers.douyin_swipe_ops import _adb_path
|
|
|
|
# 清除环境变量
|
|
old = os.environ.pop("ADB_EXECUTABLE", None)
|
|
try:
|
|
result = _adb_path(None)
|
|
assert result == "adb"
|
|
finally:
|
|
if old is not None:
|
|
os.environ["ADB_EXECUTABLE"] = old
|
|
|
|
def test_respects_env_var(self):
|
|
from douyin.handlers.douyin_swipe_ops import _adb_path
|
|
|
|
old = os.environ.get("ADB_EXECUTABLE")
|
|
try:
|
|
os.environ["ADB_EXECUTABLE"] = "/custom/path/adb"
|
|
result = _adb_path(None)
|
|
assert result == "/custom/path/adb"
|
|
finally:
|
|
if old is not None:
|
|
os.environ["ADB_EXECUTABLE"] = old
|
|
else:
|
|
os.environ.pop("ADB_EXECUTABLE", None)
|
|
|
|
def test_strips_quotes_from_env_var(self):
|
|
from douyin.handlers.douyin_swipe_ops import _adb_path
|
|
|
|
old = os.environ.get("ADB_EXECUTABLE")
|
|
try:
|
|
os.environ["ADB_EXECUTABLE"] = '"/path/with spaces/adb"'
|
|
result = _adb_path(None)
|
|
assert result == "/path/with spaces/adb"
|
|
finally:
|
|
if old is not None:
|
|
os.environ["ADB_EXECUTABLE"] = old
|
|
else:
|
|
os.environ.pop("ADB_EXECUTABLE", None)
|
|
|
|
def test_uses_operation_adb_path_if_available(self):
|
|
from douyin.handlers.douyin_swipe_ops import _adb_path
|
|
|
|
class MockOperation:
|
|
def _adb_path(self):
|
|
return "/mock/adb"
|
|
|
|
result = _adb_path(MockOperation())
|
|
assert result == "/mock/adb"
|
|
|
|
def test_ignores_operation_without_adb_path(self):
|
|
from douyin.handlers.douyin_swipe_ops import _adb_path
|
|
|
|
class MockOperationNoMethod:
|
|
pass
|
|
|
|
old = os.environ.pop("ADB_EXECUTABLE", None)
|
|
try:
|
|
result = _adb_path(MockOperationNoMethod())
|
|
assert result == "adb"
|
|
finally:
|
|
if old is not None:
|
|
os.environ["ADB_EXECUTABLE"] = old
|
|
|
|
|
|
class TestSwipeHandler:
|
|
"""SwipeHandler 类测试。"""
|
|
|
|
def test_class_exists(self):
|
|
from douyin.handlers.douyin_swipe_handler import SwipeHandler
|
|
|
|
assert SwipeHandler is not None
|
|
|
|
def test_class_is_importable(self):
|
|
from douyin.handlers import douyin_swipe_handler
|
|
|
|
assert hasattr(douyin_swipe_handler, "SwipeHandler")
|
|
|
|
def test_swipe_up_skips_extra_swipe_when_record_consumes(self):
|
|
"""若记录阶段已触发回顶并消费本轮切房,不应再额外上滑一次。"""
|
|
from douyin.handlers.douyin_swipe_handler import SwipeHandler
|
|
|
|
class _A:
|
|
pass
|
|
|
|
a = _A()
|
|
a.reservation_focus_until_ts = 0.0
|
|
a._skip_record_title_during_seek = False
|
|
a._room_pos_to_title_map = {}
|
|
a.room_pos = 5
|
|
a.device_id = "MOCK"
|
|
a._log_kv = MagicMock(return_value=None)
|
|
a._record_room_title_and_pos = MagicMock(return_value=True)
|
|
|
|
with patch(
|
|
"douyin.handlers.douyin_swipe_ops.swipe_up_switch_live"
|
|
) as mock_swipe_up:
|
|
SwipeHandler.swipe_up_to_next_live(a)
|
|
|
|
a._record_room_title_and_pos.assert_called_once()
|
|
mock_swipe_up.assert_not_called()
|
|
assert int(getattr(a, "room_pos", 0) or 0) == 5
|
|
assert bool(getattr(a, "_record_title_before_swipe_up", False)) is False
|
|
|
|
def test_swipe_up_clears_fudai_content_cache(self):
|
|
"""切换直播间后不应把上一房福袋内容带到下一房映射表。"""
|
|
from douyin.handlers.douyin_swipe_handler import SwipeHandler
|
|
|
|
class _A:
|
|
pass
|
|
|
|
a = _A()
|
|
a.reservation_focus_until_ts = 0.0
|
|
a._skip_record_title_during_seek = True
|
|
a.room_pos = 1
|
|
a.device_id = "MOCK"
|
|
a._log_kv = MagicMock(return_value=None)
|
|
a._current_fudai_content = "上一房福袋"
|
|
a._last_fudai_detail_tuple = ("old",)
|
|
a._fudai_detail_first_read = True
|
|
|
|
with patch("douyin.handlers.douyin_swipe_ops.swipe_up_switch_live"):
|
|
SwipeHandler.swipe_up_to_next_live(a)
|
|
|
|
assert a._current_fudai_content == ""
|
|
assert a._last_fudai_detail_tuple is None
|
|
assert a._fudai_detail_first_read is False
|
|
|
|
def test_reset_to_top_keep_mapping_does_not_clear_map(self):
|
|
"""回顶后应保留运行时映射表。"""
|
|
from douyin.handlers.douyin_swipe_handler import SwipeHandler
|
|
|
|
class _A:
|
|
pass
|
|
|
|
a = _A()
|
|
a._resetting_after_swipes = False
|
|
a.room_pos = 3
|
|
a._swipe_times = 9
|
|
a._just_switched_room = False
|
|
a._room_pos_to_title_map = {
|
|
0: {"title": "A", "title_raw": "A"},
|
|
1: {"title": "B", "title_raw": "B"},
|
|
2: {"title": "C", "title_raw": "C"},
|
|
}
|
|
a._log_kv = MagicMock(return_value=None)
|
|
a.operation = MagicMock()
|
|
a.operation.delay = MagicMock(return_value=None)
|
|
|
|
with patch("douyin.handlers.douyin_swipe_ops.swipe_down_back_live") as mock_down:
|
|
ok = SwipeHandler.reset_to_top_keep_mapping(a, reason="ut", min_back_times=1)
|
|
|
|
assert ok is True
|
|
assert mock_down.call_count >= 1
|
|
assert int(getattr(a, "room_pos", -1)) == 0
|
|
assert isinstance(a._room_pos_to_title_map, dict)
|
|
assert len(a._room_pos_to_title_map) == 3
|
|
assert 0 in a._room_pos_to_title_map and 2 in a._room_pos_to_title_map
|
|
|
|
def test_escape_same_room_after_reset_clicks_back_and_switches_next(self):
|
|
"""回顶后仍停在同一直播间时,应返回并切到下一个直播间。"""
|
|
from douyin.handlers.douyin_swipe_handler import SwipeHandler
|
|
|
|
class _A:
|
|
pass
|
|
|
|
a = _A()
|
|
a._escaping_same_room_after_reset = False
|
|
a.room_pos = 7
|
|
a._swipe_times = 3
|
|
a._just_switched_room = False
|
|
a.device_id = "MOCK"
|
|
a._log_kv = MagicMock(return_value=None)
|
|
a.operation = MagicMock()
|
|
a.operation.click_back = MagicMock(return_value=None)
|
|
a.operation.delay = MagicMock(return_value=None)
|
|
a._current_fudai_content = "上一房福袋"
|
|
a._last_fudai_detail_tuple = ("old",)
|
|
a._fudai_detail_first_read = True
|
|
a.current_room_title = "旧标题"
|
|
a.current_room_title_raw = "旧标题"
|
|
|
|
with patch("douyin.handlers.douyin_swipe_ops.swipe_up_switch_live") as mock_next:
|
|
ok = SwipeHandler.escape_same_room_after_reset(a, reason="ut")
|
|
|
|
assert ok is True
|
|
a.operation.click_back.assert_called_once_with("MOCK")
|
|
mock_next.assert_called_once_with(a)
|
|
assert int(getattr(a, "room_pos", -1)) == 8
|
|
assert int(getattr(a, "_swipe_times", -1)) == 4
|
|
assert bool(getattr(a, "_just_switched_room", False)) is True
|
|
assert a._current_fudai_content == ""
|
|
assert a._last_fudai_detail_tuple is None
|
|
assert a._fudai_detail_first_read is False
|
|
assert a.current_room_title == ""
|
|
assert a.current_room_title_raw == ""
|
|
|
|
|
|
class TestSwipeOps:
|
|
"""douyin_swipe_ops 模块测试。"""
|
|
|
|
def test_swipe_functions_exist(self):
|
|
from douyin.handlers.douyin_swipe_ops import (
|
|
swipe_up_switch_live,
|
|
swipe_down_back_live,
|
|
)
|
|
|
|
assert callable(swipe_up_switch_live)
|
|
assert callable(swipe_down_back_live)
|
|
|
|
def test_adb_swipe_direct_exists(self):
|
|
from douyin.handlers.douyin_swipe_ops import _adb_swipe_direct
|
|
|
|
assert callable(_adb_swipe_direct)
|
|
|
|
|
|
class TestPopupHandler:
|
|
"""PopupHandler 类测试。"""
|
|
|
|
def test_module_importable(self):
|
|
from douyin.handlers import douyin_popup_handler
|
|
|
|
assert douyin_popup_handler is not None
|
|
|
|
def test_class_exists(self):
|
|
from douyin.handlers.douyin_popup_handler import PopupHandler
|
|
|
|
assert PopupHandler is not None
|
|
|
|
|
|
class TestScreenshotHandler:
|
|
"""ScreenshotHandler 类测试。"""
|
|
|
|
def test_module_importable(self):
|
|
from douyin.handlers import douyin_screenshot_handler
|
|
|
|
assert douyin_screenshot_handler is not None
|
|
|
|
|
|
class TestScreenshotManager:
|
|
"""ScreenshotManager 类测试。"""
|
|
|
|
def test_module_importable(self):
|
|
from douyin.handlers import douyin_screenshot_manager
|
|
|
|
assert douyin_screenshot_manager is not None
|