Files
MeshTerm/tests/test_widgets.py
T
Jean-Pierre MartineauandClaude Opus 4.8 969989ab45 Node hues: a 256-colour spectrum keyed on the first byte
Node-identity colouring drew from an 8-entry palette, folding the first
key byte down with `% 8` so ~32 keys shared each hue and a handful of
nodes on screen often collided. Map the first byte straight onto the HSV
wheel instead — 0x00 red, sweeping the full spectrum to 0xff — at a fixed
saturation/value (0.65/0.95) tuned to stay vivid and readable on the dark
theme across every hue. All 256 first-byte values now give 256 distinct
hues.

Only node identity changes: node_style was the sole consumer of the old
NAME_COLORS palette, which is removed. The rest of the theme (brand,
status, SNR, battery, you-white) is untouched. Keying stays on the first
byte alone, so a 2-hex path hop and a full 64-hex key still agree — one
node, one colour everywhere. Red is no longer reserved out of the node
spectrum (0x00 = red), but node names are always bold and in name lanes,
distinct from the err-styled status marks.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-19 20:53:10 -04:00

177 lines
7.6 KiB
Python

"""Shared-widget helper tests: the canonical formatting primitives every screen leans on.
Most widgets are exercised through their host screens' tests; what lives here are the
pure text helpers whose exact output *is* the app-wide convention — get these right once
and every caller inherits it.
"""
from __future__ import annotations
from meshterm.ui.widgets import _format_age, format_ago, path_text
def test_format_age_is_the_bare_column_form() -> None:
"""The lane form stays suffix-free at every magnitude, for aligned age columns."""
assert _format_age(None) == "never"
assert _format_age(5) == "now"
assert _format_age(90) == "1m"
assert _format_age(7200) == "2h"
assert _format_age(180000) == "2d"
assert _format_age(1300000) == "2w"
def test_format_ago_speaks_grammatical_prose() -> None:
"""The prose form says "5m ago" but bare "now"/"never" — never "now ago"."""
assert format_ago(5) == "now"
assert format_ago(None) == "never"
assert format_ago(90) == "1m ago"
assert format_ago(7200) == "2h ago"
def _resolve(hop: str) -> str:
return {"aa": "Alice", "3d": "YUL"}.get(hop, hop)
def test_path_text_names_hops_and_keeps_hashes_bare() -> None:
"""Named hops read as their name alone; unnamed ones as their bare hash."""
text = path_text(["aa", "77", "3d"], _resolve)
assert text.plain == "Alice → 77 → YUL" # no parenthesized hash after a name
def test_path_text_marks_us_white_and_names_in_their_hue() -> None:
"""Our own node takes the white ``you`` style; other names their palette hue."""
text = path_text(["aa", "3d"], _resolve, self_name="Alice")
styles = {text.plain[s.start : s.end]: str(s.style) for s in text.spans}
assert styles.get("Alice") == "you"
assert "YUL" in styles and styles["YUL"] != "you"
def test_path_text_empty_reads_as_direct() -> None:
"""No hops (or only empty tokens) renders the caller's empty word, muted."""
assert path_text([], _resolve).plain == "direct"
assert path_text(["", ""], _resolve).plain == "direct"
assert path_text([], _resolve, empty="direct — no relays").plain == "direct — no relays"
def test_path_text_lights_the_hash_prefix() -> None:
"""An unnamed hop renders through the shared hash widget, its prefix lit in the
node's hash-derived hue."""
from meshterm.ui.theme import node_style
text = path_text(["77bb"], _resolve, prefix_bytes=1)
lit = [
text.plain[s.start : s.end]
for s in text.spans
if str(s.style) == node_style("77bb")
]
assert "77" in lit # the addressed prefix stands out within the hash
def test_path_text_hash_as_name_shows_grey_identity_hash() -> None:
"""An unnamed hop stands in its own hash at the mode width, muted, plus its byte."""
text = path_text(
["e839f2ab"], _resolve, prefix_bytes=3,
show_hash=True, hash_bytes=1, hash_as_name=True,
)
assert text.plain == "e839f2 (e8)" # mode width identity, then the addressed byte
styles = {text.plain[s.start : s.end]: str(s.style) for s in text.spans}
assert styles.get("e839f2") == "muted" # greyed — colour is the "this is a name" cue
from meshterm.ui.theme import node_style
assert not any(str(s.style) == node_style("e839f2ab") for s in text.spans) # no prefix lit
def test_path_text_hash_as_name_drops_the_byte_when_it_is_the_whole_hash() -> None:
"""When the mode width is one byte, the identity already is the byte — no ``(e8)``."""
text = path_text(
["e839f2ab"], _resolve, prefix_bytes=1,
show_hash=True, hash_bytes=1, hash_as_name=True,
)
assert text.plain == "e8"
def test_path_text_trace_flavour_annotates_hashes_and_brackets_us() -> None:
"""show_hash appends the addressed hash to names; None hops are our device."""
text = path_text(
[None, "aa", "77bb", None], _resolve, self_name="Homestead",
show_hash=True, hash_bytes=1, device_hash="c0ffee",
)
assert text.plain == "Homestead (c0) → Alice (aa) → 77 → Homestead (c0)"
def test_path_text_dims_the_tail_from_dim_from() -> None:
"""The mirrored return leg (and the arrows into it) render faint."""
text = path_text(
[None, "aa", "3d", "aa", None], _resolve, self_name="us",
show_hash=True, dim_from=3,
)
styles = [(text.plain[s.start : s.end], str(s.style)) for s in text.spans]
assert ("us", "you") in styles # the departure keeps the white you
assert ("us", "faint") in styles # the landing back on us is faint
assert any(run.startswith("Alice") and style == "faint" for run, style in styles)
assert any(run.startswith("YUL") and style != "faint" for run, style in styles)
def test_path_text_cursor_arrow_reads_as_a_reverse_block() -> None:
"""The composer's insertion cursor: that one arrow renders as a selected block."""
text = path_text(["aa", "77", "3d"], _resolve, cursor_arrow=1)
assert text.plain == "Alice → 77 → YUL" # spacing survives the split append
styles = [(text.plain[s.start : s.end], str(s.style)) for s in text.spans]
assert styles.count(("→", "selected")) == 1 # only arrow 1, between 77 and YUL
assert (" → ", "muted") in styles # the other arrow stays plain
# The cursor wins over dim_from: the arrow into a dimmed tail still reads selected.
dimmed = path_text([None, "aa", None], _resolve, self_name="us", dim_from=2, cursor_arrow=1)
assert any(str(s.style) == "selected" for s in dimmed.spans)
# --- the name→key resolver and the keyless-stays-muted rule --------------------------
def test_make_name_key_resolver_contacts_win_over_stored_names() -> None:
"""Contacts resolve first; stored advert names fill in strangers; misses are None."""
from meshterm.core.models import Contact
from meshterm.services.trace_runner import make_name_key_resolver
contacts = [Contact(name="Alice", public_key="d4" + "0" * 62)]
stored = {"60aabbccdd11": "Bob", "77ee00112233": "Alice"} # a stale stored Alice
key_of = make_name_key_resolver(contacts, stored)
assert key_of("Alice") == "d4" + "0" * 62 # the contact's key, not the stored id
assert key_of("alice") == "d4" + "0" * 62 # casefolded
assert key_of("Bob") == "60aabbccdd11" # a stored-name stranger still lands
assert key_of("Zed") is None # nobody carries the name
def test_name_style_without_a_key_is_muted() -> None:
"""A keyless name has no hue — colour is reserved for keyed identities."""
from meshterm.ui.theme import name_style, node_style
assert name_style("Stranger") == "muted"
keyed = name_style("Alice", "d4" + "0" * 62)
assert keyed == node_style("d4" + "0" * 62) and keyed.startswith("bold #")
def test_name_rgb_keyless_lands_on_the_muted_grey() -> None:
"""The raster twin: a keyless name's RGB is the muted grey, never a crash."""
from meshterm.ui.widgets import name_rgb
assert name_rgb("Stranger") == (148, 163, 184)
assert name_rgb("Alice", "d4" + "0" * 62) != (148, 163, 184)
def test_route_graph_source_label_takes_its_resolved_keys_hue() -> None:
"""The graph's left endpoint hue rides key_of: resolved → the key's hue, else muted."""
from meshterm.ui.widgets import name_rgb, route_graph_style
_, _, rgb_known = route_graph_style(
resolve=lambda h: h, self_name="us", source="Alice",
key_of=lambda name: "d4" + "0" * 62 if name == "Alice" else None,
)
from meshterm.ui.pathgraph import SRC_NODE
assert rgb_known(SRC_NODE) == name_rgb("Alice", "d4" + "0" * 62)
_, _, rgb_unknown = route_graph_style(
resolve=lambda h: h, self_name="us", source="Alice",
)
assert rgb_unknown(SRC_NODE) == (148, 163, 184) # unresolvable origin stays muted