From ec261217dbce13ea7a53e5d6468e611ef510a687 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sat, 5 Oct 2024 17:17:03 +0100 Subject: [PATCH] feat: add more tests --- src/endstone_test/event_listener.py | 4 ++++ src/endstone_test/tests/on_load/test_block.py | 7 ++++++ .../tests/on_load/test_command.py | 5 +++++ .../tests/on_load/test_plugin.py | 22 +++++++++++++++++++ .../tests/on_load/test_scoreboard.py | 10 +++++++-- .../tests/on_load/test_server.py | 7 ++++-- 6 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/endstone_test/event_listener.py b/src/endstone_test/event_listener.py index 1c8f79d..27de9b5 100644 --- a/src/endstone_test/event_listener.py +++ b/src/endstone_test/event_listener.py @@ -99,6 +99,10 @@ def on_thunder_change(self, event: ThunderChangeEvent): def on_weather_change(self, event: WeatherChangeEvent): self._plugin.logger.info(f"Weather state changed to {event.to_weather_state}") + @event_handler + def on_server_command(self, event: ServerCommandEvent): + self._plugin.logger.info(f"{event.sender.name} executed command: {event.command}") + @event_handler def on_server_list_ping(self, event: ServerListPingEvent): event.motd = ColorFormat.BOLD + event.motd diff --git a/src/endstone_test/tests/on_load/test_block.py b/src/endstone_test/tests/on_load/test_block.py index cf786b8..222dacd 100644 --- a/src/endstone_test/tests/on_load/test_block.py +++ b/src/endstone_test/tests/on_load/test_block.py @@ -22,3 +22,10 @@ def test_create_block_data_custom_block_states(server: Server) -> None: assert "ground_sign_direction" in block_data.block_states assert block_data.block_states["ground_sign_direction"] == 8 assert "block_light_level" not in block_data.block_states + + +def test_get_block_outside_world_boundaries(server: Server) -> None: + with pytest.raises(RuntimeError) as exec_info: + server.level.dimensions[0].get_block_at(x=0, y=-256, z=0) + + assert "outside of the world boundaries" in str(exec_info.value) diff --git a/src/endstone_test/tests/on_load/test_command.py b/src/endstone_test/tests/on_load/test_command.py index de29814..c64c542 100644 --- a/src/endstone_test/tests/on_load/test_command.py +++ b/src/endstone_test/tests/on_load/test_command.py @@ -19,3 +19,8 @@ def test_command_executor(plugin: Plugin, command: PluginCommand) -> None: executor = TestCommandExecutor() command.executor = executor assert command.executor is executor + + +def test_server_command_sender(plugin: Plugin) -> None: + server = plugin.server + assert server.command_sender.name == "Server" diff --git a/src/endstone_test/tests/on_load/test_plugin.py b/src/endstone_test/tests/on_load/test_plugin.py index 46eecf2..0737c1c 100644 --- a/src/endstone_test/tests/on_load/test_plugin.py +++ b/src/endstone_test/tests/on_load/test_plugin.py @@ -1,8 +1,30 @@ from endstone.plugin import Plugin from endstone_test import EndstoneTest +from endstone.command import CommandSenderWrapper def test_instance(plugin: Plugin): assert plugin is not None assert isinstance(plugin, EndstoneTest) + + +def test_schedule_task(plugin: Plugin) -> None: + # case study: https://github.com/EndstoneMC/endstone/issues/31 + server = plugin.server + assert server.dispatch_command(server.command_sender, "save hold") + + # since /save hold takes time to save, we run /save query in a delayed task to avoid blocking the thread + def save_query(): + messages = [] + sender = CommandSenderWrapper(server.command_sender, + on_message=lambda msg: messages.extend([msg.translate, *msg.with_])) + ready = server.dispatch_command(sender, "save query") + if not ready: + server.scheduler.run_task(plugin, save_query, delay=5) + return + + assert 'commands.save-all.success' in messages + assert server.dispatch_command(server.command_sender, "save resume") + + server.scheduler.run_task(plugin, save_query, delay=5) diff --git a/src/endstone_test/tests/on_load/test_scoreboard.py b/src/endstone_test/tests/on_load/test_scoreboard.py index 8d48dd0..d94e4fd 100644 --- a/src/endstone_test/tests/on_load/test_scoreboard.py +++ b/src/endstone_test/tests/on_load/test_scoreboard.py @@ -17,14 +17,19 @@ def scoreboard(server: Server) -> Scoreboard: @pytest.fixture(scope="function", autouse=True) def before_each_after_each(server: Server, scoreboard: Scoreboard): # Before each test run - server.dispatch_command(server.command_sender, "scoreboard objectives remove test_objective") + objective = scoreboard.get_objective("test_objective") + if objective is not None: + server.dispatch_command(server.command_sender, "scoreboard objectives remove test_objective") + objective = scoreboard.get_objective("test_objective") assert objective is None yield # After each test run - server.dispatch_command(server.command_sender, "scoreboard objectives remove test_objective") + objective = scoreboard.get_objective("test_objective") + if objective is not None: + server.dispatch_command(server.command_sender, "scoreboard objectives remove test_objective") objective = scoreboard.get_objective("test_objective") assert objective is None @@ -35,6 +40,7 @@ def test_add_objective(server: Server, scoreboard: Scoreboard) -> None: assert objective.criteria.name == "dummy" assert objective.display_name == objective.name assert objective.display_slot is None + assert objective.sort_order is None assert objective.name == "test_objective" assert objective.scoreboard is scoreboard diff --git a/src/endstone_test/tests/on_load/test_server.py b/src/endstone_test/tests/on_load/test_server.py index ee05194..65f0ab4 100644 --- a/src/endstone_test/tests/on_load/test_server.py +++ b/src/endstone_test/tests/on_load/test_server.py @@ -1,6 +1,9 @@ +import time + import pytest -from endstone import Server, __minecraft_version__ +from endstone import Server, Translatable, __minecraft_version__ from endstone.plugin import Plugin +from endstone.command import CommandSenderWrapper @pytest.fixture @@ -19,7 +22,7 @@ def test_server_level(server: Server) -> None: def test_dispatch_command(server: Server) -> None: - server.dispatch_command(server.command_sender, "say Hello, World!") + assert server.dispatch_command(server.command_sender, "say Hello, World!") def test_max_players(server: Server) -> None: