import json from pathlib import Path import re import tempfile import unittest import zipfile from tools import gen_version_consts as gen from tools import lab INFO = { "id": "26.4", "name": "26.4", "world_version": 5100, "protocol_version": 780, "pack_version": {"resource_major": 99, "resource_minor": 0, "data_major": 124, "data_minor": 2}, "build_time": "2026-12-01T09:05:07+00:00", "java_version": 25, "stable": False, } class GenVersionConstsTests(unittest.TestCase): def test_renders_every_fact(self): text = gen.render(INFO) for line in ['pub const VERSION_ID: &str = "26.4";', 'pub const VANILLA_VERSION: &str = "26.4";', "pub const PROTOCOL_VERSION: i32 = 780;", "pub const DATA_VERSION: i32 = 5100;", "pub const RESOURCE_PACK_FORMAT: (i64, i64) = (99, 0);", "pub const DATA_PACK_FORMAT: (i64, i64) = (124, 2);", "pub const JAVA_VERSION: u32 = 25;", "pub const STABLE: bool = false;", 'pub const BUILD_TIME: &str = "Tue Dec 01 09:05:07 UTC 2026";']: self.assertIn(line, text) def test_build_time_is_printed_in_utc(self): self.assertEqual(gen.java_date("2026-09-15T13:20:48+02:00"), "Tue Sep 15 11:20:48 UTC 2026") def test_reads_the_jar(self): with tempfile.TemporaryDirectory() as directory: jar = Path(directory) / "client.jar" with zipfile.ZipFile(jar, "w") as archive: archive.writestr("version.json", json.dumps(INFO)) self.assertEqual(lab.version_info(jar), INFO) def test_committed_file_is_the_pinned_version(self): """A pin bump regenerates version.rs: the lock and the constants never disagree.""" text = gen.OUT.read_text() pinned = json.loads(lab.LOCK.read_text())["minecraft"] self.assertEqual(re.search(r'VANILLA_VERSION: &str = "([^"]+)"', text)[1], pinned["version"]) self.assertEqual(int(re.search(r"JAVA_VERSION: u32 = (\d+)", text)[1]), pinned["java_major"]) self.assertIn("Generated by tools/gen_version_consts.py", text) if __name__ == "__main__": unittest.main()