forked from yusufkaraaslan/Skill_Seekers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_c3_integration.py
More file actions
362 lines (313 loc) · 13.9 KB
/
test_c3_integration.py
File metadata and controls
362 lines (313 loc) · 13.9 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
#!/usr/bin/env python3
"""
Integration tests for C3.5 - Architectural Overview & Skill Integrator
Tests the integration of C3.x codebase analysis features into unified skills:
- Default ON behavior for enable_codebase_analysis
- --skip-codebase-analysis CLI flag
- ARCHITECTURE.md generation with 8 sections
- C3.x reference directory structure
- Graceful degradation on failures
"""
import json
import os
import shutil
import tempfile
from unittest.mock import patch
import pytest
from skill_seekers.cli.config_validator import ConfigValidator
# Import modules to test
from skill_seekers.cli.unified_scraper import UnifiedScraper
from skill_seekers.cli.unified_skill_builder import UnifiedSkillBuilder
class TestC3Integration:
"""Test C3.5 integration features."""
@pytest.fixture
def temp_dir(self):
"""Create temporary directory for tests."""
temp = tempfile.mkdtemp()
yield temp
shutil.rmtree(temp, ignore_errors=True)
@pytest.fixture
def mock_config(self, temp_dir):
"""Create mock unified config with GitHub source."""
return {
"name": "test-c3",
"description": "Test C3.5 integration",
"merge_mode": "rule-based",
"sources": [
{
"type": "github",
"repo": "test/repo",
"local_repo_path": temp_dir,
"enable_codebase_analysis": True,
"ai_mode": "none",
}
],
}
@pytest.fixture
def mock_c3_data(self):
"""Create mock C3.x analysis data."""
return {
"patterns": [
{
"file_path": "src/factory.py",
"patterns": [
{
"pattern_type": "Factory",
"class_name": "WidgetFactory",
"confidence": 0.95,
"indicators": ["create_method", "product_interface"],
}
],
}
],
"test_examples": {
"total_examples": 15,
"high_value_count": 9,
"examples": [
{
"description": "Create widget instance",
"category": "instantiation",
"confidence": 0.85,
"file_path": "tests/test_widget.py",
"code_snippet": 'widget = Widget(name="test")',
}
],
"examples_by_category": {"instantiation": 5, "method_call": 6, "workflow": 4},
},
"how_to_guides": {
"guides": [
{
"id": "create_widget",
"title": "How to create a widget",
"description": "Step-by-step guide",
"steps": [
{
"action": "Import Widget class",
"code_example": "from widgets import Widget",
"language": "python",
}
],
}
],
"total_count": 1,
},
"config_patterns": {
"config_files": [
{
"relative_path": "config.json",
"type": "json",
"purpose": "Application configuration",
"settings": [{"key": "debug", "value": "true", "value_type": "boolean"}],
}
],
"ai_enhancements": {
"overall_insights": {
"security_issues_found": 1,
"recommended_actions": ["Move secrets to .env"],
}
},
},
"architecture": {
"patterns": [
{
"pattern_name": "MVC",
"confidence": 0.89,
"framework": "Flask",
"evidence": [
"models/ directory",
"views/ directory",
"controllers/ directory",
],
}
],
"frameworks_detected": ["Flask", "SQLAlchemy"],
"languages": {"python": 42, "javascript": 8},
"directory_structure": {"src": 25, "tests": 15, "docs": 3},
},
}
def test_codebase_analysis_enabled_by_default(self, mock_config, temp_dir): # noqa: ARG002
"""Test that enable_codebase_analysis defaults to True."""
# Config with GitHub source but no explicit enable_codebase_analysis
config_without_flag = {
"name": "test",
"description": "Test",
"sources": [{"type": "github", "repo": "test/repo", "local_repo_path": temp_dir}],
}
# Save config
config_path = os.path.join(temp_dir, "config.json")
with open(config_path, "w") as f:
json.dump(config_without_flag, f)
# Create scraper
scraper = UnifiedScraper(config_path)
# Verify default is True
github_source = scraper.config["sources"][0]
assert github_source.get("enable_codebase_analysis", True)
def test_skip_codebase_analysis_flag(self, mock_config, temp_dir):
"""Test --skip-codebase-analysis CLI flag disables analysis."""
# Save config
config_path = os.path.join(temp_dir, "config.json")
with open(config_path, "w") as f:
json.dump(mock_config, f)
# Create scraper
scraper = UnifiedScraper(config_path)
# Simulate --skip-codebase-analysis flag behavior
for source in scraper.config.get("sources", []):
if source["type"] == "github":
source["enable_codebase_analysis"] = False
# Verify flag disabled it
github_source = scraper.config["sources"][0]
assert not github_source["enable_codebase_analysis"]
def test_architecture_md_generation(self, mock_config, mock_c3_data, temp_dir):
"""Test ARCHITECTURE.md is generated with all 8 sections."""
# Create skill builder with C3.x data (multi-source list format)
github_data = {"readme": "Test README", "c3_analysis": mock_c3_data}
scraped_data = {
"github": [{"repo": "test/repo", "repo_id": "test_repo", "idx": 0, "data": github_data}]
}
builder = UnifiedSkillBuilder(mock_config, scraped_data)
builder.skill_dir = temp_dir
# Generate C3.x references
c3_dir = os.path.join(temp_dir, "references", "codebase_analysis")
os.makedirs(c3_dir, exist_ok=True)
builder._generate_architecture_overview(c3_dir, mock_c3_data, github_data)
# Verify ARCHITECTURE.md exists
arch_file = os.path.join(c3_dir, "ARCHITECTURE.md")
assert os.path.exists(arch_file)
# Read and verify content
with open(arch_file) as f:
content = f.read()
# Verify all 8 sections exist
assert "## 1. Overview" in content
assert "## 2. Architectural Patterns" in content
assert "## 3. Technology Stack" in content
assert "## 4. Design Patterns" in content
assert "## 5. Configuration Overview" in content
assert "## 6. Common Workflows" in content
assert "## 7. Usage Examples" in content
assert "## 8. Entry Points & Directory Structure" in content
# Verify specific data is present
assert "MVC" in content
assert "Flask" in content
assert "Factory" in content
assert "15 usage example(s)" in content or "15 total" in content
assert "Security Alert" in content
def test_c3_reference_directory_structure(self, mock_config, mock_c3_data, temp_dir):
"""Test correct C3.x reference directory structure is created."""
# Create skill builder with C3.x data (multi-source list format)
github_data = {"readme": "Test README", "c3_analysis": mock_c3_data}
scraped_data = {
"github": [{"repo": "test/repo", "repo_id": "test_repo", "idx": 0, "data": github_data}]
}
builder = UnifiedSkillBuilder(mock_config, scraped_data)
builder.skill_dir = temp_dir
# Generate C3.x references
c3_dir = os.path.join(temp_dir, "references", "codebase_analysis")
os.makedirs(c3_dir, exist_ok=True)
builder._generate_architecture_overview(c3_dir, mock_c3_data, github_data)
builder._generate_pattern_references(c3_dir, mock_c3_data.get("patterns"))
builder._generate_example_references(c3_dir, mock_c3_data.get("test_examples"))
builder._generate_guide_references(c3_dir, mock_c3_data.get("how_to_guides"))
builder._generate_config_references(c3_dir, mock_c3_data.get("config_patterns"))
builder._copy_architecture_details(c3_dir, mock_c3_data.get("architecture"))
# Verify directory structure
assert os.path.exists(os.path.join(c3_dir, "ARCHITECTURE.md"))
assert os.path.exists(os.path.join(c3_dir, "patterns"))
assert os.path.exists(os.path.join(c3_dir, "examples"))
assert os.path.exists(os.path.join(c3_dir, "guides"))
assert os.path.exists(os.path.join(c3_dir, "configuration"))
assert os.path.exists(os.path.join(c3_dir, "architecture_details"))
# Verify index files
assert os.path.exists(os.path.join(c3_dir, "patterns", "index.md"))
assert os.path.exists(os.path.join(c3_dir, "examples", "index.md"))
assert os.path.exists(os.path.join(c3_dir, "guides", "index.md"))
assert os.path.exists(os.path.join(c3_dir, "configuration", "index.md"))
assert os.path.exists(os.path.join(c3_dir, "architecture_details", "index.md"))
# Verify JSON data files
assert os.path.exists(os.path.join(c3_dir, "patterns", "detected_patterns.json"))
assert os.path.exists(os.path.join(c3_dir, "examples", "test_examples.json"))
assert os.path.exists(os.path.join(c3_dir, "configuration", "config_patterns.json"))
def test_graceful_degradation_on_c3_failure(self, mock_config, temp_dir):
"""Test skill builds even if C3.x analysis fails."""
# Mock _run_c3_analysis to raise exception
with patch("skill_seekers.cli.unified_scraper.UnifiedScraper._run_c3_analysis") as mock_c3:
mock_c3.side_effect = Exception("C3.x analysis failed")
# Save config
config_path = os.path.join(temp_dir, "config.json")
with open(config_path, "w") as f:
json.dump(mock_config, f)
# Mock GitHubScraper (correct module path for import)
with patch("skill_seekers.cli.github_scraper.GitHubScraper") as mock_github:
mock_github.return_value.scrape.return_value = {
"readme": "Test README",
"issues": [],
"releases": [],
}
scraper = UnifiedScraper(config_path)
# This should not raise an exception
try:
scraper._scrape_github(mock_config["sources"][0])
# If we get here, graceful degradation worked
assert True
except Exception as e:
pytest.fail(f"Should handle C3.x failure gracefully but raised: {e}")
def test_config_validator_accepts_c3_properties(self, temp_dir):
"""Test config validator accepts new C3.5 properties."""
config = {
"name": "test",
"description": "Test",
"sources": [
{
"type": "github",
"repo": "test/repo",
"enable_codebase_analysis": True,
"ai_mode": "auto",
}
],
}
# Save config
config_path = os.path.join(temp_dir, "config.json")
with open(config_path, "w") as f:
json.dump(config, f)
# Validate
validator = ConfigValidator(config_path)
assert validator.validate()
def test_config_validator_rejects_invalid_ai_mode(self, temp_dir):
"""Test config validator rejects invalid ai_mode values."""
config = {
"name": "test",
"description": "Test",
"sources": [
{
"type": "github",
"repo": "test/repo",
"ai_mode": "invalid_mode", # Invalid!
}
],
}
# Save config
config_path = os.path.join(temp_dir, "config.json")
with open(config_path, "w") as f:
json.dump(config, f)
# Validate should raise
validator = ConfigValidator(config_path)
with pytest.raises(ValueError, match="Invalid ai_mode"):
validator.validate()
def test_skill_md_includes_c3_summary(self, mock_config, mock_c3_data, temp_dir):
"""Test SKILL.md includes C3.x architecture summary."""
scraped_data = {"github": {"data": {"readme": "Test README", "c3_analysis": mock_c3_data}}}
builder = UnifiedSkillBuilder(mock_config, scraped_data)
builder.skill_dir = temp_dir
builder._generate_skill_md()
# Read SKILL.md
skill_file = os.path.join(temp_dir, "SKILL.md")
with open(skill_file) as f:
content = f.read()
# Verify C3.x summary section exists
assert "## 🏗️ Architecture & Code Analysis" in content
assert "Primary Architecture" in content
assert "MVC" in content
assert "Design Patterns" in content
assert "Factory" in content
assert "references/codebase_analysis/ARCHITECTURE.md" in content
if __name__ == "__main__":
pytest.main([__file__, "-v"])