gateway: show/create the SimpleX contact link (simplex.connect_link seed)

Add a reusable corrId->Future request path to SimplexAdapter so the daemon
can be queried off the event loop, expose it as GET /address on the local
messaging bridge (injected address_provider; 503 unwired, 502 on daemon
failure), and ship a simplex.connect_link seed that reports the bot's short
and full contact link honestly, creating it on first call.
This commit is contained in:
Denton Social committed 2026-09-28 05:14:01 -05:00
1 parent 9057213217
commit a9b096f2e8
12 files changed
+532 -28

No files matched your search

+90
View File
@@ -361,3 +361,93 @@ def test_service_pull_mode_buffers_without_dispatch(tmp_path):
assert drain_outbound(service) == [], "pull mode must not reply or dispatch"
assert scheduler.current is None
assert scheduler.pending is None
# ---- adapter: correlated request/response + contact address ----
class FakeWS:
"""A fake simplex-chat socket that answers correlated commands."""
def __init__(self, adapter, responses):
self.adapter = adapter
self.responses = list(responses)
self.sent = []
async def send(self, raw):
message = json.loads(raw)
self.sent.append(message)
response = self.responses.pop(0)
await self.adapter._consume(
json.dumps({"corrId": message["corrId"], "resp": response}), None, self
)
def test_correlated_response_resolves_pending_future():
adapter = SimplexAdapter({})
async def scenario():
adapter._loop = asyncio.get_running_loop()
adapter._ws = FakeWS(adapter, [])
future = adapter._loop.create_future()
adapter._pending["req-9"] = future
await adapter._consume(
json.dumps({"corrId": "req-9", "resp": {"type": "ok", "value": 1}}), None, adapter._ws
)
assert "req-9" not in adapter._pending, "a resolved request must be dropped"
return future.result()
assert asyncio.run(scenario()) == {"type": "ok", "value": 1}
def test_address_returns_existing_link():
adapter = SimplexAdapter({"user_id": 3})
async def scenario():
adapter._loop = asyncio.get_running_loop()
ws = FakeWS(
adapter,
[
{
"type": "userContactLink",
"contactLink": {"connShortLink": "simplex:/a", "connFullLink": "https://a"},
}
],
)
adapter._ws = ws
return await adapter.address(timeout=5), ws
result, ws = asyncio.run(scenario())
assert result == {"short_link": "simplex:/a", "full_link": "https://a", "created": False}
assert [m["cmd"] for m in ws.sent] == ["/_show_address 3"]
def test_address_creates_when_missing():
adapter = SimplexAdapter({"user_id": 1})
async def scenario():
adapter._loop = asyncio.get_running_loop()
ws = FakeWS(
adapter,
[
{"type": "userContactLink", "contactLink": None},
{
"type": "userContactLinkCreated",
"connLinkContact": {"connShortLink": "simplex:/new", "connFullLink": ""},
},
],
)
adapter._ws = ws
return await adapter.address(timeout=5), ws
result, ws = asyncio.run(scenario())
assert result == {"short_link": "simplex:/new", "full_link": "", "created": True}
assert [m["cmd"] for m in ws.sent] == ["/_show_address 1", "/_address 1"]
def test_request_address_without_connection_raises():
import pytest
adapter = SimplexAdapter({})
with pytest.raises(RuntimeError):
adapter.request_address(timeout=1)
+36 -2
View File
@@ -17,9 +17,11 @@ from semif_agent.gateway.base import InboundMessage, OutboundMessage
from semif_agent.gateway.bridge import MessagingBridge
def start_bridge(**config):
def start_bridge(address_provider=None, **config):
outbound = queue.Queue()
bridge = MessagingBridge(outbound, config=config)
bridge = MessagingBridge(
outbound, config=config, address_provider=address_provider
)
port = bridge.start()
return bridge, outbound, f"http://127.0.0.1:{port}"
@@ -160,3 +162,35 @@ def test_token_is_required_when_configured():
assert get(f"{base}/inbox", token="sekret") == {"messages": []}
finally:
bridge.stop()
def test_address_uses_provider():
link = {"short_link": "simplex:/abc", "full_link": "https://x", "created": False}
bridge, _, base = start_bridge(address_provider=lambda: link)
try:
assert get(f"{base}/address") == link
finally:
bridge.stop()
def test_address_without_provider_is_unavailable():
bridge, _, base = start_bridge()
try:
with pytest.raises(urllib.error.HTTPError) as exc:
get(f"{base}/address")
assert exc.value.code == 503
finally:
bridge.stop()
def test_address_provider_failure_is_reported():
def boom():
raise RuntimeError("not connected")
bridge, _, base = start_bridge(address_provider=boom)
try:
with pytest.raises(urllib.error.HTTPError) as exc:
get(f"{base}/address")
assert exc.value.code == 502
finally:
bridge.stop()
+1
View File
@@ -27,6 +27,7 @@ def test_expected_seeds_exist():
ids = {seed_id(seed) for seed in SEED_DIRS}
assert "calendar.next_event" in ids
assert "simplex.next_message" in ids
assert "simplex.connect_link" in ids
@pytest.mark.parametrize("seed", SEED_DIRS, ids=[seed_id(s) for s in SEED_DIRS])