fix(gateway): preserve Telegram DM topic routing metadata in synthetic notifications

This commit is contained in:
Dusk1e
2026-05-31 12:13:46 -07:00
committed by kshitij
parent 59cc7c305d
commit 4259bab7d4
3 changed files with 121 additions and 25 deletions
+20 -9
View File
@@ -59,6 +59,7 @@ async def test_restart_command_writes_notify_file(tmp_path, monkeypatch):
data = json.loads(notify_path.read_text())
assert data["platform"] == "telegram"
assert data["chat_id"] == "42"
assert data["chat_type"] == "dm"
assert "thread_id" not in data # no thread → omitted
@@ -112,8 +113,7 @@ async def test_restart_command_preserves_thread_id(tmp_path, monkeypatch):
runner, _adapter = make_restart_runner()
runner.request_restart = MagicMock(return_value=True)
source = make_restart_source(chat_id="99")
source.thread_id = "topic_7"
source = make_restart_source(chat_id="99", thread_id="777")
event = MessageEvent(
text="/restart",
@@ -125,7 +125,8 @@ async def test_restart_command_preserves_thread_id(tmp_path, monkeypatch):
await runner._handle_restart_command(event)
data = json.loads((tmp_path / ".restart_notify.json").read_text())
assert data["thread_id"] == "topic_7"
assert data["chat_type"] == "dm"
assert data["thread_id"] == "777"
@pytest.mark.asyncio
@@ -258,17 +259,22 @@ async def test_send_home_channel_startup_notification_preserves_thread_metadata(
platform=Platform.TELEGRAM,
chat_id="parent-42",
name="Ops Topic",
thread_id="topic-7",
thread_id="777",
)
adapter._get_dm_topic_info = MagicMock(return_value={"name": "Ops Topic"})
adapter.send = AsyncMock(return_value=SendResult(success=True, message_id="home"))
delivered = await runner._send_home_channel_startup_notifications()
assert delivered == {("telegram", "parent-42", "topic-7")}
assert delivered == {("telegram", "parent-42", "777")}
adapter.send.assert_called_once_with(
"parent-42",
"♻️ Gateway online — Hermes is back and ready.",
metadata={"thread_id": "topic-7"},
metadata={
"thread_id": "777",
"telegram_dm_topic_reply_fallback": True,
"direct_messages_topic_id": "777",
},
)
@@ -373,7 +379,8 @@ async def test_send_restart_notification_with_thread(tmp_path, monkeypatch):
notify_path.write_text(json.dumps({
"platform": "telegram",
"chat_id": "99",
"thread_id": "topic_7",
"chat_type": "dm",
"thread_id": "777",
}))
runner, adapter = make_restart_runner()
@@ -381,9 +388,13 @@ async def test_send_restart_notification_with_thread(tmp_path, monkeypatch):
delivered_target = await runner._send_restart_notification()
assert delivered_target == ("telegram", "99", "topic_7")
assert delivered_target == ("telegram", "99", "777")
call_args = adapter.send.call_args
assert call_args[1]["metadata"] == {"thread_id": "topic_7"}
assert call_args[1]["metadata"] == {
"thread_id": "777",
"telegram_dm_topic_reply_fallback": True,
"direct_messages_topic_id": "777",
}
assert not notify_path.exists()
+7 -1
View File
@@ -210,6 +210,7 @@ class TestHandleUpdateCommand:
data = json.loads(pending_path.read_text())
assert data["platform"] == "telegram"
assert data["chat_id"] == "99999"
assert data["chat_type"] == "dm"
assert "timestamp" in data
assert not (hermes_home / ".update_exit_code").exists()
@@ -469,6 +470,7 @@ class TestSendUpdateNotification:
pending = {
"platform": "telegram",
"chat_id": "67890",
"chat_type": "dm",
"thread_id": "777",
"user_id": "12345",
}
@@ -482,7 +484,11 @@ class TestSendUpdateNotification:
with patch("gateway.run._hermes_home", hermes_home):
await runner._send_update_notification()
assert mock_adapter.send.call_args.kwargs["metadata"] == {"thread_id": "777"}
assert mock_adapter.send.call_args.kwargs["metadata"] == {
"thread_id": "777",
"telegram_dm_topic_reply_fallback": True,
"direct_messages_topic_id": "777",
}
@pytest.mark.asyncio
async def test_strips_ansi_codes(self, tmp_path):