Skip to content

Your lightweight open-source personal agent.

test(matrix): cover streamed replies and cross-room isolation

Xubin Rendf5e6a00e3dd
diff --git a/nanobot/channels/matrix/tests/test_matrix_channel.py b/nanobot/channels/matrix/tests/test_matrix_channel.py
index e83f76a51..1c58318d2 100644
--- a/nanobot/channels/matrix/tests/test_matrix_channel.py
+++ b/nanobot/channels/matrix/tests/test_matrix_channel.py
@@ -2555,6 +2555,53 @@ async def test_send_delta_threaded_edit_keeps_replace_and_thread_relation(monkey
}
+async def test_send_delta_room_reply_edits_the_bot_event(monkeypatch) -> None:
+ channel = MatrixChannel(_make_config(), MessageBus())
+ client = _FakeAsyncClient("", "", "", None)
+ channel.client = client
+ client.room_send_response.event_id = "$bot"
+ times = iter([100.0, 102.0])
+ monkeypatch.setattr(channel, "monotonic_time", lambda: next(times))
+ metadata = {"message_id": "$user", "event_id": "$user"}
+
+ await channel.send_delta("!source:example.org", "Hello", metadata)
+ await channel.send_delta("!source:example.org", " world", metadata)
+ await channel.send_delta("!source:example.org", "", metadata, stream_end=True)
+
+ assert len(client.room_send_calls) == 3
+ assert client.room_send_calls[0]["content"]["m.relates_to"] == {
+ "m.in_reply_to": {"event_id": "$user"},
+ }
+ for call in client.room_send_calls[1:]:
+ assert call["content"]["m.relates_to"] == {
+ "rel_type": "m.replace", "event_id": "$bot",
+ }
+ assert call["content"]["m.new_content"]["body"] == "Hello world"
+ assert call["content"]["m.new_content"]["m.relates_to"] == {
+ "m.in_reply_to": {"event_id": "$user"},
+ }
+
+
+async def test_message_tool_does_not_carry_reply_to_another_room(tmp_path) -> None:
+ from nanobot.agent.tools.message import MessageTool
+
+ channel = MatrixChannel(_make_config(), MessageBus())
+ client = _FakeAsyncClient("", "", "", None)
+ channel.client = client
+ tool = MessageTool(
+ channel.send, default_channel="matrix", default_chat_id="!source:example.org",
+ default_message_id="$user", workspace=tmp_path,
+ )
+ tool._fallback_metadata = {"message_id": "$user", "event_id": "$user"}
+
+ await tool.execute("proactive", channel="matrix", chat_id="!other:example.org")
+
+ assert client.room_send_calls[-1]["room_id"] == "!other:example.org"
+ assert "m.relates_to" not in client.room_send_calls[-1]["content"]
+
+
@pytest.mark.asyncio
async def test_send_delta_stream_end_noop_when_buffer_missing() -> None:
channel = MatrixChannel(_make_config(), MessageBus())