Skip to content

Your lightweight open-source personal agent.

test(matrix): verify reply metadata and attachment compatibility

Xubin Renf8d655d6124b
diff --git a/nanobot/channels/matrix/tests/test_matrix_channel.py b/nanobot/channels/matrix/tests/test_matrix_channel.py
index 1c58318d2..b17a5561c 100644
--- a/nanobot/channels/matrix/tests/test_matrix_channel.py
+++ b/nanobot/channels/matrix/tests/test_matrix_channel.py
@@ -1720,7 +1720,12 @@ async def test_on_message_sets_message_id_for_room_level_event() -> None:
@pytest.mark.asyncio
-async def test_send_adds_in_reply_to_for_room_level_message_id() -> None:
[email protected]("metadata", [
+ {"message_id": "$user1", "event_id": "$user1"},
+ {"event_id": "$user1"},
+ {"message_id": "$user1", "event_id": "$older"},
+])
+async def test_send_adds_in_reply_to_for_room_level_message_id(metadata) -> None:
channel = MatrixChannel(_make_config(), MessageBus())
client = _FakeAsyncClient("", "", "", None)
channel.client = client
@@ -1730,7 +1735,7 @@ async def test_send_adds_in_reply_to_for_room_level_message_id() -> None:
channel="matrix",
chat_id="!room:matrix.org",
content="Hi",
- metadata={"message_id": "$user1", "event_id": "$user1"},
+ metadata=metadata,
)
)
@@ -1738,6 +1743,41 @@ async def test_send_adds_in_reply_to_for_room_level_message_id() -> None:
assert content["m.relates_to"] == {"m.in_reply_to": {"event_id": "$user1"}}
[email protected]("metadata", [{}, {"message_id": ""}, {"event_id": 123}])
+async def test_send_without_valid_reply_target_stays_top_level(metadata) -> None:
+ channel = MatrixChannel(_make_config(), MessageBus())
+ client = _FakeAsyncClient("", "", "", None)
+ channel.client = client
+
+ await channel.send(OutboundMessage(
+ channel="matrix", chat_id="!room:matrix.org", content="Hi", metadata=metadata,
+ ))
+
+ assert "m.relates_to" not in client.room_send_calls[0]["content"]
+
+
[email protected]("encrypted", [False, True])
+async def test_send_room_reply_attachment_keeps_source_event(tmp_path, encrypted) -> None:
+ channel = MatrixChannel(_make_config(e2ee_enabled=encrypted), MessageBus())
+ client = _FakeAsyncClient("", "", "", None)
+ client.rooms["!room:matrix.org"] = SimpleNamespace(encrypted=encrypted)
+ channel.client = client
+ file_path = tmp_path / "result.txt"
+ file_path.write_text("result", encoding="utf-8")
+
+ await channel.send(OutboundMessage(
+ channel="matrix", chat_id="!room:matrix.org", content="Result",
+ media=[str(file_path)], metadata={"message_id": "$user"},
+ ))
+
+ assert client.upload_calls[0]["encrypt"] is encrypted
+ assert len(client.room_send_calls) == 2
+ for call in client.room_send_calls:
+ assert call["content"]["m.relates_to"] == {"m.in_reply_to": {"event_id": "$user"}}
+
+
@pytest.mark.asyncio
async def test_send_prefers_thread_relation_over_plain_reply() -> None:
channel = MatrixChannel(_make_config(), MessageBus())
@@ -2584,7 +2624,12 @@ async def test_send_delta_room_reply_edits_the_bot_event(monkeypatch) -> None:
@pytest.mark.asyncio
-async def test_message_tool_does_not_carry_reply_to_another_room(tmp_path) -> None:
[email protected]("target", ["!source:example.org", "!other:example.org"])
[email protected]("use_request_context", [False, True])
+async def test_message_tool_scopes_reply_to_source_room(tmp_path, target, use_request_context) -> None:
+ from contextlib import nullcontext
+
+ from nanobot.agent.tools.context import RequestContext, request_context
from nanobot.agent.tools.message import MessageTool
channel = MatrixChannel(_make_config(), MessageBus())
@@ -2596,10 +2641,19 @@ async def test_message_tool_does_not_carry_reply_to_another_room(tmp_path) -> No
)
tool._fallback_metadata = {"message_id": "$user", "event_id": "$user"}
- await tool.execute("proactive", channel="matrix", chat_id="!other:example.org")
+ context = RequestContext(
+ channel="matrix", chat_id="!source:example.org", message_id="$user",
+ metadata={"message_id": "$user", "event_id": "$user"},
+ )
+ with request_context(context) if use_request_context else nullcontext():
+ await tool.execute("Result", channel="matrix", chat_id=target)
- assert client.room_send_calls[-1]["room_id"] == "!other:example.org"
- assert "m.relates_to" not in client.room_send_calls[-1]["content"]
+ assert client.room_send_calls[-1]["room_id"] == target
+ content = client.room_send_calls[-1]["content"]
+ if target == "!source:example.org":
+ assert content["m.relates_to"] == {"m.in_reply_to": {"event_id": "$user"}}
+ else:
+ assert "m.relates_to" not in content
@pytest.mark.asyncio