219 lines
6.1 KiB
Python
219 lines
6.1 KiB
Python
"""bin 模块单元测试。
|
|
|
|
覆盖:
|
|
- manage_logs: format_size, get_log_base_names 纯函数
|
|
- run_cli: parse_args 函数
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import pytest
|
|
|
|
|
|
class TestFormatSize:
|
|
"""format_size 纯函数测试。"""
|
|
|
|
def test_small_size(self):
|
|
from bin.manage_logs import format_size
|
|
|
|
result = format_size(0.5)
|
|
assert "MB" in result or "KB" in result or "0" in result
|
|
|
|
def test_medium_size(self):
|
|
from bin.manage_logs import format_size
|
|
|
|
result = format_size(100.0)
|
|
assert isinstance(result, str)
|
|
|
|
def test_large_size(self):
|
|
from bin.manage_logs import format_size
|
|
|
|
result = format_size(1500.0)
|
|
assert isinstance(result, str)
|
|
assert "GB" in result or "1" in result
|
|
|
|
def test_zero_size(self):
|
|
from bin.manage_logs import format_size
|
|
|
|
result = format_size(0.0)
|
|
assert isinstance(result, str)
|
|
|
|
|
|
class TestGetLogBaseNames:
|
|
"""get_log_base_names 函数测试。"""
|
|
|
|
def test_function_exists(self):
|
|
from bin.manage_logs import get_log_base_names
|
|
|
|
assert callable(get_log_base_names)
|
|
|
|
def test_nonexistent_dir_returns_empty(self):
|
|
from bin.manage_logs import get_log_base_names
|
|
|
|
result = get_log_base_names('/nonexistent/path')
|
|
assert result == []
|
|
|
|
|
|
class TestManageLogsModule:
|
|
"""manage_logs 模块测试。"""
|
|
|
|
def test_show_stats_exists(self):
|
|
from bin.manage_logs import show_stats
|
|
|
|
assert callable(show_stats)
|
|
|
|
def test_manual_cleanup_exists(self):
|
|
from bin.manage_logs import manual_cleanup
|
|
|
|
assert callable(manual_cleanup)
|
|
|
|
def test_manual_compress_exists(self):
|
|
from bin.manage_logs import manual_compress
|
|
|
|
assert callable(manual_compress)
|
|
|
|
def test_show_monitor_status_exists(self):
|
|
from bin.manage_logs import show_monitor_status
|
|
|
|
assert callable(show_monitor_status)
|
|
|
|
def test_main_exists(self):
|
|
from bin.manage_logs import main
|
|
|
|
assert callable(main)
|
|
|
|
|
|
class TestRunCliParseArgs:
|
|
"""run_cli.parse_args 函数测试。"""
|
|
|
|
def test_function_exists(self):
|
|
from bin.run_cli import parse_args
|
|
|
|
assert callable(parse_args)
|
|
|
|
def test_empty_args(self):
|
|
from bin.run_cli import parse_args
|
|
|
|
result = parse_args([])
|
|
assert isinstance(result, argparse.Namespace)
|
|
assert result.reenter_live_entry_mode == 'screen_center'
|
|
|
|
def test_device_id_arg(self):
|
|
from bin.run_cli import parse_args
|
|
|
|
result = parse_args(['--device-id', 'test_device'])
|
|
assert result.device_id == 'test_device'
|
|
|
|
def test_y_pianyi_arg(self):
|
|
from bin.run_cli import parse_args
|
|
|
|
result = parse_args(['--y-pianyi', '100'])
|
|
assert result.y_pianyi == 100
|
|
|
|
def test_regression_collection_args(self):
|
|
from bin.run_cli import parse_args
|
|
|
|
result = parse_args([
|
|
'--disable-regression-collection',
|
|
'--regression-collection-max-images',
|
|
'120',
|
|
'--regression-full-run-package',
|
|
])
|
|
assert result.disable_regression_collection is True
|
|
assert result.regression_collection_max_images == 120
|
|
assert result.regression_full_run_package is True
|
|
|
|
|
|
class TestRunCliModule:
|
|
"""run_cli 模块测试。"""
|
|
|
|
def test_ensure_adb_in_path_exists(self):
|
|
from bin.run_cli import ensure_adb_in_path
|
|
|
|
assert callable(ensure_adb_in_path)
|
|
|
|
def test_main_exists(self):
|
|
from bin.run_cli import main
|
|
|
|
assert callable(main)
|
|
|
|
def test_cold_start_force_stops_all_douyin_packages_then_launches(self, monkeypatch):
|
|
from bin import run_cli
|
|
|
|
class FakeOperation:
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
def _run_adb_shell(self, device_id, cmd, timeout_seconds=3.0):
|
|
self.calls.append(("shell", device_id, cmd, timeout_seconds))
|
|
return True
|
|
|
|
def delay(self, seconds):
|
|
self.calls.append(("delay", seconds))
|
|
|
|
def launch_package(self, device_id, package_name="", timeout_seconds=5.0):
|
|
self.calls.append(("launch", device_id, package_name, timeout_seconds))
|
|
return True, "com.ss.android.ugc.aweme"
|
|
|
|
def get_top_package(self, device_id):
|
|
self.calls.append(("top", device_id))
|
|
return "com.ss.android.ugc.aweme"
|
|
|
|
class FakeAnalyser:
|
|
operation = FakeOperation()
|
|
|
|
monkeypatch.setattr(
|
|
run_cli.path_config,
|
|
"get_douyin_packages",
|
|
lambda: ("pkg.a", "pkg.b"),
|
|
)
|
|
|
|
assert run_cli._cold_start_douyin_for_test(FakeAnalyser(), "dev1") is True
|
|
calls = FakeAnalyser.operation.calls
|
|
assert ("shell", "dev1", "am force-stop pkg.a", 3.0) in calls
|
|
assert ("shell", "dev1", "am force-stop pkg.b", 3.0) in calls
|
|
assert ("launch", "dev1", "", 5.0) in calls
|
|
assert ("top", "dev1") in calls
|
|
|
|
def test_cold_start_fails_when_foreground_is_not_douyin(self, monkeypatch):
|
|
from bin import run_cli
|
|
|
|
class FakeOperation:
|
|
def _run_adb_shell(self, device_id, cmd, timeout_seconds=3.0):
|
|
return True
|
|
|
|
def delay(self, seconds):
|
|
return None
|
|
|
|
def launch_package(self, device_id, package_name="", timeout_seconds=5.0):
|
|
return True, "com.ss.android.ugc.aweme"
|
|
|
|
def get_top_package(self, device_id):
|
|
return "com.android.launcher"
|
|
|
|
class FakeAnalyser:
|
|
operation = FakeOperation()
|
|
|
|
monkeypatch.setattr(
|
|
run_cli.path_config,
|
|
"get_douyin_packages",
|
|
lambda: ("com.ss.android.ugc.aweme",),
|
|
)
|
|
|
|
assert run_cli._cold_start_douyin_for_test(FakeAnalyser(), "dev1") is False
|
|
|
|
|
|
class TestBinModules:
|
|
"""bin 子模块存在性测试。"""
|
|
|
|
def test_run_cli_importable(self):
|
|
from bin import run_cli
|
|
|
|
assert run_cli is not None
|
|
|
|
def test_manage_logs_importable(self):
|
|
from bin import manage_logs
|
|
|
|
assert manage_logs is not None
|