import platform

import pytest

from astropy.samp import conf
from astropy.samp.hub import SAMPHubServer
from astropy.samp.hub_proxy import SAMPHubProxy


def setup_module(module):
    conf.use_internet = False


@pytest.mark.skipif(platform.system() == "Darwin", reason="Takes too long on OSX")
class TestHubProxy:
    def setup_method(self, method):
        self.hub = SAMPHubServer(web_profile=False, mode="multiple", pool_size=1)
        self.hub.start()

        self.proxy = SAMPHubProxy()
        self.proxy.connect(hub=self.hub, pool_size=1)

    def teardown_method(self, method):
        if self.proxy.is_connected:
            self.proxy.disconnect()

        self.hub.stop()

    def test_is_connected(self):
        assert self.proxy.is_connected

    def test_disconnect(self):
        self.proxy.disconnect()

    def test_ping(self):
        self.proxy.ping()

    def test_registration(self):
        result = self.proxy.register(self.proxy.lockfile["samp.secret"])
        self.proxy.unregister(result["samp.private-key"])


@pytest.mark.skipif(platform.system() == "Darwin", reason="Takes too long on OSX")
def test_custom_lockfile(tmp_path):
    lockfile = str(tmp_path / ".samptest")

    hub = SAMPHubServer(web_profile=False, lockfile=lockfile, pool_size=1)
    hub.start()

    proxy = SAMPHubProxy()
    proxy.connect(hub=hub, pool_size=1)

    hub.stop()
