"""Tests for the server module.""" import asyncio from unittest.mock import AsyncMock, MagicMock, patch import pytest from mudlib import server def test_port_constant(): assert server.PORT == 6789 assert isinstance(server.PORT, int) def test_shell_exists(): assert callable(server.shell) assert asyncio.iscoroutinefunction(server.shell) def test_run_server_exists(): assert callable(server.run_server) assert asyncio.iscoroutinefunction(server.run_server) @pytest.mark.asyncio async def test_shell_greets_and_echoes(): reader = AsyncMock() writer = MagicMock() writer.is_closing.return_value = False writer.drain = AsyncMock() writer.close = MagicMock() readline = "mudlib.server.readline2" with patch(readline, new_callable=AsyncMock) as mock_readline: mock_readline.side_effect = ["hello", "quit"] await server.shell(reader, writer) calls = [str(call) for call in writer.write.call_args_list] assert any("Welcome" in call for call in calls) assert any("hello" in call for call in calls) assert any("Goodbye" in call for call in calls) writer.close.assert_called_once() @pytest.mark.asyncio async def test_shell_handles_eof(): reader = AsyncMock() writer = MagicMock() writer.is_closing.return_value = False writer.drain = AsyncMock() writer.close = MagicMock() readline = "mudlib.server.readline2" with patch(readline, new_callable=AsyncMock) as mock_readline: mock_readline.return_value = None await server.shell(reader, writer) writer.close.assert_called_once() @pytest.mark.asyncio async def test_shell_handles_quit(): reader = AsyncMock() writer = MagicMock() writer.is_closing.return_value = False writer.drain = AsyncMock() writer.close = MagicMock() readline = "mudlib.server.readline2" with patch(readline, new_callable=AsyncMock) as mock_readline: mock_readline.return_value = "quit" await server.shell(reader, writer) calls = [str(call) for call in writer.write.call_args_list] assert any("Goodbye" in call for call in calls) writer.close.assert_called_once()