1010 set_data_normalized ,
1111 truncate_and_annotate_messages ,
1212 truncate_messages_by_size ,
13+ _find_truncation_index ,
1314)
1415from sentry_sdk .serializer import serialize
1516from sentry_sdk .utils import safe_serialize
@@ -209,27 +210,53 @@ def large_messages():
209210class TestTruncateMessagesBySize :
210211 def test_no_truncation_needed (self , sample_messages ):
211212 """Test that messages under the limit are not truncated"""
212- result = truncate_messages_by_size (
213+ result , removed_count = truncate_messages_by_size (
213214 sample_messages , max_bytes = MAX_GEN_AI_MESSAGE_BYTES
214215 )
215216 assert len (result ) == len (sample_messages )
216217 assert result == sample_messages
218+ assert removed_count == 0
217219
218220 def test_truncation_removes_oldest_first (self , large_messages ):
219221 """Test that oldest messages are removed first during truncation"""
220222 small_limit = 3000
221- result = truncate_messages_by_size (large_messages , max_bytes = small_limit )
223+ result , removed_count = truncate_messages_by_size (
224+ large_messages , max_bytes = small_limit
225+ )
222226 assert len (result ) < len (large_messages )
223227
224228 if result :
225229 assert result [- 1 ] == large_messages [- 1 ]
230+ assert removed_count == len (large_messages ) - len (result )
226231
227232 def test_empty_messages_list (self ):
228233 """Test handling of empty messages list"""
229- result = truncate_messages_by_size (
234+ result , removed_count = truncate_messages_by_size (
230235 [], max_bytes = MAX_GEN_AI_MESSAGE_BYTES // 500
231236 )
232237 assert result == []
238+ assert removed_count == 0
239+
240+ def test_find_truncation_index (
241+ self ,
242+ ):
243+ """Test that the truncation index is found correctly"""
244+ # when represented in JSON, these are each 7 bytes long
245+ messages = ["A" * 5 , "B" * 5 , "C" * 5 , "D" * 5 , "E" * 5 ]
246+ truncation_index = _find_truncation_index (messages , 20 )
247+ assert truncation_index == 3
248+ assert messages [truncation_index :] == ["D" * 5 , "E" * 5 ]
249+
250+ messages = ["A" * 5 , "B" * 5 , "C" * 5 , "D" * 5 , "E" * 5 ]
251+ truncation_index = _find_truncation_index (messages , 40 )
252+ assert truncation_index == 0
253+ assert messages [truncation_index :] == [
254+ "A" * 5 ,
255+ "B" * 5 ,
256+ "C" * 5 ,
257+ "D" * 5 ,
258+ "E" * 5 ,
259+ ]
233260
234261 def test_progressive_truncation (self , large_messages ):
235262 """Test that truncation works progressively with different limits"""
@@ -250,20 +277,6 @@ def test_progressive_truncation(self, large_messages):
250277 assert current_count >= 1
251278 prev_count = current_count
252279
253- def test_exact_size_boundary (self ):
254- """Test behavior at exact size boundaries"""
255- messages = [{"role" : "user" , "content" : "test" }]
256-
257- serialized = serialize (messages , is_vars = False )
258- json_str = json .dumps (serialized , separators = ("," , ":" ))
259- exact_size = len (json_str .encode ("utf-8" ))
260-
261- result = truncate_messages_by_size (messages , max_bytes = exact_size )
262- assert len (result ) == 1
263-
264- result = truncate_messages_by_size (messages , max_bytes = exact_size - 1 )
265- assert len (result ) == 1
266-
267280
268281class TestTruncateAndAnnotateMessages :
269282 def test_no_truncation_returns_list (self , sample_messages ):
0 commit comments