add LXMF gateway

This commit is contained in:
gabby committed 2026-10-05 23:07:57 +00:00
1 parent 1d9730bfcb
commit 49f67575cf
11 files changed
+1136 -163

No files matched your search

+86 -9
View File
@@ -10,6 +10,7 @@ live-daemon integration concern, not a unit test.
import asyncio
import json
import time
from semif_agent.cli import _gateway_address_callback
from semif_agent.decisions import Request
@@ -42,10 +43,12 @@ def build_scheduler(tmp_path):
def drain_outbound(service) -> list[str]:
texts = []
while not service.outbound.empty():
item = service.outbound.get_nowait()
if item is not None:
texts.append(item.text)
for platform in service.platforms:
outbound = service.outbound[platform]
while not outbound.empty():
item = outbound.get_nowait()
if item is not None:
texts.append(item.text)
return texts
@@ -213,9 +216,9 @@ def test_service_refuses_other_chat_during_pending(tmp_path):
def test_service_requeue_hook_carries_ownership(tmp_path):
scheduler = build_scheduler(tmp_path)
service = build_service(scheduler)
service._owners["parent"] = "4"
service._owners["parent"] = ("simplex", "4")
service.on_request_requeued("parent", "child")
assert service._owner_of("child") == "4"
assert service._owner_of("child") == ("simplex", "4")
def test_service_drain_routes_queued_run_to_owner(tmp_path):
@@ -223,7 +226,7 @@ def test_service_drain_routes_queued_run_to_owner(tmp_path):
service = build_service(scheduler)
request = Request("queued work", source="simplex:4")
scheduler.queue.push(request, 0.5)
service._owners[request.id] = "4"
service._owners[request.id] = ("simplex", "4")
service.drain()
outbound = drain_outbound(service)
@@ -245,7 +248,7 @@ def test_service_answered_question_routes_back(tmp_path):
question="Which account?",
)
)
service._owners["r1"] = "4"
service._owners["r1"] = ("simplex", "4")
service.surface()
assert any("Which account?" in text for text in drain_outbound(service))
@@ -266,7 +269,7 @@ def test_service_surfaces_and_resolves_approval(tmp_path):
description="Follow a package.",
)
)
service._owners["r1"] = "4"
service._owners["r1"] = ("simplex", "4")
service.surface()
assert any(
"Approve creating new skill tracking.track_live" in text
@@ -276,3 +279,77 @@ def test_service_surfaces_and_resolves_approval(tmp_path):
service.handle_inbound(InboundMessage(text="yes", chat_id="4", contact_id="4"))
assert scheduler.pending_approvals() == []
assert any("approved" in text for text in drain_outbound(service))
# ---- LXMF adapter ----
def test_lxmf_default_deny_and_allowlist():
from semif_agent.gateway.lxmf import LxmfAdapter
assert LxmfAdapter({}).is_authorized("a" * 32, "alice") is False
assert LxmfAdapter({"allowed_users": ["a" * 32]}).is_authorized("a" * 32, "alice") is True
assert LxmfAdapter({"allowed_users": ["alice"]}).is_authorized("a" * 32, "alice") is True
assert LxmfAdapter({"allowed_users": ["bob"]}).is_authorized("a" * 32, "alice") is False
assert LxmfAdapter({"allow_all_users": True}).is_authorized("a" * 32, None) is True
def test_lxmf_batching_concatenates_rapid_messages():
from semif_agent.gateway.lxmf import LxmfAdapter
adapter = LxmfAdapter({"allow_all_users": True, "text_batch_delay": 0.05})
seen: list = []
adapter._on_inbound = seen.append
for text in ("one", "two"):
adapter._on_item(
{"source_hash": "ab" * 16, "content": text, "display_name": None}
)
time.sleep(0.2)
assert len(seen) == 1
assert "one" in seen[0].text and "two" in seen[0].text
def test_lxmf_denies_unknown_sender_without_dispatch():
from semif_agent.gateway.lxmf import LxmfAdapter
adapter = LxmfAdapter({})
seen: list = []
adapter._on_inbound = seen.append
adapter._on_item({"source_hash": "cd" * 16, "content": "hi"})
time.sleep(0.1)
assert seen == []
# ---- service: multiple platforms ----
class FakeLxmfAdapter:
name = "lxmf"
def build_multi_service(scheduler):
return GatewayService(
scheduler, [FakeAdapter(), FakeLxmfAdapter()], config={}
)
def test_service_refuses_other_platform_during_pending(tmp_path):
scheduler = build_scheduler(tmp_path)
service = build_multi_service(scheduler)
scheduler._run_skill(need_input_skill([]), Request("track it", source="simplex:4"))
question = scheduler.pending.question
service.handle_inbound(
InboundMessage(text="hello", chat_id="abc", contact_id="abc"), platform="lxmf"
)
assert scheduler.pending is not None
assert scheduler.pending.question == question
assert any("another conversation" in text for text in drain_outbound(service))
def test_service_reply_routes_to_owning_platform(tmp_path):
scheduler = build_scheduler(tmp_path)
service = build_multi_service(scheduler)
service.handle_inbound(
InboundMessage(text="hi", chat_id="abc", contact_id="abc"), platform="lxmf"
)
assert not service.outbound["lxmf"].empty()
assert service.outbound["simplex"].empty()
+96
View File
@@ -0,0 +1,96 @@
"""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 LxmfDaemon, normalize_message
# ---- 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"] == ""