Files
MeshTerm/tests/test_site_suggest.py
T
Jean-Pierre MartineauandClaude Opus 5.5 ec64076d44 Outbound paths start the suggested routes, for each site and fixed device
The user names the ways out of their node, one or more hops long, in order.
Each site of this computer (Home, Foulab) has its own list, one site is
active, and a fixed device keeps its own list.

- Explore > Outbound paths: the sites with the active mark, the fixed
  devices, and a staged editor for each list. Each path shows how well its
  first hop hears us (out) and how well we hear it (in). Measure sends one
  trace out along a path and back, after the cooldown.
- The path composer has an outbound mode, and puts the active paths first
  while the insertion slot is at our node (trace target, trace path, the
  TX sweep route).
- MeshTopology.outbound_scenarios: one route through each path. It scores
  from the link after the path, lets the user's order win a near tie, and
  skips a path whose first hop was silent for outbound_silent_hours. These
  routes lead Explore paths, the node page's Routes tab, and the trace auto
  route, ahead of a device route that starts on another hop.
- A first link out of our node is scored from what the far end measured,
  when a trace gave such readings.
- The site suggestion: when the radio hears the first hop of another site
  three times, the main menu asks to use that site and says what it heard.
  "Never suggest" sets the new suggest_site preference off.
- CLI: meshterm outbound [show|sites|use|set|remove|fix|unfix], plain and
  --json.

Refs: #35

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 14:21:46 -05:00

98 lines
4.2 KiB
Python

# SPDX-License-Identifier: Apache-2.0
"""Tests for the site suggestion: what counts as heard, and when a suggestion is due."""
from __future__ import annotations
from meshterm.core.models import Observation
from meshterm.core.outbound_store import Site
from meshterm.services.site_suggest import HEARD_TO_SUGGEST, SiteSuggestion, SiteWatch, heard_hop
HOME = Site("Home", [("3d63c6429436",), ("27d4396a2967", "f2c24f54551e")])
LAB = Site("Foulab", [("a100008dc55e", "b2b2")])
def test_the_heard_node_is_the_last_relay_or_the_sender() -> None:
"""A relayed packet names its last relay. A direct packet names its sender."""
assert heard_hop(Observation(node="aa", kind="packet", path="11,22,A1")) == "a1"
assert heard_hop(Observation(node="BB", kind="packet", path="")) == "bb"
assert heard_hop(Observation(node="cc", kind="advert", path=None)) is None
assert heard_hop(Observation(node=None, kind="packet", path="")) is None
def test_three_packets_from_another_sites_first_hop_make_a_suggestion() -> None:
"""The first hop of the Foulab path, heard three times, suggests Foulab."""
watch = SiteWatch([HOME, LAB], "Home")
for _ in range(HEARD_TO_SUGGEST - 1):
assert watch.note("a1") is None # a 1-byte hash matches the stored key prefix
assert watch.note("a100") == SiteSuggestion("Foulab", "a100008dc55e", HEARD_TO_SUGGEST)
assert watch.settled
assert watch.note("a1") is None # one question for each session
def test_a_first_hop_of_the_active_site_settles_the_question() -> None:
"""Once the radio hears the way out of the active site, there is nothing to suggest."""
watch = SiteWatch([HOME, LAB], "home")
watch.note("a1")
assert watch.note("27d4") is None
assert watch.settled
assert all(watch.note("a1") is None for _ in range(5))
def test_a_hop_that_is_not_a_first_hop_counts_for_nothing() -> None:
"""Only the first hop of a path says where the radio is: the second hop is far away."""
watch = SiteWatch([HOME, LAB], "Home")
assert all(watch.note("b2b2") is None for _ in range(5))
assert not watch.settled
async def test_the_service_keeps_one_suggestion_for_the_menu(tmp_path) -> None: # noqa: ANN001
"""Packets from the hub make a pending suggestion, unless the connected device is fixed."""
from types import SimpleNamespace
from meshterm.core.events import MeshEvent
from meshterm.core.outbound_store import OutboundStore
from meshterm.services.site_suggest import SiteSuggester
store = OutboundStore(tmp_path / "o.json")
store.put_site("Home", list(HOME.paths))
store.put_site("Foulab", list(LAB.paths))
store.set_active("Home")
handlers: list = []
async def self_info() -> dict:
return {"public_key": "c0" * 32}
ctx = SimpleNamespace(
preferences=SimpleNamespace(suggest_site=True),
outbound_store=store,
events=SimpleNamespace(subscribe=lambda handler, *kinds: handlers.append(handler) or None),
devstate=SimpleNamespace(self_info=self_info),
)
service = SiteSuggester(ctx)
ready: list = []
service.on_ready = lambda: ready.append(True)
await service.start()
packet = MeshEvent.observation_event(Observation(node="99", kind="packet", path="77,a1"))
pending = [handlers[0](packet) for _ in range(HEARD_TO_SUGGEST)]
assert pending[:-1] == [None, None]
await pending[-1]
assert ready == [True]
assert service.take() == SiteSuggestion("Foulab", "a100008dc55e", HEARD_TO_SUGGEST)
assert service.take() is None # one time only
store.fix_device("c0" * 32, "Roof", [])
fixed = SiteSuggester(ctx)
handlers.clear()
await fixed.start()
last = [handlers[0](packet) for _ in range(HEARD_TO_SUGGEST)][-1]
await last
assert fixed.take() is None # the active site does not apply to a fixed device
def test_with_no_active_site_any_site_can_be_suggested_and_one_site_needs_no_watch() -> None:
"""No active site: the first site heard is suggested. A single active site settles at once."""
watch = SiteWatch([HOME, LAB], None)
suggestions = [watch.note("3d") for _ in range(HEARD_TO_SUGGEST)]
assert suggestions[-1] == SiteSuggestion("Home", "3d63c6429436", HEARD_TO_SUGGEST)
assert SiteWatch([HOME], "Home").settled