"""Unit tests for handler/client.py"""

from unittest.mock import patch, MagicMock
_mock_env = MagicMock()
_mock_env.register.proxy_socket = "/var/run/ieops/proxy.sock"
_mock_env.register.brslet_socket = "/var/run/ieops/brslet.sock"
with (
    patch("gmi_ieops.handler.client.uvicorn_logger", MagicMock()),
    patch("gmi_ieops.handler.client.env", _mock_env),
):
    from gmi_ieops.handler.client import _build_unix_url, Payload, Response, _get_socket_paths
    import gmi_ieops.handler.client as _mod
_mod.env = _mock_env


class TestClient:

    def test_build_unix_url(self):
        url = _build_unix_url("/var/run/ieops/proxy.sock", "register")
        assert url == "http+unix://%2Fvar%2Frun%2Fieops%2Fproxy.sock/register"
        url2 = _build_unix_url("/tmp/test.sock", "api/model/register")
        assert url2.startswith("http+unix://") and url2.endswith("/api/model/register")
        assert "%2F" in url2

    def test_response(self):
        r = Response(message="ok", data={"k": "v"})
        assert r.message == "ok" and r.data == {"k": "v"}
        assert "ok" in repr(r)
        assert Response(message="e").data is None

    def test_socket_paths(self):
        paths = _get_socket_paths()
        assert paths["proxy"] == "/var/run/ieops/proxy.sock"
        assert paths["brslet"] == "/var/run/ieops/brslet.sock"

    def test_payload(self):
        p: Payload = {"path": "api/test", "payload": {"key": "value"}}
        assert p["path"] == "api/test"
