/ tests / cli / test_cli_browser_connect.py
test_cli_browser_connect.py
  1  """Tests for CLI browser CDP auto-launch helpers."""
  2  
  3  import os
  4  import subprocess
  5  from unittest.mock import patch
  6  
  7  from cli import HermesCLI
  8  from hermes_cli.browser_connect import manual_chrome_debug_command
  9  
 10  
 11  def _assert_chrome_debug_cmd(cmd, expected_chrome, expected_port):
 12      """Verify the auto-launch command has all required flags."""
 13      assert cmd[0] == expected_chrome
 14      assert f"--remote-debugging-port={expected_port}" in cmd
 15      assert "--no-first-run" in cmd
 16      assert "--no-default-browser-check" in cmd
 17      user_data_args = [a for a in cmd if a.startswith("--user-data-dir=")]
 18      assert len(user_data_args) == 1, "Expected exactly one --user-data-dir flag"
 19      assert "chrome-debug" in user_data_args[0]
 20  
 21  
 22  class TestChromeDebugLaunch:
 23      def test_windows_launch_uses_browser_found_on_path(self):
 24          captured = {}
 25  
 26          def fake_popen(cmd, **kwargs):
 27              captured["cmd"] = cmd
 28              captured["kwargs"] = kwargs
 29              return object()
 30  
 31          with patch("hermes_cli.browser_connect.shutil.which", side_effect=lambda name: r"C:\Chrome\chrome.exe" if name == "chrome.exe" else None), \
 32               patch("hermes_cli.browser_connect.os.path.isfile", side_effect=lambda path: path == r"C:\Chrome\chrome.exe"), \
 33               patch("subprocess.Popen", side_effect=fake_popen):
 34              assert HermesCLI._try_launch_chrome_debug(9333, "Windows") is True
 35  
 36          _assert_chrome_debug_cmd(captured["cmd"], r"C:\Chrome\chrome.exe", 9333)
 37          # Windows uses creationflags (POSIX-only start_new_session would raise).
 38          assert "start_new_session" not in captured["kwargs"]
 39          flags = captured["kwargs"].get("creationflags", 0)
 40          expected = getattr(subprocess, "DETACHED_PROCESS", 0) | getattr(
 41              subprocess, "CREATE_NEW_PROCESS_GROUP", 0
 42          )
 43          assert flags == expected
 44  
 45      def test_windows_launch_falls_back_to_common_install_dirs(self, monkeypatch):
 46          captured = {}
 47          program_files = r"C:\Program Files"
 48          # Use os.path.join so path separators match cross-platform
 49          installed = os.path.join(program_files, "Google", "Chrome", "Application", "chrome.exe")
 50  
 51          def fake_popen(cmd, **kwargs):
 52              captured["cmd"] = cmd
 53              captured["kwargs"] = kwargs
 54              return object()
 55  
 56          monkeypatch.setenv("ProgramFiles", program_files)
 57          monkeypatch.delenv("ProgramFiles(x86)", raising=False)
 58          monkeypatch.delenv("LOCALAPPDATA", raising=False)
 59  
 60          with patch("hermes_cli.browser_connect.shutil.which", return_value=None), \
 61               patch("hermes_cli.browser_connect.os.path.isfile", side_effect=lambda path: path == installed), \
 62               patch("subprocess.Popen", side_effect=fake_popen):
 63              assert HermesCLI._try_launch_chrome_debug(9222, "Windows") is True
 64  
 65          _assert_chrome_debug_cmd(captured["cmd"], installed, 9222)
 66  
 67      def test_manual_command_uses_detected_linux_browser(self):
 68          with patch("hermes_cli.browser_connect.shutil.which", side_effect=lambda name: "/usr/bin/chromium" if name == "chromium" else None), \
 69               patch("hermes_cli.browser_connect.os.path.isfile", side_effect=lambda path: path == "/usr/bin/chromium"):
 70              command = manual_chrome_debug_command(9222, "Linux")
 71  
 72          assert command is not None
 73          assert command.startswith("/usr/bin/chromium --remote-debugging-port=9222")
 74  
 75      def test_manual_command_uses_wsl_windows_chrome_when_available(self):
 76          chrome = "/mnt/c/Program Files/Google/Chrome/Application/chrome.exe"
 77  
 78          with patch("hermes_cli.browser_connect.shutil.which", return_value=None), \
 79               patch("hermes_cli.browser_connect.os.path.isfile", side_effect=lambda path: path == chrome):
 80              command = manual_chrome_debug_command(9222, "Linux")
 81  
 82          assert command is not None
 83          # Linux/WSL uses POSIX shell quoting (single quotes around paths with spaces).
 84          assert command.startswith(f"'{chrome}' --remote-debugging-port=9222")
 85  
 86      def test_manual_command_uses_windows_quoting_on_windows(self):
 87          chrome = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
 88  
 89          with patch("hermes_cli.browser_connect.shutil.which", side_effect=lambda name: chrome if name == "chrome.exe" else None), \
 90               patch("hermes_cli.browser_connect.os.path.isfile", side_effect=lambda path: path == chrome):
 91              command = manual_chrome_debug_command(9222, "Windows")
 92  
 93          assert command is not None
 94          # Windows uses cmd.exe-compatible quoting via subprocess.list2cmdline.
 95          assert command.startswith(f'"{chrome}" --remote-debugging-port=9222')
 96          assert "'" not in command
 97  
 98      def test_manual_command_returns_none_when_linux_browser_missing(self):
 99          with patch("hermes_cli.browser_connect.shutil.which", return_value=None), \
100               patch("hermes_cli.browser_connect.os.path.isfile", return_value=False):
101              assert manual_chrome_debug_command(9222, "Linux") is None