|
| 1 | +import pytest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +import socketio |
| 4 | +from socketio import client |
| 5 | +import wx |
| 6 | +from invesalius.net.remote_control import RemoteControl |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def setup_remote_control(): |
| 10 | + with patch("socketio.Client") as mock_socketio, patch("wx.CallAfter") as mock_callafter: |
| 11 | + mock_sio_instance = MagicMock() |
| 12 | + mock_socketio.return_value = mock_sio_instance |
| 13 | + |
| 14 | + remote_host = "http://localhost:5000" |
| 15 | + remote_control = RemoteControl(remote_host) |
| 16 | + yield remote_control, mock_socketio, mock_callafter |
| 17 | + |
| 18 | + |
| 19 | +def test_initialization(setup_remote_control): |
| 20 | + remote_control, _, _ = setup_remote_control |
| 21 | + |
| 22 | + assert remote_control._remote_host == "http://localhost:5000" |
| 23 | + assert remote_control._connected is False |
| 24 | + assert remote_control._sio is None |
| 25 | + |
| 26 | + |
| 27 | +def test_on_connect(setup_remote_control): |
| 28 | + remote_control, _, _ = setup_remote_control |
| 29 | + |
| 30 | + remote_control._on_connect() |
| 31 | + |
| 32 | + assert remote_control._connected is True |
| 33 | + |
| 34 | + |
| 35 | +def test_on_disconnect(setup_remote_control): |
| 36 | + remote_control,_, _ = setup_remote_control |
| 37 | + |
| 38 | + remote_control._on_disconnect() |
| 39 | + |
| 40 | + assert remote_control._connected is False |
| 41 | + |
| 42 | + |
| 43 | +def test_to_neuronavigation(setup_remote_control): |
| 44 | + remote_control, _, _ = setup_remote_control |
| 45 | + |
| 46 | + with patch("invesalius.pubsub.pub.sendMessage_no_hook") as mock_publisher: |
| 47 | + msg = {"topic": "test_topic", "data": {"key": "value"}} |
| 48 | + remote_control._to_neuronavigation(msg) |
| 49 | + mock_publisher.assert_called_once_with(topicName="test_topic", key="value") |
| 50 | + |
| 51 | + |
| 52 | +def test_to_neuronavigation_wrapper(setup_remote_control): |
| 53 | + remote_control, _, mock_callafter = setup_remote_control |
| 54 | + |
| 55 | + msg = {"topic": "test_topic", "data": {"key": "value"}} |
| 56 | + |
| 57 | + remote_control._to_neuronavigation_wrapper(msg) |
| 58 | + |
| 59 | + mock_callafter.assert_called_once_with(remote_control._to_neuronavigation, msg) |
| 60 | + |
| 61 | + |
| 62 | +def test_connect(setup_remote_control): |
| 63 | + remote_control, mock_socketio, mock_callafter = setup_remote_control |
| 64 | + |
| 65 | + mock_sio_instance = mock_socketio.return_value |
| 66 | + |
| 67 | + with patch("time.sleep", return_value=None): |
| 68 | + |
| 69 | + remote_control._connected = True |
| 70 | + remote_control.connect() |
| 71 | + mock_socketio.assert_called_once() |
| 72 | + |
| 73 | + mock_sio_instance.on.assert_any_call("connect", remote_control._on_connect) |
| 74 | + mock_sio_instance.on.assert_any_call("disconnect", remote_control._on_disconnect) |
| 75 | + mock_sio_instance.on.assert_any_call("to_neuronavigation", remote_control._to_neuronavigation_wrapper) |
| 76 | + |
| 77 | + mock_sio_instance.connect.assert_called_once_with(remote_control._remote_host) |
| 78 | + |
| 79 | + mock_sio_instance.emit.assert_called_once_with("restart_robot_main_loop") |
0 commit comments