bridges: refresh SimpleX contact list from the daemon
The bridge only learned contacts from observed inbound senders, so /send could not address a contact it had never received from. Query /_contacts <userId> (active user from /user, cached) over the existing corrId->Future path, merge into the inbox on (re)connect via an on_connected hook and on demand (GET /contacts, send-resolution miss). Best-effort: a disconnected daemon keeps the cached set.
This commit is contained in:
1 parent
fb5303928b
commit
4a4fe850e0
6 files changed
+387
-34
No files matched your search
+75
-1
@@ -7,6 +7,7 @@ mechanics a skill depends on: peek/pop of buffered inbound messages, recipient
|
||||
resolution, outbound routing, address lookup, and token/body validation.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
@@ -20,9 +21,12 @@ from semif_agent.bridges.simplex import SimplexBridge
|
||||
class FakeDaemon:
|
||||
"""Stands in for the bridge's own simplex-chat connection."""
|
||||
|
||||
def __init__(self, link=None, error=None):
|
||||
def __init__(self, link=None, error=None, contacts=None, contacts_error=None):
|
||||
self.link = link
|
||||
self.error = error
|
||||
self.contacts_list = contacts or []
|
||||
self.contacts_error = contacts_error
|
||||
self.refresh_count = 0
|
||||
self.sent = []
|
||||
self.closed = False
|
||||
self.on_message = None
|
||||
@@ -42,6 +46,15 @@ class FakeDaemon:
|
||||
raise self.error
|
||||
return self.link
|
||||
|
||||
def request_contacts(self, timeout=20.0):
|
||||
self.refresh_count += 1
|
||||
if self.contacts_error is not None:
|
||||
raise self.contacts_error
|
||||
return list(self.contacts_list)
|
||||
|
||||
async def contacts(self, timeout=20.0):
|
||||
return self.request_contacts(timeout)
|
||||
|
||||
def close(self):
|
||||
self.closed = True
|
||||
|
||||
@@ -161,6 +174,67 @@ def test_send_resolves_known_display_name():
|
||||
bridge.stop()
|
||||
|
||||
|
||||
def test_contacts_refreshed_from_daemon():
|
||||
# A contact the bridge has never received from must still be visible: the
|
||||
# daemon knows them, so /send can address them.
|
||||
daemon = FakeDaemon(contacts=[{"id": "42", "display_name": "Zed"}])
|
||||
bridge, _, base = start_bridge(daemon=daemon)
|
||||
try:
|
||||
assert get(f"{base}/contacts") == {
|
||||
"contacts": [{"id": "42", "display_name": "Zed"}]
|
||||
}
|
||||
assert daemon.refresh_count >= 1
|
||||
finally:
|
||||
bridge.stop()
|
||||
|
||||
|
||||
def test_send_resolves_daemon_only_contact():
|
||||
daemon = FakeDaemon(contacts=[{"id": "42", "display_name": "Zed"}])
|
||||
bridge, _, base = start_bridge(daemon=daemon)
|
||||
try:
|
||||
result = post(f"{base}/send", {"recipient": "Zed", "text": "hello"})
|
||||
assert result == {"ok": True, "contact_id": "42"}
|
||||
assert daemon.sent == [("42", "hello")]
|
||||
finally:
|
||||
bridge.stop()
|
||||
|
||||
|
||||
def test_refresh_on_connect_primes_contacts():
|
||||
daemon = FakeDaemon(contacts=[{"id": "42", "display_name": "Zed"}])
|
||||
bridge, _, base = start_bridge(daemon=daemon)
|
||||
try:
|
||||
asyncio.run(bridge._refresh_on_connect())
|
||||
assert get(f"{base}/contacts")["contacts"] == [
|
||||
{"id": "42", "display_name": "Zed"}
|
||||
]
|
||||
finally:
|
||||
bridge.stop()
|
||||
|
||||
|
||||
def test_contacts_failure_keeps_cached_and_stays_available():
|
||||
bridge, _, base = start_bridge()
|
||||
try:
|
||||
bridge._on_message(inbound("hi", contact_id="4", display_name="Alice"))
|
||||
bridge.daemon.contacts_error = RuntimeError("not connected")
|
||||
assert get(f"{base}/contacts") == {
|
||||
"contacts": [{"id": "4", "display_name": "Alice"}]
|
||||
}
|
||||
finally:
|
||||
bridge.stop()
|
||||
|
||||
|
||||
def test_daemon_refresh_does_not_clobber_inbound_name():
|
||||
daemon = FakeDaemon(contacts=[{"id": "4", "display_name": ""}])
|
||||
bridge, _, base = start_bridge(daemon=daemon)
|
||||
try:
|
||||
bridge._on_message(inbound("hi", contact_id="4", display_name="Alice"))
|
||||
assert get(f"{base}/contacts") == {
|
||||
"contacts": [{"id": "4", "display_name": "Alice"}]
|
||||
}
|
||||
finally:
|
||||
bridge.stop()
|
||||
|
||||
|
||||
def test_send_rejects_unknown_recipient():
|
||||
bridge, daemon, base = start_bridge()
|
||||
try:
|
||||
|
||||
@@ -14,6 +14,7 @@ import pytest
|
||||
from semif_agent.simplex_ws import (
|
||||
SimplexDaemon,
|
||||
accept_command,
|
||||
contact_entry,
|
||||
contact_link,
|
||||
parse_direct_text_item,
|
||||
send_text_command,
|
||||
@@ -135,6 +136,21 @@ def test_contact_link_reads_both_shapes():
|
||||
assert contact_link({}) == {}
|
||||
|
||||
|
||||
def test_contact_entry_reads_v7_shape():
|
||||
assert contact_entry(
|
||||
{"contactId": 4, "displayName": None, "profile": {"displayName": "Alice"}}
|
||||
) == {"id": "4", "display_name": "Alice"}
|
||||
assert contact_entry({"contactId": 7, "localDisplayName": "bob_1"}) == {
|
||||
"id": "7",
|
||||
"display_name": "bob_1",
|
||||
}
|
||||
assert contact_entry({"chatId": 9, "profile": {"displayName": "Carol"}}) == {
|
||||
"id": "9",
|
||||
"display_name": "Carol",
|
||||
}
|
||||
assert contact_entry({"profile": {"displayName": "no id"}}) is None
|
||||
|
||||
|
||||
# ---- daemon: accept + round-trips ----
|
||||
|
||||
class FakeWS:
|
||||
@@ -269,3 +285,118 @@ def test_request_address_without_connection_raises():
|
||||
daemon = SimplexDaemon("ws://x")
|
||||
with pytest.raises(RuntimeError):
|
||||
daemon.request_address(timeout=1)
|
||||
|
||||
|
||||
# ---- daemon: contacts + on_connected hook ----
|
||||
|
||||
def test_contacts_parses_contacts_list():
|
||||
daemon = SimplexDaemon("ws://x", user_id=1)
|
||||
|
||||
async def scenario():
|
||||
daemon._loop = asyncio.get_running_loop()
|
||||
ws = FakeWS(
|
||||
daemon,
|
||||
[
|
||||
{"type": "activeUser", "user": {"userId": 3}},
|
||||
{
|
||||
"type": "contactsList",
|
||||
"contacts": [
|
||||
{"contactId": 4, "profile": {"displayName": "Alice"}},
|
||||
{"contactId": 7, "localDisplayName": "bob_1"},
|
||||
{"profile": {"displayName": "no id"}},
|
||||
],
|
||||
},
|
||||
],
|
||||
)
|
||||
daemon._ws = ws
|
||||
return await daemon.contacts(timeout=5), ws
|
||||
|
||||
result, ws = asyncio.run(scenario())
|
||||
assert result == [
|
||||
{"id": "4", "display_name": "Alice"},
|
||||
{"id": "7", "display_name": "bob_1"},
|
||||
]
|
||||
assert [m["cmd"] for m in ws.sent] == ["/user", "/_contacts 3"]
|
||||
|
||||
|
||||
def test_contacts_falls_back_to_configured_user_id():
|
||||
daemon = SimplexDaemon("ws://x", user_id=1)
|
||||
|
||||
async def scenario():
|
||||
daemon._loop = asyncio.get_running_loop()
|
||||
ws = FakeWS(
|
||||
daemon,
|
||||
[
|
||||
{"type": "chatCmdError", "chatError": {"type": "error"}},
|
||||
{"type": "contactsList", "contacts": []},
|
||||
],
|
||||
)
|
||||
daemon._ws = ws
|
||||
return await daemon.contacts(timeout=5), ws
|
||||
|
||||
result, ws = asyncio.run(scenario())
|
||||
assert result == []
|
||||
assert [m["cmd"] for m in ws.sent] == ["/user", "/_contacts 1"]
|
||||
|
||||
|
||||
def test_active_user_is_cached_across_calls():
|
||||
daemon = SimplexDaemon("ws://x", user_id=1)
|
||||
|
||||
async def scenario():
|
||||
daemon._loop = asyncio.get_running_loop()
|
||||
ws = FakeWS(
|
||||
daemon,
|
||||
[
|
||||
{"type": "activeUser", "user": {"userId": 3}},
|
||||
{"type": "contactsList", "contacts": []},
|
||||
{"type": "contactsList", "contacts": []},
|
||||
],
|
||||
)
|
||||
daemon._ws = ws
|
||||
await daemon.contacts(timeout=5)
|
||||
await daemon.contacts(timeout=5)
|
||||
return ws
|
||||
|
||||
ws = asyncio.run(scenario())
|
||||
assert [m["cmd"] for m in ws.sent] == ["/user", "/_contacts 3", "/_contacts 3"]
|
||||
|
||||
|
||||
def test_contacts_returns_empty_on_error_response():
|
||||
daemon = SimplexDaemon("ws://x", user_id=1)
|
||||
|
||||
async def scenario():
|
||||
daemon._loop = asyncio.get_running_loop()
|
||||
ws = FakeWS(
|
||||
daemon,
|
||||
[
|
||||
{"type": "activeUser", "user": {"userId": 1}},
|
||||
{"type": "chatCmdError", "chatError": {"type": "error"}},
|
||||
],
|
||||
)
|
||||
daemon._ws = ws
|
||||
return await daemon.contacts(timeout=5)
|
||||
|
||||
assert asyncio.run(scenario()) == []
|
||||
|
||||
|
||||
def test_request_contacts_without_connection_raises():
|
||||
daemon = SimplexDaemon("ws://x")
|
||||
with pytest.raises(RuntimeError):
|
||||
daemon.request_contacts(timeout=1)
|
||||
|
||||
|
||||
def test_on_connected_hook_runs_and_failures_are_swallowed():
|
||||
calls = []
|
||||
|
||||
async def hook():
|
||||
calls.append(1)
|
||||
|
||||
asyncio.run(SimplexDaemon("ws://x", on_connected=hook)._notify_connected())
|
||||
assert calls == [1]
|
||||
|
||||
async def bad():
|
||||
raise ValueError("nope")
|
||||
|
||||
# A hook failure must not propagate (it would tear down the connection).
|
||||
asyncio.run(SimplexDaemon("ws://x", on_connected=bad)._notify_connected())
|
||||
asyncio.run(SimplexDaemon("ws://x")._notify_connected())
|
||||
Reference in new issue
Block a user