forked from All-Hands-AI/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_search_utils.py
137 lines (114 loc) Β· 4.09 KB
/
test_search_utils.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import json
import pytest
from openhands.storage.conversation.file_conversation_store import FileConversationStore
from openhands.storage.memory import InMemoryFileStore
from openhands.utils.search_utils import iterate, offset_to_page_id, page_id_to_offset
def test_offset_to_page_id():
# Test with has_next=True
assert bool(offset_to_page_id(10, True))
assert bool(offset_to_page_id(0, True))
# Test with has_next=False should return None
assert offset_to_page_id(10, False) is None
assert offset_to_page_id(0, False) is None
def test_page_id_to_offset():
# Test with None should return 0
assert page_id_to_offset(None) == 0
def test_bidirectional_conversion():
# Test converting offset to page_id and back
test_offsets = [0, 1, 10, 100, 1000]
for offset in test_offsets:
page_id = offset_to_page_id(offset, True)
assert page_id_to_offset(page_id) == offset
@pytest.mark.asyncio
async def test_iterate_empty():
store = FileConversationStore(InMemoryFileStore({}))
results = []
async for result in iterate(store.search):
results.append(result)
assert len(results) == 0
@pytest.mark.asyncio
async def test_iterate_single_page():
store = FileConversationStore(
InMemoryFileStore(
{
'sessions/conv1/metadata.json': json.dumps(
{
'conversation_id': 'conv1',
'github_user_id': '123',
'selected_repository': 'repo1',
'title': 'First conversation',
'created_at': '2025-01-16T19:51:04Z',
}
),
'sessions/conv2/metadata.json': json.dumps(
{
'conversation_id': 'conv2',
'github_user_id': '123',
'selected_repository': 'repo1',
'title': 'Second conversation',
'created_at': '2025-01-17T19:51:04Z',
}
),
}
)
)
results = []
async for result in iterate(store.search):
results.append(result)
assert len(results) == 2
assert results[0].conversation_id == 'conv2' # newest first
assert results[1].conversation_id == 'conv1'
@pytest.mark.asyncio
async def test_iterate_multiple_pages():
# Create test data with 5 conversations
store = FileConversationStore(
InMemoryFileStore(
{
f'sessions/conv{i}/metadata.json': json.dumps(
{
'conversation_id': f'conv{i}',
'github_user_id': '123',
'selected_repository': 'repo1',
'title': f'Conversation {i}',
'created_at': f'2025-01-{15+i}T19:51:04Z',
}
)
for i in range(1, 6)
}
)
)
results = []
async for result in iterate(store.search, limit=2):
results.append(result)
assert len(results) == 5
# Should be sorted by date, newest first
assert [r.conversation_id for r in results] == [
'conv5',
'conv4',
'conv3',
'conv2',
'conv1',
]
@pytest.mark.asyncio
async def test_iterate_with_invalid_conversation():
store = FileConversationStore(
InMemoryFileStore(
{
'sessions/conv1/metadata.json': json.dumps(
{
'conversation_id': 'conv1',
'github_user_id': '123',
'selected_repository': 'repo1',
'title': 'Valid conversation',
'created_at': '2025-01-16T19:51:04Z',
}
),
'sessions/conv2/metadata.json': 'invalid json', # Invalid conversation
}
)
)
results = []
async for result in iterate(store.search):
results.append(result)
assert len(results) == 1
assert results[0].conversation_id == 'conv1'