Skip to content

The agent that grows with you

test(telegram): two invariant tests for JSON-string allowlists, under the plugin's test mirror

teknium1a3b4d70ea27c
diff --git a/tests/gateway/test_telegram_allowlist_json.py b/tests/gateway/test_telegram_allowlist_json.py
deleted file mode 100644
index a6cdc54fe30..00000000000
--- a/tests/gateway/test_telegram_allowlist_json.py
+++ /dev/null
@@ -1,83 +0,0 @@
-import json
-from types import SimpleNamespace
-
-from gateway.config import Platform, PlatformConfig
-
-
-def _make_json_adapter(allowed_chats):
- from plugins.platforms.telegram.adapter import TelegramAdapter
-
- extra = {
- "allowed_chats": allowed_chats,
- "allowed_topics": [],
- "group_allowed_chats": [],
- }
- adapter = object.__new__(TelegramAdapter)
- adapter.platform = Platform.TELEGRAM
- adapter.config = PlatformConfig(enabled=True, token="***", extra=extra)
- adapter._bot = SimpleNamespace(id=999, username="hermes_bot")
- return adapter
-
-
-def _group_msg(chat_id=-100):
- return SimpleNamespace(
- message_id=42,
- text="hello",
- caption=None,
- entities=[],
- caption_entities=[],
- message_thread_id=None,
- chat=SimpleNamespace(id=chat_id, type="group", title="G", is_forum=False),
- from_user=SimpleNamespace(id=111, full_name="A B", first_name="A"),
- reply_to_message=None,
- date=None,
- )
-
-
-def test_allowed_chats_json_string_parses_as_allowlist():
- adapter = _make_json_adapter('["-100","-200"]')
- assert adapter._telegram_allowed_chats() == {"-100", "-200"}
-
-
-def test_allowed_chats_json_string_end_to_end_gating():
- adapter = _make_json_adapter(json.dumps(["-100"]))
- assert adapter._should_process_message(_group_msg(chat_id=-100)) is True
- assert adapter._should_process_message(_group_msg(chat_id=-300)) is False
-
-
-def test_allowed_chats_comma_string_still_works():
- adapter = _make_json_adapter("-100,-200")
- assert adapter._telegram_allowed_chats() == {"-100", "-200"}
-
-
-def test_allowed_chats_native_list_still_works():
- adapter = _make_json_adapter(["-100", "-200"])
- assert adapter._telegram_allowed_chats() == {"-100", "-200"}
-
-
-def test_allowed_chats_malformed_json_falls_back_to_comma_split():
- adapter = _make_json_adapter('["-100", "-200')
- assert adapter._telegram_allowed_chats() == {'["-100"', '"-200'}
-
-
-def test_ignored_threads_json_string_parses():
- from plugins.platforms.telegram.adapter import TelegramAdapter
-
- extra = {"ignored_threads": '["7", "9"]'}
- adapter = object.__new__(TelegramAdapter)
- adapter.platform = Platform.TELEGRAM
- adapter.config = PlatformConfig(enabled=True, token="***", extra=extra)
- adapter._bot = SimpleNamespace(id=999, username="hermes_bot")
- assert adapter._telegram_ignored_threads() == {7, 9}
-
-
-def test_all_allowlist_keys_decode_json_string():
- adapter = _make_json_adapter('["-100"]')
- adapter.config.extra["group_allowed_chats"] = '["-300"]'
- adapter.config.extra["allowed_topics"] = '["5"]'
- adapter.config.extra["free_response_chats"] = '["-400"]'
- adapter.config.extra["free_response_topics"] = '["-100:3"]'
- assert adapter._telegram_group_allowed_chats() == {"-300"}
- assert adapter._telegram_allowed_topics() == {"5"}
- assert adapter._telegram_free_response_chats() == {"-400"}
- assert adapter._telegram_free_response_topics() == {"-100:3"}
diff --git a/tests/plugins/platforms/telegram/test_allowlist_json_adapter.py b/tests/plugins/platforms/telegram/test_allowlist_json_adapter.py
new file mode 100644
index 00000000000..528336b4131
--- /dev/null
+++ b/tests/plugins/platforms/telegram/test_allowlist_json_adapter.py
@@ -0,0 +1,52 @@
+"""``hermes config set telegram.allowed_chats '["a","b"]'`` stores a JSON-encoded *string*; every
+Telegram allowlist reader must decode it instead of comma-splitting the brackets onto the ids."""
+
+from types import SimpleNamespace
+
+from gateway.config import Platform, PlatformConfig
+
+
+def _adapter(extra):
+ from plugins.platforms.telegram.adapter import TelegramAdapter
+
+ adapter = object.__new__(TelegramAdapter)
+ adapter.platform = Platform.TELEGRAM
+ adapter.config = PlatformConfig(enabled=True, token="***", extra=extra)
+ adapter._bot = SimpleNamespace(id=999, username="hermes_bot")
+ return adapter
+
+
+def _group_msg(chat_id):
+ return SimpleNamespace(
+ message_id=42, text="hello", caption=None, entities=[], caption_entities=[],
+ message_thread_id=None, reply_to_message=None, date=None,
+ chat=SimpleNamespace(id=chat_id, type="group", title="G", is_forum=False),
+ from_user=SimpleNamespace(id=111, full_name="A B", first_name="A"),
+ )
+
+
+def test_json_string_allowlists_decode_across_every_key():
+ adapter = _adapter({
+ "allowed_chats": '["-100","-200"]',
+ "group_allowed_chats": '["-300"]',
+ "allowed_topics": '["5"]',
+ "free_response_chats": '["-400"]',
+ "free_response_topics": '["-100:3"]',
+ "ignored_threads": '["7", "9"]',
+ })
+ assert adapter._telegram_allowed_chats() == {"-100", "-200"}
+ assert adapter._telegram_group_allowed_chats() == {"-300"}
+ assert adapter._telegram_allowed_topics() == {"5"}
+ assert adapter._telegram_free_response_chats() == {"-400"}
+ assert adapter._telegram_free_response_topics() == {"-100:3"}
+ assert adapter._telegram_ignored_threads() == {7, 9}
+ # The user-visible symptom: a JSON-string allowlist dropped every group message.
+ gated = _adapter({"allowed_chats": '["-100","-200"]'})
+ assert gated._should_process_message(_group_msg(-100)) is True
+ assert gated._should_process_message(_group_msg(-999)) is False
+
+
+def test_comma_and_malformed_strings_keep_the_legacy_split():
+ assert _adapter({"allowed_chats": "-100, -200"})._telegram_allowed_chats() == {"-100", "-200"}
+ assert _adapter({"allowed_chats": ["-100", "-200"]})._telegram_allowed_chats() == {"-100", "-200"}
+ assert _adapter({"allowed_chats": '["-100", "-200'})._telegram_allowed_chats() == {'["-100"', '"-200'}