Tests: one way to read a screen

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 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Fable 5 committed 2026-07-29 23:12:24 -04:00
1 parent f3de1d1213
commit 4aef3e7b82
13 files changed
+39 -49

No files matched your search

+22 -2
View File
@@ -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]:
+1 -7
View File
@@ -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
+1 -3
View File
@@ -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:
+3 -4
View File
@@ -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
+1 -9
View File
@@ -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 -------------------------------------------------------------------
+2 -3
View File
@@ -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
+1 -6
View File
@@ -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 -------------------------------------------------------------
+1 -3
View File
@@ -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:
+1 -3
View File
@@ -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:
+1 -2
View File
@@ -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:
+1 -2
View File
@@ -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:
+3 -2
View File
@@ -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 -------------------------------------------------------------
+1 -3
View File
@@ -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 ----------------------------------------------------------------------------