|
10 | 10 | from ephys_link.back_end.platform_handler import PlatformHandler |
11 | 11 | from ephys_link.back_end.server import Server |
12 | 12 | from ephys_link.front_end.console import Console |
13 | | -from ephys_link.utils.constants import PROXY_CLIENT_NOT_INITIALIZED_ERROR, SERVER_NOT_INITIALIZED_ERROR |
| 13 | +from ephys_link.utils.constants import ( |
| 14 | + PROXY_CLIENT_NOT_INITIALIZED_ERROR, |
| 15 | + SERVER_NOT_INITIALIZED_ERROR, |
| 16 | + cannot_connect_as_client_is_already_connected_error, |
| 17 | + client_disconnected_without_being_connected_error, |
| 18 | +) |
14 | 19 | from tests.conftest import DUMMY_STRING, DUMMY_STRING_LIST |
15 | 20 |
|
16 | 21 |
|
@@ -176,3 +181,71 @@ def run_coroutine(coroutine: Awaitable[None]) -> None: |
176 | 181 | assert init_error.value.args[0] == PROXY_CLIENT_NOT_INITIALIZED_ERROR |
177 | 182 | patched_connect.assert_not_awaited() # pyright: ignore[reportUnusedCallResult] |
178 | 183 | patched_wait.assert_not_awaited() # pyright: ignore[reportUnusedCallResult] |
| 184 | + |
| 185 | + @pytest.mark.asyncio |
| 186 | + async def test_connect_success(self, server: Server, console: Console, mocker: MockerFixture) -> None: |
| 187 | + """Server should allow connection if there is no existing connection.""" |
| 188 | + # Spy console. |
| 189 | + spied_info_print = mocker.spy(console, "info_print") |
| 190 | + |
| 191 | + # Act. |
| 192 | + result = await server.connect(DUMMY_STRING, DUMMY_STRING) |
| 193 | + |
| 194 | + # Assert. |
| 195 | + spied_info_print.assert_any_call("CONNECTION REQUEST", DUMMY_STRING) |
| 196 | + assert server._client_sid == DUMMY_STRING # noqa: SLF001 # pyright: ignore[reportPrivateUsage] |
| 197 | + spied_info_print.assert_any_call("CONNECTION GRANTED", DUMMY_STRING) |
| 198 | + assert result |
| 199 | + |
| 200 | + @pytest.mark.asyncio |
| 201 | + async def test_connect_failure(self, server: Server, console: Console, mocker: MockerFixture) -> None: |
| 202 | + """Server should not allow connection if there is an existing connection.""" |
| 203 | + # Spy console. |
| 204 | + spied_info_print = mocker.spy(console, "info_print") |
| 205 | + spied_error_print = mocker.spy(console, "error_print") |
| 206 | + |
| 207 | + # Mock client sid. |
| 208 | + _ = mocker.patch.object(server, "_client_sid", new=DUMMY_STRING) |
| 209 | + |
| 210 | + # Act. |
| 211 | + result = await server.connect(DUMMY_STRING, DUMMY_STRING) |
| 212 | + |
| 213 | + # Assert. |
| 214 | + spied_info_print.assert_any_call("CONNECTION REQUEST", DUMMY_STRING) |
| 215 | + spied_error_print.assert_called_once_with( |
| 216 | + "CONNECTION REFUSED", cannot_connect_as_client_is_already_connected_error(DUMMY_STRING, DUMMY_STRING) |
| 217 | + ) |
| 218 | + assert not result |
| 219 | + |
| 220 | + @pytest.mark.asyncio |
| 221 | + async def test_disconnect_success(self, server: Server, console: Console, mocker: MockerFixture) -> None: |
| 222 | + """Server should allow disconnection if the SID matches the existing connection.""" |
| 223 | + # Spy console. |
| 224 | + spied_info_print = mocker.spy(console, "info_print") |
| 225 | + |
| 226 | + # Mock client sid. |
| 227 | + _ = mocker.patch.object(server, "_client_sid", new=DUMMY_STRING) |
| 228 | + |
| 229 | + # Act. |
| 230 | + await server.disconnect(DUMMY_STRING) |
| 231 | + |
| 232 | + # Assert. |
| 233 | + spied_info_print.assert_any_call("DISCONNECTION REQUEST", DUMMY_STRING) |
| 234 | + assert server._client_sid == "" # noqa: SLF001 # pyright: ignore[reportPrivateUsage] |
| 235 | + spied_info_print.assert_any_call("DISCONNECTED", DUMMY_STRING) |
| 236 | + |
| 237 | + @pytest.mark.asyncio |
| 238 | + async def test_disconnect_failure(self, server: Server, console: Console, mocker: MockerFixture) -> None: |
| 239 | + """Server should not allow disconnection if there is no existing connection.""" |
| 240 | + # Spy console. |
| 241 | + spied_info_print = mocker.spy(console, "info_print") |
| 242 | + spied_error_print = mocker.spy(console, "error_print") |
| 243 | + |
| 244 | + # Act. |
| 245 | + await server.disconnect(DUMMY_STRING) |
| 246 | + |
| 247 | + # Assert. |
| 248 | + spied_info_print.assert_any_call("DISCONNECTION REQUEST", DUMMY_STRING) |
| 249 | + spied_error_print.assert_called_once_with( |
| 250 | + "DISCONNECTION", client_disconnected_without_being_connected_error(DUMMY_STRING) |
| 251 | + ) |
0 commit comments