meshcore_prefs: a spec per known key -- range and step checked before sending, the value read out of replies that are not bare; gps and powersaving
ci / test (push) Failing after 1m29s

The prefs were a free dict compared as text with the `get` reply, so lint and
sanity could not see that several keys never converge:
- `get radio.silence` -> '30 min (now x1), quiet ...', `get batt.icon` ->
  'on (level 3/5, ...)', `get dutycycle` -> '50.0%': a change on every run,
  then a failure after the `set`;
- `advert.interval` is stored halved (25 reads back as 24, and the node takes
  only 0 or 60-240), `agc.reset.interval` in steps of 4;
- `set repeat <anything but off>` means ON, so `repeat: 0` turned it on;
- lat/lon compared to 1e-3 (~100 m), so a smaller move never went out;
- errors like 'ERR - ...', 'unknown config: ...', 'Invalid ...' did not match
  the expected reply and ended as a timeout after five resends.

module_utils/meshcore_prefspec.py now holds kind, range, step, tolerance and a
reply parser for every pref of the meshcore-tth CLI (CommonCLI.cpp,
CommonRadioPrefs.cpp, NodeMaintenance.cpp, CommonGpsPrefs.cpp); the module
refuses a value the node would refuse or alter before it sends anything, in
check mode too. Unknown keys still go, compared as before, with a warning.
radio.txstall (RAM only) and radio.rxps (compound) are refused with a reason.

New keys that are plain commands in the firmware: gps (off/on/duty),
gps.period, gps.firstfix and powersaving; gps.period goes first and conflicts
with gps off/duty.

Tests: replies as the firmware prints them and as a bench board and three
repeaters answered; with MESHCORE_TTH_SRC set, a drift check that every key is
still in the CLI sources.
This commit is contained in:
tth committed 2026-11-15 14:03:11 +01:00
1 parent 4dfe998409
commit 8306d91b48
5 files changed
+511 -38

No files matched your search

