"""Tests for tools/world_lights_paths.py, built from synthetic 0x7a resources (no game data). python -I tests/test_world_lights_paths.py """ import contextlib import io import json import math import os import struct import sys import tempfile import unittest import zlib from unittest import mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Imported by name (not from a spec) so that --jobs worker processes can import it too, also under spawn. sys.path.insert(0, os.path.join(ROOT, "tools")) import world_lights_paths as T # noqa: E402 M = T.M PTR = 0x50000000 def pool(block, offset=0): """A pool reference: 1-based block number in bits 0..19, byte offset from bit 20.""" return block | (offset << 20) class Resource: """Lay out a 0x7a resource: header, struct table, member tables, block table, then the blocks.""" def __init__(self): self.structs = [] # (hash, size, [(member hash, offset, type)]) self.blocks = [] # (hash, bytes) def struct(self, hash_, size, members): self.structs.append((hash_, size, members)) def block(self, hash_, data): self.blocks.append((hash_, bytes(data))) return len(self.blocks) # 1-based block number def build(self): st, it = 0x60, 0x60 + 32 * len(self.structs) mt = it + 16 * len(self.blocks) b = bytearray(mt + 16 * sum(len(m) for _, _, m in self.structs)) b[0x10:0x14] = b"0DRP" b[0x14] = 0x7A struct.pack_into(" bytes: """A map resource with one entity that owns a light effect (one light, offset 1 m up).""" r = Resource() ent_members = [(M["position"], 0x00, 0x32), (M["rotation"], 0x10, 0x34), (M["scaleXY"], 0x20, 0x21), (M["scaleZ"], 0x24, 0x21), (M["archetypeName"], 0x28, 0x4A), (M["extensions"], 0x30, 0x52)] r.struct(T.ENTITY, 0x40, ent_members) r.struct(T.MAPDATA, 0x10, [(M["name"], 0x00, 0x4A)]) r.struct(T.LIGHT_EXT, EXT_SIZE, EXT_MEMBERS) light_members = [m for m in LIGHT_MEMBERS if not posn_only or m[0] != M["position120"]] r.struct(T.LIGHT, LIGHT_SIZE, light_members) ent = bytearray(0x40) s = math.sqrt(0.5) struct.pack_into("<3f", ent, 0x00, 100.0, 200.0, 10.0) struct.pack_into("<4f", ent, 0x10, 0.0, 0.0, s, s) # stored +90 degrees about Z: the entity turns -90 struct.pack_into("<2f", ent, 0x20, scale_xy, scale_z) struct.pack_into(" (0, -2, 3). for a, b in zip(row["position"], (100.0, 198.0, 13.0)): self.assertAlmostEqual(a, b, places=3) for a, b in zip(row["direction"], (0.0, -1.0, 0.0)): self.assertAlmostEqual(a, b, places=4) self.assertEqual(row["archetype"], "cafef00d") def test_export_normalizes_the_stored_light_rotation(self) -> None: resource = bytearray(map_resource()) meta = T.Meta(resource) (entity_offset,) = meta.objects(T.ENTITY) rotation_offset = meta.off(T.ENTITY, "rotation") assert rotation_offset is not None struct.pack_into("<4f", resource, entity_offset + rotation_offset, 0.0, 0.0, 0.5, 0.5) (row,) = T.inspect_resource(resource, lights_out=True)["world_lights"] self.assertEqual(row["position"], [100.0, 198.0, 13.0]) self.assertEqual(row["direction"], [0.0, -1.0, 0.0]) def test_place_identity(self): p, d = T.place([1.0, 2.0, 3.0], [0.0, 0.0, 1.0], [10.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], 1.0, 1.0) self.assertEqual(p, [11.0, 2.0, 3.0]) self.assertEqual(d, [0.0, 0.0, 1.0]) def test_unreferenced_effect_is_counted(self): r = Resource() r.struct(T.LIGHT_EXT, EXT_SIZE, EXT_MEMBERS) r.struct(T.LIGHT, LIGHT_SIZE, LIGHT_MEMBERS) eff = bytearray(EXT_SIZE) struct.pack_into(" 1 and 1 -> 0 self.assertEqual(s["num_links_matches_range"], 4) self.assertEqual((s["components"], s["largest_component_nodes"]), (2, 3)) self.assertEqual(s["isolated_nodes"], 1) self.assertEqual((s["junction_nodes_resolved"], s["junction_entrances"]), (1, 3)) self.assertEqual(s["junction_entrances_resolved"], 2) self.assertEqual((s["signalised_junctions"], s["phases"], s["entrances_stop"]), (1, 2, 1)) self.assertEqual(s["junction_nodes_flagged_junction"], 1) self.assertEqual(hist["link_flag_bits"][10], 2) self.assertEqual(hist["lanes_from"][0], 2) # Even-sized samples take the stdlib median (the mean of the two middle # values), not the upper-middle element lengths[len // 2] used before. self.assertEqual(hist["link_length_m"], {"min": 10, "median": 25.0, "max": 40}) self.assertEqual(hist["phase_duration_s"], {"min": 8.0, "median": 19.0, "max": 30.0}) class ScanTest(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.rpf = os.path.join(self.tmp.name, "levels.rpf") write_rpf(self.rpf, [light_resource(), map_resource(), node_resource(GRAPH_NODES), link_resource(GRAPH_LINKS), junction_resource(100, [101], [5.0])]) self.entities = os.path.join(self.tmp.name, "entities.jsonl") with open(self.entities, "w", encoding="utf-8", newline="\n") as f: for x in (0.0, 50.0): f.write(json.dumps({"archetype": "a1b2c3d4", "position": [x, 0.0, 0.0], "rotation": [0.0, 0.0, 0.0, 1.0], "scale_xy": 1.0, "scale_z": 1.0}) + "\n") f.write(json.dumps({"archetype": "0badbeef", "position": [0, 0, 0], "rotation": [0, 0, 0, 1]}) + "\n") def tearDown(self): self.tmp.cleanup() def test_a_truncated_rpf9_header_is_one_error_line(self) -> None: # Before: scan() unpacked the data-start word from an unchecked 8-byte head, so a 5-byte "9FPR" file # died in a struct.error traceback. A truncated header is a usage error: one ERROR line, exit 2. short = os.path.join(self.tmp.name, "short.rpf") with open(short, "wb") as f: f.write(b"9FPR\x01") err = io.StringIO() with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(err): rc = T.main([short]) self.assertEqual(rc, 2) self.assertIn(f"ERROR: {short}: the RPF9 header is 5 bytes, the format needs 8", err.getvalue()) self.assertNotIn("Traceback", err.getvalue()) self.assertNotIn("struct.error", err.getvalue()) def test_summary_write_failure_is_one_error_line(self) -> None: # --out naming a directory: IsADirectoryError/PermissionError on most systems. The input half # of main already answers with one ERROR line; the write half must too. target = os.path.join(self.tmp.name, "as-a-directory") os.makedirs(target) err = io.StringIO() with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(err): rc = T.main([self.rpf, "--entities", self.entities, "--out", target]) self.assertEqual(rc, 2) self.assertIn("ERROR: cannot write", err.getvalue()) self.assertNotIn("Traceback", err.getvalue()) self.assertNotIn("IsADirectoryError", err.getvalue()) self.assertNotIn("PermissionError", err.getvalue()) def test_lights_out_uses_posn_when_position120_is_absent(self) -> None: archive = os.path.join(self.tmp.name, "posn-only.rpf") write_rpf(archive, [light_resource(posn_only=True), map_resource(posn_only=True)]) output = os.path.join(self.tmp.name, "posn-only.jsonl") summary = os.path.join(self.tmp.name, "posn-only-summary.json") with contextlib.redirect_stdout(io.StringIO()): self.assertEqual(T.main([archive, "--entities", self.entities, "--out", summary, "--lights-out", output]), 0) with open(output, encoding="utf-8") as handle: placements = [json.loads(line) for line in handle] self.assertEqual([row["position"] for row in placements if row["owner"] == "entity"], [[100.0, 198.0, 13.0]]) self.assertEqual([row["position"] for row in placements if row["owner"] == "archetype"], [[0.0, 0.0, 0.0], [0.0, 0.1, 0.0], [50.0, 0.0, 0.0], [50.0, 0.1, 0.0]]) def test_scan_and_summary(self): total = T.scan(self.rpf) summary = T.summarize(total, self.entities) self.assertEqual(summary["light_resources"], 2) self.assertEqual(summary["lights"]["lights"], 3) self.assertEqual(summary["archetypes_with_lights"], 1) self.assertEqual(summary["placement"]["entities"], 3) self.assertEqual(summary["placement"]["entities_with_lights"], 2) self.assertEqual(summary["placement"]["archetype_lights_placed"], 4) self.assertEqual(summary["lane_graph"]["nodes"], 4) self.assertEqual(summary["lane_graph"]["junctions"], 1) self.assertEqual(summary["graph_resources"], {"junctions": 1, "links": 1, "nodes": 1}) def test_duplicate_light_definitions_compare_values_not_just_counts(self): total = T.scan(self.rpf) total["archetype_lights"] = { 1: [[{"posn": [0, 1, 0]}], [{"posn": [0, 1, 0]}]], 2: [[{"posn": [0, 1, 0]}], [{"posn": [1, 0, 0]}]], 3: [[], [{"intensity": 2}]], 4: [[{"intensity": 2}]], } summary = T.summarize(total) self.assertEqual(summary["archetypes_defined_more_than_once"], 3) self.assertEqual(summary["archetype_light_definitions_differ"], 2) def test_jobs_give_the_same_summary(self): one = T.summarize(T.scan(self.rpf, jobs=1)) two = T.summarize(T.scan(self.rpf, jobs=2)) self.assertEqual(one, two) def test_cli_rejects_negative_start_before_scanning(self): for start in ("-1", "-0x10"): with self.subTest(start=start): out = os.path.join(self.tmp.name, "summary.json") err = io.StringIO() with mock.patch.object(T, "scan", side_effect=AssertionError("unexpected scan")) as scan, \ contextlib.redirect_stderr(err): rc = T.main([self.rpf, f"--start={start}", "--out", out]) scan.assert_not_called() self.assertEqual(rc, 2) self.assertIn("--start must be non-negative", err.getvalue()) self.assertFalse(os.path.exists(out)) def test_cli_rejects_shared_report_destination(self): out = os.path.join(self.tmp.name, "lights.json") alias = os.path.join(self.tmp.name, ".", "lights.json") err = io.StringIO() with contextlib.redirect_stderr(err), self.assertRaises(SystemExit) as cm: T.main([self.rpf, "--out", out, "--lights-out", alias]) self.assertEqual(cm.exception.code, 2) self.assertIn("--out and --lights-out must use different paths", err.getvalue()) self.assertFalse(os.path.exists(out)) def test_cli_rejects_case_alias_before_scan_on_a_case_insensitive_volume(self): marker = os.path.join(self.tmp.name, "case-check") with open(marker, "w", encoding="utf-8"): pass if not os.path.exists(os.path.join(self.tmp.name, "CASE-CHECK")): self.skipTest("test directory is case-sensitive") original_files = set(os.listdir(self.tmp.name)) out = os.path.join(self.tmp.name, "lights.json") alias = os.path.join(self.tmp.name, "LIGHTS.JSON") err = io.StringIO() # Exercise case-insensitive lookup without Windows's lexical normalization. with mock.patch.object(T.os.path, "normcase", side_effect=lambda path: path), \ mock.patch.object(T, "scan", side_effect=AssertionError("scan started before rejecting the alias")) as scan, \ contextlib.redirect_stderr(err), \ self.assertRaises(SystemExit) as cm: T.main([self.rpf, "--out", out, "--lights-out", alias]) scan.assert_not_called() self.assertEqual(cm.exception.code, 2) self.assertIn("--out and --lights-out must use different paths", err.getvalue()) self.assertEqual(set(os.listdir(self.tmp.name)), original_files) def test_cli_writes_summary_and_placed_lights(self): out = os.path.join(self.tmp.name, "summary.json") lights = os.path.join(self.tmp.name, "lights.jsonl") with contextlib.redirect_stdout(io.StringIO()): rc = T.main([self.rpf, "--out", out, "--entities", self.entities, "--lights-out", lights]) self.assertEqual(rc, 0) with open(out, encoding="utf-8") as f: summary = json.load(f) limited = os.path.join(self.tmp.name, "limited.json") limited_lights = os.path.join(self.tmp.name, "limited.jsonl") with contextlib.redirect_stdout(io.StringIO()): self.assertEqual(T.main([self.rpf, "--start", "0", "--limit", "0x1000", "--out", limited, "--entities", self.entities, "--lights-out", limited_lights]), 0) with open(limited, encoding="utf-8") as f: self.assertEqual(json.load(f), summary) with open(lights, encoding="utf-8") as f: rows = [json.loads(line) for line in f] self.assertEqual(len(rows), 1 + 4) # one entity light, 2 x 2 archetype lights self.assertEqual(sorted(r["owner"] for r in rows), ["archetype"] * 4 + ["entity"]) self.assertEqual(summary["placement"]["archetype_lights_placed"], 4) xs = sorted(r["position"][0] for r in rows if r["owner"] == "archetype") self.assertEqual(xs, [0.0, 0.0, 50.0, 50.0]) def test_cli_rejects_report_aliasing_an_input(self): with open(self.rpf, "rb") as f: rpf_bytes = f.read() with open(self.entities, "rb") as f: entities_bytes = f.read() cases = [ (["--out", self.rpf], "--out must use a different path than archive"), (["--out", self.entities], "--out must use a different path than --entities"), (["--lights-out", self.rpf], "--lights-out must use a different path than archive"), (["--lights-out", self.entities], "--lights-out must use a different path than --entities"), ] for args, msg in cases: err = io.StringIO() with mock.patch.object(T, "scan", side_effect=AssertionError("scan started before rejecting the alias")) as scan, \ contextlib.redirect_stderr(err), self.assertRaises(SystemExit) as cm: T.main([self.rpf, "--entities", self.entities] + args) scan.assert_not_called() self.assertEqual(cm.exception.code, 2) self.assertIn(msg, err.getvalue()) with open(self.rpf, "rb") as f: self.assertEqual(f.read(), rpf_bytes) with open(self.entities, "rb") as f: self.assertEqual(f.read(), entities_bytes) def test_light_export_preserves_zero_entity_scale(self) -> None: write_rpf(self.rpf, [light_resource(), map_resource(scale_xy=0.0, scale_z=0.0)]) lights = os.path.join(self.tmp.name, "zero-scale.jsonl") with open(self.entities, "w", encoding="utf-8", newline="\n") as f: for x, scales in ((10, {"scale_xy": 0.0, "scale_z": 0.0}), (20, {})): f.write(json.dumps({"archetype": "a1b2c3d4", "position": [x, 0, 0], "rotation": [0, 0, 0, 1], **scales}) + "\n") with contextlib.redirect_stdout(io.StringIO()): self.assertEqual(T.main([self.rpf, "--entities", self.entities, "--lights-out", lights]), 0) with open(lights, encoding="utf-8") as f: rows = [json.loads(line) for line in f] self.assertEqual([row["owner"] for row in rows], ["entity", "archetype", "archetype", "archetype", "archetype"]) for row, position in zip(rows, ([100.0, 200.0, 10.0], [10.0, 0.0, 0.0], [10.0, 0.0, 0.0], [20.0, 0.5, 2.5], [20.0, 0.0, 1.0])): with self.subTest(owner=row["owner"], expected=position): self.assertEqual(row["position"], position) def test_bad_entities_leaves_the_previous_lights_report_alone(self): # --lights-out used to be opened (truncating it) before --entities was parsed. lights = os.path.join(self.tmp.name, "lights.jsonl") with open(lights, "w", encoding="utf-8", newline="\n") as f: f.write('{"old": true}\n') bad = os.path.join(self.tmp.name, "bad.jsonl") with open(bad, "w", encoding="utf-8", newline="\n") as f: f.write("{not json\n") err = io.StringIO() with contextlib.redirect_stderr(err): rc = T.main([self.rpf, "--entities", bad, "--lights-out", lights]) self.assertEqual(rc, 2) with open(lights, encoding="utf-8") as f: self.assertEqual(f.read(), '{"old": true}\n') self.assertFalse(os.path.exists(lights + ".partial"), "a failed run left its staged report behind") lines = err.getvalue().strip().splitlines() self.assertEqual(len(lines), 1, lines) self.assertIn("bad.jsonl", lines[0]) self.assertNotIn("Traceback", err.getvalue()) def test_lights_report_that_is_a_directory_leaves_no_partial(self): lights = os.path.join(self.tmp.name, "lights.jsonl") os.makedirs(lights) err = io.StringIO() with contextlib.redirect_stderr(err): rc = T.main([self.rpf, "--entities", self.entities, "--lights-out", lights]) self.assertEqual(rc, 2) self.assertFalse(os.path.exists(lights + ".partial"), "a failed replace left its staged report behind") self.assertEqual(len(err.getvalue().strip().splitlines()), 1, err.getvalue()) self.assertNotIn("Traceback", err.getvalue()) def test_stage_cleanup_restores_write_mode_on_replace_failure(self): with tempfile.TemporaryDirectory() as tmp: lights = os.path.join(tmp, "lights.jsonl") with open(lights, "w", encoding="utf-8") as f: f.write('{"old": true}\n') os.chmod(lights, 0o444) chmod_calls = [] real_chmod = os.chmod def track_chmod(p, mode, **kwargs): chmod_calls.append((p, mode)) return real_chmod(p, mode, **kwargs) with mock.patch("world_lights_paths.os.replace", side_effect=OSError("replace failed")), \ mock.patch("world_lights_paths.os.chmod", side_effect=track_chmod): with self.assertRaises(OSError): T.summarize({"lights": {}, "archetype_lights": {}, "resources": 0, "incomplete": 0, "light_resources": 0, "ranges": {}, "world_lights": []}, lights_out=lights) self.assertTrue(any(mode == 0o600 for p, mode in chmod_calls)) def test_bad_input_is_one_line(self): err = io.StringIO() with contextlib.redirect_stderr(err): self.assertEqual(T.main([os.path.join(self.tmp.name, "missing.rpf")]), 2) self.assertEqual(T.main([self.rpf, "--jobs", "0"]), 2) self.assertEqual(T.main([self.rpf, "--entities", os.path.join(self.tmp.name, "none.jsonl")]), 2) self.assertEqual(T.main([self.rpf, "--limit", "0"]), 2) self.assertEqual(T.main([self.rpf, "--limit", "-1"]), 2) lines = err.getvalue().strip().splitlines() self.assertEqual(len(lines), 5) self.assertEqual(lines[3], "ERROR: --limit must be at least 1") self.assertEqual(lines[4], "ERROR: --limit must be at least 1") def test_cli_refuses_malformed_entities_rows_before_the_scan(self): good = '{"archetype": "a1b2c3d4", "position": [0.0, 0.0, 0.0], "rotation": [0.0, 0.0, 0.0, 1.0]}\n' cases = [ ("badjson", '{"archetype": "a1b2c3d4", "position": [0,0,0]\n', "not valid JSON"), ("notjson", "garbage line\n", "not valid JSON"), ("nonobject", "[1, 2, 3]\n", "not a JSON object"), ("scalar", "123\n", "not a JSON object"), ("badhex", '{"archetype": "zzz"}\n', "archetype is not a hex hash"), ("unhashable", '{"archetype": {"x": 1}}\n', "archetype is not a hash:"), ("shortrot", good.replace("[0.0, 0.0, 0.0, 1.0]", "[0.0, 0.0]"), "rotation is not 4 numbers"), ("scalarpos", good.replace('"position": [0.0, 0.0, 0.0]', '"position": 5'), "position is not 3 numbers"), ("boolpos", good.replace('"position": [0.0, 0.0, 0.0]', '"position": [true, 0.0, 0.0]'), "position is not 3 numbers"), ("emptypos", good.replace('"position": [0.0, 0.0, 0.0]', '"position": []'), "position is not 3 numbers"), ("strscale", good.replace('1.0]}\n', '1.0], "scale_xy": "2"}\n'), "scale_xy is not a number"), ] for name, line, expected in cases: with self.subTest(case=name): ent = os.path.join(self.tmp.name, name + ".jsonl") with open(ent, "w", encoding="utf-8", newline="\n") as f: f.write(line) err = io.StringIO() with mock.patch.object(T, "scan", side_effect=AssertionError("scan started before refusing the entities") ) as scan, contextlib.redirect_stderr(err): self.assertEqual(T.main([self.rpf, "--entities", ent]), 2) scan.assert_not_called() lines = err.getvalue().strip().splitlines() self.assertEqual(len(lines), 1) self.assertTrue(lines[0].startswith("ERROR: %s:1:" % ent), lines[0]) self.assertIn(expected, lines[0]) def test_cli_refuses_non_utf8_entities_file(self): ent = os.path.join(self.tmp.name, "binary.jsonl") with open(ent, "wb") as f: f.write(b"\xff\xfe binary garbage\n") err = io.StringIO() with mock.patch.object(T, "scan", side_effect=AssertionError("scan started before refusing the entities")) as scan, \ contextlib.redirect_stderr(err): self.assertEqual(T.main([self.rpf, "--entities", ent]), 2) scan.assert_not_called() self.assertEqual(err.getvalue().strip().splitlines(), ["ERROR: %s: not UTF-8 text" % ent]) def test_malformed_entities_write_no_lights_file_or_summary(self): ent = os.path.join(self.tmp.name, "late-bad.jsonl") with open(ent, "w", encoding="utf-8", newline="\n") as f: f.write('{"archetype": "a1b2c3d4", "position": [0.0, 0.0, 0.0], "rotation": [0.0, 0.0, 0.0, 1.0]}\n') f.write('{"archetype": "zzz"}\n') out = os.path.join(self.tmp.name, "never.json") lights = os.path.join(self.tmp.name, "never.jsonl") err = io.StringIO() with contextlib.redirect_stderr(err): self.assertEqual(T.main([self.rpf, "--out", out, "--entities", ent, "--lights-out", lights]), 2) self.assertIn("archetype is not a hex hash", err.getvalue()) self.assertFalse(os.path.exists(lights)) self.assertFalse(os.path.exists(out)) def test_entities_rows_with_null_fields_and_blank_lines_are_accepted(self): ent = os.path.join(self.tmp.name, "nulls.jsonl") with open(ent, "w", encoding="utf-8", newline="\n") as f: f.write("\n") f.write(json.dumps({"archetype": "a1b2c3d4", "position": None, "rotation": None, "scale_xy": None, "scale_z": None}) + "\n") out = os.path.join(self.tmp.name, "nulls.json") with contextlib.redirect_stdout(io.StringIO()): self.assertEqual(T.main([self.rpf, "--out", out, "--entities", ent]), 0) with open(out, encoding="utf-8") as f: summary = json.load(f) self.assertEqual(summary["placement"]["entities"], 1) self.assertEqual(summary["placement"]["entities_with_lights"], 1) self.assertEqual(summary["placement"]["archetype_lights_placed"], 2) class GraphStatsFirstLinkTest(unittest.TestCase): @staticmethod def _node(idx, first_link): return { "address": (0, idx), "key": 100 + idx, "first_link": first_link, "num_links": 0, "flags": 0, "road_class": 0, "kind": 0, "density": 0, "speed": 10.0, "street": "", "position": [0.0, 0.0, 0.0], } def test_graph_stats_handles_none_first_link_without_type_error(self): # Integer followed by None used to crash with TypeError in firsts[i] <= firsts[i + 1]. s_mixed, _ = T.graph_stats([self._node(0, 0), self._node(1, None)], [], []) self.assertEqual(s_mixed["first_link_monotonic"], 0) # A single node with None first_link must not vacuously report monotonic=1. s_single_none, _ = T.graph_stats([self._node(0, None)], [], []) self.assertEqual(s_single_none["first_link_monotonic"], 0) s_leading_none, _ = T.graph_stats([self._node(0, None), self._node(1, 0)], [], []) self.assertEqual(s_leading_none["first_link_monotonic"], 0) s_ok, _ = T.graph_stats([self._node(0, 0), self._node(1, 0), self._node(2, 2)], [], []) self.assertEqual(s_ok["first_link_monotonic"], 1) if __name__ == "__main__": unittest.main()