68 lines
1.9 KiB
Python
68 lines
1.9 KiB
Python
import unittest
|
|
|
|
from munch import Munch
|
|
from minecraft.networking.packets import PacketBuffer
|
|
from minecraft.networking.modern import ClientTickEnd
|
|
|
|
from mosfet.commands import Commands
|
|
from mosfet.game import Game
|
|
from mosfet.protocol.modern import ClientCommandPacket
|
|
|
|
|
|
class FakeConnection:
|
|
def __init__(self):
|
|
self.packets = []
|
|
|
|
def write_packet(self, packet, force=False):
|
|
self.packets.append((packet, force))
|
|
|
|
|
|
class FakeChat:
|
|
def __init__(self):
|
|
self.handler = None
|
|
self.messages = []
|
|
|
|
def set_handler(self, handler):
|
|
self.handler = handler
|
|
|
|
def send(self, message):
|
|
self.messages.append(message)
|
|
|
|
|
|
class TestRespawn(unittest.TestCase):
|
|
def test_client_command_encodes_perform_respawn(self):
|
|
packet = ClientCommandPacket(action_id=0)
|
|
buffer = PacketBuffer()
|
|
packet.write_fields(buffer)
|
|
self.assertEqual(packet.id, 0x0C)
|
|
self.assertEqual(buffer.get_writable(), b'\x00')
|
|
|
|
def test_game_respawn_sends_modern_client_command_and_tick_end(self):
|
|
connection = FakeConnection()
|
|
game = Game.__new__(Game)
|
|
game.g = Munch(connection=connection)
|
|
|
|
game.respawn()
|
|
|
|
packet, forced = connection.packets[0]
|
|
self.assertIsInstance(packet, ClientCommandPacket)
|
|
self.assertEqual(packet.action_id, 0)
|
|
self.assertTrue(forced)
|
|
self.assertIsInstance(connection.packets[1][0], ClientTickEnd)
|
|
self.assertIn('respawn_requested_at', game.g)
|
|
|
|
def test_respawn_command_is_enabled(self):
|
|
chat = FakeChat()
|
|
calls = []
|
|
game = Munch(respawn=lambda: calls.append('respawn'))
|
|
commands = Commands(Munch(name='mosfet1', chat=chat, game=game))
|
|
|
|
commands.handle_chat(('SYSTEM', None, 'server: 1respawn'))
|
|
|
|
self.assertEqual(calls, ['respawn'])
|
|
self.assertEqual(chat.messages, ['ok'])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|