Files
MeshTerm/tests/test_places.py
T
Jean-Pierre MartineauandClaude Haiku 5.5 6c63697e21 Nodes get a place: the named area nearest their location
A background job finds the place of each node that advertises a location
(Villeray, Montréal; a village; a peak near its summit). The map tiles give
the first answer offline; Nominatim corrects it when online (place_source,
default tiles then Nominatim; nominatim_url). Places are stored in the new
node_places table, a typed place in places.json wins over a found one.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-03 02:47:05 -05:00

343 lines
13 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# SPDX-License-Identifier: Apache-2.0
"""The place of a node: the rules, the typed places, the stored places, and the job.
The tiles here are made in the test: each place is a point feature at a known location, so
a test states what the map holds and what the answer must be. No test reads the network.
"""
from __future__ import annotations
import asyncio
import json
import logging
from datetime import datetime, timedelta, timezone
from pathlib import Path
from types import SimpleNamespace
import pytest
from meshterm.core.geo import TILE_PX, lonlat_to_world
from meshterm.core.models import Observation
from meshterm.core.mvt import GEOM_POINT, Feature, Layer
from meshterm.core.place_store import PlaceStore
from meshterm.core.places import (
SOURCE_NOMINATIM,
SOURCE_TILES,
SOURCE_USER,
Place,
moved,
place_from_nominatim,
place_from_tiles,
)
from meshterm.core.preferences import Preferences
from meshterm.persistence.repository import Repository
from meshterm.services import places as places_service
from meshterm.services.places import PlaceService
_EXTENT = 4096
#: A point in a city, and the offsets (km) that the tests put places at.
_LAT, _LON = 45.5440, -73.6150
_KM_LAT = 1 / 111.2 # degrees of latitude in one km
def _north(km: float) -> tuple[float, float]:
"""A location ``km`` north of the test point."""
return _LAT + km * _KM_LAT, _LON
class _Map:
"""Tiles made from named points: each point goes into its tile at each zoom."""
def __init__(self) -> None:
self.points: list[tuple[str, str, str, float, float, int]] = []
self.missing: set[tuple[int, int, int]] = set()
self.loads: list[tuple[int, int, int]] = []
def add(self, layer: str, cls: str, name: str, at: tuple[float, float], rank: int = 10) -> None:
self.points.append((layer, cls, name, at[0], at[1], rank))
def load(self, z: int, x: int, y: int) -> list[Layer] | None:
self.loads.append((z, x, y))
if (z, x, y) in self.missing:
return None
layers = {name: Layer(name, _EXTENT) for name in ("place", "mountain_peak")}
for layer, cls, name, lat, lon, rank in self.points:
wx, wy = lonlat_to_world(lat, lon, z)
if int(wx // TILE_PX) != x or int(wy // TILE_PX) != y:
continue
px = int((wx / TILE_PX - x) * _EXTENT)
py = int((wy / TILE_PX - y) * _EXTENT)
tags = {"class": cls, "name": name, "rank": rank}
layers[layer].features.append(Feature(GEOM_POINT, [[(px, py)]], tags))
return list(layers.values())
# -- the rules ----------------------------------------------------------------
def test_a_neighbourhood_gets_the_city_that_holds_it() -> None:
world = _Map()
world.add("place", "neighbourhood", "Villeray", _north(0.5))
world.add("place", "city", "Montréal", _north(-6.0), rank=3)
place = place_from_tiles(_LAT, _LON, world.load)
assert place == Place("Villeray", "Montréal", SOURCE_TILES)
assert place.label == "Villeray, Montréal"
assert place.short == "Villeray"
def test_the_most_specific_area_wins_over_a_nearer_borough() -> None:
world = _Map()
world.add("place", "suburb", "Villeray–Saint-Michel–Parc-Extension", _north(0.2))
world.add("place", "neighbourhood", "Villeray", _north(1.0))
world.add("place", "city", "Montréal", _north(-6.0), rank=3)
assert place_from_tiles(_LAT, _LON, world.load).fine == "Villeray"
def test_a_large_city_wins_over_a_nearer_small_one() -> None:
"""The rank of a label weighs the distance: a node in Ahuntsic is in Montréal."""
world = _Map()
world.add("place", "neighbourhood", "Ahuntsic", _north(0.3))
world.add("place", "city", "Laval", _north(4.4), rank=11)
world.add("place", "city", "Montréal", _north(-9.4), rank=3)
assert place_from_tiles(_LAT, _LON, world.load).coarse == "Montréal"
def test_a_peak_near_the_summit_wins() -> None:
world = _Map()
world.add("mountain_peak", "peak", "Mont-Royal", _north(0.2))
world.add("place", "neighbourhood", "Villeray", _north(0.5))
world.add("place", "city", "Montréal", _north(-6.0), rank=3)
place = place_from_tiles(_LAT, _LON, world.load)
assert place == Place("Mont-Royal", None, SOURCE_TILES, peak=True)
def test_a_peak_past_its_radius_does_not_win() -> None:
world = _Map()
world.add("mountain_peak", "peak", "Mont-Royal", _north(0.6))
world.add("place", "neighbourhood", "Villeray", _north(0.5))
world.add("place", "city", "Montréal", _north(-6.0), rank=3)
assert place_from_tiles(_LAT, _LON, world.load).fine == "Villeray"
def test_a_village_stands_alone_and_needs_no_fine_tiles() -> None:
world = _Map()
world.add("place", "village", "Val-David", _north(1.5))
place = place_from_tiles(_LAT, _LON, world.load)
assert place == Place("Val-David", None, SOURCE_TILES)
assert {z for z, _, _ in world.loads} == {11} # the country costs only z11 tiles
def test_a_node_far_from_all_places_gets_the_nearest_settlement() -> None:
world = _Map()
world.add("place", "town", "Mont-Laurier", _north(30.0))
assert place_from_tiles(_LAT, _LON, world.load).fine == "Mont-Laurier"
def test_no_place_near_is_an_empty_answer() -> None:
place = place_from_tiles(_LAT, _LON, _Map().load)
assert place is not None and place.empty
assert place.label == "" and place.short == ""
def test_a_missing_home_tile_is_no_answer() -> None:
world = _Map()
world.add("place", "village", "Val-David", _north(1.5))
wx, wy = lonlat_to_world(_LAT, _LON, 11)
world.missing.add((11, int(wx // TILE_PX), int(wy // TILE_PX)))
assert place_from_tiles(_LAT, _LON, world.load) is None
def test_a_bad_location_is_no_answer() -> None:
assert place_from_tiles(0.0, 0.0, _Map().load) is None
assert place_from_tiles(-97.0, -1042.0, _Map().load) is None
def test_a_label_names_a_city_one_time() -> None:
assert Place("Montréal", "Montréal", SOURCE_TILES).label == "Montréal"
assert Place(None, "Laval", SOURCE_TILES).short == "Laval"
def test_moved_is_past_half_a_km() -> None:
assert not moved(_LAT, _LON, *_north(0.4))
assert moved(_LAT, _LON, *_north(0.6))
def test_nominatim_gives_fine_and_coarse() -> None:
doc = {"address": {"neighbourhood": "Villeray", "suburb": "VSP", "city": "Montréal"}}
assert place_from_nominatim(doc) == Place("Villeray", "Montréal", SOURCE_NOMINATIM)
def test_nominatim_town_alone() -> None:
doc = {"address": {"village": "Val-David", "county": "Les Laurentides"}}
assert place_from_nominatim(doc) == Place("Val-David", None, SOURCE_NOMINATIM)
def test_nominatim_error_is_an_empty_answer_and_junk_is_none() -> None:
assert place_from_nominatim({"error": "Unable to geocode"}).empty
assert place_from_nominatim(None) is None
assert place_from_nominatim({"nothing": 1}) is None
# -- the typed places ---------------------------------------------------------
def test_a_typed_place_is_kept_and_cleared(tmp_path: Path) -> None:
path = tmp_path / "places.json"
store = PlaceStore(path)
store.set("A1B2C3D4E5F6AABB", "Toit du stade", when=100)
assert PlaceStore(path).get("a1b2c3d4e5f6") == "Toit du stade"
store.set("a1b2c3d4e5f6", " ", when=200)
assert PlaceStore(path).get("a1b2c3d4e5f6") is None
def test_a_field_no_record_knows_is_gone_at_the_next_write(tmp_path: Path) -> None:
path = tmp_path / "places.json"
path.write_text(
json.dumps({"places": {"a1b2c3d4e5f6": {"name": "X", "set_at": 1, "old": 2}}}),
encoding="utf-8",
)
store = PlaceStore(path)
store.set("b1b2c3d4e5f6", "Y", when=3)
saved = json.loads(path.read_text(encoding="utf-8"))
assert saved["places"]["a1b2c3d4e5f6"] == {"name": "X", "set_at": 1}
# -- the stored places --------------------------------------------------------
def _heard(repo: Repository, node: str, lat: float, lon: float, minutes_ago: int) -> None:
run = repo.start_run("monitor", {}, None)
when = datetime.now(timezone.utc) - timedelta(minutes=minutes_ago)
repo.record_observation(run, Observation(node=node, lat=lat, lon=lon, observed_at=when))
def test_node_locations_gives_the_newest(tmp_path: Path) -> None:
repo = Repository(tmp_path / "p.db")
_heard(repo, "aaaaaaaaaaaa", 45.0, -73.0, minutes_ago=30)
_heard(repo, "aaaaaaaaaaaa", 46.0, -74.0, minutes_ago=5)
_heard(repo, "bbbbbbbbbbbb", 47.0, -75.0, minutes_ago=10)
locations = repo.node_locations()
assert locations["aaaaaaaaaaaa"][:2] == (46.0, -74.0)
stamp = locations["bbbbbbbbbbbb"][2]
assert set(repo.node_locations(since=stamp)) == {"aaaaaaaaaaaa"}
def test_a_stored_place_reads_back(tmp_path: Path) -> None:
repo = Repository(tmp_path / "p.db")
repo.save_node_place("aaaaaaaaaaaa", 45.0, -73.0, Place("Mont-Royal", None, "tiles", True))
repo.save_node_place("bbbbbbbbbbbb", 46.0, -74.0, Place(None, None, "nominatim"))
stored = repo.node_places()
assert stored["aaaaaaaaaaaa"] == (Place("Mont-Royal", None, "tiles", True), 45.0, -73.0)
assert stored["bbbbbbbbbbbb"][0].empty
# -- the service --------------------------------------------------------------
def _service(tmp_path: Path, source: str = "tiles_nominatim") -> tuple[PlaceService, Repository]:
repo = Repository(tmp_path / "p.db")
ctx = SimpleNamespace(
repo=repo,
place_store=PlaceStore(tmp_path / "places.json"),
preferences=Preferences(values={"place_source": source}),
log=logging.getLogger("test"),
)
return PlaceService(ctx), repo # type: ignore[arg-type]
def _run(coro): # noqa: ANN001, ANN202 - a small helper for the async steps
return asyncio.run(coro)
def test_a_typed_place_wins_and_an_empty_place_is_none(tmp_path: Path) -> None:
service, repo = _service(tmp_path)
repo.save_node_place("aaaaaaaaaaaa", 45.0, -73.0, Place("Villeray", "Montréal", "tiles"))
repo.save_node_place("bbbbbbbbbbbb", 45.0, -73.0, Place(None, None, "tiles"))
assert service.get("AAAAAAAAAAAA0000").label == "Villeray, Montréal"
assert service.get("bbbbbbbbbbbb") is None
before = service.revision
service.set_typed("aaaaaaaaaaaa", "Le toit")
assert service.revision > before
assert service.get("aaaaaaaaaaaa") == Place("Le toit", None, SOURCE_USER)
def test_the_job_uses_the_tiles_then_nominatim(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
service, repo = _service(tmp_path)
_heard(repo, "aaaaaaaaaaaa", _LAT, _LON, minutes_ago=1)
world = _Map()
world.add("place", "neighbourhood", "Ahuntsic", _north(0.3))
world.add("place", "city", "Laval", _north(4.4), rank=3)
monkeypatch.setattr(service, "_load_tile", world.load)
service._scan_after = 0.0
_run(service.step())
assert service.get("aaaaaaaaaaaa").label == "Ahuntsic, Laval"
asked: list[tuple[float, float]] = []
def nominatim(url: str, lat: float, lon: float) -> object:
asked.append((lat, lon))
return {"address": {"neighbourhood": "Ahuntsic", "city": "Montréal"}}
monkeypatch.setattr(places_service, "ask_nominatim", nominatim)
_run(service.step())
assert service.get("aaaaaaaaaaaa") == Place("Ahuntsic", "Montréal", SOURCE_NOMINATIM)
assert len(asked) == 1
_run(service.step())
assert len(asked) == 1 # one answer for each location: never asked again
def test_tiles_only_never_asks_nominatim(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
service, repo = _service(tmp_path, source="tiles")
_heard(repo, "aaaaaaaaaaaa", _LAT, _LON, minutes_ago=1)
world = _Map()
world.add("place", "village", "Val-David", _north(1.0))
monkeypatch.setattr(service, "_load_tile", world.load)
monkeypatch.setattr(places_service, "ask_nominatim", lambda *a: pytest.fail("asked"))
service._scan_after = 0.0
_run(service.step())
_run(service.step())
assert service.get("aaaaaaaaaaaa").fine == "Val-David"
def test_a_peak_stays_when_nominatim_could_correct(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
service, repo = _service(tmp_path)
_heard(repo, "aaaaaaaaaaaa", _LAT, _LON, minutes_ago=1)
repo.save_node_place("aaaaaaaaaaaa", _LAT, _LON, Place("Mont-Royal", None, "tiles", True))
monkeypatch.setattr(places_service, "ask_nominatim", lambda *a: pytest.fail("asked"))
service._scan_after = 0.0
_run(service.step())
assert service.get("aaaaaaaaaaaa").peak
def test_a_node_that_moved_is_found_again(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
service, repo = _service(tmp_path, source="tiles")
repo.save_node_place("aaaaaaaaaaaa", *_north(-5.0), Place("Old", None, "tiles"))
_heard(repo, "aaaaaaaaaaaa", _LAT, _LON, minutes_ago=1)
world = _Map()
world.add("place", "village", "New", _north(1.0))
monkeypatch.setattr(service, "_load_tile", world.load)
service._scan_after = 0.0
_run(service.step())
assert service.get("aaaaaaaaaaaa").fine == "New"
def test_no_answer_makes_the_node_wait(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
service, repo = _service(tmp_path, source="tiles")
_heard(repo, "aaaaaaaaaaaa", _LAT, _LON, minutes_ago=1)
calls: list[int] = []
def offline(z: int, x: int, y: int) -> None:
calls.append(z)
return None
monkeypatch.setattr(service, "_load_tile", offline)
service._scan_after = 0.0
_run(service.step())
_run(service.step())
assert len(calls) == 1 # the second step does not ask again at once
assert service.get("aaaaaaaaaaaa") is None