diff --git a/meshterm/ui/packet_viewer.py b/meshterm/ui/packet_viewer.py index bb75d8d..144711e 100644 --- a/meshterm/ui/packet_viewer.py +++ b/meshterm/ui/packet_viewer.py @@ -731,15 +731,31 @@ class PacketViewer(Screen): mid-chip — and hanging its continuations under the value block. The dialog has the rows to spend, so a long chain reads whole here, unlike the feed row behind it, which has to elide its middle to stay on one line. + + A trace with no hops is the one case where an empty chain does not mean a direct + shot. A trace names its relays nowhere — it records a *reading* per hop instead + (see :func:`~meshterm.core.frames.trace_link_snrs`) — so a walked trace arrives + with an empty path and a full set of readings, and "direct — no relays" would be + a flat contradiction of the ``links`` row two lines above. It says how far the + packet got instead, which is the one thing those readings do establish. """ return path_line( (entry.path or "").split(","), self._resolve, prefix_bytes=self._prefix_bytes, self_name=self._self_name, - empty="direct — no relays", + empty=self._empty_via(entry), ) + @staticmethod + def _empty_via(entry: PacketEntry) -> str: + """What an empty relay chain means for this frame's class.""" + readings = (entry.raw or {}).get("trace_snrs") if isinstance(entry.raw, dict) else None + if readings: + hops = len(readings) + return f"{hops} hop{'' if hops == 1 else 's'} walked — a trace logs each leg, not its relays" + return "direct — no relays" + def _packet_rows(self, entry: PacketEntry) -> list[tuple[str, RenderableType]]: """A raw ``packet`` entry's addressing, parsed class/route, relay path, and — for a channel frame naming a channel we hold the key for — its channel and plaintext. @@ -756,7 +772,11 @@ class PacketViewer(Screen): # last repeater, and saying so stops the figure being read as the origin's signal; # on one that crossed nothing it *is* the origin's, which is worth saying outright # — a frame heard straight off its sender is the best evidence a link ever gives. - relayed = bool([hop for hop in (entry.path or "").split(",") if hop]) + # A trace that walked counts as relayed even with no hops to name: its readings + # are the proof something forwarded it (see :meth:`_via_path`). + relayed = bool( + [hop for hop in (entry.path or "").split(",") if hop] or raw.get("trace_snrs") + ) rows.append(( "", Text( "reception describes the last relay, not the origin" if relayed diff --git a/tests/test_packet_viewer.py b/tests/test_packet_viewer.py index de22053..2e82bf2 100644 --- a/tests/test_packet_viewer.py +++ b/tests/test_packet_viewer.py @@ -414,3 +414,39 @@ def test_packet_viewer_says_whose_signal_the_reading_is() -> None: assert "last relay, not the origin" in note("a1b2c3,d4e5f6") assert "the sender itself" in note("") assert "last relay" not in note("") + + +def test_packet_viewer_does_not_call_a_walked_trace_direct() -> None: + """A trace names no relays, so an empty chain there means "unrecorded", not "none". + + Its readings prove three nodes forwarded it; saying "direct — no relays" two lines + under a links row listing three legs would contradict the card's own evidence. + """ + entry = PacketEntry( + when=utcnow(), kind="packet", path="", snr=13.75, + raw={"payload_typename": "TRACE", "trace_snrs": [13.25, -5.0, -4.25]}, + ) + body = _plain(_viewer(entry).render_body(80)) + assert "3 hops walked" in body + assert "direct — no relays" not in body + assert "last relay, not the origin" in body # something *did* relay it + + +def test_packet_viewer_still_calls_an_unwalked_frame_direct() -> None: + """Nothing forwarded it and nothing measured it — that really is a direct shot.""" + for raw in ({"payload_typename": "TRACE", "trace_snrs": []}, + {"payload_typename": "TEXT_MSG"}): + body = _plain(_viewer( + PacketEntry(when=utcnow(), kind="packet", path="", snr=9.0, raw=raw) + ).render_body(80)) + assert "direct — no relays" in body, raw + assert "the sender itself" in body, raw + + +def test_packet_viewer_counts_one_walked_hop_in_the_singular() -> None: + """One leg is "1 hop walked", not "1 hops walked".""" + entry = PacketEntry( + when=utcnow(), kind="packet", path="", snr=13.75, + raw={"payload_typename": "TRACE", "trace_snrs": [13.25]}, + ) + assert "1 hop walked" in _plain(_viewer(entry).render_body(80))