Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

test: move minecraft suites to the extension repo, adjust ratchet

Juan Denis73de373b8010
diff --git a/backend/tests/BASELINE_COUNT b/backend/tests/BASELINE_COUNT
index beb18d70..42fc6af6 100644
--- a/backend/tests/BASELINE_COUNT
+++ b/backend/tests/BASELINE_COUNT
@@ -1 +1 @@
-3218
+3197
diff --git a/backend/tests/test_gamekit_minecraft.py b/backend/tests/test_gamekit_minecraft.py
deleted file mode 100644
index 53b399a2..00000000
--- a/backend/tests/test_gamekit_minecraft.py
+++ /dev/null
@@ -1,271 +0,0 @@
-"""gamekit framework unit tests (plan 53 Phase 1).
-
-gamekit is the dependency-free, extractable game-server framework inside the
-serverkit-minecraft extension (D1). It ships no ServerKit imports, so these tests
-load it straight off disk (its backend dir on sys.path) and exercise the four
-adapters with a scripted fake RCON socket, fixture logs, and temp worlds — no
-Docker, no live server. The Docker create/console/backup wiring rides these
-adapters and is verified separately on the dev box.
-"""
-import datetime
-import json
-import os
-import struct
-import sys
-
-import pytest
-
-_REPO_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
-_MC = os.path.join(_REPO_ROOT, 'builtin-extensions', 'serverkit-minecraft')
-_MC_BACKEND = os.path.join(_MC, 'backend')
-if _MC_BACKEND not in sys.path:
- sys.path.insert(0, _MC_BACKEND)
-
-from gamekit import rcon, log_events, config_form, save_backup # noqa: E402
-
-
-# --------------------------------------------------------------------------- #
-# RCON (fake socket, no network)
-# --------------------------------------------------------------------------- #
-
-class FakeRconSocket:
- """Scripted Source-RCON server over the socket interface RconClient uses."""
-
- def __init__(self, password='secret', responses=None, fail_auth=False):
- self.password = password
- self.fail_auth = fail_auth
- self.responses = responses or {}
- self._out = b''
- self.sent_commands = []
- self.closed = False
-
- def settimeout(self, _t):
- pass
-
- def connect(self, _addr):
- pass
-
- def close(self):
- self.closed = True
-
- def sendall(self, data):
- (length,) = struct.unpack('<i', data[:4])
- payload = data[4:4 + length]
- req_id, req_type = struct.unpack('<ii', payload[:8])
- body = payload[8:-2].decode('utf-8')
- if req_type == rcon.SERVERDATA_AUTH:
- ok = (not self.fail_auth) and body == self.password
- echo_id = req_id if ok else -1
- self._out += rcon.encode_packet(echo_id, rcon.SERVERDATA_AUTH_RESPONSE, '')
- elif req_type == rcon.SERVERDATA_EXECCOMMAND:
- self.sent_commands.append(body)
- resp = self.responses.get(body, f'ran: {body}')
- self._out += rcon.encode_packet(req_id, rcon.SERVERDATA_RESPONSE_VALUE, resp)
-
- def recv(self, n):
- chunk, self._out = self._out[:n], self._out[n:]
- return chunk
-
-
-def test_rcon_auth_and_command_roundtrip():
- sock = FakeRconSocket(password='secret',
- responses={'list': 'There are 2 of a max of 20 players online: Steve, Alex'})
- with rcon.RconClient(password='secret', sock=sock) as rc:
- out = rc.command('list')
- assert 'Steve' in out and 'Alex' in out
- assert sock.sent_commands == ['list']
- assert sock.closed is True
-
-
-def test_rcon_command_before_auth_raises():
- sock = FakeRconSocket(password='secret')
- rc = rcon.RconClient(password='secret', sock=sock).connect()
- with pytest.raises(rcon.RconError):
- rc.command('list')
-
-
-def test_rcon_auth_failure_raises():
- sock = FakeRconSocket(password='secret', fail_auth=True)
- rc = rcon.RconClient(password='wrong', sock=sock).connect()
- with pytest.raises(rcon.RconAuthError):
- rc.authenticate()
-
-
-def test_rcon_packet_framing_is_little_endian():
- pkt = rcon.encode_packet(7, rcon.SERVERDATA_EXECCOMMAND, 'say hi')
- (length,) = struct.unpack('<i', pkt[:4])
- assert length == len(pkt) - 4
- req_id, req_type = struct.unpack('<ii', pkt[4:12])
- assert req_id == 7 and req_type == rcon.SERVERDATA_EXECCOMMAND
- assert pkt.endswith(b'\x00\x00')
-
-
-# --------------------------------------------------------------------------- #
-# log_events
-# --------------------------------------------------------------------------- #
-
-def test_java_log_events_in_order():
- lines = [
- '[12:34:56] [Server thread/INFO]: Done (12.345s)! For help, type "help"',
- '[12:35:01] [Server thread/INFO]: Steve joined the game',
- '[12:36:10] [Server thread/INFO]: <Steve> hello world',
- '[12:40:00] [Server thread/INFO]: Steve left the game',
- '[12:41:00] [Server thread/INFO]: Stopping server',
- ]
- events = log_events.parse_lines(lines)
- assert [e['event'] for e in events] == [
- 'server_started', 'player_join', 'chat', 'player_leave', 'server_stopping']
- assert events[0]['elapsed'] == '12.345'
- assert events[1]['player'] == 'Steve'
- assert events[2]['message'] == 'hello world'
-
-
-def test_bedrock_log_events():
- lines = [
- '[2026-07-22 12:00:00 INFO] Server started.',
- '[2026-07-22 12:01:00 INFO] Player connected: Steve, xuid: 123',
- '[2026-07-22 12:05:00 INFO] Player disconnected: Steve, xuid: 123',
- ]
- events = log_events.parse_lines(lines)
- assert [e['event'] for e in events] == ['server_started', 'player_join', 'player_leave']
- assert events[1]['player'] == 'Steve'
-
-
-def test_non_event_lines_ignored():
- assert log_events.parse_line('[12:00:00] [Server thread/INFO]: Preparing spawn area: 42%') is None
- assert log_events.parse_line('') is None
-
-
-# --------------------------------------------------------------------------- #
-# config_form
-# --------------------------------------------------------------------------- #
-
-_PROPS = (
- '# Minecraft server properties\n'
- 'motd=Hello World\n'
- 'difficulty=hard\n'
- 'max-players=20\n'
- 'white-list=true\n'
- 'custom-unlisted-key=keep-me\n'
-)
-
-
-def _sidecar_meta():
- with open(os.path.join(_MC, 'backend', 'gamekit', 'server_properties_meta.json'),
- 'r', encoding='utf-8') as f:
- return json.load(f)
-
-
-def test_parse_properties_ignores_comments_and_blanks():
- props = config_form.parse_properties(_PROPS)
- assert props['motd'] == 'Hello World'
- assert props['max-players'] == '20'
- assert 'custom-unlisted-key' in props
- assert not any(k.startswith('#') for k in props)
-
-
-def test_build_form_groups_and_coerces_with_real_sidecar():
- form = config_form.build_form(_PROPS, _sidecar_meta())
- all_fields = {f['key']: f for g in form['groups'] for f in g['fields']}
- assert all_fields['max-players']['value'] == 20 # coerced int
- assert all_fields['white-list']['value'] is True # coerced bool
- assert 'options' in all_fields['difficulty'] # from sidecar
- # unlisted key still surfaces, in the implicit 'other' group
- assert all_fields['custom-unlisted-key']['type'] == 'string'
- group_ids = [g['id'] for g in form['groups']]
- assert group_ids == sorted(group_ids, key=lambda g:
- {'general': 1, 'world': 2, 'players': 3, 'gameplay': 4,
- 'network': 5, 'performance': 6, 'other': 99}.get(g, 50))
-
-
-def test_apply_changes_preserves_and_appends():
- new = config_form.apply_changes(_PROPS, {'difficulty': 'peaceful', 'pvp': False})
- assert 'difficulty=peaceful' in new
- assert 'motd=Hello World' in new # untouched key preserved
- assert '# Minecraft server properties' in new # comment preserved
- assert 'custom-unlisted-key=keep-me' in new # unknown key preserved
- assert 'pvp=false' in new # new key appended, bool rendered
-
-
-# --------------------------------------------------------------------------- #
-# save_backup
-# --------------------------------------------------------------------------- #
-
-class _FakeRcon:
- def __init__(self):
- self.cmds = []
-
- def command(self, c):
- self.cmds.append(c)
- return ''
-
-
-def test_hot_backup_quiesce_order_and_produces_archive(tmp_path):
- world = tmp_path / 'world'
- world.mkdir()
- (world / 'level.dat').write_bytes(b'x' * 32)
- dest = tmp_path / 'backups'
- rc = _FakeRcon()
-
- res = save_backup.hot_backup(str(world), str(dest), 'world_v1.21', rcon=rc)
-
- assert res['success'] is True and res['skipped'] is False
- # the correct hot-backup sequence, in order
- assert rc.cmds == ['save-off', 'save-all flush', 'save-on']
- assert os.path.isfile(res['path'])
- assert res['path'].endswith('world_v1.21.zip')
-
-
-def test_hot_backup_skips_empty_world(tmp_path):
- world = tmp_path / 'world'
- world.mkdir()
- res = save_backup.hot_backup(str(world), str(tmp_path / 'b'), 'x.zip')
- assert res['skipped'] is True
- assert res['commands'] == []
-
-
-def test_save_on_runs_even_if_zip_fails(tmp_path, monkeypatch):
- world = tmp_path / 'world'
- world.mkdir()
- (world / 'a.dat').write_bytes(b'y')
- rc = _FakeRcon()
- # Fail mid-sequence, after quiesce has started, to prove save-on still runs.
- def boom(_src, _path):
- raise RuntimeError('disk full')
- monkeypatch.setattr(save_backup, '_zip_dir', boom)
- with pytest.raises(RuntimeError):
- save_backup.hot_backup(str(world), str(tmp_path / 'backups'), 'x.zip', rcon=rc)
- assert rc.cmds == ['save-off', 'save-all flush', 'save-on'] # resume always issued
-
-
-def test_retention_prunes_oldest_keeps_newest(tmp_path):
- dest = tmp_path / 'b'
- dest.mkdir()
- for i in range(4):
- p = dest / f'w{i}.zip'
- p.write_bytes(b'z')
- os.utime(p, (1000 + i, 1000 + i))
- pruned = save_backup.apply_retention(str(dest), 2)
- assert set(pruned) == {'w0.zip', 'w1.zip'}
- remaining = sorted(f for f in os.listdir(dest) if f.endswith('.zip'))
- assert remaining == ['w2.zip', 'w3.zip']
-
-
-def test_archive_name_is_self_describing():
- when = datetime.datetime(2026, 7, 22, 12, 0)
- assert save_backup.archive_name('myworld', '1.21', when) == 'myworld_v1.21_2026-07-22_1200.zip'
-
-
-# --------------------------------------------------------------------------- #
-# manifest well-formed
-# --------------------------------------------------------------------------- #
-
-def test_extension_manifest_is_well_formed():
- with open(os.path.join(_MC, 'plugin.json'), 'r', encoding='utf-8') as f:
- manifest = json.load(f)
- for key in ('name', 'display_name', 'version', 'entry_point', 'url_prefix',
- 'models', 'category'):
- assert manifest.get(key), f'manifest missing {key}'
- assert manifest['category'] == 'games'
- assert manifest['entry_point'] == 'minecraft:minecraft_bp'
- assert manifest['url_prefix'] == '/api/v1/minecraft'
diff --git a/backend/tests/test_minecraft_extension_install.py b/backend/tests/test_minecraft_extension_install.py
deleted file mode 100644
index 3ef8407c..00000000
--- a/backend/tests/test_minecraft_extension_install.py
+++ /dev/null
@@ -1,72 +0,0 @@
-"""Prove the serverkit-minecraft extension wires up (plan 53 Phase 1).
-
-Installing the builtin registers /api/v1/minecraft, creates its extension-owned
-ext_serverkit_minecraft_* tables (models:register, D6), and its list route
-responds — which also exercises the dashed-package import bridge reaching gamekit.
-The create/console/backup Docker flow is verified separately on the dev box; this
-locks the offline wiring.
-"""
-import sys
-
-import pytest
-
-import app as app_pkg
-from app import db
-from app.models.plugin import InstalledPlugin
-from app.services import plugin_service
-
-SLUG = 'serverkit-minecraft'
-_PKG = f'app.plugins.{SLUG}'
-
-
-def install_dirs(tmp_path, monkeypatch):
- backend = tmp_path / 'plugins_backend'
- frontend = tmp_path / 'plugins_frontend'
- backend.mkdir()
- frontend.mkdir()
- monkeypatch.setattr(plugin_service, 'BACKEND_PLUGINS_DIR', str(backend))
- monkeypatch.setattr(plugin_service, 'FRONTEND_PLUGINS_DIR', str(frontend))
-
- added = str(backend)
- import importlib
- app_pkg_plugins = importlib.import_module('app.plugins')
- if added not in app_pkg_plugins.__path__:
- app_pkg_plugins.__path__.append(added)
-
- yield {'backend': backend, 'frontend': frontend}
-
- if added in app_pkg_plugins.__path__:
- app_pkg_plugins.__path__.remove(added)
- for name in list(sys.modules):
- if name == _PKG or name.startswith(_PKG + '.'):
- del sys.modules[name]
-
-
-def test_minecraft_builtin_is_available(app):
- available = {e['slug'] for e in plugin_service.list_builtin_extensions()}
- assert SLUG in available, 'serverkit-minecraft builtin folder should exist'
-
-
-def test_install_registers_routes_and_models(app, client, auth_headers, install_dirs):
- plugin = plugin_service.install_builtin_extension(SLUG)
- assert plugin.status == InstalledPlugin.STATUS_ACTIVE
- assert plugin.has_backend is True
- assert plugin.url_prefix == '/api/v1/minecraft'
-
- # extension-owned tables were created (models:register + create_all)
- insp = db.inspect(db.engine)
- tables = set(insp.get_table_names())
- assert 'ext_serverkit_minecraft_servers' in tables
- assert 'ext_serverkit_minecraft_backups' in tables
-
- # the list route responds (proves blueprint mount + dashed-import to gamekit)
- resp = client.get('/api/v1/minecraft', headers=auth_headers)
- assert resp.status_code == 200, resp.status_code
- assert resp.get_json() == {'servers': []}
-
-
-def test_uninstall_removes_plugin(app, install_dirs):
- plugin = plugin_service.install_builtin_extension(SLUG)
- assert plugin_service.uninstall_plugin(plugin.id) is True
- assert InstalledPlugin.query.filter_by(slug=SLUG).first() is None