Our node page offers the This node tools of the main menu
Device info, Device config, and Send advert now sit on our own node page, read from the menu's registry and run through the menu's own runner (`_run_selection`, now public as `run_tool`). When Device config renames or moves our node, the page opens again with the new values, and our row in the Contacts list takes the new name in place. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1b0dccc1c1
commit
85fac33356
8 files changed
+252
-29
No files matched your search
@@ -2102,6 +2102,25 @@ def test_k_on_the_contacts_list_asks_to_toggle_the_lock_of_the_highlighted_conta
|
||||
assert "^K" not in plain.footer_hint
|
||||
|
||||
|
||||
def test_our_row_takes_a_new_name_in_place() -> None:
|
||||
"""After our page renamed our node (Device config), our row shows the new name.
|
||||
|
||||
The row stays the same row: the highlight stays where it was.
|
||||
"""
|
||||
from meshterm.core.models import Contact
|
||||
from meshterm.ui.contacts_screen import ContactsScreen
|
||||
from tests.conftest import plain
|
||||
|
||||
alice = Contact(name="Alice", public_key="aa" * 32, key_prefix="aa" * 6)
|
||||
screen = ContactsScreen("Us", "cc" * 32, [alice], 1, {}, _contacts_sort())
|
||||
screen.handle("down")
|
||||
highlighted = screen._current_choice().value
|
||||
screen.refresh_self("Renamed", "cc" * 32)
|
||||
body = plain(screen.render_body(72))
|
||||
assert "Renamed" in body and "Us (you)" not in body
|
||||
assert screen._current_choice().value is highlighted
|
||||
|
||||
|
||||
async def test_k_on_the_contacts_list_toggles_the_lock_in_place(tmp_path) -> None: # noqa: ANN001
|
||||
"""^K writes the lock to the store, and the row gains its padlock on the same screen.
|
||||
|
||||
|
||||
@@ -2178,7 +2178,7 @@ async def _run_failing_tool(tmp_path: Path, monkeypatch, exc: Exception) -> _Too
|
||||
monkeypatch.setattr(tools, "get_tool", lambda name: _Tool())
|
||||
monkeypatch.setattr(ctx, "ui", ui)
|
||||
try:
|
||||
await menu._run_selection(ctx, "contacts")
|
||||
await menu.run_tool(ctx, "contacts")
|
||||
finally:
|
||||
ctx.repo.close()
|
||||
return ui
|
||||
|
||||
@@ -802,7 +802,7 @@ async def test_pop_all_passes_straight_through_an_except_exception_guard() -> No
|
||||
"""An unwind is not a tool failure.
|
||||
|
||||
The app wraps tool runs in ``except Exception``, so that a broken tool cannot stop the
|
||||
menu (:func:`meshterm.ui.menu._run_selection` is the widest such guard). An unwind must
|
||||
menu (:func:`meshterm.ui.menu.run_tool` is the widest such guard). An unwind must
|
||||
cross these guards. They must not catch it and report it as an error. This is the reason
|
||||
that :class:`PopToMenu` derives from ``BaseException``.
|
||||
"""
|
||||
|
||||
+131
-18
@@ -1540,13 +1540,8 @@ def test_the_place_row_follows_the_finder_above_where() -> None:
|
||||
assert "Villeray" not in _plain(screen.render_body(72))
|
||||
|
||||
|
||||
async def test_our_own_page_opens_and_closes(tmp_path) -> None: # noqa: ANN001
|
||||
"""Enter on our row of the Contacts list opens our page, and Esc closes it.
|
||||
|
||||
The page of our node has no contact. Before, the action rows read a value that only the
|
||||
page of a contact sets, and the page raised ``UnboundLocalError`` before it showed.
|
||||
"""
|
||||
import asyncio
|
||||
def _own_page_ctx(tmp_path): # noqa: ANN001, ANN202 - a test AppContext
|
||||
"""A context with the simulator as its device, a store on disk, and a full-screen session."""
|
||||
import io
|
||||
|
||||
from rich.console import Console
|
||||
@@ -1556,10 +1551,11 @@ async def test_our_own_page_opens_and_closes(tmp_path) -> None: # noqa: ANN001
|
||||
from meshterm.core.config import Settings
|
||||
from meshterm.core.device_store import DeviceStore
|
||||
from meshterm.persistence.repository import Repository
|
||||
from meshterm.ui.node_detail_screen import NodeDetailScreen, open_node_detail
|
||||
from meshterm.tools import load_all_tools
|
||||
from meshterm.ui.surface import TuiUi
|
||||
from meshterm.ui.tui.session import TuiSession
|
||||
|
||||
load_all_tools() # the page reads the menu sections from the registry
|
||||
settings = Settings(config_dir=tmp_path, db_path=tmp_path / "history.db")
|
||||
ctx = AppContext(
|
||||
console=Console(file=io.StringIO()),
|
||||
@@ -1570,18 +1566,52 @@ async def test_our_own_page_opens_and_closes(tmp_path) -> None: # noqa: ANN001
|
||||
mock=True,
|
||||
)
|
||||
ctx.ui = TuiUi(TuiSession())
|
||||
return ctx
|
||||
|
||||
|
||||
async def _next_page(ctx, task, *, after=None): # noqa: ANN001, ANN202 - a NodeDetailScreen
|
||||
"""The node page on top of the session, once it is a page other than ``after``.
|
||||
|
||||
The page of a node with a location reads the map in a thread, so the wait has a time
|
||||
limit and not a count of loop turns. If the task ends first, this helper raises what
|
||||
the task raised, or fails.
|
||||
"""
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
from meshterm.ui.node_detail_screen import NodeDetailScreen
|
||||
|
||||
deadline = time.monotonic() + 10
|
||||
while time.monotonic() < deadline:
|
||||
top = ctx.ui.session.top
|
||||
if isinstance(top, NodeDetailScreen) and top is not after:
|
||||
return top
|
||||
if task.done():
|
||||
task.result()
|
||||
raise AssertionError("the page closed before it showed")
|
||||
await asyncio.sleep(0.001)
|
||||
raise AssertionError("no node page showed")
|
||||
|
||||
|
||||
def _action_keys(page) -> list[str]: # noqa: ANN001 - a NodeDetailScreen
|
||||
"""The tokens of the action rows of the Info tab of ``page``."""
|
||||
return [action.key for action in page._info_actions]
|
||||
|
||||
|
||||
async def test_our_own_page_opens_and_closes(tmp_path) -> None: # noqa: ANN001
|
||||
"""Enter on our row of the Contacts list opens our page, and Esc closes it.
|
||||
|
||||
The page of our node has no contact. Before, the action rows read a value that only the
|
||||
page of a contact sets, and the page raised ``UnboundLocalError`` before it showed.
|
||||
"""
|
||||
import asyncio
|
||||
|
||||
from meshterm.ui.node_detail_screen import open_node_detail
|
||||
|
||||
ctx = _own_page_ctx(tmp_path)
|
||||
task = asyncio.ensure_future(open_node_detail(ctx, None))
|
||||
try:
|
||||
page = None
|
||||
for _ in range(500):
|
||||
top = ctx.ui.session.top
|
||||
if isinstance(top, NodeDetailScreen) or task.done():
|
||||
page = top if isinstance(top, NodeDetailScreen) else None
|
||||
break
|
||||
await asyncio.sleep(0)
|
||||
if task.done():
|
||||
task.result() # raise what the page raised, if anything
|
||||
assert page is not None
|
||||
page = await _next_page(ctx, task)
|
||||
assert page.title.endswith("(you)")
|
||||
page.resolve(CANCEL)
|
||||
assert await task is False
|
||||
@@ -1589,3 +1619,86 @@ async def test_our_own_page_opens_and_closes(tmp_path) -> None: # noqa: ANN001
|
||||
if not task.done():
|
||||
task.cancel()
|
||||
ctx.repo.close()
|
||||
|
||||
|
||||
async def test_our_own_page_offers_the_this_node_section(tmp_path) -> None: # noqa: ANN001
|
||||
"""Our page ends on the rows of the "This node" section of the main menu, in its order.
|
||||
|
||||
The page of a contact does not offer them: these tools act on our node only.
|
||||
"""
|
||||
import asyncio
|
||||
|
||||
from meshterm.ui.node_detail_screen import open_node_detail
|
||||
|
||||
ctx = _own_page_ctx(tmp_path)
|
||||
task = asyncio.ensure_future(open_node_detail(ctx, None))
|
||||
try:
|
||||
page = await _next_page(ctx, task)
|
||||
keys = _action_keys(page)
|
||||
assert keys[-3:] == ["tool:info", "tool:config", "tool:advert"]
|
||||
body = _plain(page.render_body(72))
|
||||
for title in ("Device info", "Device config", "Send advert"):
|
||||
assert title in body
|
||||
page.resolve(CANCEL)
|
||||
await task
|
||||
|
||||
contact = next(iter(await ctx.devstate.contacts()))
|
||||
task = asyncio.ensure_future(open_node_detail(ctx, contact))
|
||||
page = await _next_page(ctx, task)
|
||||
assert not [key for key in _action_keys(page) if key.startswith("tool:")]
|
||||
page.resolve(CANCEL)
|
||||
await task
|
||||
finally:
|
||||
if not task.done():
|
||||
task.cancel()
|
||||
ctx.repo.close()
|
||||
|
||||
|
||||
async def test_a_this_node_row_runs_its_tool_and_a_rename_opens_the_page_again(
|
||||
tmp_path, monkeypatch
|
||||
) -> None: # noqa: ANN001
|
||||
"""A row runs its tool as the menu does. After a rename, the page shows the new name.
|
||||
|
||||
A tool that changes nothing that the page shows leaves the same page, with its
|
||||
highlight. A rename (Device config) opens a new page in its place, one level above the
|
||||
same screen.
|
||||
"""
|
||||
import asyncio
|
||||
|
||||
from meshterm.ui import menu
|
||||
from meshterm.ui.node_detail_screen import open_node_detail
|
||||
|
||||
ctx = _own_page_ctx(tmp_path)
|
||||
info = dict(await ctx.devstate.self_info())
|
||||
ran: list[str] = []
|
||||
|
||||
async def _run_tool(_ctx, name: str) -> None: # noqa: ANN001
|
||||
ran.append(name)
|
||||
if name == "config":
|
||||
info["name"] = "Renamed"
|
||||
|
||||
async def _self_info() -> dict:
|
||||
return dict(info)
|
||||
|
||||
monkeypatch.setattr(menu, "run_tool", _run_tool)
|
||||
monkeypatch.setattr(ctx.devstate, "self_info", _self_info)
|
||||
depth = len(ctx.ui.session._stack)
|
||||
task = asyncio.ensure_future(open_node_detail(ctx, None))
|
||||
try:
|
||||
page = await _next_page(ctx, task)
|
||||
page.resolve("tool:info")
|
||||
for _ in range(50):
|
||||
await asyncio.sleep(0)
|
||||
assert ran == ["info"] and ctx.ui.session.top is page
|
||||
|
||||
page.resolve("tool:config")
|
||||
renamed = await _next_page(ctx, task, after=page)
|
||||
assert ran == ["info", "config"]
|
||||
assert renamed.title == "Node — Renamed (you)"
|
||||
assert len(ctx.ui.session._stack) == depth + 1
|
||||
renamed.resolve(CANCEL)
|
||||
assert await task is False
|
||||
finally:
|
||||
if not task.done():
|
||||
task.cancel()
|
||||
ctx.repo.close()
|
||||
Reference in new issue
Block a user