From 364a73b4f86f09e334c2d0507c63b36fd0a0e9ac Mon Sep 17 00:00:00 2001 From: Titus Date: Tue, 24 Nov 2026 02:55:26 +0100 Subject: [PATCH] 0.148.0: pins_known_to, which of a map's Pins one reader knows pins_known_to(pins, served) answers which Pins place an element that is among the elements one reader was served, and reads nothing else, so a map drawn as one Character sees it cannot show more than the world told that reader. Each known Pin says whether its element was served whole or as a stub the reader only knows of; the Pins whose element was not served are a count, never ids; a Pin that places nothing is counted apart. The cases (31, and 8 refusals) are the npm SDK's file byte for byte, its sha256 pinned in the test. Held beside them: a tuple read as a list, any other value refused, the inputs left as they were, two calls answering two objects, known_of a stub only when it is exactly True, and the answer beside map_space's. Co-Authored-By: Claude Fable 5.2 --- .gitattributes | 2 + CHANGELOG.md | 4 + src/onlyworlds/__init__.py | 16 +- src/onlyworlds/_version.py | 2 +- src/onlyworlds/map_space.py | 92 +- tests/fixtures/pins-known-to.cases.json | 1588 +++++++++++++++++++++++ tests/test_pins_known_to.py | 123 ++ 7 files changed, 1824 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/pins-known-to.cases.json create mode 100644 tests/test_pins_known_to.py diff --git a/.gitattributes b/.gitattributes index 3160fbc..8587835 100644 --- a/.gitattributes +++ b/.gitattributes @@ -28,3 +28,5 @@ tests/fixtures/address-in-prose.json -text tests/fixtures/name-finder.cases.json -text # The Kanka door's tables and wording, the same bytes as the npm SDK's copy (both tests hold its sha256). src/onlyworlds/convert/kanka.json -text +# The known Pins reader's shared cases, the same bytes as the npm SDK's copy (both tests hold its sha256). +tests/fixtures/pins-known-to.cases.json -text diff --git a/CHANGELOG.md b/CHANGELOG.md index 234723a..173de9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is ` ## Unreleased +## 0.148.0 (2026-11-24) + +- **Added: `pins_known_to(pins, served)`**: which of a map's Pins one reader knows, for a map drawn as one Character sees it. A Pin is known when the element it places is among `served`, the elements as that reader was served them (a list read with the reader's own key, or the owner's read with `view_as`); the function reads nothing else, so it cannot show a reader more than the world told them. The answer is `{"known", "unknown", "out"}`: `known` lists each known Pin as `{"pin", "map", "element_type", "element_id", "how"}` by Pin id in code point order, where `how` is `"whole"` or, when the element reached the reader as a stub it only knows of (`known_of` exactly `True`), `"known_of"`, so a drawing can tell a place heard of from a place known; `unknown` counts the Pins that place an element the reader was not served, as a number and never as ids; `out["no_link"]` counts the Pins that place nothing. The type and the id are compared as written, the last of two served rows with one type and id decides, and the answer carries no name or body of any element. It does not ask whether a Pin has a point or its Map exists: `map_space` reads that, and a drawing keeps the Pins both name. `pins` may be a whole world's list: only its rows of type `pin` are read. The Pins are the caller's to bring, from whichever read holds the map (an open world's, the owner's): a reader's own list need not hold them. A list or a tuple is taken for either argument; anything else is a `TypeError`. A rule of this package and the npm SDK (`pinsKnownTo` in `@onlyworlds/sdk` 4.200.0), not of the standard: the two run the same 31 cases and 8 refusals (`tests/fixtures/pins-known-to.cases.json`). Types `PinsKnownTo`, `PinKnown`. + ## 0.147.0 (2026-11-24) - **Added: `find.shared` on `name_finder`'s answer**: every name two or more elements hold, each a `NameShared` (`name`, `elements`), in the order each name was first met. `name` is the name as it is compared (trimmed, and folded when `case` is `"folded"`); `elements` is a tuple of the objects that were passed in, in the order given. `find` never answers such a name, and until now nothing said it was there: a tool reads the list to tell a writer why a name stays plain, or to test that no two elements of a world share one. A name too short to be looked for is not on it. `shared` is a read-only tuple, read once when the finder is made. New public names: `NameFinder` (what `name_finder` answers, called on a text as before) and `NameShared`. diff --git a/src/onlyworlds/__init__.py b/src/onlyworlds/__init__.py index e1aea41..dcee27d 100644 --- a/src/onlyworlds/__init__.py +++ b/src/onlyworlds/__init__.py @@ -115,7 +115,18 @@ trail_by_place, ) from .lens import DATE_FIELD, LensMail, LensReading, date_of, is_lens, is_template, lenses_in_use, resolve_lens -from .map_space import MapLayout, MapSpace, MapSpaceMap, MapSpaceOutline, MapSpacePin, layout_of, map_space +from .map_space import ( + MapLayout, + MapSpace, + MapSpaceMap, + MapSpaceOutline, + MapSpacePin, + PinKnown, + PinsKnownTo, + layout_of, + map_space, + pins_known_to, +) from .message import ( EditRead, SenderRead, @@ -242,6 +253,8 @@ "NameShared", "Opener", "Patch", + "PinKnown", + "PinsKnownTo", "PreconditionFailed", "ProposedError", "ProposedMark", @@ -324,6 +337,7 @@ "out_degree_of", "parse_ow_link", "parse_retry_after", + "pins_known_to", "plan_push", "proposed_mark_of", "push", diff --git a/src/onlyworlds/_version.py b/src/onlyworlds/_version.py index 013e5b9..bd1f11a 100644 --- a/src/onlyworlds/_version.py +++ b/src/onlyworlds/_version.py @@ -1 +1 @@ -__version__ = "0.147.0" +__version__ = "0.148.0" diff --git a/src/onlyworlds/map_space.py b/src/onlyworlds/map_space.py index 3d1d5a6..07ae899 100644 --- a/src/onlyworlds/map_space.py +++ b/src/onlyworlds/map_space.py @@ -16,11 +16,14 @@ (``x_layout`` with a non-empty string ``method``; ``"hand"`` is a board), the served Map its layout stands on, and the Pins a new lay-out keeps where they are. ``map_space`` reads none of these keys. The fixture's ``layout_cases`` are replayed whole by ``tests/test_layout_of.py``. + +``pins_known_to(pins, served)`` is not the profile's: a rule of this package and the npm SDK. It answers which of a +map's Pins one reader knows, from the elements that reader was served and nothing else. """ from __future__ import annotations -from collections.abc import Iterable, Mapping +from collections.abc import Iterable, Mapping, Sequence from typing import Any, Literal, NotRequired, TypedDict from .lens import _whole @@ -92,6 +95,29 @@ class MapLayout(TypedDict): hand: list[str] +PinKnownHow = Literal["whole", "known_of"] + + +class PinKnown(TypedDict): + """A Pin a reader knows: the element it places was served to that reader, ``whole`` or as a stub (``known_of``).""" + + pin: str + #: The Pin's ``map`` as written when it is a non-empty string, else None. Not checked against any Map. + map: str | None + element_type: str + element_id: str + how: PinKnownHow + + +class PinsKnownTo(TypedDict): + """``pins_known_to``'s answer: ``known`` by Pin id in code point order, ``unknown`` a count of the Pins that place + an element the reader was not served, ``out`` with ``no_link`` (a Pin that places nothing) when one was counted.""" + + known: list[PinKnown] + unknown: int + out: dict[str, int] + + def _id(value: Any) -> str | None: """An id: a non-empty string, else None.""" return value if isinstance(value, str) and value != "" else None @@ -252,3 +278,67 @@ def layout_of( if this is not None and pin.get("map") == this and (method == "hand" or pin.get("x_hand") is True) ) return {"layout": method, "ground": ground, "hand": hand} + + +def pins_known_to(pins: Sequence[Any], served: Sequence[Any]) -> PinsKnownTo: + """Which of ``pins`` a reader knows, for a map drawn as one Character sees it: ``{known, unknown, out}``, all + three keys always present. + + ``pins`` is a list of elements, of which the rows of type ``pin`` are read (so a whole world's list can be + passed). ``served`` is the list of elements as that reader was served them, stubs included: a list read with the + reader's own key, or the owner's read as that Character (``view_as``). The function reads nothing else, so it + cannot show a reader more than the world told them. It does not ask whether a Pin has a point or its Map exists: + that is ``map_space``'s reading, and a drawing keeps the Pins both name. + + A Pin is known when a row of ``served`` has the Pin's ``element_type`` as its ``type`` and the Pin's + ``element_id`` as its ``id``, both as written (``Location`` is no ``location``, and the same id under another + type is another element). ``how`` is ``known_of`` when that row is a stub (``known_of`` exactly ``True``), else + ``whole``; of two served rows with one type and id, the last one decides. A Pin with only one of ``element_type`` + and ``element_id`` places nothing: it is counted in ``out["no_link"]`` and never known. A row without a string + ``type`` and a non-empty string ``id`` is no element and is not counted; of two Pin rows with one id, the last + one is read. ``known`` is in Pin id order by code point; ``unknown`` is a count, never ids. + + The answer carries ids and the two words only, never an element's name or body. A rule of this package and the + npm SDK (``pinsKnownTo``), not of the standard; the shared cases are ``tests/fixtures/pins-known-to.cases.json``. + Raises ``TypeError`` when ``pins`` or ``served`` is not a list or a tuple. + """ + if not isinstance(pins, (list, tuple)): + raise TypeError("pins_known_to: pins is a list of elements.") + if not isinstance(served, (list, tuple)): + raise TypeError("pins_known_to: served is a list of elements.") + + served_how: dict[str, dict[str, PinKnownHow]] = {} + for element in served: + if not isinstance(element, Mapping) or not isinstance(element.get("type"), str): + continue + element_id = _id(element.get("id")) + if element_id is None: + continue + served_how.setdefault(element["type"], {})[element_id] = ( + "known_of" if element.get("known_of") is True else "whole" + ) + + rows: dict[str, Mapping[str, Any]] = {} + for pin in pins: + if isinstance(pin, Mapping) and pin.get("type") == "pin" and (pin_id := _id(pin.get("id"))) is not None: + rows[pin_id] = pin + + known: list[PinKnown] = [] + unknown = 0 + no_link = 0 + for pin_id in sorted(rows): + pin = rows[pin_id] + placed_type, placed_id = _id(pin.get("element_type")), _id(pin.get("element_id")) + if placed_type is None or placed_id is None: + no_link += 1 + continue + how = served_how.get(placed_type, {}).get(placed_id) + if how is None: + unknown += 1 + continue + placed_map = _id(pin.get("map")) + known.append( + {"pin": pin_id, "map": placed_map, "element_type": placed_type, "element_id": placed_id, "how": how} + ) + out: dict[str, int] = {"no_link": no_link} if no_link else {} + return {"known": known, "unknown": unknown, "out": out} diff --git a/tests/fixtures/pins-known-to.cases.json b/tests/fixtures/pins-known-to.cases.json new file mode 100644 index 0000000..55192ad --- /dev/null +++ b/tests/fixtures/pins-known-to.cases.json @@ -0,0 +1,1588 @@ +{ + "about": "Cases for pinsKnownTo (@onlyworlds/sdk) and pins_known_to (the onlyworlds Python package): the same bytes in both repositories. A case passes pins and served as they stand and wants the whole answer, key order included: known by Pin id in code point order, each as pin, map, element_type, element_id, how; unknown as a count; out with no_link only when one was counted. A refusal passes its two values and wants a TypeError that names the argument in refuses. Made by test/fixtures/generate-pins-known-to-cases.mjs in the SDK repository; every want is written by hand there.", + "version": 1, + "cases": [ + { + "name": "a Pin whose element was served whole", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "a Pin whose element was served as a stub", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1", + "known_of": true + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "known_of" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "a Pin whose element was not served", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-2", + "name": "location loc-2" + } + ], + "want": { + "known": [], + "unknown": 1, + "out": {} + } + }, + { + "name": "nothing served: every linked Pin is unknown", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p2", + "map": "m1", + "element_type": "character", + "element_id": "cha-1", + "x": 10, + "y": 20 + } + ], + "served": [], + "want": { + "known": [], + "unknown": 2, + "out": {} + } + }, + { + "name": "no Pins", + "pins": [], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [], + "unknown": 0, + "out": {} + } + }, + { + "name": "one of each: whole, stub, unseen, no link", + "pins": [ + { + "type": "pin", + "id": "p4", + "map": "m1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p3", + "map": "m1", + "element_type": "event", + "element_id": "eve-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p2", + "map": "m2", + "element_type": "institution", + "element_id": "ins-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "character", + "element_id": "cha-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "character", + "id": "cha-1", + "name": "character cha-1" + }, + { + "type": "institution", + "id": "ins-1", + "name": "institution ins-1", + "known_of": true + }, + { + "type": "event", + "id": "eve-2", + "name": "event eve-2" + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "character", + "element_id": "cha-1", + "how": "whole" + }, + { + "pin": "p2", + "map": "m2", + "element_type": "institution", + "element_id": "ins-1", + "how": "known_of" + } + ], + "unknown": 1, + "out": { + "no_link": 1 + } + } + }, + { + "name": "the type is read as written: the Pin says Location, the row location", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "Location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [], + "unknown": 1, + "out": {} + } + }, + { + "name": "the type is read as written: the row says Location, the Pin location", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "Location", + "id": "loc-1", + "name": "Location loc-1" + } + ], + "want": { + "known": [], + "unknown": 1, + "out": {} + } + }, + { + "name": "a type that is no element type is matched as written all the same", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "wizard", + "element_id": "w-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "wizard", + "id": "w-1", + "name": "wizard w-1" + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "wizard", + "element_id": "w-1", + "how": "whole" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "the same id under another type is another element", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "x-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "character", + "id": "x-1", + "name": "character x-1" + } + ], + "want": { + "known": [], + "unknown": 1, + "out": {} + } + }, + { + "name": "the id is read as written: another case is another id", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "LOC-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [], + "unknown": 1, + "out": {} + } + }, + { + "name": "two served rows of one type and id, whole then stub: the last decides", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + }, + { + "type": "location", + "id": "loc-1", + "name": "location loc-1", + "known_of": true + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "known_of" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "two served rows of one type and id, stub then whole: the last decides", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1", + "known_of": true + }, + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "a stub of another type with the same id does not make a whole element a stub", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "x-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "x-1", + "name": "location x-1" + }, + { + "type": "character", + "id": "x-1", + "name": "character x-1", + "known_of": true + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "location", + "element_id": "x-1", + "how": "whole" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "known_of is a stub only when it is true", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p2", + "map": "m1", + "element_type": "location", + "element_id": "loc-2", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p3", + "map": "m1", + "element_type": "location", + "element_id": "loc-3", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p4", + "map": "m1", + "element_type": "location", + "element_id": "loc-4", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p5", + "map": "m1", + "element_type": "location", + "element_id": "loc-5", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1", + "known_of": "true" + }, + { + "type": "location", + "id": "loc-2", + "name": "location loc-2", + "known_of": 1 + }, + { + "type": "location", + "id": "loc-3", + "name": "location loc-3", + "known_of": false + }, + { + "type": "location", + "id": "loc-4", + "name": "location loc-4", + "known_of": null + }, + { + "type": "location", + "id": "loc-5", + "name": "location loc-5", + "known_of": true + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + }, + { + "pin": "p2", + "map": "m1", + "element_type": "location", + "element_id": "loc-2", + "how": "whole" + }, + { + "pin": "p3", + "map": "m1", + "element_type": "location", + "element_id": "loc-3", + "how": "whole" + }, + { + "pin": "p4", + "map": "m1", + "element_type": "location", + "element_id": "loc-4", + "how": "whole" + }, + { + "pin": "p5", + "map": "m1", + "element_type": "location", + "element_id": "loc-5", + "how": "known_of" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "a served row that is no element is not read", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p2", + "map": "m1", + "element_type": "location", + "element_id": "loc-2", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p3", + "map": "m1", + "element_type": "location", + "element_id": "7", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p4", + "map": "m1", + "element_type": "location", + "element_id": "loc-4", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "id": "loc-1", + "name": "no type" + }, + { + "type": 7, + "id": "loc-2" + }, + { + "type": "location", + "id": 7 + }, + { + "type": "location", + "id": "" + }, + { + "type": "location" + }, + null, + "loc-4", + [ + "location", + "loc-4" + ], + [ + { + "type": "location", + "id": "loc-4" + } + ] + ], + "want": { + "known": [], + "unknown": 4, + "out": {} + } + }, + { + "name": "a Pin with only a type, only an id, empty words or nulls places nothing", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location" + }, + { + "type": "pin", + "id": "p2", + "map": "m1", + "element_id": "loc-1" + }, + { + "type": "pin", + "id": "p3", + "map": "m1", + "element_type": "", + "element_id": "loc-1" + }, + { + "type": "pin", + "id": "p4", + "map": "m1", + "element_type": "location", + "element_id": "" + }, + { + "type": "pin", + "id": "p5", + "map": "m1", + "element_type": null, + "element_id": null + }, + { + "type": "pin", + "id": "p6", + "map": "m1" + }, + { + "type": "pin", + "id": "p7", + "map": "m1", + "element_type": "location", + "element_id": 7 + }, + { + "type": "pin", + "id": "p8", + "map": "m1", + "element_type": [ + "location" + ], + "element_id": "loc-1" + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + }, + { + "type": "location", + "id": "", + "name": "location " + }, + { + "type": "", + "id": "loc-1", + "name": " loc-1" + }, + { + "type": "location", + "id": 7 + } + ], + "want": { + "known": [], + "unknown": 0, + "out": { + "no_link": 8 + } + } + }, + { + "name": "the map is echoed as written, and null when it is no id", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m-not-served", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p2", + "map": "", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p3", + "map": null, + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p4", + "map": 7, + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p5", + "element_type": "location", + "element_id": "loc-1" + }, + { + "type": "pin", + "id": "p6", + "map": " m1 ", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m-not-served", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + }, + { + "pin": "p2", + "map": null, + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + }, + { + "pin": "p3", + "map": null, + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + }, + { + "pin": "p4", + "map": null, + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + }, + { + "pin": "p5", + "map": null, + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + }, + { + "pin": "p6", + "map": " m1 ", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "a Pin with no point is answered all the same", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1" + }, + { + "type": "pin", + "id": "p2", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": "ten", + "y": null + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + }, + { + "pin": "p2", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "two Pin rows with one id, known then unseen: the last is read", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p1", + "map": "m2", + "element_type": "location", + "element_id": "loc-9", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [], + "unknown": 1, + "out": {} + } + }, + { + "name": "two Pin rows with one id, unseen then known: the last is read", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-9", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p1", + "map": "m2", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m2", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "two Pin rows with one id, linked then with no link: the last is read", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p1", + "map": "m1" + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [], + "unknown": 0, + "out": { + "no_link": 1 + } + } + }, + { + "name": "rows that are no Pin are not read and not counted", + "pins": [ + { + "type": "map", + "id": "m1", + "name": "map m1" + }, + { + "type": "location", + "id": "loc-1", + "name": "location loc-1", + "element_type": "location", + "element_id": "loc-1" + }, + { + "type": "Pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1" + }, + { + "type": "pin", + "map": "m1", + "element_type": "location", + "element_id": "loc-1" + }, + { + "type": "pin", + "id": "", + "map": "m1", + "element_type": "location", + "element_id": "loc-1" + }, + { + "type": "pin", + "id": 7, + "map": "m1", + "element_type": "location", + "element_id": "loc-1" + }, + { + "id": "p2", + "map": "m1", + "element_type": "location", + "element_id": "loc-1" + }, + null, + "pin", + [ + { + "type": "pin", + "id": "p3", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + { + "type": "pin", + "id": "p9", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [ + { + "pin": "p9", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "a served Pin does not make what it places known", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "map", + "id": "m1", + "name": "map m1" + } + ], + "want": { + "known": [], + "unknown": 1, + "out": {} + } + }, + { + "name": "a Pin that places a Pin is known when that Pin was served", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "pin", + "element_id": "p2", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p2", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "pin", + "id": "p2", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "pin", + "element_id": "p2", + "how": "whole" + } + ], + "unknown": 1, + "out": {} + } + }, + { + "name": "a Pin that places the reader's own Character", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "character", + "element_id": "cha-self", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "character", + "id": "cha-self", + "name": "character cha-self", + "self": true + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "character", + "element_id": "cha-self", + "how": "whole" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "several Pins on several Maps place one element: each is known", + "pins": [ + { + "type": "pin", + "id": "p3", + "map": "m3", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p2", + "map": "m2", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1", + "known_of": true + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "known_of" + }, + { + "pin": "p2", + "map": "m2", + "element_type": "location", + "element_id": "loc-1", + "how": "known_of" + }, + { + "pin": "p3", + "map": "m3", + "element_type": "location", + "element_id": "loc-1", + "how": "known_of" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "known is ordered by Pin id as text: a capital before a small letter, 10 before 9", + "pins": [ + { + "type": "pin", + "id": "pin-a", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "pin-9", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "pin-B", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "pin-10", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [ + { + "pin": "pin-10", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + }, + { + "pin": "pin-9", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + }, + { + "pin": "pin-B", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + }, + { + "pin": "pin-a", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "known is ordered by code point, not by UTF-16 unit", + "pins": [ + { + "type": "pin", + "id": "pin-🐉", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "pin-~", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-1", + "name": "location loc-1" + } + ], + "want": { + "known": [ + { + "pin": "pin-~", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + }, + { + "pin": "pin-🐉", + "map": "m1", + "element_type": "location", + "element_id": "loc-1", + "how": "whole" + } + ], + "unknown": 0, + "out": {} + } + }, + { + "name": "an id above the BMP is matched whole", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-🐉", + "x": 10, + "y": 20 + }, + { + "type": "pin", + "id": "p2", + "map": "m1", + "element_type": "location", + "element_id": "loc-🐊", + "x": 10, + "y": 20 + } + ], + "served": [ + { + "type": "location", + "id": "loc-🐉", + "name": "location loc-🐉" + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "location", + "element_id": "loc-🐉", + "how": "whole" + } + ], + "unknown": 1, + "out": {} + } + }, + { + "name": "the answer carries no name or body of the element", + "pins": [ + { + "type": "pin", + "id": "p1", + "map": "m1", + "element_type": "character", + "element_id": "cha-1", + "x": 10, + "y": 20, + "name": "the Pin's own name", + "description": "x" + } + ], + "served": [ + { + "type": "character", + "id": "cha-1", + "name": "A secret name", + "description": "A body", + "known_of": true, + "supertype": "Spy" + } + ], + "want": { + "known": [ + { + "pin": "p1", + "map": "m1", + "element_type": "character", + "element_id": "cha-1", + "how": "known_of" + } + ], + "unknown": 0, + "out": {} + } + } + ], + "refusals": [ + { + "name": "pins is null", + "pins": null, + "served": [], + "refuses": "pins" + }, + { + "name": "pins is an object", + "pins": { + "type": "pin", + "id": "p1" + }, + "served": [], + "refuses": "pins" + }, + { + "name": "pins is a string", + "pins": "pin", + "served": [], + "refuses": "pins" + }, + { + "name": "pins is a number", + "pins": 7, + "served": [], + "refuses": "pins" + }, + { + "name": "served is null", + "pins": [], + "served": null, + "refuses": "served" + }, + { + "name": "served is an object", + "pins": [], + "served": { + "type": "location", + "id": "loc-1" + }, + "refuses": "served" + }, + { + "name": "served is a string", + "pins": [], + "served": "location", + "refuses": "served" + }, + { + "name": "both are wrong: pins is named", + "pins": null, + "served": null, + "refuses": "pins" + } + ] +} diff --git a/tests/test_pins_known_to.py b/tests/test_pins_known_to.py new file mode 100644 index 0000000..6ad6678 --- /dev/null +++ b/tests/test_pins_known_to.py @@ -0,0 +1,123 @@ +"""``pins_known_to``: which of a map's Pins one reader knows. The shared cases +(``tests/fixtures/pins-known-to.cases.json``, the same bytes as the npm SDK's +``test/fixtures/pins-known-to.cases.json``; both suites hold its sha256, every want written by hand in the SDK's +generator): the whole answer of each case, key order included, and each refusal; then what the cases cannot hold: +the inputs left as they were, a tuple, and the answer beside ``map_space``'s.""" + +from __future__ import annotations + +import hashlib +import json +from pathlib import Path +from typing import Any + +import pytest + +from onlyworlds import map_space, pins_known_to + +FIXTURE = Path(__file__).resolve().parent / "fixtures" / "pins-known-to.cases.json" +FIXTURE_SHA256 = "80f66dba8aa1a009770bdcffbf457ec84b78f7e4270a2fb9cf8614a4b76e39f6" + + +def _data() -> dict[str, Any]: + """The file parsed afresh: a test that checks the inputs were not written to reads its own copy.""" + data: dict[str, Any] = json.loads(FIXTURE.read_bytes()) + return data + + +def _text(value: Any) -> str: + """JSON text holds the key order and every value: two answers are the same exactly when their texts are.""" + return json.dumps(value, ensure_ascii=False, separators=(",", ":")) + + +def test_the_cases_file_is_the_pinned_bytes() -> None: + assert hashlib.sha256(FIXTURE.read_bytes()).hexdigest() == FIXTURE_SHA256 + data = _data() + assert data["version"] == 1 + assert len(data["cases"]) == 31 + assert len(data["refusals"]) == 8 + + +def test_every_shared_case_the_whole_answer_key_order_included() -> None: + wrong = [] + for case in _data()["cases"]: + got, want = _text(pins_known_to(case["pins"], case["served"])), _text(case["want"]) + if got != want: + wrong.append(f"{case['name']}\n want {want}\n got {got}") + assert not wrong, f"{len(wrong)} case(s) disagree:\n " + "\n ".join(wrong) + + +def test_every_shared_refusal_a_type_error_that_names_the_argument_and_no_other() -> None: + wrong = [] + for refusal in _data()["refusals"]: + other = "served" if refusal["refuses"] == "pins" else "pins" + try: + pins_known_to(refusal["pins"], refusal["served"]) + except TypeError as error: + # "is a list": the interpreter's own "object is not iterable" is not this refusal. + if f"{refusal['refuses']} is a list" not in str(error) or f"{other} is a list" in str(error): + wrong.append(f"{refusal['name']}: {error!s} does not name {refusal['refuses']} alone") + except Exception as error: + wrong.append(f"{refusal['name']}: raised {type(error).__name__}") + else: + wrong.append(f"{refusal['name']}: answered") + assert not wrong, f"{len(wrong)} refusal(s) disagree:\n " + "\n ".join(wrong) + + +@pytest.mark.parametrize("value", [None, {"type": "pin"}, "pin", 7, iter([]), {"p1"}]) +def test_only_a_list_or_a_tuple_is_taken(value: Any) -> None: + with pytest.raises(TypeError, match="pins is a list"): + pins_known_to(value, []) + with pytest.raises(TypeError, match="served is a list"): + pins_known_to([], value) + + +def test_a_tuple_reads_as_a_list() -> None: + for case in _data()["cases"]: + assert _text(pins_known_to(tuple(case["pins"]), tuple(case["served"]))) == _text(case["want"]), case["name"] + + +def test_the_inputs_are_left_as_they_were() -> None: + before, run = _data()["cases"], _data()["cases"] + for case in run: + pins_known_to(case["pins"], case["served"]) + for was, now in zip(before, run, strict=True): + assert _text(now["pins"]) == _text(was["pins"]), f"{was['name']}: pins" + assert _text(now["served"]) == _text(was["served"]), f"{was['name']}: served" + + +def test_two_calls_answer_two_objects() -> None: + pins = [{"type": "pin", "id": "p1", "map": "m1", "element_type": "location", "element_id": "l1"}] + served = [{"type": "location", "id": "l1"}] + a, b = pins_known_to(pins, served), pins_known_to(pins, served) + assert a is not b + assert a["known"] is not b["known"] + assert a["known"][0] is not b["known"][0] + assert a["out"] is not b["out"] + + +def test_known_of_is_a_stub_only_when_it_is_exactly_true() -> None: + """Python's own trap, which the shared case's ``1`` holds only by identity: ``1 == True``.""" + pins = [{"type": "pin", "id": "p1", "element_type": "location", "element_id": "l1"}] + assert pins_known_to(pins, [{"type": "location", "id": "l1", "known_of": 1}])["known"][0]["how"] == "whole" + assert pins_known_to(pins, [{"type": "location", "id": "l1", "known_of": 1.0}])["known"][0]["how"] == "whole" + assert pins_known_to(pins, [{"type": "location", "id": "l1", "known_of": True}])["known"][0]["how"] == "known_of" + + +def test_beside_map_space_a_drawing_keeps_the_pins_both_name() -> None: + world: list[dict[str, Any]] = [ + {"type": "map", "id": "m1", "width": 100, "height": 100}, + {"type": "pin", "id": "p1", "map": "m1", "x": 1, "y": 2, "element_type": "location", "element_id": "l1"}, + {"type": "pin", "id": "p2", "map": "m1", "x": 3, "y": 4, "element_type": "location", "element_id": "l2"}, + {"type": "pin", "id": "p3", "map": "m1", "element_type": "location", "element_id": "l1"}, # no point + {"type": "pin", "id": "p4", "map": "gone", "x": 5, "y": 6, "element_type": "location", "element_id": "l1"}, + {"type": "location", "id": "l1"}, + {"type": "location", "id": "l2"}, + ] + served = [{"type": "location", "id": "l1", "known_of": True}] + answer = pins_known_to(world, served) + assert [k["pin"] for k in answer["known"]] == ["p1", "p3", "p4"] + assert answer["unknown"] == 1 + how = {k["pin"]: k["how"] for k in answer["known"]} + drawn = [p for p in map_space(world)["pins"] if p["pin"] in how] + assert [(p["pin"], p["x"], p["y"], how[p["pin"]]) for p in drawn] == [("p1", 1, 2, "known_of")]