Files
semif-agent/tests/test_lxmf_transport.py
T
gabby ed894bd329 lxmf: Sideband voice messages (transcribe + Piper voice replies)
The LXMF gateway now handles Sideband push-to-talk voice messages end to end.

Inbound: a PTT message carries its audio in LXMF.FIELD_AUDIO
(`[audio_mode, bytes]`); `lxmf_transport.parse_audio_field` normalizes it and
the adapter transcribes it into an ordinary command when
`gateway.lxmf.voice.enabled` is set. HQ Opus/OGG goes through faster-whisper
(requirements/transcribe.txt); non-HQ Codec2 through the optional pycodec2
bindings (requirements/codec2.txt). No microphone stack is needed.

Outbound: a chat that last messaged by voice is answered with a synthesized
voice message when `gateway.lxmf.voice.reply_with_voice` is on (the default).
`voice_transport.build_synthesizer` speaks the reply with Piper and
`voice_transport.pcm_to_opus_ogg` encodes it to OGG/Opus (48 kHz, PyAV's
libopus); `lxmf_transport.audio_fields` attaches it as a Sideband HQ PTT
message, with the reply text as the content so a text-only client still sees
it. The reply mode follows the chat's most recent inbound message. A missing
synthesizer or any synthesis/encode failure is traced and the reply falls back
to text. Deps: requirements/tts.txt (piper-tts + PyAV), still no microphone.

Also make LXMF replies result-only (`LxmfAdapter.result_only`, overridable via
`gateway.lxmf.result_only: false`): send the skill's result line, dropping
scheduler bookkeeping and the `<skill>: ok —` wrapper.

Wiring: new config keys under `gateway.lxmf` (result_only) and
`gateway.lxmf.voice` (reply_with_voice, max_speak_chars, reply_bitrate, tts);
regenerated config.example.json; the setup interview asks how to handle voice
messages and the install plan adds `install_lxmf_tts` (bootstrap installs
requirements/tts.txt and downloads the reply voice); cli anchors the LXMF
voice_dir to .runtime/voice/tts.

Tests cover the audio field shape, the outbound queue, the adapter's
voice-chat tracking and synthesis fallbacks, result-only routing, the
synthesis/encode helpers, and the setup plan. Docs updated.
2026-10-17 04:53:51 +00:00

183 lines
5.3 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