This commit is contained in:
+63
-2
@@ -1,4 +1,6 @@
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
import sys
|
||||
|
||||
from click.testing import CliRunner
|
||||
from unittest.mock import patch
|
||||
@@ -31,14 +33,18 @@ def test_project_version_falls_back_to_installed_package_metadata():
|
||||
assert _project_version() == "9.9.9"
|
||||
|
||||
def test_clients_rename_uses_command_level_browser_target():
|
||||
with patch("browser_cli.cli.send_command") as send_command:
|
||||
with patch("browser_cli.cli.REGISTRY_PATH", Path("/nonexistent/browser-cli-registry.json")), patch(
|
||||
"browser_cli.cli.send_command"
|
||||
) as send_command:
|
||||
result = CliRunner().invoke(main, ["clients", "rename", "--browser", "old-id", "work"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
send_command.assert_called_once_with("clients.rename_profile", {"alias": "work"}, profile="old-id")
|
||||
|
||||
def test_clients_rename_uses_global_browser_target_when_set():
|
||||
with patch("browser_cli.cli.send_command") as send_command:
|
||||
with patch("browser_cli.cli.REGISTRY_PATH", Path("/nonexistent/browser-cli-registry.json")), patch(
|
||||
"browser_cli.cli.send_command"
|
||||
) as send_command:
|
||||
result = CliRunner().invoke(main, ["--browser", "old-id", "clients", "rename", "work"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
@@ -72,6 +78,61 @@ def test_install_help_lists_supported_browsers():
|
||||
assert result.exit_code == 0
|
||||
assert "[chrome|chromium|brave|edge|vivaldi]" in result.output
|
||||
|
||||
|
||||
def test_install_windows_registers_native_host(tmp_path, monkeypatch):
|
||||
local_app_data = tmp_path / "LocalAppData"
|
||||
extension_dir = tmp_path / "extension"
|
||||
extension_dir.mkdir()
|
||||
native_host_src = tmp_path / "native_host.py"
|
||||
native_host_src.write_text("print('ok')", encoding="utf-8")
|
||||
writes = []
|
||||
|
||||
class FakeKey:
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc, tb):
|
||||
return False
|
||||
|
||||
fake_winreg = SimpleNamespace(
|
||||
HKEY_CURRENT_USER="HKCU",
|
||||
KEY_WRITE=0x20006,
|
||||
KEY_WOW64_32KEY=0x0200,
|
||||
KEY_WOW64_64KEY=0x0100,
|
||||
REG_SZ=1,
|
||||
)
|
||||
|
||||
def fake_create_key(root, path, reserved, access):
|
||||
return FakeKey(path)
|
||||
|
||||
def fake_set_value(key, name, reserved, reg_type, value):
|
||||
writes.append((key.path, name, value))
|
||||
|
||||
fake_winreg.CreateKeyEx = fake_create_key
|
||||
fake_winreg.SetValueEx = fake_set_value
|
||||
|
||||
monkeypatch.setenv("LOCALAPPDATA", str(local_app_data))
|
||||
|
||||
with patch("browser_cli.cli.is_windows", return_value=True), patch(
|
||||
"browser_cli.cli.Path.home", return_value=tmp_path
|
||||
), patch("browser_cli.cli.click.prompt", return_value="abc123"), patch(
|
||||
"browser_cli.cli.shutil.copy2"
|
||||
) as copy2, patch("browser_cli.cli.Path.write_text") as write_text, patch.dict(
|
||||
sys.modules, {"winreg": fake_winreg}
|
||||
):
|
||||
copy2.side_effect = lambda src, dst: Path(dst).write_text(native_host_src.read_text(encoding="utf-8"), encoding="utf-8")
|
||||
result = CliRunner().invoke(main, ["install", "edge"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert any("Software\\Microsoft\\Edge\\NativeMessagingHosts\\com.browsercli.host" in path for path, _, _ in writes)
|
||||
assert "Registered native host" in result.output
|
||||
assert "Wrote native host manifest" in result.output
|
||||
wrapper_writes = [call.args[0] for call in write_text.call_args_list if call.args]
|
||||
assert any("@echo off" in text for text in wrapper_writes)
|
||||
|
||||
def test_clients_exits_cleanly_when_registry_is_missing():
|
||||
with patch("browser_cli.cli.REGISTRY_PATH", Path("/nonexistent/browser-cli-registry.json")):
|
||||
result = CliRunner().invoke(main, ["clients"])
|
||||
|
||||
Reference in New Issue
Block a user