diff --git a/bin/run_cli.py b/bin/run_cli.py index 753f36a..44ba45c 100644 --- a/bin/run_cli.py +++ b/bin/run_cli.py @@ -105,6 +105,7 @@ os.environ.setdefault("GLOG_minloglevel", "2") os.environ.setdefault("OMP_NUM_THREADS", "1") from douyin.core.douyin_fudai import fudai_analyse +from douyin.core import path_config from douyin.license.license_manager import require_license_or_exit @@ -339,7 +340,7 @@ def parse_args(argv: list[str]) -> argparse.Namespace: p.add_argument( "--reenter-live-entry-mode", choices=["follow_list", "screen_center"], - default=None, + default="screen_center", help="重新进入直播间点击模式:follow_list=关注列表坐标,screen_center=屏幕中心坐标", ) p.add_argument( @@ -400,6 +401,66 @@ def _collect_regression_artifacts_if_enabled( print("[REGRESSION] collect failed: {}".format(e), flush=True) +def _is_douyin_top_package(package_name: str) -> bool: + top_pkg = str(package_name or "") + return bool(top_pkg) and ( + "aweme" in top_pkg + or "douyin" in top_pkg + or "com.ss.android.ugc" in top_pkg + or "musically" in top_pkg + ) + + +def _cold_start_douyin_for_test(analyser: object, device_id: str) -> bool: + op = getattr(analyser, "operation", None) + if op is None or not device_id: + return False + packages = path_config.get_douyin_packages() + for pkg in packages: + pkg = str(pkg or "").strip() + if not pkg: + continue + try: + op._run_adb_shell(device_id, f"am force-stop {pkg}", timeout_seconds=3.0) + except Exception: + pass + try: + op.delay(1.5) + except Exception: + time.sleep(1.5) + + launched_ok = False + launched_pkg = "" + try: + launched_ok, launched_pkg = op.launch_package(device_id, "", timeout_seconds=5.0) + except Exception as e: + print(f"[COLD-START] launch failed: {e}", flush=True) + + try: + op.delay(2.5) + except Exception: + time.sleep(2.5) + + top_pkg = "" + try: + top_pkg = op.get_top_package(device_id) or "" + except Exception: + top_pkg = "" + + ok = bool(launched_ok) and _is_douyin_top_package(top_pkg or launched_pkg) + print( + "[COLD-START] force_stop={} launched={} pkg={} top_pkg={} result={}".format( + len(tuple(packages)), + bool(launched_ok), + launched_pkg, + top_pkg, + "OK" if ok else "FAIL", + ), + flush=True, + ) + return ok + + def main(argv: list[str]) -> int: require_license_or_exit() ensure_adb_in_path() @@ -679,13 +740,12 @@ def main(argv: list[str]) -> int: except Exception: pass - if getattr(args, "reenter_live_entry_mode", None) is not None: - try: - analyser.reenter_live_entry_mode = str( - args.reenter_live_entry_mode or "follow_list" - ).strip().lower() - except Exception: - analyser.reenter_live_entry_mode = "follow_list" + try: + analyser.reenter_live_entry_mode = str( + args.reenter_live_entry_mode or "screen_center" + ).strip().lower() + except Exception: + analyser.reenter_live_entry_mode = "screen_center" device_id = args.device_id.strip() if args.device_id else "" if not device_id: @@ -695,6 +755,10 @@ def main(argv: list[str]) -> int: print("未检测到设备,退出。") return 2 + if not _cold_start_douyin_for_test(analyser, device_id): + print("抖音冷启动失败或前台不是抖音,退出。") + return 4 + exit_code = 0 try: analyser.fudai_choujiang( diff --git a/tests/test_bin_modules.py b/tests/test_bin_modules.py index ad7d9fc..fad494b 100644 --- a/tests/test_bin_modules.py +++ b/tests/test_bin_modules.py @@ -97,6 +97,7 @@ class TestRunCliParseArgs: 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 @@ -137,6 +138,71 @@ class TestRunCliModule: 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 子模块存在性测试。"""