File size: 2,878 Bytes
4772e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a625b1a
fd969ab
4772e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
import tempfile
from pathlib import Path

import minecraft_stack as mc


def test_operator_seed_preserves_other_operators():
    with tempfile.TemporaryDirectory() as tmp:
        path = Path(tmp) / "ops.json"
        other = {"uuid": "other", "name": "ExistingAdmin", "level": 3, "bypassesPlayerLimit": False}
        path.write_text(json.dumps([other]), encoding="utf-8")
        mc._ensure_admin_operator(path, "IffyBinkie6134")
        mc._ensure_admin_operator(path, "IffyBinkie6134")
        entries = json.loads(path.read_text(encoding="utf-8"))
        assert entries[0] == other
        assert len(entries) == 2
        assert entries[1] == {
            "uuid": mc._offline_player_uuid("IffyBinkie6134"),
            "name": "IffyBinkie6134", "level": 4, "bypassesPlayerLimit": True,
        }
        try:
            mc._ensure_admin_operator(path, "bad name; op someone")
        except ValueError:
            pass
        else:
            raise AssertionError("invalid username accepted")
        assert json.loads(path.read_text(encoding="utf-8")) == entries


def test_status_reports_real_configured_limits_and_operator():
    with tempfile.TemporaryDirectory() as tmp:
        base = Path(tmp)
        old = (mc.JAVA_DIR, mc.RELAY_DIR, mc.WORLD_DIR, mc.LOG_PATH)
        try:
            mc.JAVA_DIR, mc.RELAY_DIR = base / "java", base / "relay"
            mc.WORLD_DIR, mc.LOG_PATH = base / "world", base / "stack.log"
            mc.JAVA_DIR.mkdir()
            mc.RELAY_DIR.mkdir()
            mc.WORLD_DIR.mkdir()
            (mc.JAVA_DIR / "server.properties").write_text("max-players=100\n", encoding="utf-8")
            (mc.RELAY_DIR / "config.json").write_text('{"max_players":100}', encoding="utf-8")
            mc._ensure_admin_operator(mc.JAVA_DIR / "ops.json", mc.ADMIN_USERNAME)
            status = mc.MinecraftStack().status()
            assert status["player_limit"] == {"java": 100, "xbox_friends": 100}
            assert status["world_profile"] == "minecraft:normal"
            assert status["world_surface_y"] == 64
            assert status["admin_operator"] is True
            assert status["admin_player_file_exists"] is False
            (mc.WORLD_DIR / "playerdata").mkdir()
            (mc.WORLD_DIR / "playerdata" / f"{mc._offline_player_uuid(mc.ADMIN_USERNAME)}.dat").write_bytes(b"saved")
            assert mc.MinecraftStack().status()["admin_player_file_exists"] is True
            (mc.JAVA_DIR / "ops.json").write_text("[]", encoding="utf-8")
            assert mc.MinecraftStack().status()["admin_operator"] is False
        finally:
            mc.JAVA_DIR, mc.RELAY_DIR, mc.WORLD_DIR, mc.LOG_PATH = old


if __name__ == "__main__":
    test_operator_seed_preserves_other_operators()
    test_status_reports_real_configured_limits_and_operator()
    print("PASS player limit and persistent operator seed")