plugins/tools/test_lobby_arrakis_view_distance.py
unimatrixgit 8923dbe0d0
All checks were successful
Lobby Arrakis View Distance / configuration (push) Successful in 5s
DuneRealms Plugin CI / plugins (push) Successful in 2m53s
Handle Paper's saved exit-130 service state during Lobby restart
2026-11-30 18:20:57 +01:00

42 lines
4.2 KiB
Python

import importlib.util,unittest,tempfile,sys
from unittest.mock import patch
from types import SimpleNamespace
from pathlib import Path
spec=importlib.util.spec_from_file_location('view',Path(__file__).with_name('lobby-arrakis-view-distance.py'));module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
class WorldViewDistance(unittest.TestCase):
def test_adds_only_world_override(self):
before=b'# header\nworld-settings:\n default:\n view-distance: default\n simulation-distance: default\n guild_lobby:\n view-distance: 4\nsettings:\n save-user-cache-on-stop-only: false\n'
after,prior=module.patch(before);self.assertEqual(prior,'inherited');self.assertEqual(after.replace(b' lobby_arrakis:\n view-distance: 20\n',b''),before);self.assertEqual(module.patch(after)[0],after)
def test_preserves_other_world_values_and_line_endings(self):
before=b'world-settings:\r\n lobby_arrakis:\r\n view-distance: 5\r\n simulation-distance: 3\r\n other:\r\n view-distance: 7\r\n'
after,prior=module.patch(before);self.assertEqual(prior,'5');self.assertEqual(after,before.replace(b'view-distance: 5',b'view-distance: 20'))
def test_ambiguous_config_refused(self):
for value in (b'world-settings:\n lobby_arrakis:\n lobby_arrakis:\n',b'world-settings:\n lobby_arrakis:\n view-distance: 5\n view-distance: 7\n',b'world-settings:\nworld-settings:\n'):
with self.assertRaises(ValueError):module.patch(value)
def test_global_property_changes_with_other_world_default_preserved(self):
props=b'# Paper timestamp\nview-distance=5\nsimulation-distance=3\nresource-pack=https\\://example.test/pack.zip\n'
after,prior=module.properties(props);self.assertEqual(prior,5);self.assertEqual(after,props.replace(b'view-distance=5',b'view-distance=20'))
config=b'world-settings:\n default:\n view-distance: default\n simulation-distance: default\n other:\n view-distance: 7\n'
updated=module.pin_default(module.patch(config)[0],prior)
self.assertIn(b' default:\n view-distance: 5\n simulation-distance: default\n',updated);self.assertIn(b' lobby_arrakis:\n view-distance: 20\n',updated);self.assertIn(b' other:\n view-distance: 7\n',updated)
def test_stale_config_abort_restarts_without_overwriting_newer_files(self):
with tempfile.TemporaryDirectory() as name:
root=Path(name);(root/'lobby_base/dimensions/minecraft/lobby_arrakis').mkdir(parents=True)
before=b'world-settings:\n default:\n view-distance: default\n';(root/'spigot.yml').write_bytes(before);(root/'server.properties').write_bytes(b'view-distance=5\nsimulation-distance=3\n')
calls=[]
def control(*args):
calls.append(args)
if args[0]=='stop':(root/'server.properties').write_bytes(b'view-distance=5\nsimulation-distance=3\nmotd=newer\n')
return SimpleNamespace(returncode=0,stdout='inactive' if any(c[0]=='stop' for c in calls) else 'active')
with patch.object(module,'ROOT',root),patch.object(module,'RECEIPT_ROOT',root/'ops'),patch.object(module,'control',control),patch.object(sys,'argv',['helper','--apply']):
with self.assertRaisesRegex(RuntimeError,'no files written'):module.main()
self.assertEqual((root/'spigot.yml').read_bytes(),before);self.assertIn(b'motd=newer',(root/'server.properties').read_bytes());self.assertIn(('start',module.UNIT),calls)
def test_paper_exit_130_failed_unit_is_safely_stopped_and_restarted(self):
with tempfile.TemporaryDirectory() as name:
root=Path(name);(root/'lobby_base/dimensions/minecraft/lobby_arrakis').mkdir(parents=True);(root/'spigot.yml').write_bytes(b'world-settings:\n default:\n view-distance: default\n');(root/'server.properties').write_bytes(b'view-distance=5\nsimulation-distance=3\n')
calls=[]
def control(*args):
calls.append(args);return SimpleNamespace(returncode=0,stdout='failed' if any(c[0]=='stop' for c in calls) else 'active')
with patch.object(module,'ROOT',root),patch.object(module,'RECEIPT_ROOT',root/'ops'),patch.object(module,'control',control),patch.object(module,'write',lambda p,data,meta:p.write_bytes(data)),patch.object(sys,'argv',['helper','--apply']):module.main()
self.assertIn(b'view-distance=20',(root/'server.properties').read_bytes());self.assertIn(('start',module.UNIT),calls)
if __name__=='__main__':unittest.main()