Add persistent web chat log

This commit is contained in:
Tanner committed 2026-11-14 22:02:08 +00:00
1 parent 9be381c2de
commit 492238aa22
12 files changed
+506 -51

No files matched your search

+59 -1
View File
@@ -8,16 +8,22 @@ from munch import Munch
from minecraft.networking.modern import ClientboundPlayerChatPacket
from mosfet.commands import Commands
from mosfet.connect import component_text
from mosfet.connect import _system_chat_log_entry, component_text
from mosfet.protocol.modern import (
ModernChatManager, parse_incoming_private_message,
)
class FakeConnection:
def __init__(self):
self.packets = []
def register_packet_listener(self, *_args, **_kwargs):
pass
def write_packet(self, packet, force=False):
self.packets.append((packet, force))
class FakeChat:
def __init__(self):
@@ -42,6 +48,40 @@ class TestModernChat(unittest.TestCase):
self.assertEqual(parse_incoming_private_message(
'tanner -> me: 1ping'), ('tanner', '1ping'))
def test_death_message_translations_are_readable(self):
self.assertEqual(component_text({
'translate': 'death.attack.player',
'with': ['mosfet1', 'tanner'],
}), 'mosfet1 was slain by tanner')
self.assertEqual(component_text({
'translate': 'death.attack.player.item',
'with': ['mosfet1', 'tanner', {'text': 'diamond sword'}],
}), 'mosfet1 was slain by tanner using diamond sword')
self.assertEqual(component_text({
'translate': 'death.attack.thorns.item',
'with': ['mosfet1', 'zombie', {'text': 'thorns'}],
}), 'mosfet1 was killed by thorns while trying to hurt zombie')
def test_player_join_and_leave_translations_are_readable(self):
self.assertEqual(component_text({
'translate': 'multiplayer.player.joined',
'with': [{'text': 'tanner'}],
}), 'tanner joined the game')
self.assertEqual(component_text({
'translate': 'multiplayer.player.left',
'with': [{'text': 'tanner'}],
}), 'tanner left the game')
self.assertEqual(_system_chat_log_entry('tanner joined the game'), (
'join', 'tanner', 'tanner joined the game'))
self.assertEqual(_system_chat_log_entry('tanner left the game'), (
'leave', 'tanner', 'tanner left the game'))
def test_system_chat_log_recognizes_private_messages(self):
self.assertEqual(_system_chat_log_entry(
'tanner whispers to you: hello'), ('dm', 'tanner', 'hello'))
self.assertEqual(_system_chat_log_entry('Server restarting'), (
'system', None, 'Server restarting'))
def test_player_chat_console_uses_name_but_keeps_uuid_for_commands(self):
player_uuid = 'f6126022-2c68-37f7-be93-aaa551374663'
state = Munch(
@@ -62,6 +102,9 @@ class TestModernChat(unittest.TestCase):
self.assertEqual(received, [
('CHAT', player_uuid, 'tanner: !ping'),
])
self.assertEqual(state.chat_log[0]['type'], 'chat')
self.assertEqual(state.chat_log[0]['sender'], 'tanner')
self.assertEqual(state.chat_log[0]['message'], '!ping')
def test_player_chat_target_name_marks_dm_and_routes_reply(self):
player_uuid = 'f6126022-2c68-37f7-be93-aaa551374663'
@@ -84,6 +127,21 @@ class TestModernChat(unittest.TestCase):
self.assertEqual(received, [
('DM', player_uuid, 'tanner -> me: !ping', 'tanner'),
])
self.assertEqual(state.chat_log[0]['type'], 'dm')
def test_outgoing_chat_is_logged_but_commands_are_not(self):
connection = FakeConnection()
state = Munch(connection=connection, name='mosfet1', reload_lock=None)
manager = ModernChatManager(state)
manager.send('hello server')
manager.send('/login secret')
self.assertEqual(len(state.chat_log), 1)
self.assertEqual(state.chat_log[0]['type'], 'outgoing')
self.assertEqual(state.chat_log[0]['sender'], 'mosfet1')
self.assertEqual(state.chat_log[0]['message'], 'hello server')
self.assertEqual(len(connection.packets), 2)
def test_player_chat_parser_reads_optional_target_name(self):
def varint(value):
+15 -1
View File
@@ -69,7 +69,7 @@ class TestWebInventoryAPI(unittest.TestCase):
key: self.g.get(key) for key in
('connected', 'game', 'inv', 'chest_viewers', 'time', 'time_base',
'weather', 'rain_level', 'thunder_level', 'chunks', 'pos', 'yaw',
'path')
'path', 'chat_log')
}
self.game = FakeGame()
self.g.connected = True
@@ -187,6 +187,20 @@ class TestWebInventoryAPI(unittest.TestCase):
self.assertEqual(data['radius_x'], 48)
self.assertEqual(data['radius_z'], 36)
def test_global_api_exposes_chat_log_events(self):
self.g.chat_log = [{
'id': '123', 'timestamp': 123.0, 'type': 'join',
'sender': 'tanner', 'message': 'tanner joined the game',
}]
response = self.client.get('/api/global')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json()['chat_log'][0], {
'id': '123', 'timestamp': 123.0, 'type': 'join',
'sender': 'tanner', 'message': 'tanner joined the game',
})
def test_global_api_exposes_world_time_ticks(self):
self.g.time = 12345.5
self.g.time_base = 12345.5