mud/tests/test_quit.py
Jared Miller be63a1cbde
Extract shared test fixtures to conftest.py
Moved common test fixtures (mock_writer, mock_reader, test_zone, player,
nearby_player, clear_state) from individual test files into a shared
conftest.py. This eliminates duplication across test_power.py, test_sleep.py,
test_combat_zaxis.py, test_quit.py, test_stamina_cues.py, and
test_stamina_cue_wiring.py.

Some test files override specific fixtures where they need custom behavior
(e.g., test_quit.py adds a close method to mock_writer, stamina tests use
smaller zones and custom player positions).
2026-02-14 01:00:37 -05:00

48 lines
1.4 KiB
Python

"""Tests for quit command."""
from unittest.mock import MagicMock, patch
import pytest
from mudlib.commands.quit import cmd_quit
from mudlib.player import players
@pytest.fixture
def mock_writer():
"""Override conftest mock_writer to add close method for quit tests."""
from unittest.mock import AsyncMock
writer = MagicMock()
writer.write = MagicMock()
writer.drain = AsyncMock()
writer.close = MagicMock()
return writer
@pytest.mark.asyncio
async def test_quit_blocked_during_combat(player):
"""Test quit is blocked when player is in combat mode."""
player.mode_stack.append("combat")
await cmd_quit(player, "")
player.writer.write.assert_called_once_with("You can't quit during combat!\r\n")
player.writer.close.assert_not_called()
assert player.name in players # Still in player registry
@pytest.mark.asyncio
async def test_quit_works_when_not_in_combat(player):
"""Test quit works normally when not in combat."""
# Player starts in "normal" mode by default
with patch("mudlib.commands.quit.save_player"):
await cmd_quit(player, "")
# Should write goodbye message
player.writer.write.assert_called_with("Goodbye!\r\n")
player.writer.drain.assert_called_once()
player.writer.close.assert_called_once()
assert player.name not in players # Removed from registry
assert player.location is None # Removed from zone