From 8da00aecc9f9d0ab06fbd473de4e163cc9749e3c Mon Sep 17 00:00:00 2001 From: Titus Date: Tue, 24 Nov 2026 06:05:58 +0100 Subject: [PATCH] 0.148.1: read_world_file accepts a tool's world.json under a dot-folder inside the world The reader refused a world file whose world holds a world.json under a dot-folder (abbey/.atlas/world.json, abbey/elements/character/.trash/world.json) as a second world (OW-5). A dot-folder is never a world, wherever it sits, and read_folder never reads one, so the same tree opened as a folder and was refused as a file. The OW-5 loop now skips a path when any folder segment of it, relative to the world's root, starts with a dot. Nothing else moves: every other check still runs on those entries, a . or .. segment is still refused, they are still returned in entries, a nested world.json elsewhere (abbey/inner/world.json) is still refused, and records/ is still excepted. This follows a ruling on the world-file conformance checks OW-4 and OW-5; the conformance case comes with the next schema distribution serial. Five tests build the archives by hand, the same cases in the same order as the npm SDK's: three accepted shapes (a root dot-folder, one under a type folder, a ..atlas name), each written out with to_folder and read with read_folder against the same tree written directly; the refusals that stay; and records/. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 ++ src/onlyworlds/_version.py | 2 +- src/onlyworlds/worldfile.py | 11 +++-- tests/test_worldfile.py | 81 +++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 173de9a..86df296 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.1 (2026-11-24) + +- **Fixed: `read_world_file` no longer refuses a world file whose world holds a tool's own `world.json` under a dot-folder** (`abbey/.atlas/world.json`, or deeper, `abbey/elements/character/.trash/world.json`). A dot-folder is never a world, wherever it sits, as the folder reader already had it: `read_folder` never reads a dot-folder. So the same world now opens as a file and as a folder. Only the check for a second `world.json` inside the world skips those paths; every other check still runs on their entries (paths, names, duplicates, collisions, size and count), and they are still returned in `entries`. A nested `world.json` outside a dot-folder and outside `records/` (`abbey/inner/world.json`) is still refused. This follows a ruling on the world-file conformance checks OW-4 and OW-5; the conformance case comes with the next schema distribution serial. + ## 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`. diff --git a/src/onlyworlds/_version.py b/src/onlyworlds/_version.py index bd1f11a..dab408e 100644 --- a/src/onlyworlds/_version.py +++ b/src/onlyworlds/_version.py @@ -1 +1 @@ -__version__ = "0.148.0" +__version__ = "0.148.1" diff --git a/src/onlyworlds/worldfile.py b/src/onlyworlds/worldfile.py index 77c5ad7..560a0e6 100644 --- a/src/onlyworlds/worldfile.py +++ b/src/onlyworlds/worldfile.py @@ -30,8 +30,9 @@ - ``marker`` (OW-6): a root ``mimetype`` entry with other bytes than the media type. A marker is never required. - ``not_a_world``, ``more_than_one`` (OW-2, OW-3, OW-5): no world, two worlds, a second - ``world.json`` inside the world (one under ``records/`` is a producer's data), a - ``world.json`` that will not parse or has no string id. + ``world.json`` inside the world (one under ``records/`` is a producer's data; one under a + dot-folder is a tool's, since a dot-folder is never a world), a ``world.json`` that will not + parse or has no string id. Files beside the world's folder and archive junk (``__MACOSX``, ``.DS_Store``, ``Thumbs.db``) are ignored (OW-4), still checked and counted. An ``api`` block in @@ -711,10 +712,14 @@ def read_world_file( if len(roots) > 1: _refuse("more_than_one", f"The file holds worlds in {', '.join(roots)}.") prefix = roots[0] - # OW-5: no world.json inside the world but its own, except a producer's under records/. + # OW-5: no world.json inside the world but its own, except a producer's under records/. A dot-folder is + # never a world, wherever it sits: a tool's own world.json under one is not looked at here, as a folder + # reader never reads it. Only this check skips those paths; every check above ran on them. inside = {n for n in candidates if n.startswith(prefix)} for n in sorted(inside): rel = n[len(prefix) :] + if any(seg.startswith(".") for seg in rel.split("/")[:-1]): + continue if rel != WORLD_FILE and rel.endswith(f"/{WORLD_FILE}") and not rel.startswith("records/"): _refuse("more_than_one", f"A second world.json sits inside the world: {n}.") diff --git a/tests/test_worldfile.py b/tests/test_worldfile.py index c630ef5..35884e3 100644 --- a/tests/test_worldfile.py +++ b/tests/test_worldfile.py @@ -417,6 +417,87 @@ def test_to_folder_writes_every_entry_byte_for_byte_and_never_over_a_folder(tmp_ assert not (tmp_path / "evil.json").exists() and not (tmp_path / "forged").exists() +# A dot-folder is never a world, wherever it sits: OW-5 does not look under one. The same five +# cases, in the same order, are in the npm SDK's test/world-file.test.mjs. +ABBEY = "0192a000-aaaa-7bbb-8ccc-0000000000ab" +ADA = "0192a000-aaaa-7bbb-8ccc-0000000000a1" +ELSEWHERE = encode({"id": "0192a000-aaaa-7bbb-8ccc-0000000000ef", "name": "Elsewhere"}) + + +def abbey_tree(more: dict[str, bytes] | None = None) -> dict[str, bytes]: + """Abbey's world folder, path -> bytes: its world.json, one character, and ``more``.""" + return { + "world.json": encode({"id": ABBEY, "name": "Abbey"}), + "elements/character/ada.json": encode({"id": ADA, "name": "Ada"}), + **(more or {}), + } + + +def abbey_zip(files: dict[str, bytes]) -> bytes: + """The tree zipped under one top-level folder ``abbey/``, built here: the writer never emits a + dot-folder unprompted.""" + return zip_of({f"abbey/{rel}": data for rel, data in files.items()}) + + +def folder_view(root: Path) -> dict[str, Any]: + """What ``read_folder`` makes of a folder, every path relative to it.""" + folder = read_folder(root) + + def rel(p: Path) -> str: + return p.relative_to(root).as_posix() + + return { + "world": folder.world, + "elements": sorted( + (el.layout, rel(el.path), el.id, el.type, encode(el.body)) for el in folder.all_elements().values() + ), + "skipped": [(rel(s.path), s.reason) for s in folder.skipped], + "duplicates": [(d.id, rel(d.kept), rel(d.ignored)) for d in folder.duplicates], + "ignored_dirs": [rel(p) for p in folder.ignored_dirs], + } + + +def reads_as_its_folder(files: dict[str, bytes], tmp_path: Path) -> None: + """The file is accepted as abbey, carries every entry, and reads to exactly what the folder reads to.""" + r = read_world_file(abbey_zip(files)) + assert (r.world_id, r.name, r.top) == (ABBEY, "Abbey", "abbey") + assert list(r.entries) == sorted(files) + from_file = folder_view(r.to_folder(tmp_path / "from-file")) + assert [e[1] for e in from_file["elements"]] == ["elements/character/ada.json"] + assert from_file == folder_view(make_folder(tmp_path / "folder", files)) + + +def test_ow5_a_tools_world_json_in_a_dot_folder_at_the_root_is_no_second_world(tmp_path: Path) -> None: + reads_as_its_folder( + abbey_tree({".atlas/world.json": ELSEWHERE, ".atlas/world-prefs.json": encode({"zoom": 2})}), tmp_path + ) + + +def test_ow5_a_dot_folder_deeper_in_the_world_is_no_second_world_either(tmp_path: Path) -> None: + reads_as_its_folder(abbey_tree({"elements/character/.trash/world.json": ELSEWHERE}), tmp_path) + + +def test_ow5_a_name_that_is_dots_and_more_is_a_dot_folder_name(tmp_path: Path) -> None: + reads_as_its_folder(abbey_tree({"..atlas/world.json": ELSEWHERE}), tmp_path) + + +def test_ow5_still_refused_each_by_its_own_reason() -> None: + refuses("more_than_one", abbey_zip(abbey_tree({"inner/world.json": ELSEWHERE}))) + refuses("unsafe_path", abbey_zip(abbey_tree({"inner/.atlas/../world.json": ELSEWHERE}))) + refuses("unsafe_path", abbey_zip(abbey_tree({".atlas/./world.json": ELSEWHERE}))) + # Only a folder's name counts: the dot-folder beside it is skipped, inner/world.json is not, and a + # dot file is no folder. + for beside in ({"inner/.hidden/world.json": ELSEWHERE}, {"inner/.hidden": b"x"}): + error = refuses("more_than_one", abbey_zip(abbey_tree({**beside, "inner/world.json": ELSEWHERE}))) + assert str(error) == "A second world.json sits inside the world: abbey/inner/world.json." + + +def test_ow5_records_is_still_excepted() -> None: + r = read_world_file(abbey_zip(abbey_tree({"records/lab/world.json": ELSEWHERE}))) + assert r.world_id == ABBEY + assert list(r.entries) == ["elements/character/ada.json", "records/lab/world.json", "world.json"] + + # --- the writer --------------------------------------------------------------------------------