From 097d2ef4956cacc8ec76afcd9f399a113434398a Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Sun, 13 Sep 2026 01:16:27 -0400 Subject: [PATCH] Under --mock, devices lists the simulator and scans nothing --mock promises a session with no real hardware, and a scan was the one thing the devices tool did regardless: it walked the serial ports and switched the Bluetooth radio on, which broke the promise on the first command a stranger without a radio would try, and which the test sweep had to work around with --no-ble. Now the simulator is the inventory under --mock: one row with the flag itself as its target, since that is how this device is selected, and a stable id of its own. The sweep's workaround is gone and a contract test replaces both discovery entry points with ones that fail if reached. Co-Authored-By: Claude Fable 5.1 --- docs/cli.md | 7 +++++-- meshterm/core/discovery.py | 8 +++++++ meshterm/tools/devices.py | 20 +++++++++++++++--- tests/test_cli_contract.py | 43 +++++++++++++++++++++++++++++++------- 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/docs/cli.md b/docs/cli.md index 7e66672..f559db3 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -584,11 +584,14 @@ repeat](#the-shapes-that-repeat), the table names the shape rather than restatin #### `meshterm devices` List every attached serial device and in-range Bluetooth companion. Opens no radio — it -only enumerates what is attached or advertising. +only enumerates what is attached or advertising. Under `--mock` it enumerates nothing at +all: the simulator is the one device, listed with `--mock` as its target, and neither the +serial ports nor the Bluetooth radio are touched — a session that promised no real +hardware keeps the promise here too. | Option | What it does | | --- | --- | -| `--ble` / `--no-ble` | Include a Bluetooth LE scan. On by default; skipping it saves a few seconds. | +| `--ble` / `--no-ble` | Include a Bluetooth LE scan. On by default; skipping it saves a few seconds. Moot under `--mock`. | ```console $ meshterm devices --no-ble diff --git a/meshterm/core/discovery.py b/meshterm/core/discovery.py index 774ffb6..a0131ee 100644 --- a/meshterm/core/discovery.py +++ b/meshterm/core/discovery.py @@ -35,6 +35,10 @@ _log = logging.getLogger(__name__) TRANSPORT_SERIAL = "serial" TRANSPORT_BLE = "ble" TRANSPORT_TCP = "tcp" +#: The built-in simulator, as a device inventory reports it under ``--mock``. Never +#: discovered — there is nothing to find — but a session that promised no real hardware +#: still has exactly one device, and this is what it is called. +TRANSPORT_MOCK = "mock" #: Default TCP port assumed when a network companion is named as a bare host with no port. #: 5000 is the conventional MeshCore companion-over-TCP / ``meshcored`` listen port. @@ -144,6 +148,8 @@ class DiscoveredDevice: """ if self.is_tcp: return f"{self.host}:{self.tcp_port}" + if self.transport == TRANSPORT_MOCK: + return "--mock" # the flag is the target: it is how this device is selected return self.address or self.port if self.is_ble else self.port @property @@ -159,6 +165,8 @@ class DiscoveredDevice: """ if self.is_tcp: return f"tcp:{self.host}:{self.tcp_port}" + if self.transport == TRANSPORT_MOCK: + return "mock:simulator" if self.is_ble: return f"ble:{(self.address or self.name or '').lower()}" if self.serial_number: diff --git a/meshterm/tools/devices.py b/meshterm/tools/devices.py index ace3ace..b6657ba 100644 --- a/meshterm/tools/devices.py +++ b/meshterm/tools/devices.py @@ -17,7 +17,7 @@ import typer from ..context import AppContext from ..core import exitcodes -from ..core.discovery import discover_all, discover_devices +from ..core.discovery import TRANSPORT_MOCK, DiscoveredDevice, discover_all, discover_devices from .base import Tool, ToolResult, register if TYPE_CHECKING: # pragma: no cover - typing only @@ -51,8 +51,22 @@ class DevicesTool(Tool): Returns: A :class:`ToolResult` summarizing how many devices were found. """ - scan_ble = params.get("ble", True) - devices = await discover_all(ble=scan_ble) if scan_ble else discover_devices() + if ctx.mock: + # `--mock` promises a session with no real hardware, and a scan is the one + # thing this tool does — it walked the serial ports and switched the Bluetooth + # radio on regardless, which is a promise broken on the first command a + # stranger without a radio would try. So under --mock the simulator *is* the + # inventory: one row, and nothing on the machine touched. + devices = [ + DiscoveredDevice( + transport=TRANSPORT_MOCK, + description="the built-in simulator (nothing transmits)", + name="simulator", + ) + ] + else: + scan_ble = params.get("ble", True) + devices = await discover_all(ble=scan_ble) if scan_ble else discover_devices() known = ctx.device_store.load_all() remembered = ctx.device_store.load() active = ctx.selected_device diff --git a/tests/test_cli_contract.py b/tests/test_cli_contract.py index e39f4f1..68df5f7 100644 --- a/tests/test_cli_contract.py +++ b/tests/test_cli_contract.py @@ -81,13 +81,11 @@ _NOT_ONE_SHOT = {("monitor",), ("chat", "listen"), ("tx-optimize",)} #: The one command whose output *is* its colour, and says so (:mod:`meshterm.ui.specimen`). _KEEPS_ITS_COLOUR = {("specimen",)} -#: Extra arguments the derived sweep passes a leaf that would otherwise reach for real -#: hardware. ``devices`` enumerates serial ports *and* scans for Bluetooth companions, so -#: run bare it turned this machine's radio on three times per suite run and took its wall -#: time from whatever happened to be in the room. ``--no-ble`` drops the scan and keeps the -#: (cheap, local) serial walk. The tool is deliberately untouched: what the sweep checks is -#: the *shape* of an answer, and one transport's worth of rows proves that as well as two. -_SWEEP_ARGS: dict[tuple[str, ...], tuple[str, ...]] = {("devices",): ("--no-ble",)} +#: Extra arguments the derived sweep passes a leaf. Empty since ``devices`` learned that +#: ``--mock`` means no scan: it used to enumerate serial ports *and* switch this machine's +#: Bluetooth radio on three times per suite run, and the sweep passed ``--no-ble`` to +#: spare it. Kept as the hook it is, in case another leaf ever reaches for hardware. +_SWEEP_ARGS: dict[tuple[str, ...], tuple[str, ...]] = {} def _sweep(leaf: tuple[str, ...]) -> tuple[str, ...]: @@ -708,6 +706,37 @@ def test_a_closing_message_goes_to_stderr_too(run) -> None: # noqa: ANN001 assert "records stored" in result.stderr +# -- devices under --mock ------------------------------------------------------------- + + +def test_mock_devices_scans_nothing_and_lists_the_simulator( + run, # noqa: ANN001 + monkeypatch: pytest.MonkeyPatch, +) -> None: + """``--mock`` promises no real hardware, so the inventory is the simulator and no scan. + + Both discovery entry points are replaced with ones that fail loudly: the tool must not + reach either. The one row it lists names ``--mock`` as its target, because that flag is + how this device is selected — the same rule as ``--port``/``--ble`` for the others. + """ + from meshterm.tools import devices as tool + + def never(*args, **kwargs): # noqa: ANN002, ANN003, ANN202 + raise AssertionError("a --mock session scanned for real hardware") + + monkeypatch.setattr(tool, "discover_devices", never) + monkeypatch.setattr(tool, "discover_all", never) + + result = run("devices") + assert result.exit_code == exitcodes.OK + assert "--mock" in result.stdout and "mock" in result.stdout + + machine = run("--json", "devices") + assert machine.exit_code == exitcodes.OK + (row,) = json.loads(machine.stdout) + assert row["target"] == "--mock" and row["transport"] == "mock" + + # -- the map --------------------------------------------------------------------------