From f759a4d8acdd6773bdf27e58d7925f556831ef84 Mon Sep 17 00:00:00 2001 From: Titus Date: Thu, 26 Nov 2026 18:09:00 +0100 Subject: [PATCH] Add purity tests for map_space, layout_of and report_threads_of --- tests/test_layout_of.py | 22 ++++++++++++++++++++++ tests/test_map_space.py | 20 ++++++++++++++++++++ tests/test_report.py | 22 ++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/tests/test_layout_of.py b/tests/test_layout_of.py index bb19178..bcf50d9 100644 --- a/tests/test_layout_of.py +++ b/tests/test_layout_of.py @@ -137,3 +137,25 @@ def test_a_map_row_without_a_string_id_is_no_map() -> None: this["id"] = no_id pins = [{"type": "pin", "id": "p", "map": no_id, "x_hand": True}] assert layout_of(this, [this, ground], pins) == {"layout": None, "ground": None, "hand": []}, no_id + + +def test_layout_of_is_pure() -> None: + cases = {c["name"]: c for c in load_fixture()["layout_cases"]} + case = cases["hand-in-id-order"] + elements: list[dict[str, Any]] = case["elements"] + maps = [e for e in elements if e["type"] == "map"] + pins = [e for e in elements if e["type"] == "pin"] + this = rows_read(case, elements)[0] + before = json.dumps([this, maps, pins], sort_keys=True) + got = layout_of(this, maps, pins) + assert json.dumps([this, maps, pins], sort_keys=True) == before, "layout_of moved its input" + first = json.dumps(got, sort_keys=True) + assert got["layout"] is not None and got["hand"] + got["layout"] = "planted" + got["hand"][0] = "planted" + got["hand"].append("extra") + assert json.dumps([this, maps, pins], sort_keys=True) == before + again = layout_of(this, maps, pins) + assert json.dumps(again, sort_keys=True) == first + assert again["layout"] != "planted" + assert again["hand"] != got["hand"] diff --git a/tests/test_map_space.py b/tests/test_map_space.py index 3865347..780f5d9 100644 --- a/tests/test_map_space.py +++ b/tests/test_map_space.py @@ -63,3 +63,23 @@ def test_a_whole_float_reads_as_its_integer_and_a_boolean_is_no_number() -> None assert got["zones"] == [{"zone": "z", "map": "m", "shape": "polygon", "points": [[2, 3, 0]]}] assert got["out"] == {"no_point": 3} assert all(type(n) is int for n in got["zones"][0]["points"][0]) + + +def test_map_space_is_pure() -> None: + cases = {c["name"]: c for c in load_fixture()["cases"]} + elements: list[dict[str, Any]] = cases["measured-frame-from-placed-points"]["elements"] + before = json.dumps(elements, sort_keys=True) + got = map_space(elements) + assert json.dumps(elements, sort_keys=True) == before, "map_space moved its input" + first = json.dumps(got, sort_keys=True) + assert got["maps"] and got["zones"] and got["pins"] and got["out"] + got["maps"][0]["id"] = "planted" + got["zones"][0]["points"][0][0] = -1 + got["zones"][0]["points"].append([-2, -2, -2]) + got["pins"][0]["x"] = -1 + got["out"]["no_point"] = -1 + assert json.dumps(elements, sort_keys=True) == before + again = map_space(elements) + assert json.dumps(again, sort_keys=True) == first + assert again["maps"][0]["id"] != "planted" + assert again["zones"][0]["points"] != got["zones"][0]["points"] diff --git a/tests/test_report.py b/tests/test_report.py index 1e71ff6..c226c7b 100644 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -278,3 +278,25 @@ def test_only_a_report_is_shown_with_talks() -> None: no_report: tuple[Any, ...] = (None, [], "a report", 7) for nothing in no_report: assert report_talks_of(nothing) == [] + + +def test_report_threads_of_is_pure() -> None: + cases = {c["name"]: c for c in load_fixture()["thread_cases"]} + narratives: list[dict[str, Any]] = cases["chain-as-written"]["narratives"] + before = json.dumps(narratives, sort_keys=True) + got = report_threads_of(narratives) + assert json.dumps(narratives, sort_keys=True) == before, "report_threads_of moved its input" + first = json.dumps(got, sort_keys=True) + assert got["parents"] and got["groups"] and got["lints"] + first_report = next(iter(got["parents"])) + got["parents"][first_report] = "missing" + got["groups"][0]["parent"] = "planted" + got["groups"][0]["reports"][0] = "planted" + got["groups"][0]["reports"].append("extra") + got["lints"][0]["report"] = "planted" + assert json.dumps(narratives, sort_keys=True) == before + again = report_threads_of(narratives) + assert json.dumps(again, sort_keys=True) == first + assert again["parents"][first_report] != "missing" + assert again["groups"][0]["parent"] != "planted" + assert again["groups"][0]["reports"] != got["groups"][0]["reports"]