tools/data/test_extract_backyards.py (5155B)
1 import json 2 import os 3 import pathlib 4 import struct 5 import tempfile 6 import unittest 7 8 import extract_backyards 9 10 11 class BackyardTests(unittest.TestCase): 12 def setUp(self): 13 source = json.loads((pathlib.Path(__file__).parent / 'testdata/backyard_doors_1491.json').read_text()) 14 self.records = {record['identity']: record for record in source['records']} 15 16 def test_original_city_entry(self): 17 descriptor = bytes.fromhex(self.records[3223782081]['raw_hex'])[64:] 18 19 attributes = extract_backyards.parse_descriptor(descriptor) 20 21 self.assertEqual(attributes[0]['functions'][0]['arguments'], [ 22 {'integer': 100001}, {'integer': 68536}, {'integer': 3000}, {'bytes_hex': '00000000'}]) 23 self.assertEqual(attributes[0]['functions'][0]['requirements'], []) 24 self.assertEqual(attributes[2], {'name': 'Backyard', 'description': ''}) 25 26 def test_original_exit_has_complementary_level_and_credit_gates(self): 27 descriptor = bytes.fromhex(self.records[3221294008]['raw_hex'])[64:] 28 29 functions = extract_backyards.parse_descriptor(descriptor)[0]['functions'] 30 31 self.assertEqual([function['id'] for function in functions], [53059, 53134]) 32 self.assertEqual(functions[0]['requirements'], [ 33 {'stat': 54, 'value': 1, 'operator': 2}, {'stat': 61, 'value': 999, 'operator': 2}, {'stat': 0, 'value': 0, 'operator': 3}]) 34 self.assertEqual(functions[1]['requirements'], [ 35 {'stat': 61, 'value': 1000, 'operator': 1}, {'stat': 54, 'value': 2, 'operator': 1}, {'stat': 0, 'value': 0, 'operator': 4}]) 36 37 def test_empty_message_string_preserves_the_following_metadata(self): 38 descriptor = bytes.fromhex(self.records[3221294008]['raw_hex'])[64:] 39 40 attributes = extract_backyards.parse_descriptor(descriptor) 41 42 self.assertEqual(attributes[0]['functions'][1]['arguments'], [ 43 {'string': 'You need to be level 2 or have at least 1000 credits to exit to the outside.'}, 44 {'string': ''}, {'bytes_hex': '00000000'}]) 45 self.assertEqual(attributes[2], {'name': 'Elevator Door', 'description': 'A typical elevator door.'}) 46 47 def test_training_exit_contains_only_metadata(self): 48 descriptor = bytes.fromhex(self.records[3221226422]['raw_hex'])[64:] 49 50 attributes = extract_backyards.parse_descriptor(descriptor) 51 52 self.assertEqual(attributes, [{'stats': [[12, 99964], [189, 1]]}, {'name': 'Exit To Omni Backyard', 'description': ''}]) 53 54 def test_clan_exit_uses_its_actual_identity(self): 55 record = self.records[3221291960] 56 raw = bytes.fromhex(record['raw_hex']) 57 58 attributes = extract_backyards.parse_descriptor(raw[64:]) 59 60 self.assertEqual(struct.unpack_from('<I', raw, 4)[0], 3221291960) 61 self.assertNotEqual(3221291960, 0xc0000000 | 952) 62 self.assertEqual(attributes, [{'stats': [[12, 99711], [189, 1]]}, {'name': 'Exit To Clan Backyard', 'description': ''}]) 63 64 def test_unknown_executable_function_is_not_admitted(self): 65 descriptor = bytearray.fromhex(self.records[3223782081]['raw_hex'])[64:] 66 struct.pack_into('>I', descriptor, 28, 53002) 67 68 with self.assertRaisesRegex(AssertionError, 'unsupported statel function'): 69 extract_backyards.parse_descriptor(descriptor) 70 71 def test_truncated_descriptor_is_rejected(self): 72 descriptor = bytes.fromhex(self.records[3221294008]['raw_hex'])[64:-1] 73 74 with self.assertRaisesRegex(ValueError, 'truncated statel descriptor'): 75 extract_backyards.parse_descriptor(descriptor) 76 77 def test_route_update_preserves_unrelated_source_and_is_repeatable(self): 78 with tempfile.TemporaryDirectory() as temporary: 79 previous = os.getcwd() 80 os.chdir(temporary) 81 try: 82 path = pathlib.Path('server/assets/zone_links.yaml') 83 path.parent.mkdir(parents=True) 84 source = '# Source\r\noriginal:\r\n 705:\r\n doors:\r\n - id: 1\r\n position: [1, 2, 3]\r\ntarget:\r\n 705:\r\n doors:\r\n - id: 1\r\n position: [4, 5, 6]\r\n rotation: [0, 0, 0, 1]\r\n - id: 2\r\n position: [7, 8, 9]\r\n walls: []\r\n' 85 path.write_bytes(source.encode('ascii')) 86 routes = [{'playfield': 705, 'door': {'id': 1, 'position': [4, 5, 6], 'rotation': [0, 0, 0, 1], 'destination': 3000, 'refusal_message': 'Refused', 'arrival': {'position': [100, 115.5, 150], 'rotation': [0, 0, 0, 1]}}}] 87 88 extract_backyards.update_routes(routes) 89 updated = path.read_bytes() 90 extract_backyards.update_routes(routes) 91 92 self.assertEqual(updated, path.read_bytes()) 93 self.assertTrue(updated.startswith(source.split('target:')[0].encode('ascii'))) 94 self.assertIn(b' - id: 2\r\n position: [7, 8, 9]\r\n walls: []\r\n', updated) 95 self.assertIn(b' destination: 3000\r\n refusal_message: "Refused"\r\n arrival:\r\n', updated) 96 finally: 97 os.chdir(previous) 98 99 100 if __name__ == '__main__': 101 unittest.main()