1919
2020@pytest .mark .asyncio
2121async def test_list_collections_success ():
22+ mock_client = AsyncMock ()
2223 with (
2324 patch ("vectorcode.mcp_main.get_collections" ) as mock_get_collections ,
2425 patch ("vectorcode.common.try_server" , return_value = True ),
26+ patch (
27+ "vectorcode.mcp_main.ClientManager._create_client" , return_value = mock_client
28+ ),
2529 ):
26- from vectorcode .mcp_main import ClientManager
27-
28- mock_client = AsyncMock ()
29- ClientManager ._create_client = AsyncMock (return_value = mock_client )
30-
3130 mock_collection1 = AsyncMock ()
3231 mock_collection1 .metadata = {"path" : "path1" }
3332 mock_collection2 = AsyncMock ()
@@ -45,15 +44,14 @@ async def async_generator():
4544
4645@pytest .mark .asyncio
4746async def test_list_collections_no_metadata ():
47+ mock_client = AsyncMock ()
4848 with (
4949 patch ("vectorcode.mcp_main.get_collections" ) as mock_get_collections ,
5050 patch ("vectorcode.common.try_server" , return_value = True ),
51+ patch (
52+ "vectorcode.mcp_main.ClientManager._create_client" , return_value = mock_client
53+ ),
5154 ):
52- from vectorcode .mcp_main import ClientManager
53-
54- mock_client = AsyncMock ()
55- ClientManager ._create_client = AsyncMock (return_value = mock_client )
56-
5755 mock_collection1 = AsyncMock ()
5856 mock_collection1 .metadata = {"path" : "path1" }
5957 mock_collection2 = AsyncMock ()
@@ -86,11 +84,15 @@ async def test_query_tool_invalid_project_root():
8684
8785@pytest .mark .asyncio
8886async def test_query_tool_success ():
87+ mock_client = AsyncMock ()
8988 with (
9089 tempfile .TemporaryDirectory () as temp_dir ,
9190 patch ("os.path.isdir" , return_value = True ),
9291 patch ("vectorcode.mcp_main.get_project_config" ) as mock_get_project_config ,
9392 patch ("vectorcode.mcp_main.get_collection" ) as mock_get_collection ,
93+ patch (
94+ "vectorcode.mcp_main.ClientManager._create_client" , return_value = mock_client
95+ ),
9496 patch (
9597 "vectorcode.subcommands.query.get_query_result_files"
9698 ) as mock_get_query_result_files ,
@@ -100,15 +102,11 @@ async def test_query_tool_success():
100102 patch ("os.path.relpath" , return_value = "rel/path.py" ),
101103 patch ("vectorcode.cli_utils.load_config_file" ) as mock_load_config_file ,
102104 ):
103- from vectorcode .mcp_main import ClientManager
104-
105105 mock_config = Config (
106106 chunk_size = 100 , overlap_ratio = 0.1 , reranker = None , project_root = temp_dir
107107 )
108108 mock_load_config_file .return_value = mock_config
109109 mock_get_project_config .return_value = mock_config
110- mock_client = AsyncMock ()
111- ClientManager ._create_client = AsyncMock (return_value = mock_client )
112110
113111 # Mock the collection's query method to return a valid QueryResult
114112 mock_collection = AsyncMock ()
@@ -141,15 +139,12 @@ async def test_query_tool_collection_access_failure():
141139 with (
142140 patch ("os.path.isdir" , return_value = True ),
143141 patch ("vectorcode.mcp_main.get_project_config" ),
144- patch ("vectorcode.mcp_main.get_collection" ), # Still mock get_collection
142+ patch ("vectorcode.mcp_main.get_collection" ),
143+ patch (
144+ "vectorcode.mcp_main.ClientManager._create_client" ,
145+ side_effect = Exception ("Failed to connect" ),
146+ ),
145147 ):
146- from vectorcode .mcp_main import ClientManager
147-
148- async def failing_get_client (* args , ** kwargs ):
149- raise Exception ("Failed to connect" )
150-
151- ClientManager ._create_client = AsyncMock (side_effect = failing_get_client )
152-
153148 with pytest .raises (McpError ) as exc_info :
154149 await query_tool (
155150 n_query = 2 , query_messages = ["keyword1" ], project_root = "/valid/path"
@@ -164,16 +159,15 @@ async def failing_get_client(*args, **kwargs):
164159
165160@pytest .mark .asyncio
166161async def test_query_tool_no_collection ():
162+ mock_client = AsyncMock ()
167163 with (
168164 patch ("os.path.isdir" , return_value = True ),
169165 patch ("vectorcode.mcp_main.get_project_config" ),
166+ patch ("vectorcode.mcp_main.get_collection" ) as mock_get_collection ,
170167 patch (
171- "vectorcode.mcp_main.get_collection"
172- ) as mock_get_collection , # Still mock get_collection
173- patch ("vectorcode.common.ClientManager" ) as MockClientManager ,
168+ "vectorcode.mcp_main.ClientManager._create_client" , return_value = mock_client
169+ ),
174170 ):
175- mock_client = AsyncMock ()
176- MockClientManager .return_value ._create_client .return_value = mock_client
177171 mock_get_collection .return_value = None
178172
179173 with pytest .raises (McpError ) as exc_info :
@@ -203,25 +197,24 @@ async def test_vectorise_files_success():
203197 file_path = f"{ temp_dir } /test_file.py"
204198 with open (file_path , "w" ) as f :
205199 f .write ("def func(): pass" )
200+ mock_client = AsyncMock ()
206201
207202 with (
208203 patch ("os.path.isdir" , return_value = True ),
209204 patch ("vectorcode.mcp_main.get_project_config" ) as mock_get_project_config ,
210205 patch ("vectorcode.mcp_main.get_collection" ) as mock_get_collection ,
206+ patch (
207+ "vectorcode.mcp_main.ClientManager._create_client" ,
208+ return_value = mock_client ,
209+ ),
211210 patch ("vectorcode.subcommands.vectorise.chunked_add" ),
212211 patch (
213212 "vectorcode.subcommands.vectorise.hash_file" , return_value = "test_hash"
214213 ),
215214 patch ("vectorcode.common.try_server" , return_value = True ),
216215 ):
217- from vectorcode .mcp_main import ClientManager
218-
219216 mock_config = Config (project_root = temp_dir )
220217 mock_get_project_config .return_value = mock_config
221- mock_client = AsyncMock ()
222-
223- # Ensure ClientManager's internal client creation method returns our mock.
224- ClientManager ._create_client = AsyncMock (return_value = mock_client )
225218
226219 mock_collection = AsyncMock ()
227220 mock_collection .get .return_value = {"ids" : [], "metadatas" : []}
@@ -241,13 +234,12 @@ async def test_vectorise_files_collection_access_failure():
241234 with (
242235 patch ("os.path.isdir" , return_value = True ),
243236 patch ("vectorcode.mcp_main.get_project_config" ),
244- patch ("vectorcode.common.ClientManager" ), # Patch ClientManager class
237+ patch (
238+ "vectorcode.mcp_main.ClientManager._create_client" ,
239+ side_effect = Exception ("Client error" ),
240+ ),
245241 patch ("vectorcode.mcp_main.get_collection" ),
246242 ):
247- from vectorcode .mcp_main import ClientManager
248-
249- ClientManager ._create_client = AsyncMock (side_effect = Exception ("Client error" ))
250-
251243 with pytest .raises (McpError ) as exc_info :
252244 await vectorise_files (paths = ["file.py" ], project_root = "/valid/path" )
253245
@@ -280,10 +272,15 @@ def mock_open_side_effect(filename, *args, **kwargs):
280272 # For other files that might be opened, return a generic mock
281273 return MagicMock ()
282274
275+ mock_client = AsyncMock ()
283276 with (
284277 patch ("os.path.isdir" , return_value = True ),
285278 patch ("vectorcode.mcp_main.get_project_config" ) as mock_get_project_config ,
286279 patch ("vectorcode.mcp_main.get_collection" ) as mock_get_collection ,
280+ patch (
281+ "vectorcode.mcp_main.ClientManager._create_client" ,
282+ return_value = mock_client ,
283+ ),
287284 patch ("vectorcode.subcommands.vectorise.chunked_add" ) as mock_chunked_add ,
288285 patch (
289286 "vectorcode.subcommands.vectorise.hash_file" , return_value = "test_hash"
@@ -297,12 +294,8 @@ def mock_open_side_effect(filename, *args, **kwargs):
297294 ),
298295 patch ("vectorcode.common.try_server" , return_value = True ),
299296 ):
300- from vectorcode .mcp_main import ClientManager
301-
302297 mock_config = Config (project_root = temp_dir )
303298 mock_get_project_config .return_value = mock_config
304- mock_client = AsyncMock ()
305- ClientManager ._create_client = AsyncMock (return_value = mock_client )
306299
307300 mock_collection = AsyncMock ()
308301 mock_collection .get .return_value = {"ids" : [], "metadatas" : []}
@@ -321,23 +314,22 @@ def mock_open_side_effect(filename, *args, **kwargs):
321314
322315@pytest .mark .asyncio
323316async def test_mcp_server ():
317+ mock_client = AsyncMock ()
324318 with (
325319 patch (
326320 "vectorcode.mcp_main.find_project_config_dir"
327321 ) as mock_find_project_config_dir ,
328322 patch ("vectorcode.mcp_main.load_config_file" ) as mock_load_config_file ,
329- # patch("vectorcode.mcp_main.get_client") as mock_get_client, # Removed
330323 patch ("vectorcode.mcp_main.get_collection" ) as mock_get_collection ,
331324 patch ("mcp.server.fastmcp.FastMCP.add_tool" ) as mock_add_tool ,
332325 patch ("vectorcode.common.try_server" , return_value = True ),
326+ patch (
327+ "vectorcode.mcp_main.ClientManager._create_client" , return_value = mock_client
328+ ),
333329 ):
334- from vectorcode .mcp_main import ClientManager
335-
336330 mock_find_project_config_dir .return_value = "/path/to/config"
337331 mock_load_config_file .return_value = Config (project_root = "/path/to/project" )
338- mock_client = AsyncMock ()
339332
340- ClientManager ._create_client = AsyncMock (return_value = mock_client )
341333 mock_collection = AsyncMock ()
342334 mock_get_collection .return_value = mock_collection
343335
@@ -348,6 +340,7 @@ async def test_mcp_server():
348340
349341@pytest .mark .asyncio
350342async def test_mcp_server_ls_on_start ():
343+ mock_client = AsyncMock ()
351344 mock_collection = AsyncMock ()
352345
353346 with (
@@ -361,15 +354,15 @@ async def test_mcp_server_ls_on_start():
361354 ) as mock_get_collections ,
362355 patch ("mcp.server.fastmcp.FastMCP.add_tool" ) as mock_add_tool ,
363356 patch ("vectorcode.common.try_server" , return_value = True ),
357+ patch (
358+ "vectorcode.mcp_main.ClientManager._create_client" , return_value = mock_client
359+ ),
364360 ):
365- from vectorcode .mcp_main import ClientManager , mcp_config
361+ from vectorcode .mcp_main import mcp_config
366362
367363 mcp_config .ls_on_start = True
368364 mock_find_project_config_dir .return_value = "/path/to/config"
369365 mock_load_config_file .return_value = Config (project_root = "/path/to/project" )
370- mock_client = AsyncMock ()
371-
372- ClientManager ._create_client = AsyncMock (return_value = mock_client )
373366
374367 mock_collection .metadata = {"path" : "/path/to/project" }
375368 mock_get_collection .return_value = mock_collection
0 commit comments