test(context): add edge case tests for truncation and scoring concurrency
- Add tests for truncation edge cases, including zero tokens, short content, and marker handling. - Add concurrency tests for scoring to verify per-context locking and handling of multiple contexts.
This commit is contained in:
@@ -212,3 +212,80 @@ class TestContextCompressor:
|
||||
assert compressor._get_strategy_for_type(ContextType.KNOWLEDGE) == "sentence"
|
||||
assert compressor._get_strategy_for_type(ContextType.CONVERSATION) == "end"
|
||||
assert compressor._get_strategy_for_type(ContextType.TOOL) == "middle"
|
||||
|
||||
|
||||
class TestTruncationEdgeCases:
|
||||
"""Tests for edge cases in truncation to prevent regressions."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_truncation_ratio_with_zero_original_tokens(self) -> None:
|
||||
"""Test that truncation ratio handles zero original tokens without division by zero."""
|
||||
strategy = TruncationStrategy()
|
||||
|
||||
# Empty content should not raise ZeroDivisionError
|
||||
result = await strategy.truncate_to_tokens("", max_tokens=100)
|
||||
|
||||
assert result.truncation_ratio == 0.0
|
||||
assert result.original_tokens == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_truncate_end_with_zero_available_tokens(self) -> None:
|
||||
"""Test truncation when marker tokens exceed max_tokens."""
|
||||
strategy = TruncationStrategy()
|
||||
content = "Some content to truncate"
|
||||
|
||||
# max_tokens less than marker tokens should return just marker
|
||||
result = await strategy.truncate_to_tokens(content, max_tokens=1, strategy="end")
|
||||
|
||||
# Should handle gracefully without crashing
|
||||
assert strategy.TRUNCATION_MARKER in result.content or result.content == content
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_truncate_with_content_that_has_zero_tokens(self) -> None:
|
||||
"""Test truncation when content estimates to zero tokens."""
|
||||
strategy = TruncationStrategy()
|
||||
|
||||
# Very short content that might estimate to 0 tokens
|
||||
result = await strategy.truncate_to_tokens("a", max_tokens=100)
|
||||
|
||||
# Should not raise ZeroDivisionError
|
||||
assert result.content in ("a", "a" + strategy.TRUNCATION_MARKER)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_content_for_tokens_zero_target(self) -> None:
|
||||
"""Test _get_content_for_tokens with zero target tokens."""
|
||||
strategy = TruncationStrategy()
|
||||
|
||||
result = await strategy._get_content_for_tokens(
|
||||
content="Some content",
|
||||
target_tokens=0,
|
||||
from_start=True,
|
||||
)
|
||||
|
||||
assert result == ""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sentence_truncation_with_no_sentences(self) -> None:
|
||||
"""Test sentence truncation with content that has no sentence boundaries."""
|
||||
strategy = TruncationStrategy()
|
||||
content = "this is content without any sentence ending punctuation"
|
||||
|
||||
result = await strategy.truncate_to_tokens(
|
||||
content, max_tokens=5, strategy="sentence"
|
||||
)
|
||||
|
||||
# Should handle gracefully
|
||||
assert result is not None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_middle_truncation_very_short_content(self) -> None:
|
||||
"""Test middle truncation with content shorter than preserved portions."""
|
||||
strategy = TruncationStrategy(preserve_ratio_start=0.7)
|
||||
content = "ab" # Very short
|
||||
|
||||
result = await strategy.truncate_to_tokens(
|
||||
content, max_tokens=1, strategy="middle"
|
||||
)
|
||||
|
||||
# Should handle gracefully without negative indices
|
||||
assert result is not None
|
||||
|
||||
Reference in New Issue
Block a user