tth.meshcore: MeshCore repeaters as code, over RF
ci / test (push) Successful in 2m39s

Ansible collection that converges MeshCore repeaters through a companion
radio (TCP, USB serial or BLE): admin ACL, CLI prefs, region tree,
firmware facts, companion remote-CLI ACL, and the meshcore-tth extras
(BLE on/off and own BLE PIN, channel-hash filter, message age, advert
fairness). Every change is read, diffed, sent and read back; --check
only reads. ansible-test sanity, unit tests and a galaxy build in CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-03 02:23:24 +01:00
commit 0a4cfafc78
27 files changed
+3195

No files matched your search

@@ -0,0 +1,88 @@
"""Unit tests for `ver` and BLE reply parsing. Inputs are real replies."""
import pytest
from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_fw import (
ble_pin_valid,
blepwr_set_ok,
fw_state,
parse_ble,
parse_ble_pin,
parse_blepwr,
parse_version,
)
# a repeater right after a flash
VER = "v1.17.1-tth76685cf (Build: 07 Oct 2026)"
# ── versions ────────────────────────────────────────────────────────────────
def test_parse_version_from_ver_reply_and_board_probe():
assert parse_version(VER) == "v1.17.1-tth76685cf"
assert parse_version("3333333333333333 xiao/text v1.17.1-tthdf35580+") == "v1.17.1-tthdf35580+"
assert parse_version("0123456789ABCDEF xiao/nic -") is None
def test_parse_version_solo_companion():
assert parse_version("v2.0-tthe266e89 (Build: 16 Oct 2026)") == "v2.0-tthe266e89"
assert parse_version("v2.0-rc3-tth8345a5a (Build: x)") == "v2.0-rc3-tth8345a5a"
assert parse_version("v2.0+tth.20261016152233 (Build: x)") == "v2.0+tth.20261016152233"
def test_parse_version_old_dashed_stamp():
assert parse_version("v1.16.0-tth-956238c (Build: x)") == "v1.16.0-tth-956238c"
def test_fw_state():
w = "v1.17.1-tth76685cf"
assert fw_state(w, w) == "current"
assert fw_state("v1.17.1-tthb3b4cd6", w) == "outdated"
assert fw_state(None, w) == "unknown"
def test_dirty_node_is_never_current():
# the same sha with '+' is NOT what we built
assert fw_state("v1.17.1-tth76685cf+", "v1.17.1-tth76685cf") == "dirty"
def test_refuses_dirty_wanted():
with pytest.raises(ValueError):
fw_state("v1.17.1-tth76685cf", "v1.17.1-tth76685cf+")
# ── BLE replies ─────────────────────────────────────────────────────────────
def test_ble_replies():
assert parse_ble("BLE: on (advertising)") == "on"
assert parse_ble("BLE: off") == "off"
assert parse_blepwr("BLE conn tx: 0 dBm, adv tx: 8 dBm") == 0
assert parse_blepwr("BLE conn tx: -4 dBm, adv tx: 8 dBm") == -4
with pytest.raises(ValueError):
parse_ble("Unknown command")
def test_blepwr_must_be_saved():
assert blepwr_set_ok("OK - BLE conn tx 8 dBm (1/1 conn), saved")
assert not blepwr_set_ok("OK - BLE conn tx 8 dBm (0/0 conn), NOT saved")
assert not blepwr_set_ok("ERR - BLE conn tx 8 dBm (0/1 conn)")
def test_stock_meshcore_version():
assert parse_version("v1.18.0 (Build: 25 Oct 2026)") == "v1.18.0"
assert parse_version("MeshCore v1.18.0 or so") is None # not a `ver` reply
def test_ble_pin_status():
# meshcore-tth c2053768 on a XIAO, 3. 11. 2026
assert parse_ble("BLE: off, PIN build default") == "off"
assert parse_ble_pin("BLE: off, PIN build default") == "build default"
assert parse_ble_pin("BLE: on (advertising), PIN own") == "own"
assert parse_ble_pin("BLE: on (connected)") is None # firmware without `ble pin`
def test_ble_pin_format():
assert ble_pin_valid("482913")
for bad in ("12345", "1234567", "12a456", "", None, 123456):
assert not ble_pin_valid(bad)
@@ -0,0 +1,248 @@
"""Unit tests for the radio-free planning logic. Inputs are real replies."""
import pytest
from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_plan import (
filter_hash_cmd,
norm_entry,
norm_hash,
parse_fairness_advert,
parse_fairness_dry,
parse_filter_age,
parse_filter_dry,
parse_filter_hash,
parse_regions,
plan_acl,
plan_filter_hash,
plan_regions,
region_reply_truncated,
same_value,
)
# real `region` reply of a repeater
LTM = "* F\n cz F\n cz-ulk^ F\n cz-sudety F\n cs F\n"
# another repeater (home on the wildcard)
HREB = "*^ F\n cz F\n cz-kvk F\n cs F\n"
TTH = "a1" * 32
L1 = "b2" * 32
SCRAPE2 = "c3" * 32
TEST2 = "d4" * 32
# ── regions ─────────────────────────────────────────────────────────────────
def test_parse_region_tree():
t = parse_regions(LTM)
assert t["*"] == {"parent": None, "flood": True, "home": False}
assert t["cz-ulk"] == {"parent": "cz", "flood": True, "home": True}
assert t["cs"]["parent"] == "*"
def test_parse_home_on_wildcard_and_denied_flood():
t = parse_regions(HREB + " zz\n")
assert t["*"]["home"] is True
assert t["zz"] == {"parent": "*", "flood": False, "home": False}
def test_parse_rejects_garbage():
with pytest.raises(ValueError):
parse_regions("* F\n a b c d\n")
def test_matching_tree_needs_nothing():
want = [{"name": "cz"}, {"name": "cz-ulk", "parent": "cz"}, {"name": "cz-sudety", "parent": "cz"}, {"name": "cs"}]
assert plan_regions(parse_regions(LTM), want, home="cz-ulk") == []
def test_add_remove_and_order():
want = [{"name": "cz"}, {"name": "cz-kvk", "parent": "cz"}, {"name": "cs"}, {"name": "eu", "flood": False}]
cmds = plan_regions(parse_regions(LTM), want)
# children removed before parents could be; nothing here removes cz itself
assert "region remove cz-ulk" in cmds and "region remove cz-sudety" in cmds
assert cmds.index("region put cz-kvk cz") > max(
cmds.index("region remove cz-ulk"), cmds.index("region remove cz-sudety")
)
# put allows flood by default, so a denied region needs denyf after put
assert cmds.index("region denyf eu") == cmds.index("region put eu") + 1
def test_remove_deepest_first():
cmds = plan_regions(parse_regions(LTM), [{"name": "cs"}])
assert cmds.index("region remove cz-ulk") < cmds.index("region remove cz")
assert cmds.index("region remove cz-sudety") < cmds.index("region remove cz")
def test_put_parent_before_child():
want = [{"name": "sk-ba", "parent": "sk"}, {"name": "sk"}]
cmds = plan_regions(parse_regions("* F\n"), want)
assert cmds == ["region put sk", "region put sk-ba sk"]
def test_flood_flip_and_home():
want = [{"name": "cz", "flood": False}, {"name": "cz-kvk", "parent": "cz"}, {"name": "cs"}]
assert plan_regions(parse_regions(HREB), want, home="cz-kvk") == ["region denyf cz", "region home cz-kvk"]
def test_truncation_detected():
assert not region_reply_truncated(LTM)
assert region_reply_truncated("x" * 156)
# ── ACL ─────────────────────────────────────────────────────────────────────
def test_acl_adds_missing_admin():
current = {L1[:12]: 3, SCRAPE2[:12]: 3}
add, remove = plan_acl(current, [L1, SCRAPE2, TTH])
assert add == [TTH] and remove == []
def test_acl_removes_unknown_and_wrong_perm():
current = {L1[:12]: 3, "aaaaaaaaaaaa": 3, TTH[:12]: 1}
add, remove = plan_acl(current, [L1, TTH])
assert add == [TTH] # present, but not admin
assert remove == ["aaaaaaaaaaaa"]
def test_acl_non_exclusive_keeps_extras():
assert plan_acl({"aaaaaaaaaaaa": 3}, [L1], exclusive=False) == ([L1], [])
def test_acl_keeps_the_managing_identity():
# the companion the module runs through must never lock itself out
_add, remove = plan_acl({TEST2[:12]: 3, L1[:12]: 3}, [L1], keep=[TEST2])
assert remove == []
# ── prefs ───────────────────────────────────────────────────────────────────
@pytest.mark.parametrize(
"cur,want,same",
[
("869.4320068", "869.432", True),
("120", 120, True),
("on", "on", True),
("on", "off", False),
("1", "on", True),
("25", "24", False),
("my-repeater", "my-repeater", True),
],
)
def test_same_value(cur, want, same):
assert same_value(cur, want) is same
def test_render_roundtrip():
from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_plan import render_regions
for text in (LTM, HREB):
assert render_regions(parse_regions(text)) == text
def test_expected_tree_matches_plan():
from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_plan import expected_tree, render_regions
want = [{"name": "cz"}, {"name": "cz-ulk", "parent": "cz"}, {"name": "cz-kvk", "parent": "cz"}, {"name": "cs"}]
out = render_regions(expected_tree(parse_regions(LTM.replace(" cz-sudety F\n", "")), want))
assert out == "* F\n cz F\n cz-ulk^ F\n cz-kvk F\n cs F\n"
# ── filter hash ─────────────────────────────────────────────────────────────
def test_parse_filter_hash_real_replies():
# bench repeater, 14. 10. 2026
assert parse_filter_hash("filter hash: dropped 0, deny: (nic)") == ([], 0)
assert parse_filter_hash("filter hash: dropped 1, deny: 43 09") == (["43", "09"], 1)
# older firmware: no command / swallowed by the sender-name filter
assert parse_filter_hash("Unknown command") is None
assert parse_filter_hash("Err - filter [add|del <name>|list|clear]") is None
# meshcore-tth throttle: passed counter + `hash/secs` entries
assert parse_filter_hash("filter hash: dropped 7, passed 2, deny: 43/60 09") == (["43/60", "09"], 7)
assert parse_filter_hash("filter hash: dropped 0, passed 0, deny: (nic)") == ([], 0)
assert parse_filter_hash("Err - filter [add|del <name>|list|clear|hash|age|dryrun]") is None
def test_parse_filter_age_real_replies():
# bench boards, 2. 11. 2026
assert parse_filter_age("filter age off, dropped 0") == {
"minutes": 0,
"dry_run": False,
"clock_unset": False,
"dropped": 0,
}
assert parse_filter_age("OK - filter age 1440m, dropped 0")["minutes"] == 1440
assert parse_filter_age("filter age 60m DRY-RUN, dropped 6") == {
"minutes": 60,
"dry_run": True,
"clock_unset": False,
"dropped": 6,
}
assert parse_filter_age("filter age 60m (clock not set, inactive), dropped 0")["clock_unset"]
# older firmware: unknown, or taken by the sender-name filter
assert parse_filter_age("Unknown command") is None
assert parse_filter_age("Err - filter [add|del <name>|list|clear]") is None
def test_parse_filter_dry_real_replies():
assert parse_filter_dry("filter dry-run off") is False
assert parse_filter_dry("OK - filter dry-run on") is True
assert parse_filter_dry("Unknown command") is None
def test_norm_entry():
assert norm_entry("43") == norm_entry(0x43) == "43"
assert norm_entry("0x43/60") == norm_entry(" 43/60 ") == "43/60"
assert norm_entry("43/0") == "43"
with pytest.raises(ValueError):
norm_entry("43/70000")
with pytest.raises(ValueError):
norm_entry("43/abc")
def test_filter_hash_cmd():
assert filter_hash_cmd("43") == "filter hash add 43"
assert filter_hash_cmd("43/60") == "filter hash add 43 60"
assert filter_hash_cmd("43/60", delete=True) == "filter hash del 43"
def test_norm_hash():
assert norm_hash("43") == norm_hash("0x43") == norm_hash(0x43) == "43"
assert norm_hash("9") == "09"
with pytest.raises(ValueError):
norm_hash("143")
def test_plan_filter_hash():
assert plan_filter_hash([], ["43"]) == (["43"], [])
assert plan_filter_hash(["43"], ["0x43"]) == ([], [])
assert plan_filter_hash(["43", "09"], ["43"]) == ([], ["09"])
assert plan_filter_hash(["43", "09"], ["43"], exclusive=False) == ([], [])
# block -> throttle and back: re-add in place, no delete in between
assert plan_filter_hash(["43"], ["43/60"]) == (["43/60"], [])
assert plan_filter_hash(["43/60"], ["43"]) == (["43"], [])
assert plan_filter_hash(["43/60"], ["0x43/60"]) == ([], [])
def test_parse_fairness_real_replies():
# replies of MeshCore 39031b18 on the bench, 1. 11. 2026
assert parse_fairness_advert("advert window 24h DRY-RUN, held 0 (0s air), cache 0/256, evicted 0") == {
"hours": 24,
"dry_run": True,
"held": 0,
"air_s": 0,
"cache": 0,
"evicted": 0,
}
assert parse_fairness_advert("advert window 1h, held 10 (8s air), cache 1/256, evicted 0")["held"] == 10
assert parse_fairness_advert("advert window 0h, held 10 (8s air), cache 1/256, evicted 0")["hours"] == 0
assert parse_fairness_dry("fairness dry-run on") is True
assert parse_fairness_dry("OK - fairness dry-run off") is False
def test_parse_fairness_old_firmware():
old = "Err - fairness [on|off|grid|cap ...|refill ...]"
assert parse_fairness_advert(old) is None
assert parse_fairness_dry(old) is None