import json from pathlib import Path import pytest from browser_cli.client import BrowserNotConnected, _resolve_socket def test_resolve_socket_raises_when_registry_missing(monkeypatch): monkeypatch.delenv("BROWSER_CLI_PROFILE", raising=False) monkeypatch.setattr("browser_cli.client.REGISTRY_PATH", Path("/nonexistent/browser-cli-registry.json")) with pytest.raises(BrowserNotConnected, match="Cannot resolve a browser socket automatically"): _resolve_socket() def test_resolve_socket_uses_only_active_registry_entry(monkeypatch, tmp_path): monkeypatch.delenv("BROWSER_CLI_PROFILE", raising=False) socket_path = tmp_path / "browser.sock" socket_path.write_text("") registry_path = tmp_path / "registry.json" registry_path.write_text(json.dumps({"abc-uuid": str(socket_path)})) monkeypatch.setattr("browser_cli.client.REGISTRY_PATH", registry_path) assert _resolve_socket() == str(socket_path) def test_resolve_socket_raises_when_multiple_active_entries(monkeypatch, tmp_path): monkeypatch.delenv("BROWSER_CLI_PROFILE", raising=False) first_socket = tmp_path / "one.sock" second_socket = tmp_path / "two.sock" first_socket.write_text("") second_socket.write_text("") registry_path = tmp_path / "registry.json" registry_path.write_text(json.dumps({"uuid-1": str(first_socket), "uuid-2": str(second_socket)})) monkeypatch.setattr("browser_cli.client.REGISTRY_PATH", registry_path) with pytest.raises(BrowserNotConnected, match="Multiple browser instances are active: uuid-1, uuid-2"): _resolve_socket()