"""
This program has been developed by students from the bachelor Computer Science at Utrecht
University within the Software Project course.
© Copyright Utrecht University (Department of Information and Computing Sciences)
"""

from unittest.mock import MagicMock, patch

import zmq

from control_backend.main import setup_sockets


def test_setup_sockets_proxy():
    mock_context = MagicMock()
    mock_pub = MagicMock()
    mock_sub = MagicMock()

    mock_context.socket.side_effect = [mock_pub, mock_sub]

    with patch("zmq.asyncio.Context.instance", return_value=mock_context):
        with patch("zmq.proxy") as mock_proxy:
            setup_sockets()

            mock_pub.bind.assert_called()
            mock_sub.bind.assert_called()
            mock_proxy.assert_called_with(mock_sub, mock_pub)

            # Check cleanup
            mock_pub.close.assert_called()
            mock_sub.close.assert_called()


def test_setup_sockets_proxy_error():
    mock_context = MagicMock()
    mock_pub = MagicMock()
    mock_sub = MagicMock()
    mock_context.socket.side_effect = [mock_pub, mock_sub]

    with patch("zmq.asyncio.Context.instance", return_value=mock_context):
        with patch("zmq.proxy", side_effect=zmq.ZMQError):
            with patch("control_backend.main.logger") as mock_logger:
                setup_sockets()
                mock_logger.warning.assert_called()
                mock_pub.close.assert_called()
                mock_sub.close.assert_called()
