# Path: matl_online/extensions.py

# 
# Path: tests/helpers.py
# def session_id_for_client(client: SocketIOTestClient, namespace: str = '/') -> Optional[str]:
#     """Retrieve the session id for a SocketIOTestClient."""
#     return client.socketio.server.manager.sid_from_eio_sid(client.eio_sid, namespace)


from flask_socketio import SocketIOTestClient
from matl_online.extensions import socketio
from .helpers import session_id_for_client
"""Unit tests for socket interaction between server and client."""




def session(client: SocketIOTestClient):
    """Retrieve a client's session."""
    return socketio.server.environ[client.eio_sid].get('saved_session', {})


class TestSockets:
    """Series of tests to ensure the expected data is passed via sockets."""

    def test_connection(self, socketclient):
        """During initial connection, session ID's should be sent."""
        events = socketclient.get_received()

        assert len(events) == 1
        assert events[0]['namespace'] == '/'
        assert events[0]['name'] == 'connection'

        payload = events[0]['args']

        assert len(payload) == 1
        assert 'session_id' in payload[0]


# Path: matl_online/extensions.py

# 
# Path: tests/helpers.py
# def session_id_for_client(client: SocketIOTestClient, namespace: str = '/') -> Optional[str]:
#     """Retrieve the session id for a SocketIOTestClient."""
#     return client.socketio.server.manager.sid_from_eio_sid(client.eio_sid, namespace)


# Path: tests/test_sockets.py
from flask_socketio import SocketIOTestClient
from matl_online.extensions import socketio
from .helpers import session_id_for_client

"""Unit tests for socket interaction between server and client."""




def session(client: SocketIOTestClient):
    """Retrieve a client's session."""
    return socketio.server.environ[client.eio_sid].get('saved_session', {})


class TestSockets:
    """Series of tests to ensure the expected data is passed via sockets."""

    def test_connection(self, socketclient):
        """During initial connection, session ID's should be sent."""
        events = socketclient.get_received()

        assert len(events) == 1
        assert events[0]['namespace'] == '/'
        assert events[0]['name'] == 'connection'

        payload = events[0]['args']

        assert len(payload) == 1
        assert 'session_id' in payload[0]


        assert payload[0]['session_id'] == session_id_for_client(socketclient)