1
1
import os
2
2
import tempfile
3
3
from argparse import ArgumentParser
4
- from contextlib import asynccontextmanager
5
4
from unittest .mock import AsyncMock , MagicMock , mock_open , patch
6
5
7
6
import pytest
22
21
async def test_list_collections_success ():
23
22
with (
24
23
patch ("vectorcode.mcp_main.get_collections" ) as mock_get_collections ,
25
- patch ("vectorcode.common.ClientManager" ) as MockClientManager ,
24
+ patch ("vectorcode.common.try_server" , return_value = True ) ,
26
25
):
26
+ from vectorcode .mcp_main import ClientManager
27
+
27
28
mock_client = AsyncMock ()
28
- MockClientManager .return_value ._create_client = AsyncMock (
29
- return_value = mock_client
30
- )
29
+ ClientManager ._create_client = AsyncMock (return_value = mock_client )
31
30
32
31
mock_collection1 = AsyncMock ()
33
32
mock_collection1 .metadata = {"path" : "path1" }
@@ -48,12 +47,13 @@ async def async_generator():
48
47
async def test_list_collections_no_metadata ():
49
48
with (
50
49
patch ("vectorcode.mcp_main.get_collections" ) as mock_get_collections ,
51
- patch ("vectorcode.common.ClientManager" ) as MockClientManager ,
50
+ patch ("vectorcode.common.try_server" , return_value = True ) ,
52
51
):
52
+ from vectorcode .mcp_main import ClientManager
53
+
53
54
mock_client = AsyncMock ()
54
- MockClientManager .return_value ._create_client = asynccontextmanager (
55
- AsyncMock (return_value = mock_client )
56
- )
55
+ ClientManager ._create_client = AsyncMock (return_value = mock_client )
56
+
57
57
mock_collection1 = AsyncMock ()
58
58
mock_collection1 .metadata = {"path" : "path1" }
59
59
mock_collection2 = AsyncMock ()
@@ -93,19 +93,19 @@ async def test_query_tool_success():
93
93
patch (
94
94
"vectorcode.subcommands.query.get_query_result_files"
95
95
) as mock_get_query_result_files ,
96
+ patch ("vectorcode.common.try_server" , return_value = True ),
96
97
patch ("builtins.open" , create = True ) as mock_open ,
97
98
patch ("os.path.isfile" , return_value = True ),
98
99
patch ("os.path.relpath" , return_value = "rel/path.py" ),
99
100
patch ("vectorcode.cli_utils.load_config_file" ) as mock_load_config_file ,
100
- patch ("vectorcode.common.ClientManager" ) as MockClientManager ,
101
101
):
102
+ from vectorcode .mcp_main import ClientManager
103
+
102
104
mock_config = Config (chunk_size = 100 , overlap_ratio = 0.1 , reranker = None )
103
105
mock_load_config_file .return_value = mock_config
104
106
mock_get_project_config .return_value = mock_config
105
107
mock_client = AsyncMock ()
106
- MockClientManager .return_value ._create_client = AsyncMock (
107
- return_value = mock_client
108
- )
108
+ ClientManager ._create_client = AsyncMock (return_value = mock_client )
109
109
110
110
# Mock the collection's query method to return a valid QueryResult
111
111
mock_collection = AsyncMock ()
@@ -139,13 +139,13 @@ async def test_query_tool_collection_access_failure():
139
139
patch ("os.path.isdir" , return_value = True ),
140
140
patch ("vectorcode.mcp_main.get_project_config" ),
141
141
patch ("vectorcode.mcp_main.get_collection" ), # Still mock get_collection
142
- patch ("vectorcode.common.ClientManager" ) as MockClientManager ,
143
142
):
143
+ from vectorcode .mcp_main import ClientManager
144
144
145
145
async def failing_get_client (* args , ** kwargs ):
146
146
raise Exception ("Failed to connect" )
147
147
148
- MockClientManager . return_value . _create_client . side_effect = failing_get_client
148
+ ClientManager . _create_client = AsyncMock ( side_effect = failing_get_client )
149
149
150
150
with pytest .raises (McpError ) as exc_info :
151
151
await query_tool (
@@ -169,7 +169,8 @@ async def test_query_tool_no_collection():
169
169
) as mock_get_collection , # Still mock get_collection
170
170
patch ("vectorcode.common.ClientManager" ) as MockClientManager ,
171
171
):
172
- MockClientManager .return_value ._create_client .return_value = AsyncMock ()
172
+ mock_client = AsyncMock ()
173
+ MockClientManager .return_value ._create_client .return_value = mock_client
173
174
mock_get_collection .return_value = None
174
175
175
176
with pytest .raises (McpError ) as exc_info :
@@ -201,23 +202,23 @@ async def test_vectorise_files_success():
201
202
f .write ("def func(): pass" )
202
203
203
204
with (
204
- patch ("vectorcode.common.ClientManager" ) as MockClientManager ,
205
205
patch ("os.path.isdir" , return_value = True ),
206
206
patch ("vectorcode.mcp_main.get_project_config" ) as mock_get_project_config ,
207
207
patch ("vectorcode.mcp_main.get_collection" ) as mock_get_collection ,
208
208
patch ("vectorcode.subcommands.vectorise.chunked_add" ),
209
209
patch (
210
210
"vectorcode.subcommands.vectorise.hash_file" , return_value = "test_hash"
211
211
),
212
+ patch ("vectorcode.common.try_server" , return_value = True ),
212
213
):
214
+ from vectorcode .mcp_main import ClientManager
215
+
213
216
mock_config = Config (project_root = temp_dir )
214
217
mock_get_project_config .return_value = mock_config
215
218
mock_client = AsyncMock ()
216
219
217
220
# Ensure ClientManager's internal client creation method returns our mock.
218
- MockClientManager .return_value ._create_client = AsyncMock (
219
- return_value = mock_client
220
- )
221
+ ClientManager ._create_client = AsyncMock (return_value = mock_client )
221
222
222
223
mock_collection = AsyncMock ()
223
224
mock_collection .get .return_value = {"ids" : [], "metadatas" : []}
@@ -233,21 +234,16 @@ async def test_vectorise_files_success():
233
234
234
235
235
236
@pytest .mark .asyncio
236
- async def test_vectorise_files_collection_access_failure (): # Removed client_manager fixture
237
+ async def test_vectorise_files_collection_access_failure ():
237
238
with (
238
239
patch ("os.path.isdir" , return_value = True ),
239
240
patch ("vectorcode.mcp_main.get_project_config" ),
240
- # patch("vectorcode.mcp_main.get_client", side_effect=Exception("Client error")), # Removed explicit patch
241
- patch (
242
- "vectorcode.common.ClientManager"
243
- ) as MockClientManager , # Patch ClientManager class
241
+ patch ("vectorcode.common.ClientManager" ), # Patch ClientManager class
244
242
patch ("vectorcode.mcp_main.get_collection" ),
245
243
):
244
+ from vectorcode .mcp_main import ClientManager
246
245
247
- async def failing_get_client (* args , ** kwargs ):
248
- raise Exception ("Client error" )
249
-
250
- MockClientManager .return_value ._create_client = failing_get_client
246
+ ClientManager ._create_client = AsyncMock (side_effect = Exception ("Client error" ))
251
247
252
248
with pytest .raises (McpError ) as exc_info :
253
249
await vectorise_files (paths = ["file.py" ], project_root = "/valid/path" )
@@ -296,14 +292,14 @@ def mock_open_side_effect(filename, *args, **kwargs):
296
292
"os.path.isfile" ,
297
293
side_effect = lambda x : x in [file1 , excluded_file , exclude_spec_file ],
298
294
),
299
- patch ("vectorcode.common.ClientManager" ) as MockClientManager ,
295
+ patch ("vectorcode.common.try_server" , return_value = True ) ,
300
296
):
297
+ from vectorcode .mcp_main import ClientManager
298
+
301
299
mock_config = Config (project_root = temp_dir )
302
300
mock_get_project_config .return_value = mock_config
303
301
mock_client = AsyncMock ()
304
- MockClientManager .return_value ._create_client = AsyncMock (
305
- return_value = mock_client
306
- )
302
+ ClientManager ._create_client = AsyncMock (return_value = mock_client )
307
303
308
304
mock_collection = AsyncMock ()
309
305
mock_collection .get .return_value = {"ids" : [], "metadatas" : []}
@@ -330,13 +326,15 @@ async def test_mcp_server():
330
326
# patch("vectorcode.mcp_main.get_client") as mock_get_client, # Removed
331
327
patch ("vectorcode.mcp_main.get_collection" ) as mock_get_collection ,
332
328
patch ("mcp.server.fastmcp.FastMCP.add_tool" ) as mock_add_tool ,
333
- patch ("vectorcode.common.ClientManager" ) as MockClientManager , # Added
329
+ patch ("vectorcode.common.try_server" , return_value = True ),
334
330
):
331
+ from vectorcode .mcp_main import ClientManager
332
+
335
333
mock_find_project_config_dir .return_value = "/path/to/config"
336
334
mock_load_config_file .return_value = Config (project_root = "/path/to/project" )
337
335
mock_client = AsyncMock ()
338
336
339
- MockClientManager . return_value . get_client = AsyncMock (return_value = mock_client )
337
+ ClientManager . _create_client = AsyncMock (return_value = mock_client )
340
338
mock_collection = AsyncMock ()
341
339
mock_get_collection .return_value = mock_collection
342
340
@@ -347,30 +345,29 @@ async def test_mcp_server():
347
345
348
346
@pytest .mark .asyncio
349
347
async def test_mcp_server_ls_on_start ():
348
+ mock_collection = AsyncMock ()
349
+
350
350
with (
351
351
patch (
352
352
"vectorcode.mcp_main.find_project_config_dir"
353
353
) as mock_find_project_config_dir ,
354
354
patch ("vectorcode.mcp_main.load_config_file" ) as mock_load_config_file ,
355
- # patch("vectorcode.mcp_main.get_client") as mock_get_client, # Removed
356
355
patch ("vectorcode.mcp_main.get_collection" ) as mock_get_collection ,
357
356
patch (
358
357
"vectorcode.mcp_main.get_collections" , spec = AsyncMock
359
358
) as mock_get_collections ,
360
359
patch ("mcp.server.fastmcp.FastMCP.add_tool" ) as mock_add_tool ,
361
- patch ("vectorcode.common.ClientManager" ) as MockClientManager , # Added
360
+ patch ("vectorcode.common.try_server" , return_value = True ),
362
361
):
363
- from vectorcode .mcp_main import mcp_config
362
+ from vectorcode .mcp_main import ClientManager , mcp_config
364
363
365
364
mcp_config .ls_on_start = True
366
365
mock_find_project_config_dir .return_value = "/path/to/config"
367
366
mock_load_config_file .return_value = Config (project_root = "/path/to/project" )
368
367
mock_client = AsyncMock ()
369
368
370
- MockClientManager .return_value ._create_client = AsyncMock (
371
- return_value = mock_client
372
- )
373
- mock_collection = AsyncMock ()
369
+ ClientManager ._create_client = AsyncMock (return_value = mock_client )
370
+
374
371
mock_collection .metadata = {"path" : "/path/to/project" }
375
372
mock_get_collection .return_value = mock_collection
376
373
0 commit comments