forked from yusufkaraaslan/Skill_Seekers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_e2e_three_stream_pipeline.py
More file actions
598 lines (508 loc) · 20.6 KB
/
test_e2e_three_stream_pipeline.py
File metadata and controls
598 lines (508 loc) · 20.6 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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
"""
End-to-End Tests for Three-Stream GitHub Architecture Pipeline (Phase 5)
Tests the complete workflow:
1. Fetch GitHub repo with three streams (code, docs, insights)
2. Analyze with unified codebase analyzer (basic or c3x)
3. Merge sources with GitHub streams
4. Generate router with GitHub integration
5. Validate output structure and quality
"""
import json
from unittest.mock import Mock, patch
import pytest
from skill_seekers.cli.generate_router import RouterGenerator
from skill_seekers.cli.github_fetcher import (
CodeStream,
DocsStream,
InsightsStream,
ThreeStreamData,
)
from skill_seekers.cli.merge_sources import categorize_issues_by_topic
from skill_seekers.cli.unified_codebase_analyzer import UnifiedCodebaseAnalyzer
class TestE2EBasicWorkflow:
"""Test E2E workflow with basic analysis (fast)."""
@patch("skill_seekers.cli.unified_codebase_analyzer.GitHubThreeStreamFetcher")
def test_github_url_to_basic_analysis(self, mock_fetcher_class, tmp_path):
"""
Test complete pipeline: GitHub URL → Basic analysis → Merged output
This tests the fast path (1-2 minutes) without C3.x analysis.
"""
# Step 1: Mock GitHub three-stream fetcher
mock_fetcher = Mock()
mock_fetcher_class.return_value = mock_fetcher
# Create test code files
(tmp_path / "main.py").write_text("""
import os
import sys
def hello():
print("Hello, World!")
""")
(tmp_path / "utils.js").write_text("""
function greet(name) {
console.log(`Hello, ${name}!`);
}
""")
# Create mock three-stream data
code_stream = CodeStream(
directory=tmp_path, files=[tmp_path / "main.py", tmp_path / "utils.js"]
)
docs_stream = DocsStream(
readme="""# Test Project
A simple test project for demonstrating the three-stream architecture.
## Installation
```bash
pip install test-project
```
## Quick Start
```python
from test_project import hello
hello()
```
""",
contributing="# Contributing\n\nPull requests welcome!",
docs_files=[
{"path": "docs/guide.md", "content": "# User Guide\n\nHow to use this project."}
],
)
insights_stream = InsightsStream(
metadata={
"stars": 1234,
"forks": 56,
"language": "Python",
"description": "A test project",
},
common_problems=[
{
"title": "Installation fails on Windows",
"number": 42,
"state": "open",
"comments": 15,
"labels": ["bug", "windows"],
},
{
"title": "Import error with Python 3.6",
"number": 38,
"state": "open",
"comments": 10,
"labels": ["bug", "python"],
},
],
known_solutions=[
{
"title": "Fixed: Module not found",
"number": 35,
"state": "closed",
"comments": 8,
"labels": ["bug"],
}
],
top_labels=[
{"label": "bug", "count": 25},
{"label": "enhancement", "count": 15},
{"label": "documentation", "count": 10},
],
)
three_streams = ThreeStreamData(code_stream, docs_stream, insights_stream)
mock_fetcher.fetch.return_value = three_streams
# Step 2: Run unified analyzer with basic depth
analyzer = UnifiedCodebaseAnalyzer()
result = analyzer.analyze(
source="https://github.com/test/project", depth="basic", fetch_github_metadata=True
)
# Step 3: Validate all three streams present
assert result.source_type == "github"
assert result.analysis_depth == "basic"
# Validate code stream results
assert result.code_analysis is not None
assert result.code_analysis["analysis_type"] == "basic"
assert "files" in result.code_analysis
assert "structure" in result.code_analysis
assert "imports" in result.code_analysis
# Validate docs stream results
assert result.github_docs is not None
assert result.github_docs["readme"].startswith("# Test Project")
assert "pip install test-project" in result.github_docs["readme"]
# Validate insights stream results
assert result.github_insights is not None
assert result.github_insights["metadata"]["stars"] == 1234
assert result.github_insights["metadata"]["language"] == "Python"
assert len(result.github_insights["common_problems"]) == 2
assert len(result.github_insights["known_solutions"]) == 1
assert len(result.github_insights["top_labels"]) == 3
def test_issue_categorization_by_topic(self):
"""Test that issues are correctly categorized by topic keywords."""
problems = [
{
"title": "OAuth fails on redirect",
"number": 50,
"state": "open",
"comments": 20,
"labels": ["oauth", "bug"],
},
{
"title": "Token refresh issue",
"number": 45,
"state": "open",
"comments": 15,
"labels": ["oauth", "token"],
},
{
"title": "Async deadlock",
"number": 40,
"state": "open",
"comments": 12,
"labels": ["async", "bug"],
},
{
"title": "Database connection lost",
"number": 35,
"state": "open",
"comments": 10,
"labels": ["database"],
},
]
solutions = [
{
"title": "Fixed OAuth flow",
"number": 30,
"state": "closed",
"comments": 8,
"labels": ["oauth"],
},
{
"title": "Resolved async race",
"number": 25,
"state": "closed",
"comments": 6,
"labels": ["async"],
},
]
topics = ["oauth", "auth", "authentication"]
# Categorize issues
categorized = categorize_issues_by_topic(problems, solutions, topics)
# Validate categorization
assert "oauth" in categorized or "auth" in categorized or "authentication" in categorized
oauth_issues = (
categorized.get("oauth", [])
+ categorized.get("auth", [])
+ categorized.get("authentication", [])
)
# Should have 3 OAuth-related issues (2 problems + 1 solution)
assert len(oauth_issues) >= 2 # At least the problems
# OAuth issues should be in the categorized output
oauth_titles = [issue["title"] for issue in oauth_issues]
assert any("OAuth" in title for title in oauth_titles)
class TestE2ERouterGeneration:
"""Test E2E router generation with GitHub integration."""
def test_router_generation_with_github_streams(self, tmp_path):
"""
Test complete router generation workflow with GitHub streams.
Validates:
1. Router config created
2. Router SKILL.md includes GitHub metadata
3. Router SKILL.md includes README quick start
4. Router SKILL.md includes common issues
5. Routing keywords include GitHub labels (2x weight)
"""
# Create sub-skill configs
config1 = {
"name": "testproject-oauth",
"description": "OAuth authentication in Test Project",
"base_url": "https://github.com/test/project",
"categories": {"oauth": ["oauth", "auth"]},
}
config2 = {
"name": "testproject-async",
"description": "Async operations in Test Project",
"base_url": "https://github.com/test/project",
"categories": {"async": ["async", "await"]},
}
config_path1 = tmp_path / "config1.json"
config_path2 = tmp_path / "config2.json"
with open(config_path1, "w") as f:
json.dump(config1, f)
with open(config_path2, "w") as f:
json.dump(config2, f)
# Create GitHub streams
code_stream = CodeStream(directory=tmp_path, files=[])
docs_stream = DocsStream(
readme="""# Test Project
Fast and simple test framework.
## Installation
```bash
pip install test-project
```
## Quick Start
```python
import testproject
testproject.run()
```
""",
contributing="# Contributing\n\nWelcome!",
docs_files=[],
)
insights_stream = InsightsStream(
metadata={
"stars": 5000,
"forks": 250,
"language": "Python",
"description": "Fast test framework",
},
common_problems=[
{
"title": "OAuth setup fails",
"number": 150,
"state": "open",
"comments": 30,
"labels": ["bug", "oauth"],
},
{
"title": "Async deadlock",
"number": 142,
"state": "open",
"comments": 25,
"labels": ["async", "bug"],
},
{
"title": "Token refresh issue",
"number": 130,
"state": "open",
"comments": 20,
"labels": ["oauth"],
},
],
known_solutions=[
{
"title": "Fixed OAuth redirect",
"number": 120,
"state": "closed",
"comments": 15,
"labels": ["oauth"],
},
{
"title": "Resolved async race",
"number": 110,
"state": "closed",
"comments": 12,
"labels": ["async"],
},
],
top_labels=[
{"label": "oauth", "count": 45},
{"label": "async", "count": 38},
{"label": "bug", "count": 30},
],
)
github_streams = ThreeStreamData(code_stream, docs_stream, insights_stream)
# Generate router
generator = RouterGenerator(
[str(config_path1), str(config_path2)], github_streams=github_streams
)
# Step 1: Validate GitHub metadata extracted
assert generator.github_metadata is not None
assert generator.github_metadata["stars"] == 5000
assert generator.github_metadata["language"] == "Python"
# Step 2: Validate GitHub docs extracted
assert generator.github_docs is not None
assert "pip install test-project" in generator.github_docs["readme"]
# Step 3: Validate GitHub issues extracted
assert generator.github_issues is not None
assert len(generator.github_issues["common_problems"]) == 3
assert len(generator.github_issues["known_solutions"]) == 2
assert len(generator.github_issues["top_labels"]) == 3
# Step 4: Generate and validate router SKILL.md
skill_md = generator.generate_skill_md()
# Validate repository metadata section
assert "⭐ 5,000" in skill_md
assert "Python" in skill_md
assert "Fast test framework" in skill_md
# Validate README quick start section
assert "## Quick Start" in skill_md
assert "pip install test-project" in skill_md
# Validate examples section with converted questions (Fix 1)
assert "## Examples" in skill_md
# Issues converted to natural questions
assert (
"how do i fix oauth setup" in skill_md.lower()
or "how do i handle oauth setup" in skill_md.lower()
)
assert (
"how do i handle async deadlock" in skill_md.lower()
or "how do i fix async deadlock" in skill_md.lower()
)
# Common Issues section may still exist with other issues
# Note: Issue numbers may appear in Common Issues or Common Patterns sections
# Step 5: Validate routing keywords include GitHub labels (2x weight)
routing = generator.extract_routing_keywords()
oauth_keywords = routing["testproject-oauth"]
async_keywords = routing["testproject-async"]
# Labels should be included with 2x weight
assert oauth_keywords.count("oauth") >= 2 # Base + name + 2x from label
assert async_keywords.count("async") >= 2 # Base + name + 2x from label
# Step 6: Generate router config
router_config = generator.create_router_config()
assert router_config["name"] == "testproject"
assert router_config["_router"] is True
assert len(router_config["_sub_skills"]) == 2
assert "testproject-oauth" in router_config["_sub_skills"]
assert "testproject-async" in router_config["_sub_skills"]
class TestE2EQualityMetrics:
"""Test quality metrics as specified in Phase 5."""
def test_github_overhead_within_limits(self, tmp_path):
"""
Test that GitHub integration adds ~30-50 lines per skill (not more).
Quality metric: GitHub overhead should be minimal.
"""
# Create minimal config
config = {
"name": "test-skill",
"description": "Test skill",
"base_url": "https://github.com/test/repo",
"categories": {"api": ["api"]},
}
config_path = tmp_path / "config.json"
with open(config_path, "w") as f:
json.dump(config, f)
# Create GitHub streams with realistic data
code_stream = CodeStream(directory=tmp_path, files=[])
docs_stream = DocsStream(
readme="# Test\n\nA short README.", contributing=None, docs_files=[]
)
insights_stream = InsightsStream(
metadata={"stars": 100, "forks": 10, "language": "Python", "description": "Test"},
common_problems=[
{
"title": "Issue 1",
"number": 1,
"state": "open",
"comments": 5,
"labels": ["bug"],
},
{
"title": "Issue 2",
"number": 2,
"state": "open",
"comments": 3,
"labels": ["bug"],
},
],
known_solutions=[],
top_labels=[{"label": "bug", "count": 10}],
)
github_streams = ThreeStreamData(code_stream, docs_stream, insights_stream)
# Generate router without GitHub
generator_no_github = RouterGenerator([str(config_path)])
skill_md_no_github = generator_no_github.generate_skill_md()
lines_no_github = len(skill_md_no_github.split("\n"))
# Generate router with GitHub
generator_with_github = RouterGenerator([str(config_path)], github_streams=github_streams)
skill_md_with_github = generator_with_github.generate_skill_md()
lines_with_github = len(skill_md_with_github.split("\n"))
# Calculate GitHub overhead
github_overhead = lines_with_github - lines_no_github
# Validate overhead is within acceptable range (30-50 lines)
assert 20 <= github_overhead <= 60, (
f"GitHub overhead is {github_overhead} lines, expected 20-60"
)
def test_router_size_within_limits(self, tmp_path):
"""
Test that router SKILL.md is ~150 lines (±20).
Quality metric: Router should be concise overview, not exhaustive.
"""
# Create multiple sub-skill configs
configs = []
for i in range(4):
config = {
"name": f"test-skill-{i}",
"description": f"Test skill {i}",
"base_url": "https://github.com/test/repo",
"categories": {f"topic{i}": [f"topic{i}"]},
}
config_path = tmp_path / f"config{i}.json"
with open(config_path, "w") as f:
json.dump(config, f)
configs.append(str(config_path))
# Generate router
generator = RouterGenerator(configs)
skill_md = generator.generate_skill_md()
lines = len(skill_md.split("\n"))
# Validate router size is reasonable (60-250 lines for 4 sub-skills)
# Actual size depends on whether GitHub streams included - can be as small as 60 lines
assert 60 <= lines <= 250, f"Router is {lines} lines, expected 60-250 for 4 sub-skills"
class TestE2EBackwardCompatibility:
"""Test that old code still works without GitHub streams."""
def test_router_without_github_streams(self, tmp_path):
"""Test that router generation works without GitHub streams (backward compat)."""
config = {
"name": "test-skill",
"description": "Test skill",
"base_url": "https://example.com",
"categories": {"api": ["api"]},
}
config_path = tmp_path / "config.json"
with open(config_path, "w") as f:
json.dump(config, f)
# Generate router WITHOUT GitHub streams
generator = RouterGenerator([str(config_path)])
assert generator.github_metadata is None
assert generator.github_docs is None
assert generator.github_issues is None
# Should still generate valid SKILL.md
skill_md = generator.generate_skill_md()
assert "When to Use This Skill" in skill_md
assert "How It Works" in skill_md
# Should NOT have GitHub-specific sections
assert "⭐" not in skill_md
assert "Repository Info" not in skill_md
assert "Quick Start (from README)" not in skill_md
assert "Common Issues (from GitHub)" not in skill_md
@patch("skill_seekers.cli.unified_codebase_analyzer.GitHubThreeStreamFetcher")
def test_analyzer_without_github_metadata(self, mock_fetcher_class, tmp_path):
"""Test analyzer with fetch_github_metadata=False."""
mock_fetcher = Mock()
mock_fetcher_class.return_value = mock_fetcher
code_stream = CodeStream(directory=tmp_path, files=[])
docs_stream = DocsStream(readme=None, contributing=None, docs_files=[])
insights_stream = InsightsStream(
metadata={}, common_problems=[], known_solutions=[], top_labels=[]
)
three_streams = ThreeStreamData(code_stream, docs_stream, insights_stream)
mock_fetcher.fetch.return_value = three_streams
(tmp_path / "main.py").write_text("print('hello')")
analyzer = UnifiedCodebaseAnalyzer()
result = analyzer.analyze(
source="https://github.com/test/repo",
depth="basic",
fetch_github_metadata=False, # Explicitly disable
)
# Should not include GitHub docs/insights
assert result.github_docs is None
assert result.github_insights is None
class TestE2ETokenEfficiency:
"""Test token efficiency metrics."""
def test_three_stream_produces_compact_output(self, tmp_path):
"""
Test that three-stream architecture produces compact, efficient output.
This is a qualitative test - we verify that output is structured and
not duplicated across streams.
"""
# Create test files
(tmp_path / "main.py").write_text("import os\nprint('test')")
# Create GitHub streams
code_stream = CodeStream(directory=tmp_path, files=[tmp_path / "main.py"])
docs_stream = DocsStream(
readme="# Test\n\nQuick start guide.", contributing=None, docs_files=[]
)
insights_stream = InsightsStream(
metadata={"stars": 100}, common_problems=[], known_solutions=[], top_labels=[]
)
_three_streams = ThreeStreamData(code_stream, docs_stream, insights_stream)
# Verify streams are separate (no duplication)
assert code_stream.directory == tmp_path
assert docs_stream.readme is not None
assert insights_stream.metadata is not None
# Verify no cross-contamination
assert "Quick start guide" not in str(code_stream.files)
assert str(tmp_path) not in docs_stream.readme
if __name__ == "__main__":
pytest.main([__file__, "-v"])