The startup announce fires before AutoInterface finishes its peering handshake, so it reaches no peer; a peer discovered later (or a link that recovers) would otherwise wait out the whole `announce_interval`. Replace the blocking `_stop.wait(interval)` announce loop with a tick loop that announces on the interval, and also immediately after an interface scan sees a new online transition (AutoInterface reports `online` only after its peering wait, and every discovered peer is a fresh interface). Each announce records its time and a `reason` (startup / interval / peer discovered) on the `lxmf_announced` trace event; a scan error is traced, never fatal.
234 lines
6.8 KiB
Python
234 lines
6.8 KiB
Python
"""Pure-stdlib tests for the neutral LXMF (Reticulum) transport.
|
|
|
|
`lxmf`/`rns` are optional and lazy-imported, so these tests exercise the
|
|
stdlib-only helpers and the missing-dependency contract without the real
|
|
package (and without touching a real Reticulum network). The live transport is
|
|
a live-integration concern.
|
|
"""
|
|
|
|
from semif_agent.gateway.lxmf import LxmfAdapter
|
|
from semif_agent.lxmf_transport import (
|
|
AM_OPUS_OGG,
|
|
FIELD_AUDIO,
|
|
LxmfDaemon,
|
|
audio_fields,
|
|
normalize_message,
|
|
parse_audio_field,
|
|
)
|
|
|
|
|
|
# ---- requirements ----
|
|
|
|
def test_requires_config_dir():
|
|
ok, hint = LxmfDaemon(config_dir="", storage_path="x").check_requirements()
|
|
assert ok is False
|
|
assert "config_dir" in hint
|
|
|
|
|
|
def test_adapter_check_requirements_never_raises():
|
|
# Either lxmf is installed (True) or an install hint is returned (False);
|
|
# the adapter must never raise on a machine without the optional dep.
|
|
ok, hint = LxmfAdapter({}).check_requirements()
|
|
assert isinstance(ok, bool)
|
|
if not ok:
|
|
assert "lxmf" in hint
|
|
|
|
|
|
# ---- adapter defaults ----
|
|
|
|
def test_adapter_uses_runtime_lxmf_paths_by_default():
|
|
adapter = LxmfAdapter({})
|
|
assert adapter.config_dir.endswith(".runtime/lxmf/reticulum")
|
|
assert adapter.storage_path.endswith(".runtime/lxmf/router")
|
|
assert adapter.daemon.address == ""
|
|
|
|
|
|
# ---- normalization ----
|
|
|
|
def test_normalize_message_tolerates_fakes():
|
|
class _Src:
|
|
display_name = "alice"
|
|
|
|
class _Msg:
|
|
source_hash = bytes.fromhex("ab" * 16)
|
|
content = b"hello"
|
|
title = b"subj"
|
|
timestamp = 123.0
|
|
|
|
def get_source(self):
|
|
return _Src()
|
|
|
|
normalized = normalize_message(_Msg())
|
|
assert normalized["source_hash"] == "ab" * 16
|
|
assert normalized["content"] == "hello"
|
|
assert normalized["title"] == "subj"
|
|
assert normalized["display_name"] == "alice"
|
|
assert normalized["timestamp"] == 123.0
|
|
|
|
|
|
def test_normalize_message_prefers_as_string_helpers():
|
|
class _Msg:
|
|
source_hash = b"\x01\x02"
|
|
content = b"raw"
|
|
title = b"rawtitle"
|
|
|
|
def content_as_string(self):
|
|
return "decoded content"
|
|
|
|
def title_as_string(self):
|
|
return "decoded title"
|
|
|
|
normalized = normalize_message(_Msg())
|
|
assert normalized["source_hash"] == "0102"
|
|
assert normalized["content"] == "decoded content"
|
|
assert normalized["title"] == "decoded title"
|
|
assert normalized["display_name"] is None
|
|
|
|
|
|
def test_normalize_message_handles_none_content():
|
|
class _Msg:
|
|
source_hash = b""
|
|
content = None
|
|
title = None
|
|
|
|
def content_as_string(self):
|
|
return None
|
|
|
|
def title_as_string(self):
|
|
return None
|
|
|
|
normalized = normalize_message(_Msg())
|
|
assert normalized["source_hash"] == ""
|
|
assert normalized["content"] == ""
|
|
assert normalized["title"] == ""
|
|
|
|
|
|
# ---- voice (Sideband push-to-talk) audio field ----
|
|
|
|
def test_parse_audio_field_opus_ogg_uses_the_container_path():
|
|
audio = parse_audio_field([0x10, b"OggS-payload"])
|
|
assert audio == {
|
|
"codec": 0x10,
|
|
"codec_name": "opus_ogg",
|
|
"data": b"OggS-payload",
|
|
"kind": "opus_ogg",
|
|
"codec2_rate": None,
|
|
}
|
|
|
|
|
|
def test_parse_audio_field_codec2_maps_to_a_bitrate():
|
|
audio = parse_audio_field([0x08, b"\x00\x01"])
|
|
assert audio["codec_name"] == "codec2_2400"
|
|
assert audio["kind"] == "codec2"
|
|
assert audio["codec2_rate"] == 2400
|
|
|
|
|
|
def test_parse_audio_field_450_codec2_has_no_binding():
|
|
# 450PWB/450 have no pycodec2 bindings, so they stay undecodable.
|
|
audio = parse_audio_field([0x01, b"\x00\x01"])
|
|
assert audio["kind"] == "unknown"
|
|
assert audio["codec2_rate"] is None
|
|
|
|
|
|
def test_parse_audio_field_rejects_malformed():
|
|
assert parse_audio_field("nope") is None
|
|
assert parse_audio_field([0x10]) is None
|
|
assert parse_audio_field([0x10, "not-bytes"]) is None
|
|
assert parse_audio_field(["x", b"data"]) is None
|
|
|
|
|
|
def test_normalize_message_extracts_audio_field():
|
|
class _Msg:
|
|
source_hash = bytes.fromhex("ab" * 16)
|
|
content = b""
|
|
title = b""
|
|
fields = {0x07: [0x10, b"OggS"]}
|
|
|
|
normalized = normalize_message(_Msg())
|
|
assert normalized["audio"]["codec_name"] == "opus_ogg"
|
|
assert normalized["audio"]["data"] == b"OggS"
|
|
assert normalized["audio"]["kind"] == "opus_ogg"
|
|
assert normalized["fields"][0x07][0] == 0x10
|
|
|
|
|
|
def test_normalize_message_without_fields_has_no_audio():
|
|
class _Msg:
|
|
content = b"plain text"
|
|
|
|
normalized = normalize_message(_Msg())
|
|
assert normalized["audio"] is None
|
|
assert normalized["fields"] == {}
|
|
|
|
|
|
# ---- outbound voice replies ----
|
|
|
|
def test_audio_fields_wraps_ogg_as_sideband_hq_ptt():
|
|
fields = audio_fields(b"OggS-payload")
|
|
assert fields == {FIELD_AUDIO: [AM_OPUS_OGG, b"OggS-payload"]}
|
|
assert AM_OPUS_OGG == 0x10
|
|
|
|
|
|
def test_audio_fields_none_for_text_or_empty():
|
|
assert audio_fields(None) is None
|
|
assert audio_fields(b"") is None
|
|
|
|
|
|
def test_enqueue_carries_audio_on_the_outbound_queue():
|
|
daemon = LxmfDaemon(config_dir="x", storage_path="y")
|
|
daemon.enqueue("ab" * 16, "hello", audio=b"OggS")
|
|
assert daemon._outbound.get_nowait() == ("ab" * 16, "hello", b"OggS")
|
|
# The stop sentinel is unchanged.
|
|
daemon.enqueue(None)
|
|
assert daemon._outbound.get_nowait() is None
|
|
|
|
|
|
# ---- interface-transition re-announce ----
|
|
|
|
def test_announce_records_time_and_reason():
|
|
daemon = LxmfDaemon(config_dir="x", storage_path="y")
|
|
announced = []
|
|
|
|
class _Router:
|
|
def announce(self, destination_hash):
|
|
announced.append(destination_hash)
|
|
|
|
class _Destination:
|
|
hash = b"\x00" * 16
|
|
|
|
daemon._router = _Router()
|
|
daemon._destination = _Destination()
|
|
assert daemon._last_announce == 0.0
|
|
daemon._announce("startup")
|
|
assert announced == [b"\x00" * 16]
|
|
assert daemon._last_announce > 0.0
|
|
|
|
|
|
def test_scan_interfaces_flags_only_newly_online(monkeypatch):
|
|
import sys
|
|
import types
|
|
|
|
class _Iface:
|
|
def __init__(self, name, online):
|
|
self._name = name
|
|
self.online = online
|
|
|
|
def __str__(self):
|
|
return self._name
|
|
|
|
a = _Iface("A", False)
|
|
b = _Iface("B", True)
|
|
fake_rns = types.SimpleNamespace(
|
|
Transport=types.SimpleNamespace(interfaces=[a, b])
|
|
)
|
|
monkeypatch.setitem(sys.modules, "RNS", fake_rns)
|
|
|
|
daemon = LxmfDaemon(config_dir="x", storage_path="y")
|
|
daemon._interface_states = {"A": False, "B": True}
|
|
|
|
assert daemon._scan_interfaces() is False # steady state: nothing changed
|
|
a.online = True
|
|
assert daemon._scan_interfaces() is True # A came online
|
|
assert daemon._scan_interfaces() is False # now recorded online
|
|
fake_rns.Transport.interfaces.append(_Iface("C", True))
|
|
assert daemon._scan_interfaces() is True # a fresh peer interface
|