forked from yusufkaraaslan/Skill_Seekers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mcp_git_sources.py
More file actions
566 lines (453 loc) · 20.8 KB
/
test_mcp_git_sources.py
File metadata and controls
566 lines (453 loc) · 20.8 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#!/usr/bin/env python3
"""
MCP Integration Tests for Git Config Sources
Tests the complete MCP tool workflow for git-based config fetching
"""
import json
from unittest.mock import MagicMock, patch
import pytest
# Test if MCP is available
try:
import mcp # noqa: F401
from mcp.types import TextContent
MCP_AVAILABLE = True
except ImportError:
MCP_AVAILABLE = False
TextContent = None # Define placeholder
@pytest.fixture
def temp_dirs(tmp_path):
"""Create temporary directories for testing."""
config_dir = tmp_path / "config"
cache_dir = tmp_path / "cache"
dest_dir = tmp_path / "dest"
config_dir.mkdir()
cache_dir.mkdir()
dest_dir.mkdir()
return {"config": config_dir, "cache": cache_dir, "dest": dest_dir}
@pytest.fixture
def mock_git_repo(temp_dirs):
"""Create a mock git repository with config files."""
repo_path = temp_dirs["cache"] / "test-source"
repo_path.mkdir()
(repo_path / ".git").mkdir()
# Create sample config files
react_config = {
"name": "react",
"description": "React framework",
"base_url": "https://react.dev/",
}
(repo_path / "react.json").write_text(json.dumps(react_config, indent=2))
vue_config = {"name": "vue", "description": "Vue framework", "base_url": "https://vuejs.org/"}
(repo_path / "vue.json").write_text(json.dumps(vue_config, indent=2))
return repo_path
@pytest.mark.skipif(not MCP_AVAILABLE, reason="MCP not available")
@pytest.mark.asyncio
class TestFetchConfigModes:
"""Test fetch_config tool with different modes."""
async def test_fetch_config_api_mode_list(self):
"""Test API mode - listing available configs."""
from skill_seekers.mcp.server import fetch_config_tool
with patch("skill_seekers.mcp.server.httpx.AsyncClient") as mock_client:
# Mock API response
mock_response = MagicMock()
mock_response.json.return_value = {
"configs": [
{
"name": "react",
"category": "web-frameworks",
"description": "React framework",
"type": "single",
},
{
"name": "vue",
"category": "web-frameworks",
"description": "Vue framework",
"type": "single",
},
],
"total": 2,
}
mock_client.return_value.__aenter__.return_value.get.return_value = mock_response
args = {"list_available": True}
result = await fetch_config_tool(args)
assert len(result) == 1
assert isinstance(result[0], TextContent)
assert "react" in result[0].text
assert "vue" in result[0].text
async def test_fetch_config_api_mode_download(self, temp_dirs):
"""Test API mode - downloading specific config."""
from skill_seekers.mcp.server import fetch_config_tool
with patch("skill_seekers.mcp.server.httpx.AsyncClient") as mock_client:
# Mock API responses
mock_detail_response = MagicMock()
mock_detail_response.json.return_value = {
"name": "react",
"category": "web-frameworks",
"description": "React framework",
}
mock_download_response = MagicMock()
mock_download_response.json.return_value = {
"name": "react",
"base_url": "https://react.dev/",
}
mock_client_instance = mock_client.return_value.__aenter__.return_value
mock_client_instance.get.side_effect = [mock_detail_response, mock_download_response]
args = {"config_name": "react", "destination": str(temp_dirs["dest"])}
result = await fetch_config_tool(args)
assert len(result) == 1
assert "✅" in result[0].text
assert "react" in result[0].text
# Verify file was created
config_file = temp_dirs["dest"] / "react.json"
assert config_file.exists()
@patch("skill_seekers.mcp.server.GitConfigRepo")
async def test_fetch_config_git_url_mode(self, mock_git_repo_class, temp_dirs):
"""Test Git URL mode - direct git clone."""
from skill_seekers.mcp.server import fetch_config_tool
# Mock GitConfigRepo
mock_repo_instance = MagicMock()
mock_repo_path = temp_dirs["cache"] / "temp_react"
mock_repo_path.mkdir()
# Create mock config file
react_config = {"name": "react", "base_url": "https://react.dev/"}
(mock_repo_path / "react.json").write_text(json.dumps(react_config))
mock_repo_instance.clone_or_pull.return_value = mock_repo_path
mock_repo_instance.get_config.return_value = react_config
mock_git_repo_class.return_value = mock_repo_instance
args = {
"config_name": "react",
"git_url": "https://github.com/myorg/configs.git",
"destination": str(temp_dirs["dest"]),
}
result = await fetch_config_tool(args)
assert len(result) == 1
assert "✅" in result[0].text
assert "git URL" in result[0].text
assert "react" in result[0].text
# Verify clone was called
mock_repo_instance.clone_or_pull.assert_called_once()
# Verify file was created
config_file = temp_dirs["dest"] / "react.json"
assert config_file.exists()
@patch("skill_seekers.mcp.server.GitConfigRepo")
@patch("skill_seekers.mcp.server.SourceManager")
async def test_fetch_config_source_mode(
self, mock_source_manager_class, mock_git_repo_class, temp_dirs
):
"""Test Source mode - using named source from registry."""
from skill_seekers.mcp.server import fetch_config_tool
# Mock SourceManager
mock_source_manager = MagicMock()
mock_source_manager.get_source.return_value = {
"name": "team",
"git_url": "https://github.com/myorg/configs.git",
"branch": "main",
"token_env": "GITHUB_TOKEN",
}
mock_source_manager_class.return_value = mock_source_manager
# Mock GitConfigRepo
mock_repo_instance = MagicMock()
mock_repo_path = temp_dirs["cache"] / "team"
mock_repo_path.mkdir()
react_config = {"name": "react", "base_url": "https://react.dev/"}
(mock_repo_path / "react.json").write_text(json.dumps(react_config))
mock_repo_instance.clone_or_pull.return_value = mock_repo_path
mock_repo_instance.get_config.return_value = react_config
mock_git_repo_class.return_value = mock_repo_instance
args = {"config_name": "react", "source": "team", "destination": str(temp_dirs["dest"])}
result = await fetch_config_tool(args)
assert len(result) == 1
assert "✅" in result[0].text
assert "git source" in result[0].text
assert "team" in result[0].text
# Verify source was retrieved
mock_source_manager.get_source.assert_called_once_with("team")
# Verify file was created
config_file = temp_dirs["dest"] / "react.json"
assert config_file.exists()
async def test_fetch_config_source_not_found(self):
"""Test error when source doesn't exist."""
from skill_seekers.mcp.server import fetch_config_tool
with patch("skill_seekers.mcp.server.SourceManager") as mock_sm_class:
mock_sm = MagicMock()
mock_sm.get_source.side_effect = KeyError("Source 'nonexistent' not found")
mock_sm_class.return_value = mock_sm
args = {"config_name": "react", "source": "nonexistent"}
result = await fetch_config_tool(args)
assert len(result) == 1
assert "❌" in result[0].text
assert "not found" in result[0].text
@patch("skill_seekers.mcp.server.GitConfigRepo")
async def test_fetch_config_config_not_found_in_repo(self, mock_git_repo_class, temp_dirs):
"""Test error when config doesn't exist in repository."""
from skill_seekers.mcp.server import fetch_config_tool
# Mock GitConfigRepo
mock_repo_instance = MagicMock()
mock_repo_path = temp_dirs["cache"] / "temp_django"
mock_repo_path.mkdir()
mock_repo_instance.clone_or_pull.return_value = mock_repo_path
mock_repo_instance.get_config.side_effect = FileNotFoundError(
"Config 'django' not found in repository. Available configs: react, vue"
)
mock_git_repo_class.return_value = mock_repo_instance
args = {"config_name": "django", "git_url": "https://github.com/myorg/configs.git"}
result = await fetch_config_tool(args)
assert len(result) == 1
assert "❌" in result[0].text
assert "not found" in result[0].text
assert "Available configs" in result[0].text
@patch("skill_seekers.mcp.server.GitConfigRepo")
async def test_fetch_config_invalid_git_url(self, mock_git_repo_class):
"""Test error handling for invalid git URL."""
from skill_seekers.mcp.server import fetch_config_tool
# Mock GitConfigRepo to raise ValueError
mock_repo_instance = MagicMock()
mock_repo_instance.clone_or_pull.side_effect = ValueError("Invalid git URL: not-a-url")
mock_git_repo_class.return_value = mock_repo_instance
args = {"config_name": "react", "git_url": "not-a-url"}
result = await fetch_config_tool(args)
assert len(result) == 1
assert "❌" in result[0].text
assert "Invalid git URL" in result[0].text
@pytest.mark.skipif(not MCP_AVAILABLE, reason="MCP not available")
@pytest.mark.asyncio
class TestSourceManagementTools:
"""Test add/list/remove config source tools."""
async def test_add_config_source(self, _temp_dirs):
"""Test adding a new config source."""
from skill_seekers.mcp.server import add_config_source_tool
with patch("skill_seekers.mcp.server.SourceManager") as mock_sm_class:
mock_sm = MagicMock()
mock_sm.add_source.return_value = {
"name": "team",
"git_url": "https://github.com/myorg/configs.git",
"type": "github",
"branch": "main",
"token_env": "GITHUB_TOKEN",
"priority": 100,
"enabled": True,
"added_at": "2025-12-21T10:00:00+00:00",
}
mock_sm_class.return_value = mock_sm
args = {"name": "team", "git_url": "https://github.com/myorg/configs.git"}
result = await add_config_source_tool(args)
assert len(result) == 1
assert "✅" in result[0].text
assert "team" in result[0].text
assert "registered" in result[0].text
# Verify add_source was called
mock_sm.add_source.assert_called_once()
async def test_add_config_source_missing_name(self):
"""Test error when name is missing."""
from skill_seekers.mcp.server import add_config_source_tool
args = {"git_url": "https://github.com/myorg/configs.git"}
result = await add_config_source_tool(args)
assert len(result) == 1
assert "❌" in result[0].text
assert "name" in result[0].text.lower()
assert "required" in result[0].text.lower()
async def test_add_config_source_missing_git_url(self):
"""Test error when git_url is missing."""
from skill_seekers.mcp.server import add_config_source_tool
args = {"name": "team"}
result = await add_config_source_tool(args)
assert len(result) == 1
assert "❌" in result[0].text
assert "git_url" in result[0].text.lower()
assert "required" in result[0].text.lower()
async def test_add_config_source_invalid_name(self):
"""Test error when source name is invalid."""
from skill_seekers.mcp.server import add_config_source_tool
with patch("skill_seekers.mcp.server.SourceManager") as mock_sm_class:
mock_sm = MagicMock()
mock_sm.add_source.side_effect = ValueError(
"Invalid source name 'team@company'. Must be alphanumeric with optional hyphens/underscores."
)
mock_sm_class.return_value = mock_sm
args = {"name": "team@company", "git_url": "https://github.com/myorg/configs.git"}
result = await add_config_source_tool(args)
assert len(result) == 1
assert "❌" in result[0].text
assert "Validation Error" in result[0].text
async def test_list_config_sources(self):
"""Test listing config sources."""
from skill_seekers.mcp.server import list_config_sources_tool
with patch("skill_seekers.mcp.server.SourceManager") as mock_sm_class:
mock_sm = MagicMock()
mock_sm.list_sources.return_value = [
{
"name": "team",
"git_url": "https://github.com/myorg/configs.git",
"type": "github",
"branch": "main",
"token_env": "GITHUB_TOKEN",
"priority": 1,
"enabled": True,
"added_at": "2025-12-21T10:00:00+00:00",
},
{
"name": "company",
"git_url": "https://gitlab.company.com/configs.git",
"type": "gitlab",
"branch": "develop",
"token_env": "GITLAB_TOKEN",
"priority": 2,
"enabled": True,
"added_at": "2025-12-21T11:00:00+00:00",
},
]
mock_sm_class.return_value = mock_sm
args = {}
result = await list_config_sources_tool(args)
assert len(result) == 1
assert "📋" in result[0].text
assert "team" in result[0].text
assert "company" in result[0].text
assert "2 total" in result[0].text
async def test_list_config_sources_empty(self):
"""Test listing when no sources registered."""
from skill_seekers.mcp.server import list_config_sources_tool
with patch("skill_seekers.mcp.server.SourceManager") as mock_sm_class:
mock_sm = MagicMock()
mock_sm.list_sources.return_value = []
mock_sm_class.return_value = mock_sm
args = {}
result = await list_config_sources_tool(args)
assert len(result) == 1
assert "No config sources registered" in result[0].text
async def test_list_config_sources_enabled_only(self):
"""Test listing only enabled sources."""
from skill_seekers.mcp.server import list_config_sources_tool
with patch("skill_seekers.mcp.server.SourceManager") as mock_sm_class:
mock_sm = MagicMock()
mock_sm.list_sources.return_value = [
{
"name": "team",
"git_url": "https://github.com/myorg/configs.git",
"type": "github",
"branch": "main",
"token_env": "GITHUB_TOKEN",
"priority": 1,
"enabled": True,
"added_at": "2025-12-21T10:00:00+00:00",
}
]
mock_sm_class.return_value = mock_sm
args = {"enabled_only": True}
result = await list_config_sources_tool(args)
assert len(result) == 1
assert "enabled only" in result[0].text
# Verify list_sources was called with correct parameter
mock_sm.list_sources.assert_called_once_with(enabled_only=True)
async def test_remove_config_source(self):
"""Test removing a config source."""
from skill_seekers.mcp.server import remove_config_source_tool
with patch("skill_seekers.mcp.server.SourceManager") as mock_sm_class:
mock_sm = MagicMock()
mock_sm.remove_source.return_value = True
mock_sm_class.return_value = mock_sm
args = {"name": "team"}
result = await remove_config_source_tool(args)
assert len(result) == 1
assert "✅" in result[0].text
assert "removed" in result[0].text.lower()
assert "team" in result[0].text
# Verify remove_source was called
mock_sm.remove_source.assert_called_once_with("team")
async def test_remove_config_source_not_found(self):
"""Test removing non-existent source."""
from skill_seekers.mcp.server import remove_config_source_tool
with patch("skill_seekers.mcp.server.SourceManager") as mock_sm_class:
mock_sm = MagicMock()
mock_sm.remove_source.return_value = False
mock_sm.list_sources.return_value = [
{"name": "team", "git_url": "https://example.com/1.git"},
{"name": "company", "git_url": "https://example.com/2.git"},
]
mock_sm_class.return_value = mock_sm
args = {"name": "nonexistent"}
result = await remove_config_source_tool(args)
assert len(result) == 1
assert "❌" in result[0].text
assert "not found" in result[0].text
assert "Available sources" in result[0].text
async def test_remove_config_source_missing_name(self):
"""Test error when name is missing."""
from skill_seekers.mcp.server import remove_config_source_tool
args = {}
result = await remove_config_source_tool(args)
assert len(result) == 1
assert "❌" in result[0].text
assert "name" in result[0].text.lower()
assert "required" in result[0].text.lower()
@pytest.mark.skipif(not MCP_AVAILABLE, reason="MCP not available")
@pytest.mark.asyncio
class TestCompleteWorkflow:
"""Test complete workflow of add → fetch → remove."""
@patch("skill_seekers.mcp.server.GitConfigRepo")
@patch("skill_seekers.mcp.server.SourceManager")
async def test_add_fetch_remove_workflow(self, mock_sm_class, mock_git_repo_class, temp_dirs):
"""Test complete workflow: add source → fetch config → remove source."""
from skill_seekers.mcp.server import (
add_config_source_tool,
fetch_config_tool,
list_config_sources_tool,
remove_config_source_tool,
)
# Step 1: Add source
mock_sm = MagicMock()
mock_sm.add_source.return_value = {
"name": "team",
"git_url": "https://github.com/myorg/configs.git",
"type": "github",
"branch": "main",
"token_env": "GITHUB_TOKEN",
"priority": 100,
"enabled": True,
"added_at": "2025-12-21T10:00:00+00:00",
}
mock_sm_class.return_value = mock_sm
add_result = await add_config_source_tool(
{"name": "team", "git_url": "https://github.com/myorg/configs.git"}
)
assert "✅" in add_result[0].text
# Step 2: Fetch config from source
mock_sm.get_source.return_value = {
"name": "team",
"git_url": "https://github.com/myorg/configs.git",
"branch": "main",
"token_env": "GITHUB_TOKEN",
}
mock_repo = MagicMock()
mock_repo_path = temp_dirs["cache"] / "team"
mock_repo_path.mkdir()
react_config = {"name": "react", "base_url": "https://react.dev/"}
(mock_repo_path / "react.json").write_text(json.dumps(react_config))
mock_repo.clone_or_pull.return_value = mock_repo_path
mock_repo.get_config.return_value = react_config
mock_git_repo_class.return_value = mock_repo
fetch_result = await fetch_config_tool(
{"config_name": "react", "source": "team", "destination": str(temp_dirs["dest"])}
)
assert "✅" in fetch_result[0].text
# Verify config file created
assert (temp_dirs["dest"] / "react.json").exists()
# Step 3: List sources
mock_sm.list_sources.return_value = [
{
"name": "team",
"git_url": "https://github.com/myorg/configs.git",
"type": "github",
"branch": "main",
"token_env": "GITHUB_TOKEN",
"priority": 100,
"enabled": True,
"added_at": "2025-12-21T10:00:00+00:00",
}
]
list_result = await list_config_sources_tool({})
assert "team" in list_result[0].text
# Step 4: Remove source
mock_sm.remove_source.return_value = True
remove_result = await remove_config_source_tool({"name": "team"})
assert "✅" in remove_result[0].text