# -*- coding: utf-8 -*- """ Browser launch helper checks (no real browser started). """ import json from unittest import mock import pytest class TestPlaywrightLaunchHelpers: """Persistent context launch parts must match RPA contract.""" def test_persistent_context_includes_maximized(self): from util.playwright_stealth import persistent_context_launch_parts args, _ignore = persistent_context_launch_parts() assert "--start-maximized" in args class TestCmdOpenDirectBrowser: """account open = same-process Chrome/Edge launch (like reveal-profile).""" def test_cmd_open_launches_browser_exe(self, clean_db, tmp_path, capsys, monkeypatch): from db.accounts_repo import insert_account from service import browser_service from util.platforms import seed_platforms seed_platforms(clean_db) profile = tmp_path / "prof" aid = insert_account( clean_db, platform_key="sohu", account_label="open-test", login_id="1", login_id_type="phone", tenant_id=None, environment="production", role="default", provider_code="sohu", profile_dir=str(profile), url="https://example.com", extra_json=None, now=1_700_000_000, auth_strategy="password_auto", ) clean_db.commit() fake_exe = str(tmp_path / "chrome.exe") monkeypatch.setattr( browser_service, "resolve_chromium_executable", lambda: fake_exe, ) class FakeProc: pid = 5252 spawned = {} def fake_popen(cmd, **kwargs): spawned["cmd"] = list(cmd) spawned["kwargs"] = kwargs return FakeProc() monkeypatch.setattr(browser_service.subprocess, "Popen", fake_popen) browser_service.cmd_open(str(aid)) out = capsys.readouterr().out.strip() payload = json.loads(out.splitlines()[-1]) assert payload["success"] is True assert payload["message"] == "" assert payload["type"] == "result" assert payload["status"] == "success" assert payload["data"]["pid"] == 5252 assert payload["data"]["browser_exe"] == fake_exe assert spawned["cmd"][0] == fake_exe assert spawned["cmd"][1].startswith("--user-data-dir=") assert "--start-maximized" in spawned["cmd"] assert "--no-first-run" in spawned["cmd"] assert "--no-default-browser-check" in spawned["cmd"] assert "--disable-search-engine-choice-screen" in spawned["cmd"] assert spawned["cmd"][-1] == "https://example.com" assert "--enable-automation" not in spawned["cmd"] # must NOT spawn python / open_child_runner joined = " ".join(spawned["cmd"]) assert "open_child_runner" not in joined assert "python" not in joined.lower() class TestHumanChromeLaunchArgs: def test_flags_then_url(self, tmp_path): from service.browser_service import HUMAN_CHROME_FLAGS, human_chrome_launch_args profile = str(tmp_path / "prof") args = human_chrome_launch_args(profile, "https://chat.deepseek.com") assert args[0].startswith("--user-data-dir=") for flag in HUMAN_CHROME_FLAGS: assert flag in args assert args[-1] == "https://chat.deepseek.com" assert "--enable-automation" not in args def test_shortcut_arguments_quote_profile_and_include_url(self, tmp_path): from service.browser_service import human_chrome_shortcut_arguments profile = tmp_path / "my prof" profile.mkdir() text = human_chrome_shortcut_arguments(str(profile), "https://example.com") assert '--user-data-dir="' in text assert "--start-maximized" in text assert "--no-first-run" in text assert text.endswith("https://example.com") class TestResolveChromiumChannel: """resolve_chromium_channel returns a channel string when browsers exist.""" def test_returns_string_or_none(self): from service.browser_service import resolve_chromium_channel ch = resolve_chromium_channel() assert ch is None or ch in ("chrome", "msedge")