-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_full_integration.py
More file actions
192 lines (153 loc) · 6.65 KB
/
Copy pathtest_full_integration.py
File metadata and controls
192 lines (153 loc) · 6.65 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
#!/usr/bin/env python3
"""
Test completo de integración con Groq API
"""
import asyncio
import sys
from pathlib import Path
import pytest
# Agregar src al path
sys.path.insert(0, str(Path(__file__).parent / "src"))
from snippets.agents.context_analyzer import ContextAnalyzer
from snippets.agents.context_builder import ContextBuilder
from snippets.agents.llm_client import LLMConfig
from snippets.agents.base_agent import Snippet
@pytest.mark.asyncio
async def test_full_integration():
"""Test completo de integración Context Analyzer + Builder"""
print("🎉 FULL INTEGRATION TEST WITH GROQ API")
print("=" * 50)
# Configuración económica para testing
config = LLMConfig(
model="llama-3.1-8b-instant",
max_tokens=400,
max_cost_per_session=0.30,
temperature=0.1
)
# Snippets de prueba realista
snippets = [
Snippet(content='name = "Alice"', index=0),
Snippet(content='age = 30', index=1),
Snippet(content='def greet(person, age_val):\n return f"Hello {person}, you are {age_val} years old!"', index=2),
Snippet(content='message = greet(name, age)', index=3),
Snippet(content='print(message)', index=4)
]
target_snippet = snippets[3] # message = greet(name, age)
print(f"🎯 Target snippet: {target_snippet.content}")
print(f"📝 Available context: {len(snippets)} snippets")
print()
# PASO 1: Context Analysis
print("🔍 STEP 1: Context Analysis")
print("-" * 30)
try:
analyzer = ContextAnalyzer()
analysis_result = await analyzer.analyze(target_snippet, snippets, 3)
if analysis_result.success:
print(f"✅ Analysis successful! (confidence: {analysis_result.confidence:.2f})")
dependencies = analysis_result.data
# Mostrar dependencias encontradas
for category, items in dependencies.items():
if items and category not in ['processing_time', 'confidence', 'error', 'overall_confidence']:
if isinstance(items, dict):
print(f" 📋 {category.capitalize()}: {list(items.keys())}")
else:
print(f" 📋 {category.capitalize()}: {items}")
method = analysis_result.metadata.get('fallback', False)
print(f" 🔧 Method: {'AST Fallback' if method else 'LLM Analysis'}")
if 'llm_usage' in analysis_result.metadata:
usage = analysis_result.metadata['llm_usage']
print(f" 💰 Cost: ${usage['estimated_cost']:.6f}")
else:
print(f"❌ Analysis failed: {analysis_result.error}")
dependencies = {}
except Exception as e:
print(f"❌ Analysis error: {e}")
dependencies = {}
print()
# PASO 2: Context Building
print("🛠️ STEP 2: Context Building")
print("-" * 30)
try:
builder = ContextBuilder(enable_llm=True)
build_result = await builder.analyze(
target_snippet, snippets, 3,
dependencies=dependencies
)
if build_result.success:
context = build_result.data
method = build_result.metadata.get('method', 'unknown')
print(f"✅ Context built! (method: {method})")
print(f" 📊 Stats: {context['lines_count']} lines, safe: {context['safety_validated']}")
context_code = context['context_code']
print(f" 📝 Generated context:")
print(" ```python")
for line in context_code.split('\n'):
print(f" {line}")
print(" ```")
return context_code
else:
print(f"❌ Context building failed: {build_result.error}")
return None
except Exception as e:
print(f"❌ Context building error: {e}")
return None
async def execute_code_helper(context_code, target_snippet):
"""Test de ejecución del código completo"""
print("\n🚀 STEP 3: Code Execution Test")
print("-" * 30)
if not context_code:
print("❌ No context code to execute")
return
try:
# Construir código completo
complete_code = context_code + "\n\n" + target_snippet.content
print("📝 Complete code:")
print("```python")
for i, line in enumerate(complete_code.split('\n'), 1):
print(f"{i:2d}. {line}")
print("```")
# Ejecutar
exec_globals = {}
exec(complete_code, exec_globals)
print("\n✅ Execution successful!")
# Mostrar variables resultantes
relevant_vars = {k: v for k, v in exec_globals.items()
if not k.startswith('__') and not callable(v)}
if relevant_vars:
print("📊 Variables after execution:")
for name, value in relevant_vars.items():
print(f" {name} = {repr(value)}")
return True
except Exception as e:
print(f"❌ Execution failed: {e}")
return False
async def main():
"""Función principal"""
print("Starting full integration test with real API...")
try:
# Test de integración completa
context_code = await test_full_integration()
if context_code:
# Test de ejecución
target_snippet = Snippet(content='message = greet(name, age)', index=3)
execution_success = await execute_code_helper(context_code, target_snippet)
# Resultado final
print("\n🏆 INTEGRATION TEST RESULTS")
print("=" * 50)
print(f"✅ Context Analysis: PASS")
print(f"✅ Context Building: PASS")
print(f"{'✅' if execution_success else '❌'} Code Execution: {'PASS' if execution_success else 'FAIL'}")
if execution_success:
print("\n🎊 Full integration working perfectly!")
print(" • API connection successful")
print(" • Context analysis functional")
print(" • Context building operational")
print(" • Generated code executable")
else:
print("\n⚠️ Integration partially working - execution issues detected")
else:
print("\n❌ Integration test failed at context building stage")
except Exception as e:
print(f"\n💥 Integration test failed with error: {e}")
if __name__ == "__main__":
asyncio.run(main())