@@ -0,0 +1,183 @@
"""Unit tests for the pref specs. Replies as the firmware prints them (meshcore-tth
CommonCLI.cpp, CommonRadioPrefs.cpp, NodeMaintenance.cpp, CommonGpsPrefs.cpp), the
`gps` lines as three masts answered on 15. 11. 2026."""
import pytest
from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_prefspec import (
PrefError,
check_combination,
is_error,
order,
reading,
same,
set_cmd,
set_ok,
wanted,
)
@pytest.mark.parametrize(
"key, reply, value",
[
("radio.silence", "> 30 min (now x1), quiet 0 min, stage 0", "30"),
("radio.silence", "> off", "0"),
("radio.silence", "> 25 min (now x1), quiet 1367 min, stage 0", "25"), # bench XIAO 15. 11.
("batt.icon", "> on (level 3/5, 4100 mV, scale 3300..4200)", "on"),
("batt.icon", "> on (omitted, name too long)", "on"),
("batt.icon", "> off", "off"),
("dutycycle", "> 50.0%", "50.0"),
("repeat", "> on", "on"),
("advert.interval", "> 120", "120"),
("agc.reset.interval", "> 60", "60"),
("af", "> 1.0", "1.0"),
("lat", "> 50.548099", "50.548099"),
("loop.detect", "> moderate", "moderate"),
("owner.info", "> tth|rfa.cz", "tth|rfa.cz"),
("gps", "> duty, asleep, fix, 6 sats", "duty"),
("gps", "duty, asleep, no fix, 0 sats", "duty"), # firmware before 12. 11. 2026
("gps", "> on, active, 7 sats 12s ago, continuous", "on"),
("gps", "> off, periodic (3600s)", "off"),
("gps.period", "> 0 (continuous)", "0"),
("gps.firstfix", "> 3600", "3600"),
("gps.firstfix", "> 0 (no limit)", "0"),
("powersaving", "on", "on"),
("unknown.key", "> whatever 1", "whatever 1"),
],
)
def test_reading(key, reply, value):
assert reading(key, reply) == value
def test_reading_unreadable():
# an older firmware answers `gps period` with the `gps` line
assert reading("gps.period", "duty, asleep, no fix, 0 sats") is None
@pytest.mark.parametrize(
"reply",
[
"??: radio.silence",
"Error: unsupported",
"ERROR: Power management not supported",
"Error, gps provider not found", # a board without GPS
"unknown config: nonsense 1",
],
)
def test_error_replies(reply):
assert is_error(reply)
@pytest.mark.parametrize(
"key, want, cli",
[
("repeat", True, "on"),
("repeat", "off", "off"),
("repeat", 0, "off"), # `set repeat 0` would mean ON on the node
("powersaving", "yes", "on"),
("advert.interval", 0, "0"),
("advert.interval", 120, "120"),
("flood.advert.interval", 47, "47"),
("agc.reset.interval", 60, "60"),
("radio.silence", "off", "0"),
("radio.silence", 30, "30"),
("lat", 50.5481, "50.5481"),
("gps", "duty", "duty"),
("unknown.key", True, "on"),
],
)
def test_wanted(key, want, cli):
assert wanted(key, want) == cli
@pytest.mark.parametrize(
"key, want, why",
[
("advert.interval", 25, "range"), # 0 or 60..240
("advert.interval", 121, "multiples of 2"), # stored halved
("agc.reset.interval", 10, "multiples of 4"),
("flood.advert.interval", 2, "range"),
("flood.advert.interval", 200, "range"),
("path.hash.mode", 3, "range"),
("radio.silence", 2000, "range"),
("repeat", "maybe", "on/off"),
("gps", "auto", "one of"),
("tx", "loud", "not a number"),
("tx", True, "not a number"),
("flood.max", 1.5, "whole number"),
("freq", 869.432, "refused"),
("radio.txstall", 15, "RAM only"),
("guest.password", "x", "refused"),
("name", "x" * 40, "longer"),
],
)
def test_wanted_refuses(key, want, why):
with pytest.raises(PrefError, match=why):
wanted(key, want)
@pytest.mark.parametrize(
"key, current, want, result",
[
("radio.silence", "30", "30", True),
("radio.silence", "0", "30", False),
("dutycycle", "33.3", "33.33", True),
("lat", "50.548099", "50.5481", True),
("lat", "50.5482", "50.5481", False), # ~11 m: the old 1e-3 hid it
("batt.icon", "on", "on", True),
("unknown.key", "869.4320068", "869.432", True),
("repeat", None, "on", False),
],
)
def test_same(key, current, want, result):
assert same(key, current, want) is result
def test_commands_and_order():
assert set_cmd("gps", "duty") == "gps duty"
assert set_cmd("gps.period", "0") == "gps period 0"
assert set_cmd("radio.silence", "30") == "set radio.silence 30"
assert order(["repeat", "gps", "gps.period"]) == ["gps.period", "gps", "repeat"]
@pytest.mark.parametrize(
"key, value, reply, ok",
[
("repeat", "on", "OK - repeat is now ON", True),
("radio.silence", "30", "ERR - minutes 0..1440 (0/off = disabled)", False),
("powersaving", "on", "on - After 2 minutes", True),
("powersaving", "off", "off", True),
("powersaving", "on", "Board not supported", False),
("flood.max", "70", "Error, max 64", False),
],
)
def test_set_ok(key, value, reply, ok):
assert set_ok(key, value, reply) is ok
def test_gps_period_conflicts_with_duty():
with pytest.raises(PrefError, match="switches GPS on"):
check_combination({"gps": "duty", "gps.period": "3600"})
check_combination({"gps": "on", "gps.period": "3600"})
check_combination({"gps": "duty", "gps.period": "0"})
def test_every_spec_is_in_the_firmware():
"""Drift check against a meshcore-tth checkout (MESHCORE_TTH_SRC=<repo>): every
known key's get and set must appear in the CLI sources. Skipped without one."""
import os
import pathlib
from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_prefspec import SPECS, get_cmd
root = os.environ.get("MESHCORE_TTH_SRC")
if not root:
pytest.skip("MESHCORE_TTH_SRC not set")
helpers = pathlib.Path(root, "src", "helpers")
src = "".join(p.read_text(errors="replace") for p in helpers.glob("*.cpp"))
missing = []
for key, s in SPECS.items():
if "get" in s: # plain commands: both of them
missing += [w for w in (get_cmd(key), s["set"].split(" {v}")[0]) if f'"{w}' not in src]
elif f'"{key}' not in src and f'"get {key}' not in src: # handleGetCmd matches the bare key
missing.append(key)
assert not missing, f"not in {helpers}: {missing}"