import json from click.testing import CliRunner from browser_cli.commands.remote import remote_group from browser_cli.remote import registry as remote_registry def test_save_remote_persists_endpoint_without_key(monkeypatch, tmp_path): path = tmp_path / "remotes.json" monkeypatch.setattr(remote_registry, "REMOTE_REGISTRY_PATH", path) remote_registry.save_remote("browser-host.example:443") assert json.loads(path.read_text(encoding="utf-8")) == {"browser-host.example": {}} def test_save_remote_with_key_and_remove(monkeypatch, tmp_path): path = tmp_path / "remotes.json" monkeypatch.setattr(remote_registry, "REMOTE_REGISTRY_PATH", path) remote_registry.save_remote("browser-host.example", "agent") assert remote_registry.load_remotes() == {"browser-host.example": {"key": "agent"}} assert remote_registry.remove_remote("browser-host.example:443") is True assert remote_registry.load_remotes() == {} def test_resolve_remote_endpoint_prefers_remembered_explicit_port(monkeypatch, tmp_path): path = tmp_path / "remotes.json" monkeypatch.setattr(remote_registry, "REMOTE_REGISTRY_PATH", path) path.write_text(json.dumps({"browser-host.example": {}, "browser-host.example:8765": {}}), encoding="utf-8") assert remote_registry.resolve_remote_endpoint("browser-host.example") == "browser-host.example:8765" def test_resolve_remote_endpoint_keeps_bare_domain_without_unique_port_match(monkeypatch, tmp_path): path = tmp_path / "remotes.json" monkeypatch.setattr(remote_registry, "REMOTE_REGISTRY_PATH", path) path.write_text( json.dumps({"browser-host.example:8765": {}, "browser-host.example:9000": {}}), encoding="utf-8", ) assert remote_registry.resolve_remote_endpoint("browser-host.example") == "browser-host.example" def test_remote_add_list_remove_cli(monkeypatch, tmp_path): path = tmp_path / "remotes.json" monkeypatch.setattr(remote_registry, "REMOTE_REGISTRY_PATH", path) runner = CliRunner() add_result = runner.invoke(remote_group, ["add", "browser-host.example", "--key", "agent"]) list_result = runner.invoke(remote_group, ["list"]) remove_result = runner.invoke(remote_group, ["remove", "browser-host.example"]) assert add_result.exit_code == 0 assert "Added remote browser-host.example with key agent" in add_result.output assert list_result.exit_code == 0 assert "browser-host.example" in list_result.output assert "agent" in list_result.output assert remove_result.exit_code == 0 assert "Removed browser-host.example" in remove_result.output assert remote_registry.load_remotes() == {}