"""Validate Godot pickup resources before exporting Android. Godot's serialized .tres files only accept Color(r, g, b, a) with numeric arguments. The GDScript syntax Color("#hex") is *not* valid here and caused all item data to fail to load (white squares named "Item"). """ from pathlib import Path import re import unittest ROOT = Path(__file__).resolve().parents[1] DATA = ROOT / "pickups" / "data" NUM = r"[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?" COLOR = re.compile(rf'color\s*=\s*Color\(\s*({NUM})\s*,\s*({NUM})\s*,\s*({NUM})\s*,\s*({NUM})\s*\)') class PickupResourceTests(unittest.TestCase): def test_all_pickups_have_serializable_colors(self): resources = sorted(DATA.glob("*.tres")) self.assertEqual(len(resources), 18, "17 rewards plus one laboratory control") for path in resources: with self.subTest(path=path.name): content = path.read_text(encoding="utf-8") color = COLOR.search(content) self.assertIsNotNone( color, "A .tres color must use four numeric components, not Color('#hex')", ) self.assertTrue(all(0.0 <= float(v) <= 1.0 for v in color.groups())) def test_item_resources_have_valid_identifiers_names_and_images(self): names = set() for path in sorted(DATA.glob("*.tres")): with self.subTest(path=path.name): content = path.read_text(encoding="utf-8") identifier = re.search(r'^id = &"([^"]+)"$', content, re.M) title = re.search(r'^display_name = "([^"]+)"$', content, re.M) image = re.search(r'\[ext_resource type="Texture2D" path="res://([^"]+)"', content) self.assertIsNotNone(identifier) self.assertIsNotNone(title) self.assertIsNotNone(image) self.assertNotEqual(title.group(1), "Item") self.assertNotIn(identifier.group(1), names) names.add(identifier.group(1)) self.assertTrue((ROOT / image.group(1)).is_file()) def test_runtime_pickups_detect_player_and_only_drops_have_a_delay(self): scene = (ROOT / "pickups/pickup.tscn").read_text(encoding="utf-8") pickup = (ROOT / "pickups/pickup.gd").read_text(encoding="utf-8") drop_manager = (ROOT / "pickups/drop_manager.gd").read_text(encoding="utf-8") laboratory = (ROOT / "levels/level_01.gd").read_text(encoding="utf-8") self.assertIn("collision_mask = 2", scene) self.assertIn("monitoring = true", scene) self.assertIn("@export var collection_delay_seconds: float = 0.0", pickup) self.assertIn("get_overlapping_bodies()", pickup) for code in (drop_manager, laboratory): self.assertIn("pickup.collection_delay_seconds = Pickup.DROP_COLLECTION_DELAY_SECONDS", code) if __name__ == "__main__": unittest.main()