-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_coder.py
More file actions
310 lines (252 loc) · 8.48 KB
/
test_coder.py
File metadata and controls
310 lines (252 loc) · 8.48 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
#!/usr/bin/env python3
"""
Test Script for Coder Agent
Tests code generation with mock LLM responses to verify extraction logic.
"""
import sys
from pathlib import Path
# Add project root to path
sys.path.insert(0, str(Path(__file__).parent))
from agents.coder import CoderAgent
from tools.file_tools import FileTools
import re
def test_code_extraction():
"""Test the markdown code extraction functionality."""
print("=" * 70)
print("🧪 Testing Coder Agent Code Extraction")
print("=" * 70)
coder = CoderAgent()
# Test cases with different markdown formats
test_cases = [
{
"name": "Python code block with language specified",
"input": '''```python
def hello_world():
"""Print hello world."""
print("Hello, World!")
```''',
"language": "python",
"should_contain": "def hello_world"
},
{
"name": "Generic code block",
"input": '''```
def greet(name):
return f"Hello, {name}"
```''',
"language": "python",
"should_contain": "def greet"
},
{
"name": "Code with explanation before",
"input": '''Here's a simple function:
```python
def add(a, b):
"""Add two numbers."""
return a + b
```''',
"language": "python",
"should_contain": "def add"
},
{
"name": "HTML code block",
"input": '''```html
<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>Hello</body>
</html>
```''',
"language": "html",
"should_contain": "<!DOCTYPE html>"
},
{
"name": "JavaScript code block",
"input": '''```javascript
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
```''',
"language": "javascript",
"should_contain": "function fibonacci"
}
]
print("\n📝 Running extraction tests...")
passed = 0
failed = 0
for idx, test_case in enumerate(test_cases, 1):
print(f"\n Test {idx}: {test_case['name']}")
try:
extracted = coder._extract_code_from_markdown(
test_case["input"],
test_case["language"]
)
if test_case["should_contain"] in extracted:
print(f" ✓ PASSED")
passed += 1
else:
print(f" ✗ FAILED: Expected '{test_case['should_contain']}' not found")
print(f" Got: {extracted[:50]}...")
failed += 1
except Exception as e:
print(f" ✗ FAILED: {e}")
failed += 1
print(f"\n Results: {passed} passed, {failed} failed")
return passed, failed
def test_code_generation_simulation():
"""Simulate code generation by manually testing the save functionality."""
print("\n" + "=" * 70)
print("🧪 Testing Code Generation & File Saving")
print("=" * 70)
coder = CoderAgent()
# Simulated generated code samples
test_files = [
{
"filename": "utils/arxiv_client.py",
"code": '''"""
arXiv API Client Module
Handles communication with arXiv API.
"""
import requests
import logging
from typing import List, Dict, Any
from datetime import datetime, timedelta
logger = logging.getLogger(__name__)
class ArxivClient:
"""Client for fetching papers from arXiv."""
BASE_URL = "http://export.arxiv.org/api/query"
def __init__(self, max_retries: int = 3):
"""Initialize the ArXiv client."""
self.max_retries = max_retries
self.session = requests.Session()
def search_by_category(
self,
category: str,
days: int = 1,
max_results: int = 10
) -> List[Dict[str, Any]]:
"""
Search for papers in a specific category.
Args:
category: arXiv category (e.g., 'cs.AI')
days: Number of days back to search
max_results: Maximum results to return
Returns:
List of paper data dictionaries
"""
try:
# Build query
date_from = (datetime.now() - timedelta(days=days)).strftime("%Y%m%d")
query = f'cat:{category} AND submittedDate:[{date_from}000000 TO 9999999999]'
# Make request
params = {
'search_query': query,
'start': 0,
'max_results': max_results,
'sortBy': 'submittedDate',
'sortOrder': 'descending'
}
response = self.session.get(self.BASE_URL, params=params, timeout=10)
response.raise_for_status()
# Parse response (simplified)
papers = []
# TODO: Parse XML response and extract paper data
return papers
except Exception as e:
logger.error(f"Error searching arXiv: {e}")
raise
if __name__ == "__main__":
client = ArxivClient()
papers = client.search_by_category("cs.AI", days=1)
print(f"Found {len(papers)} papers")
'''
},
{
"filename": "models.py",
"code": '''"""
Database Models
Defines SQLAlchemy models for the application.
"""
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
db = SQLAlchemy()
class Paper(db.Model):
"""Model for arXiv papers."""
__tablename__ = 'papers'
id = db.Column(db.String(50), primary_key=True)
title = db.Column(db.String(500), nullable=False)
authors = db.Column(db.String(1000), nullable=False)
summary = db.Column(db.Text, nullable=False)
published_date = db.Column(db.DateTime, nullable=False)
pdf_url = db.Column(db.String(500), nullable=False)
category = db.Column(db.String(50), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return f"<Paper {self.id}: {self.title}>"
def to_dict(self):
"""Convert to dictionary."""
return {
'id': self.id,
'title': self.title,
'authors': self.authors,
'summary': self.summary,
'published_date': self.published_date.isoformat(),
'pdf_url': self.pdf_url,
'category': self.category,
}
class Category(db.Model):
"""Model for arXiv categories."""
__tablename__ = 'categories'
id = db.Column(db.String(20), primary_key=True)
name = db.Column(db.String(100), nullable=False)
description = db.Column(db.Text)
def __repr__(self):
return f"<Category {self.id}: {self.name}>"
'''
}
]
print("\n📝 Simulating code generation and file saving...")
for test_file in test_files:
try:
print(f"\n Saving: {test_file['filename']}")
FileTools.save_code(test_file['filename'], test_file['code'])
# Verify file was saved
if FileTools.file_exists(test_file['filename']):
# Read back and verify
saved_code = FileTools.read_code(test_file['filename'])
if saved_code == test_file['code']:
print(f" ✓ File saved and verified ({len(saved_code)} bytes)")
else:
print(f" ✗ Saved code doesn't match original")
else:
print(f" ✗ File not found after saving")
except Exception as e:
print(f" ✗ Error: {e}")
# List all generated files
print("\n 📂 Generated files in workspace:")
files = FileTools.list_files()
for file in sorted(files):
if file != "__init__.py": # Skip init
print(f" ✓ {file}")
print(f"\n Total files: {len(files)}")
def main():
try:
# Test 1: Code extraction
passed, failed = test_code_extraction()
# Test 2: Code generation simulation
test_code_generation_simulation()
# Summary
print("\n" + "=" * 70)
if failed == 0:
print("✅ All Coder Agent Tests PASSED!")
else:
print(f"⚠️ {failed} test(s) failed")
print("=" * 70)
except Exception as e:
print(f"\n❌ Test failed: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()