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>
120 lines
5.0 KiB
Python
120 lines
5.0 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
||
"""Tests for the Outbound paths pages: the rows, the evidence line, and the measure walk."""
|
||
|
||
from __future__ import annotations
|
||
|
||
from pathlib import Path
|
||
|
||
from meshterm.core.models import Contact, Hop, TraceResult, utcnow
|
||
from meshterm.core.outbound_store import OutboundStore
|
||
from meshterm.persistence.repository import TracedPath
|
||
from meshterm.services.outbound import DeviceIdentity, FirstLink, first_link
|
||
from meshterm.services.topology import build_topology
|
||
from meshterm.ui.outbound_screen import (
|
||
_count,
|
||
_editor_items,
|
||
_Env,
|
||
_evidence,
|
||
_measured,
|
||
_page,
|
||
_site_actions,
|
||
measure_walk,
|
||
)
|
||
from meshterm.ui.tui.select import Choice
|
||
|
||
US = "aaaaaaaaaaaa" + "0" * 52
|
||
HUB = Contact(name="Hub", public_key="3d63c6429436" + "0" * 52, key_prefix="3d63c6429436")
|
||
FAR = Contact(name="Far", public_key="f2c24f54551e" + "0" * 52, key_prefix="f2c24f54551e")
|
||
|
||
|
||
def _env(walks: int = 3) -> _Env:
|
||
"""A session with one trace walk through Hub to Far, repeated."""
|
||
walk = TracedPath(when=utcnow(), hops=[("3d", 9.0), ("f2", -4.0), ("3d", -3.0), (None, 11.0)])
|
||
topo = build_topology(
|
||
self_id=US, contacts=[HUB, FAR], trace_paths=[walk] * walks, packet_paths=[]
|
||
)
|
||
return _Env(DeviceIdentity(US, "Me"), [HUB, FAR], topo, 1)
|
||
|
||
|
||
def _plain(items: list) -> list[str]:
|
||
"""The text of each row and separator, as the user reads it."""
|
||
out = []
|
||
for item in items:
|
||
if isinstance(item, Choice):
|
||
text = item.text(200)
|
||
else:
|
||
text = item.title(200) if callable(item.title) else item.title
|
||
out.append(text if isinstance(text, str) else text.plain)
|
||
return out
|
||
|
||
|
||
def test_measure_walk_turns_at_the_last_hop() -> None:
|
||
"""Out along the path and back the same way: each link in both directions."""
|
||
assert measure_walk(("a1",)) == ("a1",)
|
||
assert measure_walk(("a1", "b2", "c3")) == ("a1", "b2", "c3", "b2", "a1")
|
||
|
||
|
||
def test_first_link_keeps_the_two_directions_apart() -> None:
|
||
"""OUT is what the first hop measured from us. IN is what we measured from it."""
|
||
link = first_link(_env().topology, ("3d63c6429436", "f2c24f54551e"))
|
||
assert (link.out_snr, link.out_count, link.in_snr, link.in_count) == (9.0, 3, 11.0, 3)
|
||
assert first_link(_env().topology, ("9a9a",)) == FirstLink()
|
||
|
||
|
||
def test_the_evidence_line_names_both_directions_or_says_there_is_none() -> None:
|
||
"""The line under a path reads ``out … · in … · age``, or says that nothing is known."""
|
||
line = _evidence(FirstLink(9.0, 12, None, 0, utcnow())).plain
|
||
assert line.strip().startswith("out +9 dB ×12 · in -")
|
||
assert "no readings" in _evidence(FirstLink()).plain
|
||
assert [_count(n) for n in (12, 980, 2140, 14600)] == ["12", "980", "2.1k", "14k"]
|
||
|
||
|
||
def test_the_tool_page_marks_the_active_site_and_offers_to_fix_the_device(tmp_path: Path) -> None:
|
||
"""Sites first, with ✓ on the active one, then the fixed devices and the fix row."""
|
||
store = OutboundStore(tmp_path / "o.json")
|
||
store.put_site("Home", [("3d63c6429436",)])
|
||
store.put_site("Lab", [])
|
||
store.set_active("Home")
|
||
title, prompt, items = _page(store, _env())
|
||
rows = _plain(items)
|
||
assert (title, prompt) == ("Outbound paths", "Now: site Home")
|
||
assert any(row.startswith("✓ Home") and "1 path" in row for row in rows)
|
||
assert any("Fix Me…" in row for row in rows)
|
||
store.fix_device(US, "Me", [])
|
||
_, prompt, items = _page(store, _env())
|
||
assert prompt.startswith("Now: Me's own paths")
|
||
assert not any("Fix Me…" in row for row in _plain(items))
|
||
|
||
|
||
def test_the_editor_shows_apply_only_with_staged_changes(tmp_path: Path) -> None:
|
||
"""An editor ends on its last action until a change is staged, then on Apply over Back."""
|
||
store = OutboundStore(tmp_path / "o.json")
|
||
store.put_site("Home", [("3d63c6429436",)])
|
||
clean = _plain(_editor_items(_env(), [("3d63c6429436",)], _site_actions(store, "Home"), 0))
|
||
assert not any("Apply" in row for row in clean)
|
||
assert any("Use this site" in row for row in clean)
|
||
staged = _plain(_editor_items(_env(), [], _site_actions(store, "Home"), 1))
|
||
assert any("Apply 1 staged change" in row for row in staged)
|
||
assert any("none yet" in row for row in staged)
|
||
|
||
|
||
def test_a_measure_says_who_heard_whom() -> None:
|
||
"""Each hop of the reply is one line: the receiver, the sender, and the SNR."""
|
||
walk = measure_walk(("3d63c6429436", "f2c24f54551e"))
|
||
result = TraceResult(
|
||
target="(path)",
|
||
success=True,
|
||
hops=[Hop(0, None, 9.0), Hop(1, None, -4.0), Hop(2, None, -3.0), Hop(3, None, 11.0)],
|
||
round_trip_ms=1500.0,
|
||
)
|
||
lines = _measured(_env(), walk, result).plain.splitlines()
|
||
assert lines == [
|
||
"✓ the trace came back in 1.5 s",
|
||
"Hub heard you at +9.0 dB",
|
||
"Far heard Hub at -4.0 dB",
|
||
"Hub heard Far at -3.0 dB",
|
||
"you heard Hub at +11.0 dB",
|
||
]
|
||
failed = TraceResult(target="(path)", success=False)
|
||
assert _measured(_env(), walk, failed).plain.startswith("✗ no reply")
|