92 lines
2.4 KiB
Python
92 lines
2.4 KiB
Python
"""Tests for the server module."""
|
|
|
|
import asyncio
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from mudlib import server
|
|
|
|
|
|
def test_port_constant():
|
|
"""Test that PORT is defined correctly."""
|
|
assert server.PORT == 6789
|
|
assert isinstance(server.PORT, int)
|
|
|
|
|
|
def test_shell_exists():
|
|
"""Test that the shell callback exists and is callable."""
|
|
assert callable(server.shell)
|
|
assert asyncio.iscoroutinefunction(server.shell)
|
|
|
|
|
|
def test_run_server_exists():
|
|
"""Test that run_server exists and is callable."""
|
|
assert callable(server.run_server)
|
|
assert asyncio.iscoroutinefunction(server.run_server)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_shell_greets_and_echoes():
|
|
"""Test that shell greets the player and echoes input."""
|
|
reader = AsyncMock()
|
|
writer = MagicMock()
|
|
writer.is_closing.return_value = False
|
|
writer.drain = AsyncMock()
|
|
writer.close = MagicMock()
|
|
|
|
# Simulate user typing "hello" then "quit"
|
|
reader.readline.side_effect = ["hello\r\n", "quit\r\n"]
|
|
|
|
await server.shell(reader, writer)
|
|
|
|
# Check that welcome message was written
|
|
calls = [str(call) for call in writer.write.call_args_list]
|
|
assert any("Welcome" in call for call in calls)
|
|
|
|
# Check that input was echoed
|
|
assert any("hello" in call for call in calls)
|
|
|
|
# Check that goodbye was written
|
|
assert any("Goodbye" in call for call in calls)
|
|
|
|
# Check that close was called
|
|
writer.close.assert_called_once()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_shell_handles_empty_input():
|
|
"""Test that shell handles EOF gracefully."""
|
|
reader = AsyncMock()
|
|
writer = MagicMock()
|
|
writer.is_closing.return_value = False
|
|
writer.drain = AsyncMock()
|
|
writer.close = MagicMock()
|
|
|
|
# Simulate EOF
|
|
reader.readline.return_value = ""
|
|
|
|
await server.shell(reader, writer)
|
|
|
|
# Should close cleanly
|
|
writer.close.assert_called_once()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_shell_handles_quit():
|
|
"""Test that shell exits on quit command."""
|
|
reader = AsyncMock()
|
|
writer = MagicMock()
|
|
writer.is_closing.return_value = False
|
|
writer.drain = AsyncMock()
|
|
writer.close = MagicMock()
|
|
|
|
reader.readline.return_value = "quit\r\n"
|
|
|
|
await server.shell(reader, writer)
|
|
|
|
# Check that goodbye was written
|
|
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()
|