66
77from basic_memory .mcp .tools import write_note
88from basic_memory .mcp .tools .search import search_notes , _format_search_error_response
9+ from basic_memory .schemas .search import SearchResponse
910
1011
1112@pytest .mark .asyncio
@@ -23,9 +24,14 @@ async def test_search_text(client):
2324 # Search for it
2425 response = await search_notes .fn (query = "searchable" )
2526
26- # Verify results
27- assert len (response .results ) > 0
28- assert any (r .permalink == "test/test-search-note" for r in response .results )
27+ # Verify results - handle both success and error cases
28+ if isinstance (response , SearchResponse ):
29+ # Success case - verify SearchResponse
30+ assert len (response .results ) > 0
31+ assert any (r .permalink == "test/test-search-note" for r in response .results )
32+ else :
33+ # If search failed and returned error message, test should fail with informative message
34+ pytest .fail (f"Search failed with error: { response } " )
2935
3036
3137@pytest .mark .asyncio
@@ -43,9 +49,14 @@ async def test_search_title(client):
4349 # Search for it
4450 response = await search_notes .fn (query = "Search Note" , search_type = "title" )
4551
46- # Verify results
47- assert len (response .results ) > 0
48- assert any (r .permalink == "test/test-search-note" for r in response .results )
52+ # Verify results - handle both success and error cases
53+ if isinstance (response , str ):
54+ # If search failed and returned error message, test should fail with informative message
55+ pytest .fail (f"Search failed with error: { response } " )
56+ else :
57+ # Success case - verify SearchResponse
58+ assert len (response .results ) > 0
59+ assert any (r .permalink == "test/test-search-note" for r in response .results )
4960
5061
5162@pytest .mark .asyncio
@@ -63,9 +74,14 @@ async def test_search_permalink(client):
6374 # Search for it
6475 response = await search_notes .fn (query = "test/test-search-note" , search_type = "permalink" )
6576
66- # Verify results
67- assert len (response .results ) > 0
68- assert any (r .permalink == "test/test-search-note" for r in response .results )
77+ # Verify results - handle both success and error cases
78+ if isinstance (response , SearchResponse ):
79+ # Success case - verify SearchResponse
80+ assert len (response .results ) > 0
81+ assert any (r .permalink == "test/test-search-note" for r in response .results )
82+ else :
83+ # If search failed and returned error message, test should fail with informative message
84+ pytest .fail (f"Search failed with error: { response } " )
6985
7086
7187@pytest .mark .asyncio
@@ -83,9 +99,14 @@ async def test_search_permalink_match(client):
8399 # Search for it
84100 response = await search_notes .fn (query = "test/test-search-*" , search_type = "permalink" )
85101
86- # Verify results
87- assert len (response .results ) > 0
88- assert any (r .permalink == "test/test-search-note" for r in response .results )
102+ # Verify results - handle both success and error cases
103+ if isinstance (response , SearchResponse ):
104+ # Success case - verify SearchResponse
105+ assert len (response .results ) > 0
106+ assert any (r .permalink == "test/test-search-note" for r in response .results )
107+ else :
108+ # If search failed and returned error message, test should fail with informative message
109+ pytest .fail (f"Search failed with error: { response } " )
89110
90111
91112@pytest .mark .asyncio
@@ -103,9 +124,14 @@ async def test_search_pagination(client):
103124 # Search for it
104125 response = await search_notes .fn (query = "searchable" , page = 1 , page_size = 1 )
105126
106- # Verify results
107- assert len (response .results ) == 1
108- assert any (r .permalink == "test/test-search-note" for r in response .results )
127+ # Verify results - handle both success and error cases
128+ if isinstance (response , SearchResponse ):
129+ # Success case - verify SearchResponse
130+ assert len (response .results ) == 1
131+ assert any (r .permalink == "test/test-search-note" for r in response .results )
132+ else :
133+ # If search failed and returned error message, test should fail with informative message
134+ pytest .fail (f"Search failed with error: { response } " )
109135
110136
111137@pytest .mark .asyncio
@@ -121,8 +147,13 @@ async def test_search_with_type_filter(client):
121147 # Search with type filter
122148 response = await search_notes .fn (query = "type" , types = ["note" ])
123149
124- # Verify all results are entities
125- assert all (r .type == "entity" for r in response .results )
150+ # Verify results - handle both success and error cases
151+ if isinstance (response , SearchResponse ):
152+ # Success case - verify all results are entities
153+ assert all (r .type == "entity" for r in response .results )
154+ else :
155+ # If search failed and returned error message, test should fail with informative message
156+ pytest .fail (f"Search failed with error: { response } " )
126157
127158
128159@pytest .mark .asyncio
@@ -138,8 +169,13 @@ async def test_search_with_entity_type_filter(client):
138169 # Search with entity type filter
139170 response = await search_notes .fn (query = "type" , entity_types = ["entity" ])
140171
141- # Verify all results are entities
142- assert all (r .type == "entity" for r in response .results )
172+ # Verify results - handle both success and error cases
173+ if isinstance (response , SearchResponse ):
174+ # Success case - verify all results are entities
175+ assert all (r .type == "entity" for r in response .results )
176+ else :
177+ # If search failed and returned error message, test should fail with informative message
178+ pytest .fail (f"Search failed with error: { response } " )
143179
144180
145181@pytest .mark .asyncio
@@ -156,8 +192,13 @@ async def test_search_with_date_filter(client):
156192 one_hour_ago = datetime .now () - timedelta (hours = 1 )
157193 response = await search_notes .fn (query = "recent" , after_date = one_hour_ago .isoformat ())
158194
159- # Verify we get results within timeframe
160- assert len (response .results ) > 0
195+ # Verify results - handle both success and error cases
196+ if isinstance (response , SearchResponse ):
197+ # Success case - verify we get results within timeframe
198+ assert len (response .results ) > 0
199+ else :
200+ # If search failed and returned error message, test should fail with informative message
201+ pytest .fail (f"Search failed with error: { response } " )
161202
162203
163204class TestSearchErrorFormatting :
0 commit comments