diff --git a/reme/components/client/base_client.py b/reme/components/client/base_client.py index 87d7a35e..a3b34879 100644 --- a/reme/components/client/base_client.py +++ b/reme/components/client/base_client.py @@ -27,15 +27,15 @@ class BaseClient(BaseComponent): def _resolve_service_address(self, host: str | None, port: int | None) -> tuple[str, int]: """Resolve explicit, discovered, or default network coordinates.""" if not (host and port): + fallback_host, fallback_port = REME_DEFAULT_HOST, REME_DEFAULT_PORT if service_info := os.environ.get(REME_SERVICE_INFO): try: data = json.loads(service_info) - host, port = data["host"], data["port"] + fallback_host, fallback_port = data["host"], data["port"] except Exception: self.logger.warning(f"Invalid service info: {service_info}") - host, port = REME_DEFAULT_HOST, REME_DEFAULT_PORT - else: - host, port = REME_DEFAULT_HOST, REME_DEFAULT_PORT + host = host or fallback_host + port = port or fallback_port return normalize_connect_host(host), port async def _start(self) -> None: diff --git a/tests/unit/test_base_client.py b/tests/unit/test_base_client.py new file mode 100644 index 00000000..ada9e8a2 --- /dev/null +++ b/tests/unit/test_base_client.py @@ -0,0 +1,84 @@ +"""Regression tests for BaseClient service address resolution.""" + +import pytest + +from reme.components.client.http_client import HttpClient +from reme.components.client.mcp_client import MCPClient +from reme.constants import REME_DEFAULT_HOST, REME_DEFAULT_PORT, REME_SERVICE_INFO + + +@pytest.fixture(autouse=True) +def clear_service_info(monkeypatch): + """Address resolution runs without service discovery unless opted in.""" + monkeypatch.delenv(REME_SERVICE_INFO, raising=False) + + +def test_explicit_port_only_keeps_port_and_defaults_host(): + """An explicit port is not discarded when the host is omitted.""" + assert HttpClient(port=8123).base_url == f"http://{REME_DEFAULT_HOST}:8123" + + client = MCPClient(port=8123) + assert client.host == REME_DEFAULT_HOST + assert client.port == 8123 + + +def test_explicit_host_only_keeps_host_and_defaults_port(): + """An explicit host is not discarded when the port is omitted.""" + assert HttpClient(host="192.0.2.10").base_url == f"http://192.0.2.10:{REME_DEFAULT_PORT}" + + client = MCPClient(host="192.0.2.10") + assert client.host == "192.0.2.10" + assert client.port == REME_DEFAULT_PORT + + +def test_explicit_fields_win_over_service_discovery(monkeypatch): + """Discovery fills only the missing field; explicit fields are kept.""" + monkeypatch.setenv(REME_SERVICE_INFO, '{"host": "10.0.0.1", "port": 9000}') + + assert HttpClient(port=8123).base_url == "http://10.0.0.1:8123" + assert HttpClient(host="192.0.2.10").base_url == "http://192.0.2.10:9000" + assert HttpClient(host="192.0.2.10", port=8123).base_url == "http://192.0.2.10:8123" + + client = MCPClient(port=8123) + assert client.host == "10.0.0.1" + assert client.port == 8123 + + client = MCPClient(host="192.0.2.10") + assert client.host == "192.0.2.10" + assert client.port == 9000 + + +def test_service_discovery_fills_both_fields_when_nothing_is_explicit(monkeypatch): + """Without explicit arguments the discovered address is used as before.""" + monkeypatch.setenv(REME_SERVICE_INFO, '{"host": "10.0.0.1", "port": 9000}') + + assert HttpClient().base_url == "http://10.0.0.1:9000" + + client = MCPClient() + assert client.host == "10.0.0.1" + assert client.port == 9000 + + +def test_invalid_service_info_keeps_explicit_fields_and_defaults_the_rest(monkeypatch): + """An undecodable discovery payload still preserves explicit fields.""" + monkeypatch.setenv(REME_SERVICE_INFO, "not-json") + + assert HttpClient(port=8123).base_url == f"http://{REME_DEFAULT_HOST}:8123" + assert HttpClient(host="192.0.2.10").base_url == f"http://192.0.2.10:{REME_DEFAULT_PORT}" + assert HttpClient().base_url == f"http://{REME_DEFAULT_HOST}:{REME_DEFAULT_PORT}" + + +def test_incomplete_service_info_keeps_explicit_fields_and_defaults_the_rest(monkeypatch): + """A discovery payload missing host/port keys preserves explicit fields.""" + monkeypatch.setenv(REME_SERVICE_INFO, '{"host": "10.0.0.1"}') + + assert HttpClient(port=8123).base_url == f"http://{REME_DEFAULT_HOST}:8123" + assert HttpClient().base_url == f"http://{REME_DEFAULT_HOST}:{REME_DEFAULT_PORT}" + + +def test_mcp_stdio_transport_skips_network_address_resolution(): + """The stdio transport never resolves a network service address.""" + client = MCPClient(transport="stdio", command="python", args=["server.py"]) + + assert not hasattr(client, "host") + assert not hasattr(client, "port")