@@ -84,6 +84,7 @@ def test_send_telegram_message_success(self, mock_post):
8484 {
8585 "TELEGRAM_BOT_TOKEN_TEST" : "test_token" ,
8686 "TELEGRAM_CHAT_ID_TEST" : "test_chat_id" ,
87+ "LOG_LEVEL" : "INFO" ,
8788 },
8889 ):
8990 # Should not raise any exceptions
@@ -107,23 +108,95 @@ def test_send_telegram_message_missing_credentials(self, mock_get):
107108 # Verify no request was made
108109 mock_get .assert_not_called ()
109110
110- @patch ("utils.telegram.requests.get " )
111- def test_send_telegram_message_failure (self , mock_get ):
111+ @patch ("utils.telegram.requests.post " )
112+ def test_send_telegram_message_failure (self , mock_post ):
112113 # Setup mock response for failure
113- mock_get .side_effect = requests .RequestException ("Connection error" )
114+ mock_post .side_effect = requests .RequestException ("Connection error" )
114115
115116 # Test with environment variables
116117 with patch .dict (
117118 os .environ ,
118119 {
119120 "TELEGRAM_BOT_TOKEN_TEST" : "test_token" ,
120121 "TELEGRAM_CHAT_ID_TEST" : "test_chat_id" ,
122+ "LOG_LEVEL" : "INFO" ,
121123 },
122124 ):
123125 # Should raise TelegramError
124126 with self .assertRaises (TelegramError ):
125127 send_telegram_message ("Test message" , "test" )
126128
129+ @patch ("utils.telegram.requests.post" )
130+ def test_send_telegram_message_with_topic (self , mock_post ):
131+ """When TELEGRAM_TOPIC_ID is set, message goes to topics chat with message_thread_id."""
132+ mock_response = unittest .mock .Mock ()
133+ mock_response .status_code = 200
134+ mock_response .raise_for_status = unittest .mock .Mock ()
135+ mock_post .return_value = mock_response
136+
137+ with patch .dict (
138+ os .environ ,
139+ {
140+ "TELEGRAM_BOT_TOKEN_DEFAULT" : "default_token" ,
141+ "TELEGRAM_CHAT_ID_TOPICS" : "topics_chat_id" ,
142+ "TELEGRAM_TOPIC_ID_AAVE" : "42" ,
143+ "LOG_LEVEL" : "INFO" ,
144+ },
145+ ):
146+ send_telegram_message ("Test message" , "aave" )
147+
148+ mock_post .assert_called_once ()
149+ url = mock_post .call_args [0 ][0 ]
150+ kwargs = mock_post .call_args [1 ]
151+ self .assertEqual (kwargs ["json" ]["chat_id" ], "topics_chat_id" )
152+ self .assertEqual (kwargs ["json" ]["message_thread_id" ], 42 )
153+ self .assertIn ("default_token" , url )
154+
155+ @patch ("utils.telegram.requests.post" )
156+ def test_send_telegram_message_topic_uses_default_bot (self , mock_post ):
157+ """Topic routing always uses the default bot, even if protocol-specific bot exists."""
158+ mock_response = unittest .mock .Mock ()
159+ mock_response .status_code = 200
160+ mock_response .raise_for_status = unittest .mock .Mock ()
161+ mock_post .return_value = mock_response
162+
163+ with patch .dict (
164+ os .environ ,
165+ {
166+ "TELEGRAM_BOT_TOKEN_DEFAULT" : "default_token" ,
167+ "TELEGRAM_BOT_TOKEN_AAVE" : "aave_specific_token" ,
168+ "TELEGRAM_CHAT_ID_TOPICS" : "topics_chat_id" ,
169+ "TELEGRAM_TOPIC_ID_AAVE" : "7" ,
170+ "LOG_LEVEL" : "INFO" ,
171+ },
172+ ):
173+ send_telegram_message ("Test" , "aave" )
174+ url = mock_post .call_args [0 ][0 ]
175+ self .assertIn ("default_token" , url )
176+ self .assertNotIn ("aave_specific_token" , url )
177+
178+ @patch ("utils.telegram.requests.post" )
179+ def test_send_telegram_message_no_topic_falls_back (self , mock_post ):
180+ """Without topic ID, uses legacy per-protocol chat routing."""
181+ mock_response = unittest .mock .Mock ()
182+ mock_response .status_code = 200
183+ mock_response .raise_for_status = unittest .mock .Mock ()
184+ mock_post .return_value = mock_response
185+
186+ with patch .dict (
187+ os .environ ,
188+ {
189+ "TELEGRAM_BOT_TOKEN_AAVE" : "aave_token" ,
190+ "TELEGRAM_CHAT_ID_AAVE" : "aave_chat_id" ,
191+ "TELEGRAM_CHAT_ID_TOPICS" : "topics_chat_id" ,
192+ "LOG_LEVEL" : "INFO" ,
193+ },
194+ ):
195+ send_telegram_message ("Test" , "aave" )
196+ kwargs = mock_post .call_args [1 ]
197+ self .assertEqual (kwargs ["json" ]["chat_id" ], "aave_chat_id" )
198+ self .assertNotIn ("message_thread_id" , kwargs ["json" ])
199+
127200
128201class TestAlert (unittest .TestCase ):
129202 """Tests for the Alert system."""
0 commit comments