@@ -46,13 +46,16 @@ def large_messages():
46
46
class TestTruncateMessagesBySize :
47
47
def test_no_truncation_needed (self , sample_messages ):
48
48
"""Test that messages under the limit are not truncated"""
49
- result = truncate_messages_by_size (sample_messages , max_bytes = 50000 )
49
+ result = truncate_messages_by_size (
50
+ sample_messages , max_bytes = MAX_GEN_AI_MESSAGE_BYTES
51
+ )
50
52
assert len (result ) == len (sample_messages )
51
53
assert result == sample_messages
52
54
53
55
def test_truncation_removes_oldest_first (self , large_messages ):
54
56
"""Test that oldest messages are removed first during truncation"""
55
- result = truncate_messages_by_size (large_messages , max_bytes = 5000 )
57
+ small_limit = MAX_GEN_AI_MESSAGE_BYTES // 100 # 5KB limit to force truncation
58
+ result = truncate_messages_by_size (large_messages , max_bytes = small_limit )
56
59
57
60
# Should have fewer messages
58
61
assert len (result ) < len (large_messages )
@@ -64,28 +67,38 @@ def test_truncation_removes_oldest_first(self, large_messages):
64
67
65
68
def test_empty_messages_list (self ):
66
69
"""Test handling of empty messages list"""
67
- result = truncate_messages_by_size ([], max_bytes = 1000 )
70
+ result = truncate_messages_by_size (
71
+ [], max_bytes = MAX_GEN_AI_MESSAGE_BYTES // 500
72
+ )
68
73
assert result == []
69
74
70
75
def test_single_message_under_limit (self ):
71
76
"""Test single message under size limit"""
72
77
messages = [{"role" : "user" , "content" : "Hello!" }]
73
- result = truncate_messages_by_size (messages , max_bytes = 1000 )
78
+ result = truncate_messages_by_size (
79
+ messages , max_bytes = MAX_GEN_AI_MESSAGE_BYTES // 500
80
+ )
74
81
assert result == messages
75
82
76
83
def test_single_message_over_limit (self ):
77
84
"""Test single message that exceeds size limit"""
78
85
large_content = "x" * 10000
79
86
messages = [{"role" : "user" , "content" : large_content }]
80
- result = truncate_messages_by_size (messages , max_bytes = 100 )
87
+ result = truncate_messages_by_size (messages , max_bytes = 100 ) # Very small limit
81
88
82
89
# Should return empty list if even single message is too large
83
90
assert result == []
84
91
85
92
def test_progressive_truncation (self , large_messages ):
86
93
"""Test that truncation works progressively with different limits"""
87
- # Test different size limits
88
- limits = [100000 , 50000 , 20000 , 5000 , 1000 ]
94
+ # Test different size limits based on the constant
95
+ limits = [
96
+ MAX_GEN_AI_MESSAGE_BYTES // 5 , # 100KB
97
+ MAX_GEN_AI_MESSAGE_BYTES // 10 , # 50KB
98
+ MAX_GEN_AI_MESSAGE_BYTES // 25 , # 20KB
99
+ MAX_GEN_AI_MESSAGE_BYTES // 100 , # 5KB
100
+ MAX_GEN_AI_MESSAGE_BYTES // 500 , # 1KB
101
+ ]
89
102
prev_count = len (large_messages )
90
103
91
104
for limit in limits :
@@ -142,14 +155,15 @@ def test_serialize_empty_messages(self):
142
155
143
156
def test_serialize_with_truncation (self , large_messages ):
144
157
"""Test serialization with size-based truncation"""
145
- result = serialize_gen_ai_messages (large_messages , max_bytes = 5000 )
158
+ small_limit = MAX_GEN_AI_MESSAGE_BYTES // 100 # 5KB limit to force truncation
159
+ result = serialize_gen_ai_messages (large_messages , max_bytes = small_limit )
146
160
147
161
if result : # Might be None if all messages are too large
148
162
assert isinstance (result , str )
149
163
150
164
# Verify the result is under the size limit
151
165
result_size = len (result .encode ("utf-8" ))
152
- assert result_size <= 5000
166
+ assert result_size <= small_limit
153
167
154
168
# Should be valid JSON
155
169
parsed = json .loads (result )
@@ -236,19 +250,20 @@ def test_main_function_with_normal_messages(self, sample_messages):
236
250
237
251
def test_main_function_with_large_messages (self , large_messages ):
238
252
"""Test the main function with messages requiring truncation"""
239
- result = truncate_and_serialize_messages (large_messages , max_bytes = 5000 )
253
+ small_limit = MAX_GEN_AI_MESSAGE_BYTES // 100 # 5KB limit to force truncation
254
+ result = truncate_and_serialize_messages (large_messages , max_bytes = small_limit )
240
255
241
256
assert "serialized_data" in result
242
257
assert "metadata" in result
243
258
assert "original_size" in result
244
259
245
260
# Original size should be large
246
- assert result ["original_size" ] > 5000
261
+ assert result ["original_size" ] > small_limit
247
262
248
263
# May or may not be truncated depending on how large the messages are
249
264
if result ["serialized_data" ]:
250
265
serialized_size = len (result ["serialized_data" ].encode ("utf-8" ))
251
- assert serialized_size <= 5000
266
+ assert serialized_size <= small_limit
252
267
253
268
def test_main_function_with_none_input (self ):
254
269
"""Test the main function with None input"""
@@ -277,7 +292,7 @@ def test_main_function_size_comparison(self, sample_messages):
277
292
278
293
def test_main_function_respects_custom_limit (self , large_messages ):
279
294
"""Test that the main function respects custom byte limits"""
280
- custom_limit = 2000
295
+ custom_limit = MAX_GEN_AI_MESSAGE_BYTES // 250 # 2KB limit
281
296
result = truncate_and_serialize_messages (large_messages , max_bytes = custom_limit )
282
297
283
298
if result ["serialized_data" ]:
@@ -344,15 +359,16 @@ def test_messages_with_nested_structures(self):
344
359
345
360
def test_very_small_limit (self , sample_messages ):
346
361
"""Test behavior with extremely small size limit"""
347
- result = truncate_and_serialize_messages (sample_messages , max_bytes = 10 )
362
+ tiny_limit = 10 # 10 bytes - extremely small limit
363
+ result = truncate_and_serialize_messages (sample_messages , max_bytes = tiny_limit )
348
364
349
365
# With such a small limit, likely all messages will be removed
350
366
if result ["serialized_data" ] is None :
351
367
assert result ["metadata" ]["truncated_count" ] == 0
352
368
else :
353
369
# If any data remains, it should be under the limit
354
370
size = len (result ["serialized_data" ].encode ("utf-8" ))
355
- assert size <= 10
371
+ assert size <= tiny_limit
356
372
357
373
def test_messages_with_none_values (self ):
358
374
"""Test messages containing None values"""
@@ -380,7 +396,8 @@ def test_truncation_keeps_most_recent(self):
380
396
)
381
397
382
398
# Truncate to a small size that should remove several messages
383
- result = truncate_and_serialize_messages (messages , max_bytes = 1000 )
399
+ small_limit = MAX_GEN_AI_MESSAGE_BYTES // 500 # 1KB limit to force truncation
400
+ result = truncate_and_serialize_messages (messages , max_bytes = small_limit )
384
401
385
402
if result ["serialized_data" ]:
386
403
parsed = json .loads (result ["serialized_data" ])
0 commit comments