From 4aef3e7b820eb8f95ea67788d0a0164656165524 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Wed, 29 Jul 2026 23:12:24 -0400 Subject: [PATCH] Tests: one way to read a screen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Eleven files defined the same _plain (plus test_chat's _strip_ansi and test_timemachine's renderable flavour), and two of the copies had already diverged into join-without-strip. tests/conftest.plain is now the single strip-and-join reader — a list of lines or a bare string — and every file imports it; the local variants keep their names so call sites stay put (housekeeping H-003). Co-Authored-By: Claude Fable 5 --- tests/conftest.py | 24 ++++++++++++++++++++++-- tests/test_chat.py | 8 +------- tests/test_config_editor.py | 4 +--- tests/test_dashboard.py | 7 +++---- tests/test_live_screens.py | 10 +--------- tests/test_livefeed.py | 5 ++--- tests/test_map.py | 7 +------ tests/test_message_paths_screen.py | 4 +--- tests/test_node_detail.py | 4 +--- tests/test_packet_viewer.py | 3 +-- tests/test_records_screen.py | 3 +-- tests/test_timemachine.py | 5 +++-- tests/test_walk.py | 4 +--- 13 files changed, 39 insertions(+), 49 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 72fca0d..7e084fb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,8 @@ -"""Shared fixtures: pin path rendering to plain arrows for every test. +"""Shared fixtures and helpers: plain-text screen reading, and pinned path rendering. + +:func:`plain` is THE way a test reads a rendered screen — every screen test needs the +same move (strip the ANSI colour escapes, join the rendered lines) before it can assert +on content, and each file re-deriving it is how the copies drift. The powerline verdict is environmental — the suite may run inside VS Code or Windows Terminal (both chip-capable) or a bare CI shell (not) — and screen assertions must @@ -9,12 +13,28 @@ specific tests pass explicit modes or monkeypatch the widget's own switch. from __future__ import annotations -from typing import Iterator +import re +from typing import Iterable, Iterator, Union import pytest from meshterm.ui.termfont import powerline_support +#: ANSI SGR escapes — the colour runs a rendered line carries between its characters. +_ANSI = re.compile(r"\x1b\[[0-9;]*m") + + +def plain(rendered: Union[str, Iterable[str]]) -> str: + """The plain-text form of rendered screen output, for content assertions. + + Takes what a render handed back — a list of ANSI lines (joined with newlines) or a + single already-joined string — and strips the colour escapes, so assertions read + the screen the way a viewer does. + """ + if not isinstance(rendered, str): + rendered = "\n".join(rendered) + return _ANSI.sub("", rendered) + @pytest.fixture(autouse=True) def _plain_path_rendering(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: diff --git a/tests/test_chat.py b/tests/test_chat.py index ad1d2c1..c7ef038 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -12,13 +12,7 @@ from pathlib import Path import pytest -#: Strip ANSI SGR escapes so rendered transcript lines can be asserted as plain text. -_ANSI = re.compile(r"\x1b\[[0-9;]*m") - - -def _strip_ansi(text: str) -> str: - """Remove ANSI color escapes from rendered lines for plain-text assertions.""" - return _ANSI.sub("", text) +from tests.conftest import plain as _strip_ansi # THE strip-and-join screen reader from meshterm.core.channels import DEFAULT_PUBLIC_SECRET, derive_secret from meshterm.core.config import Settings diff --git a/tests/test_config_editor.py b/tests/test_config_editor.py index 49bf350..c27df2f 100644 --- a/tests/test_config_editor.py +++ b/tests/test_config_editor.py @@ -34,9 +34,7 @@ from meshterm.ui.tui.screen import CANCEL _ANSI = re.compile(r"\x1b\[[0-9;]*m") -def _plain(lines: list[str]) -> str: - """Strip ANSI escapes and join rendered lines for content assertions.""" - return _ANSI.sub("", "\n".join(lines)) +from tests.conftest import plain as _plain # THE strip-and-join screen reader class _Fut: diff --git a/tests/test_dashboard.py b/tests/test_dashboard.py index 64c9efa..e43491d 100644 --- a/tests/test_dashboard.py +++ b/tests/test_dashboard.py @@ -46,8 +46,7 @@ def _obs(node="a1b2", kind="advert", snr=5.0, rssi=-90.0, age_s=0, **extra) -> O ) -def _plain(lines: list[str]) -> str: - return "\n".join(lines) +from tests.conftest import plain as _plain # THE strip-and-join screen reader def _stripped(lines: list[str]) -> list[str]: @@ -142,7 +141,7 @@ def test_dashboard_traffic_breaks_packets_out_by_payload_class() -> None: window=[_obs()], kinds={"advert": 4, "packet:GRP_TXT": 3, "packet:TRACE": 2, "packet": 1}, ) - body = _plain(_stripped(screen.render_body(100))) + body = _plain(screen.render_body(100)) assert "channel text" in body and "trace" in body # glossed, not raw typenames assert "GRP_TXT" not in body # The class-less remainder keeps a plain "packet" row alongside the classed ones. @@ -157,7 +156,7 @@ def test_dashboard_traffic_disambiguates_raw_advert_and_ack_rows() -> None: window=[_obs()], kinds={"advert": 4, "packet:ADVERT": 2, "ack": 3, "packet:ACK": 1}, ) - body = _plain(_stripped(screen.render_body(100))) + body = _plain(screen.render_body(100)) assert "raw advert" in body and "raw ack" in body diff --git a/tests/test_live_screens.py b/tests/test_live_screens.py index 874be43..982f422 100644 --- a/tests/test_live_screens.py +++ b/tests/test_live_screens.py @@ -8,7 +8,6 @@ and headless (the same approach as ``test_tui``). from __future__ import annotations import asyncio -import re from datetime import timedelta import pytest @@ -63,14 +62,7 @@ def _trace(*snrs: float, success: bool = True, target: str = "Alice") -> TraceRe ) -#: SGR escapes, stripped out of rendered lines so assertions read as plain text (a -#: per-hop-coloured path splits its own characters across a dozen colour runs). -_ANSI = re.compile(r"\x1b\[[0-9;]*m") - - -def _plain(lines: list[str]) -> str: - """Join rendered lines with their colour escapes stripped, for substring checks.""" - return _ANSI.sub("", "\n".join(lines)) +from tests.conftest import plain as _plain # THE strip-and-join screen reader # --- snr_bar ------------------------------------------------------------------- diff --git a/tests/test_livefeed.py b/tests/test_livefeed.py index 865eae2..8aba6d3 100644 --- a/tests/test_livefeed.py +++ b/tests/test_livefeed.py @@ -63,8 +63,7 @@ def _obs(node="a1b2", kind="advert", snr=5.0, rssi=-90.0, age_s=0, **extra) -> O ) -def _plain(lines: list[str]) -> str: - return "\n".join(lines) +from tests.conftest import plain as _plain # THE strip-and-join screen reader def _stripped(lines: list[str]) -> list[str]: @@ -359,7 +358,7 @@ def test_livefeed_windows_inside_the_fixed_screen() -> None: screen.note_viewport(24) lines = screen.render_body(100) assert len(lines) <= 24 # heading + feed window == the viewport, never more - body = _plain(_stripped(lines)) + body = _plain(lines) assert "newest first" in body # the pinned status line is still there assert "↓" in body and "more" in body # hidden feed rows are counted below diff --git a/tests/test_map.py b/tests/test_map.py index 6553346..ff1da5a 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -26,12 +26,7 @@ from meshterm.tools.map import MapTool from meshterm.ui.mapcanvas import MapCanvas, parse_hex _FIXTURE = Path(__file__).parent / "fixtures" / "tile_14_4843_5861.mvt" -_ANSI = re.compile(r"\x1b\[[0-9;]*m") - - -def _plain(lines: list[str]) -> str: - """Strip ANSI colour from rendered lines for content assertions.""" - return "\n".join(_ANSI.sub("", line) for line in lines) +from tests.conftest import plain as _plain # THE strip-and-join screen reader # -- MVT decoding ------------------------------------------------------------- diff --git a/tests/test_message_paths_screen.py b/tests/test_message_paths_screen.py index f866065..879f983 100644 --- a/tests/test_message_paths_screen.py +++ b/tests/test_message_paths_screen.py @@ -7,7 +7,6 @@ labelling are all assertable without a terminal. from __future__ import annotations -import re from datetime import timedelta from meshterm.core.models import ChatMessage, utcnow @@ -19,8 +18,7 @@ def _resolve(hop: str) -> str: return {"3d63": "YUL-Cartierville", "a1b2": "Waymarker"}.get(hop, hop) -def _plain(lines: list[str]) -> str: - return re.sub(r"\x1b\[[0-9;]*m", "", "\n".join(lines)) +from tests.conftest import plain as _plain # THE strip-and-join screen reader def _screen(arrivals: list[Arrival], **kwargs) -> MessagePathsScreen: diff --git a/tests/test_node_detail.py b/tests/test_node_detail.py index b0ea7af..7776974 100644 --- a/tests/test_node_detail.py +++ b/tests/test_node_detail.py @@ -9,7 +9,6 @@ evidence graph. from __future__ import annotations -import re from types import SimpleNamespace from rich.text import Text @@ -46,8 +45,7 @@ HUB = Contact(name="Hub", public_key="3d63c6429436" + "0" * 52, key_prefix="3d63 FAR = Contact(name="Far", public_key="f2c24f54551e" + "0" * 52, key_prefix="f2c24f54551e") -def _plain(lines: list[str]) -> str: - return re.sub(r"\x1b\[[0-9;]*m", "", "\n".join(lines)) +from tests.conftest import plain as _plain # THE strip-and-join screen reader class _FakeSession: diff --git a/tests/test_packet_viewer.py b/tests/test_packet_viewer.py index 38385e3..c405aa0 100644 --- a/tests/test_packet_viewer.py +++ b/tests/test_packet_viewer.py @@ -20,8 +20,7 @@ def _stripped(lines: list[str]) -> list[str]: return [re.sub(r"\x1b\[[0-9;]*m", "", line) for line in lines] -def _plain(lines: list[str]) -> str: - return "\n".join(_stripped(lines)) +from tests.conftest import plain as _plain # THE strip-and-join screen reader def _grp_txt_entry(raw_extra: dict) -> PacketEntry: diff --git a/tests/test_records_screen.py b/tests/test_records_screen.py index 41f489b..6b1009c 100644 --- a/tests/test_records_screen.py +++ b/tests/test_records_screen.py @@ -46,8 +46,7 @@ def _loop_shape() -> list[WalkVertex]: ] -def _plain(lines: list[str]) -> str: - return re.sub(r"\x1b\[[0-9;]*m", "", "\n".join(lines)) +from tests.conftest import plain as _plain # THE strip-and-join screen reader def _resolve(hop: str) -> str: diff --git a/tests/test_timemachine.py b/tests/test_timemachine.py index dd3bda2..f4d74bc 100644 --- a/tests/test_timemachine.py +++ b/tests/test_timemachine.py @@ -68,12 +68,13 @@ def _seeded_repo(tmp_path: Path) -> Repository: def _plain(renderables, width: int = 90) -> str: + """Render the section renderables at ``width`` and read them back as plain text.""" from rich.console import Group from meshterm.ui.tui.render import render_lines + from tests.conftest import plain - text = "\n".join(render_lines(Group(*renderables), width)) - return re.sub(r"\x1b\[[0-9;]*m", "", text) + return plain(render_lines(Group(*renderables), width)) # --- the repository queries ------------------------------------------------------------- diff --git a/tests/test_walk.py b/tests/test_walk.py index c33cdc6..31ad253 100644 --- a/tests/test_walk.py +++ b/tests/test_walk.py @@ -7,7 +7,6 @@ backtracking, find, and the canvas/list split are all assertable without a termi from __future__ import annotations -import re from datetime import timedelta from meshterm.core.models import Contact, utcnow @@ -54,8 +53,7 @@ def _screen(topo: MeshTopology, cell_h: int = 24) -> WalkScreen: return screen -def _plain(lines: list[str]) -> str: - return re.sub(r"\x1b\[[0-9;]*m", "", "\n".join(lines)) +from tests.conftest import plain as _plain # THE strip-and-join screen reader # --- rendering ----------------------------------------------------------------------------