11import os
22import tempfile
33from argparse import ArgumentParser
4- from contextlib import asynccontextmanager
54from unittest .mock import AsyncMock , MagicMock , mock_open , patch
65
76import pytest
@@ -25,9 +24,7 @@ async def test_list_collections_success():
2524 patch ("vectorcode.common.ClientManager" ) as MockClientManager ,
2625 ):
2726 mock_client = AsyncMock ()
28- MockClientManager .return_value ._create_client = AsyncMock (
29- return_value = mock_client
30- )
27+ MockClientManager .return_value ._create_client .return_value = mock_client
3128
3229 mock_collection1 = AsyncMock ()
3330 mock_collection1 .metadata = {"path" : "path1" }
@@ -51,9 +48,8 @@ async def test_list_collections_no_metadata():
5148 patch ("vectorcode.common.ClientManager" ) as MockClientManager ,
5249 ):
5350 mock_client = AsyncMock ()
54- MockClientManager .return_value ._create_client = asynccontextmanager (
55- AsyncMock (return_value = mock_client )
56- )
51+ MockClientManager .return_value ._create_client .return_value = mock_client
52+
5753 mock_collection1 = AsyncMock ()
5854 mock_collection1 .metadata = {"path" : "path1" }
5955 mock_collection2 = AsyncMock ()
@@ -103,9 +99,7 @@ async def test_query_tool_success():
10399 mock_load_config_file .return_value = mock_config
104100 mock_get_project_config .return_value = mock_config
105101 mock_client = AsyncMock ()
106- MockClientManager .return_value ._create_client = AsyncMock (
107- return_value = mock_client
108- )
102+ MockClientManager .return_value ._create_client .return_value = mock_client
109103
110104 # Mock the collection's query method to return a valid QueryResult
111105 mock_collection = AsyncMock ()
@@ -169,7 +163,8 @@ async def test_query_tool_no_collection():
169163 ) as mock_get_collection , # Still mock get_collection
170164 patch ("vectorcode.common.ClientManager" ) as MockClientManager ,
171165 ):
172- MockClientManager .return_value ._create_client .return_value = AsyncMock ()
166+ mock_client = AsyncMock ()
167+ MockClientManager .return_value ._create_client .return_value = mock_client
173168 mock_get_collection .return_value = None
174169
175170 with pytest .raises (McpError ) as exc_info :
@@ -215,9 +210,7 @@ async def test_vectorise_files_success():
215210 mock_client = AsyncMock ()
216211
217212 # Ensure ClientManager's internal client creation method returns our mock.
218- MockClientManager .return_value ._create_client = AsyncMock (
219- return_value = mock_client
220- )
213+ MockClientManager .return_value ._create_client .return_value = mock_client
221214
222215 mock_collection = AsyncMock ()
223216 mock_collection .get .return_value = {"ids" : [], "metadatas" : []}
@@ -243,11 +236,9 @@ async def test_vectorise_files_collection_access_failure(): # Removed client_ma
243236 ) as MockClientManager , # Patch ClientManager class
244237 patch ("vectorcode.mcp_main.get_collection" ),
245238 ):
246-
247- async def failing_get_client (* args , ** kwargs ):
248- raise Exception ("Client error" )
249-
250- MockClientManager .return_value ._create_client = failing_get_client
239+ MockClientManager .return_value ._create_client .side_effect = Exception (
240+ "Client error"
241+ )
251242
252243 with pytest .raises (McpError ) as exc_info :
253244 await vectorise_files (paths = ["file.py" ], project_root = "/valid/path" )
@@ -301,9 +292,7 @@ def mock_open_side_effect(filename, *args, **kwargs):
301292 mock_config = Config (project_root = temp_dir )
302293 mock_get_project_config .return_value = mock_config
303294 mock_client = AsyncMock ()
304- MockClientManager .return_value ._create_client = AsyncMock (
305- return_value = mock_client
306- )
295+ MockClientManager .return_value ._create_client .return_value = mock_client
307296
308297 mock_collection = AsyncMock ()
309298 mock_collection .get .return_value = {"ids" : [], "metadatas" : []}
@@ -336,7 +325,7 @@ async def test_mcp_server():
336325 mock_load_config_file .return_value = Config (project_root = "/path/to/project" )
337326 mock_client = AsyncMock ()
338327
339- MockClientManager .return_value .get_client = AsyncMock ( return_value = mock_client )
328+ MockClientManager .return_value ._create_client . return_value = mock_client
340329 mock_collection = AsyncMock ()
341330 mock_get_collection .return_value = mock_collection
342331
@@ -347,30 +336,29 @@ async def test_mcp_server():
347336
348337@pytest .mark .asyncio
349338async def test_mcp_server_ls_on_start ():
339+ mock_collection = AsyncMock ()
340+
350341 with (
351342 patch (
352343 "vectorcode.mcp_main.find_project_config_dir"
353344 ) as mock_find_project_config_dir ,
354345 patch ("vectorcode.mcp_main.load_config_file" ) as mock_load_config_file ,
355- # patch("vectorcode.mcp_main.get_client") as mock_get_client, # Removed
356346 patch ("vectorcode.mcp_main.get_collection" ) as mock_get_collection ,
357347 patch (
358348 "vectorcode.mcp_main.get_collections" , spec = AsyncMock
359349 ) as mock_get_collections ,
360350 patch ("mcp.server.fastmcp.FastMCP.add_tool" ) as mock_add_tool ,
361- patch ("vectorcode.common.ClientManager" ) as MockClientManager , # Added
351+ patch ("vectorcode.common.try_server" , return_value = True ),
362352 ):
363- from vectorcode .mcp_main import mcp_config
353+ from vectorcode .mcp_main import ClientManager , mcp_config
364354
365355 mcp_config .ls_on_start = True
366356 mock_find_project_config_dir .return_value = "/path/to/config"
367357 mock_load_config_file .return_value = Config (project_root = "/path/to/project" )
368358 mock_client = AsyncMock ()
369359
370- MockClientManager .return_value ._create_client = AsyncMock (
371- return_value = mock_client
372- )
373- mock_collection = AsyncMock ()
360+ ClientManager ._create_client = AsyncMock (return_value = mock_client )
361+
374362 mock_collection .metadata = {"path" : "/path/to/project" }
375363 mock_get_collection .return_value = mock_collection
376364
0 commit comments