forked from OpenHands/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_conversation_summary.py
More file actions
81 lines (59 loc) · 2.6 KB
/
Copy pathtest_conversation_summary.py
File metadata and controls
81 lines (59 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Tests for the conversation summary generator."""
from unittest.mock import MagicMock
import pytest
from openhands.core.config import LLMConfig
from openhands.utils.conversation_summary import generate_conversation_title
@pytest.mark.asyncio
async def test_generate_conversation_title_empty_message():
"""Test that an empty message returns None."""
mock_llm_registry = MagicMock()
mock_llm_config = LLMConfig(model='test-model')
result = await generate_conversation_title('', mock_llm_config, mock_llm_registry)
assert result is None
result = await generate_conversation_title(
' ', mock_llm_config, mock_llm_registry
)
assert result is None
@pytest.mark.asyncio
async def test_generate_conversation_title_success():
"""Test successful title generation."""
# Create a mock LLM registry that returns a title
mock_llm_registry = MagicMock()
mock_llm_registry.request_extraneous_completion.return_value = 'Generated Title'
mock_llm_config = LLMConfig(model='test-model')
result = await generate_conversation_title(
'Can you help me with Python?', mock_llm_config, mock_llm_registry
)
assert result == 'Generated Title'
# Verify the mock was called with the expected arguments
mock_llm_registry.request_extraneous_completion.assert_called_once()
@pytest.mark.asyncio
async def test_generate_conversation_title_long_title():
"""Test that long titles are truncated."""
# Create a mock LLM registry that returns a long title
mock_llm_registry = MagicMock()
mock_llm_registry.request_extraneous_completion.return_value = 'This is a very long title that should be truncated because it exceeds the maximum length'
mock_llm_config = LLMConfig(model='test-model')
result = await generate_conversation_title(
'Can you help me with Python?',
mock_llm_config,
mock_llm_registry,
max_length=30,
)
# Verify the title is truncated correctly
assert len(result) <= 30
assert result.endswith('...')
@pytest.mark.asyncio
async def test_generate_conversation_title_exception():
"""Test that exceptions are handled gracefully."""
# Create a mock LLM registry that raises an exception
mock_llm_registry = MagicMock()
mock_llm_registry.request_extraneous_completion.side_effect = Exception(
'Test error'
)
mock_llm_config = LLMConfig(model='test-model')
result = await generate_conversation_title(
'Can you help me with Python?', mock_llm_config, mock_llm_registry
)
# Verify that None is returned when an exception occurs
assert result is None