forked from yusufkaraaslan/Skill_Seekers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_config_fetcher.py
More file actions
343 lines (273 loc) · 12 KB
/
test_config_fetcher.py
File metadata and controls
343 lines (273 loc) · 12 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
"""Tests for config_fetcher module - automatic API config downloading."""
import json
from pathlib import Path
from unittest.mock import Mock, patch
import httpx
import pytest
from skill_seekers.cli.config_fetcher import (
fetch_config_from_api,
list_available_configs,
resolve_config_path,
)
class TestFetchConfigFromApi:
"""Tests for fetch_config_from_api function."""
@patch("skill_seekers.cli.config_fetcher.httpx.Client")
def test_successful_fetch(self, mock_client_class, tmp_path):
"""Test successful config download from API."""
# Mock API responses
mock_client = Mock()
mock_client_class.return_value.__enter__.return_value = mock_client
# Mock detail response
detail_response = Mock()
detail_response.status_code = 200
detail_response.json.return_value = {
"name": "react",
"download_url": "https://api.skillseekersweb.com/api/configs/react/download",
"category": "web-frameworks",
"type": "unified",
}
detail_response.raise_for_status = Mock()
# Mock download response
download_response = Mock()
download_response.json.return_value = {
"name": "react",
"description": "React documentation skill",
"base_url": "https://react.dev/",
}
download_response.raise_for_status = Mock()
# Setup mock to return different responses for different URLs
def get_side_effect(url, *args, **kwargs):
if "download" in url:
return download_response
return detail_response
mock_client.get.side_effect = get_side_effect
# Test fetch
destination = str(tmp_path)
result = fetch_config_from_api("react", destination=destination)
# Verify
assert result is not None
assert result.exists()
assert result.name == "react.json"
# Verify file contents
with open(result) as f:
config = json.load(f)
assert config["name"] == "react"
assert "description" in config
@patch("skill_seekers.cli.config_fetcher.httpx.Client")
def test_config_not_found(self, mock_client_class):
"""Test handling of 404 response."""
mock_client = Mock()
mock_client_class.return_value.__enter__.return_value = mock_client
# Mock 404 response
detail_response = Mock()
detail_response.status_code = 404
mock_client.get.return_value = detail_response
result = fetch_config_from_api("nonexistent")
assert result is None
@patch("skill_seekers.cli.config_fetcher.httpx.Client")
def test_no_download_url(self, mock_client_class):
"""Test handling of missing download_url."""
mock_client = Mock()
mock_client_class.return_value.__enter__.return_value = mock_client
# Mock response without download_url
detail_response = Mock()
detail_response.status_code = 200
detail_response.json.return_value = {"name": "test"}
detail_response.raise_for_status = Mock()
mock_client.get.return_value = detail_response
result = fetch_config_from_api("test")
assert result is None
@patch("skill_seekers.cli.config_fetcher.httpx.Client")
def test_http_error(self, mock_client_class):
"""Test handling of HTTP errors."""
mock_client = Mock()
mock_client_class.return_value.__enter__.return_value = mock_client
# Mock HTTP error
mock_client.get.side_effect = httpx.HTTPError("Connection failed")
result = fetch_config_from_api("react")
assert result is None
@patch("skill_seekers.cli.config_fetcher.httpx.Client")
def test_json_decode_error(self, mock_client_class):
"""Test handling of invalid JSON response."""
mock_client = Mock()
mock_client_class.return_value.__enter__.return_value = mock_client
# Mock response with invalid JSON
detail_response = Mock()
detail_response.status_code = 200
detail_response.json.side_effect = json.JSONDecodeError("Invalid", "", 0)
detail_response.raise_for_status = Mock()
mock_client.get.return_value = detail_response
result = fetch_config_from_api("react")
assert result is None
def test_normalize_config_name(self, tmp_path):
"""Test config name normalization (remove .json, remove configs/ prefix)."""
with patch("skill_seekers.cli.config_fetcher.httpx.Client") as mock_client_class:
mock_client = Mock()
mock_client_class.return_value.__enter__.return_value = mock_client
detail_response = Mock()
detail_response.status_code = 200
detail_response.json.return_value = {
"download_url": "https://api.example.com/download"
}
detail_response.raise_for_status = Mock()
download_response = Mock()
download_response.json.return_value = {"name": "test"}
download_response.raise_for_status = Mock()
def get_side_effect(url, *args, **kwargs):
if "download" in url:
return download_response
return detail_response
mock_client.get.side_effect = get_side_effect
destination = str(tmp_path)
# Test with .json extension
result1 = fetch_config_from_api("test.json", destination=destination)
assert result1 is not None
assert result1.name == "test.json"
# Test with configs/ prefix
result2 = fetch_config_from_api("configs/test", destination=destination)
assert result2 is not None
class TestListAvailableConfigs:
"""Tests for list_available_configs function."""
@patch("skill_seekers.cli.config_fetcher.httpx.Client")
def test_successful_list(self, mock_client_class):
"""Test successful config listing."""
mock_client = Mock()
mock_client_class.return_value.__enter__.return_value = mock_client
# Mock API response
response = Mock()
response.json.return_value = {
"configs": [
{"name": "react"},
{"name": "vue"},
{"name": "godot"},
],
"total": 3,
}
response.raise_for_status = Mock()
mock_client.get.return_value = response
result = list_available_configs()
assert len(result) == 3
assert "react" in result
assert "vue" in result
assert "godot" in result
@patch("skill_seekers.cli.config_fetcher.httpx.Client")
def test_category_filter(self, mock_client_class):
"""Test listing with category filter."""
mock_client = Mock()
mock_client_class.return_value.__enter__.return_value = mock_client
response = Mock()
response.json.return_value = {
"configs": [{"name": "react"}, {"name": "vue"}],
"total": 2,
}
response.raise_for_status = Mock()
mock_client.get.return_value = response
result = list_available_configs(category="web-frameworks")
assert len(result) == 2
# Verify category parameter was passed
mock_client.get.assert_called_once()
call_args = mock_client.get.call_args
assert "params" in call_args.kwargs
assert call_args.kwargs["params"]["category"] == "web-frameworks"
@patch("skill_seekers.cli.config_fetcher.httpx.Client")
def test_api_error(self, mock_client_class):
"""Test handling of API errors."""
mock_client = Mock()
mock_client_class.return_value.__enter__.return_value = mock_client
# Mock error
mock_client.get.side_effect = httpx.HTTPError("Connection failed")
result = list_available_configs()
assert result == []
class TestResolveConfigPath:
"""Tests for resolve_config_path function."""
def test_exact_path_exists(self, tmp_path):
"""Test resolution when exact path exists."""
# Create test config file
config_file = tmp_path / "test.json"
config_file.write_text('{"name": "test"}')
result = resolve_config_path(str(config_file), auto_fetch=False)
assert result is not None
assert result.exists()
assert result.name == "test.json"
def test_with_configs_prefix(self, tmp_path):
"""Test resolution with configs/ prefix."""
# Create configs directory and file
configs_dir = tmp_path / "configs"
configs_dir.mkdir()
config_file = configs_dir / "test.json"
config_file.write_text('{"name": "test"}')
# Change to tmp_path for relative path testing
import os
original_cwd = os.getcwd()
try:
os.chdir(tmp_path)
result = resolve_config_path("test.json", auto_fetch=False)
assert result is not None
assert result.exists()
assert result.name == "test.json"
finally:
os.chdir(original_cwd)
def test_auto_fetch_disabled(self):
"""Test that auto-fetch doesn't run when disabled."""
result = resolve_config_path("nonexistent.json", auto_fetch=False)
assert result is None
@patch("skill_seekers.cli.config_fetcher.fetch_config_from_api")
def test_auto_fetch_enabled(self, mock_fetch, tmp_path):
"""Test that auto-fetch runs when enabled."""
# Mock fetch to return a path
mock_config = tmp_path / "configs" / "react.json"
mock_config.parent.mkdir(exist_ok=True)
mock_config.write_text('{"name": "react"}')
mock_fetch.return_value = mock_config
result = resolve_config_path("react.json", auto_fetch=True)
# Verify fetch was called
mock_fetch.assert_called_once_with("react", destination="configs")
assert result is not None
assert result.exists()
@patch("skill_seekers.cli.config_fetcher.fetch_config_from_api")
def test_auto_fetch_failed(self, mock_fetch):
"""Test handling when auto-fetch fails."""
# Mock fetch to return None (failed)
mock_fetch.return_value = None
result = resolve_config_path("nonexistent.json", auto_fetch=True)
assert result is None
def test_config_name_normalization(self, tmp_path):
"""Test various config name formats."""
configs_dir = tmp_path / "configs"
configs_dir.mkdir()
config_file = configs_dir / "react.json"
config_file.write_text('{"name": "react"}')
import os
original_cwd = os.getcwd()
try:
os.chdir(tmp_path)
# All of these should resolve to the same file
test_cases = ["react.json", "configs/react.json"]
for config_name in test_cases:
result = resolve_config_path(config_name, auto_fetch=False)
assert result is not None, f"Failed for {config_name}"
assert result.exists()
assert result.name == "react.json"
finally:
os.chdir(original_cwd)
@pytest.mark.integration
class TestConfigFetcherIntegration:
"""Integration tests that hit real API (marked as integration)."""
def test_fetch_real_config(self, tmp_path):
"""Test fetching a real config from API."""
destination = str(tmp_path)
result = fetch_config_from_api("godot", destination=destination, timeout=10.0)
if result: # Only assert if fetch succeeded (API might be down)
assert result.exists()
assert result.name == "godot.json"
with open(result) as f:
config = json.load(f)
assert config["name"] == "godot"
assert "description" in config
def test_list_real_configs(self):
"""Test listing real configs from API."""
result = list_available_configs(timeout=10.0)
if result: # Only assert if API is available
assert len(result) > 0
assert isinstance(result, list)
assert all(isinstance(cfg, str) for cfg in result)