forked from satishskid/AIRdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_vs_embedded.py
More file actions
450 lines (383 loc) Β· 19.9 KB
/
test_api_vs_embedded.py
File metadata and controls
450 lines (383 loc) Β· 19.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
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
#!/usr/bin/env python3
"""
AIRDOCS API vs Embedded Services Comparison Test
Validates that API services provide real AI integration while embedded services route correctly
"""
import asyncio
import aiohttp
import json
import time
from datetime import datetime
from typing import Dict, List, Any
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class APIvsEmbeddedTester:
"""Test API services vs Embedded services to validate dual-architecture implementation."""
def __init__(self, base_url: str = "http://localhost:8000"):
self.base_url = base_url
self.session = None
# API services that should have real integrations
self.api_services = {
"perplexity_pro": {
"category": "research_reports",
"expected_indicators": ["real_api: true", "model_used", "llama-3.1-sonar"],
"quality_score": 94
},
"jasper_ai": {
"category": "marketing_campaigns",
"expected_indicators": ["real_api: true", "template_used", "marketing_campaign"],
"quality_score": 92
},
"semantic_scholar": {
"category": "academic_papers",
"expected_indicators": ["papers", "citations", "authors"],
"quality_score": 90
}
}
# Embedded services that should route to embedded interface
self.embedded_services = {
"genspark": {
"category": "presentations",
"embed_url": "https://www.genspark.ai/",
"quality_score": 95
},
"paperpal": {
"category": "academic_papers",
"embed_url": "https://paperpal.com/",
"quality_score": 96
},
"manus": {
"category": "presentations",
"embed_url": "https://www.manus.chat/",
"quality_score": 93
}
}
# Test prompts
self.test_prompts = {
"research_reports": "Analyze the latest trends in renewable energy adoption across European markets in 2024",
"marketing_campaigns": "Create a comprehensive B2B marketing campaign for an AI-powered project management tool",
"academic_papers": "Search for recent academic papers on machine learning applications in climate science",
"presentations": "Create a business presentation on the future of artificial intelligence in healthcare"
}
async def __aenter__(self):
self.session = aiohttp.ClientSession()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.session:
await self.session.close()
async def test_api_service(self, service_name: str, service_config: Dict[str, Any]) -> Dict[str, Any]:
"""Test an API service to verify real integration."""
logger.info(f"π Testing API service: {service_name}")
category = service_config["category"]
prompt = self.test_prompts[category]
expected_indicators = service_config["expected_indicators"]
start_time = time.time()
try:
payload = {
"prompt": prompt,
"content_category": category,
"service_preference": service_name,
"quality_level": 3
}
async with self.session.post(
f"{self.base_url}/api/generate-content",
json=payload,
timeout=aiohttp.ClientTimeout(total=60)
) as response:
response_time = time.time() - start_time
if response.status == 200:
data = await response.json()
content = data.get("content", "")
# Check for real API indicators
real_api_indicators_found = []
for indicator in expected_indicators:
if indicator.lower() in content.lower():
real_api_indicators_found.append(indicator)
# Check for mock/template indicators
mock_indicators = [
"enhanced mock content",
"template content",
"note: this is enhanced mock content",
"mock",
"template"
]
mock_indicators_found = []
for indicator in mock_indicators:
if indicator.lower() in content.lower():
mock_indicators_found.append(indicator)
is_real_api = len(real_api_indicators_found) > 0 and len(mock_indicators_found) == 0
return {
"service_name": service_name,
"category": category,
"success": True,
"is_real_api": is_real_api,
"real_api_indicators_found": real_api_indicators_found,
"mock_indicators_found": mock_indicators_found,
"content_length": len(content),
"word_count": len(content.split()),
"response_time": response_time,
"quality_score": data.get("quality_score", 0),
"content_preview": content[:500] + "..." if len(content) > 500 else content
}
else:
return {
"service_name": service_name,
"category": category,
"success": False,
"error": f"HTTP {response.status}",
"response_time": response_time
}
except Exception as e:
return {
"service_name": service_name,
"category": category,
"success": False,
"error": str(e),
"response_time": time.time() - start_time
}
async def test_embedded_service_routing(self, service_name: str, service_config: Dict[str, Any]) -> Dict[str, Any]:
"""Test embedded service routing to verify it routes to embedded interface."""
logger.info(f"π Testing embedded service routing: {service_name}")
category = service_config["category"]
try:
# Test service routing endpoint
payload = {
"content_category": category,
"prompt": self.test_prompts[category],
"quality_level": 3
}
async with self.session.post(
f"{self.base_url}/api/service/route",
json=payload,
timeout=aiohttp.ClientTimeout(total=30)
) as response:
if response.status == 200:
data = await response.json()
routing_decision = data.get("routing_decision", {})
return {
"service_name": service_name,
"category": category,
"success": True,
"routes_to_embedded": routing_decision.get("service_type") == "embedded",
"selected_service": routing_decision.get("service_name"),
"quality_score": routing_decision.get("quality_score", 0),
"oauth_required": routing_decision.get("oauth_required", False),
"embed_url": routing_decision.get("embed_url", ""),
"routing_decision": routing_decision
}
else:
return {
"service_name": service_name,
"category": category,
"success": False,
"error": f"HTTP {response.status}"
}
except Exception as e:
return {
"service_name": service_name,
"category": category,
"success": False,
"error": str(e)
}
async def test_service_configuration_endpoint(self) -> Dict[str, Any]:
"""Test the service configuration endpoint."""
logger.info("βοΈ Testing service configuration endpoint")
try:
async with self.session.get(
f"{self.base_url}/api/services/configuration",
timeout=aiohttp.ClientTimeout(total=30)
) as response:
if response.status == 200:
data = await response.json()
services = data.get("services", {})
api_services_count = len([s for s in services.values() if s.get("api_available")])
embedded_services_count = len([s for s in services.values() if not s.get("api_available")])
return {
"success": True,
"total_services": len(services),
"api_services_count": api_services_count,
"embedded_services_count": embedded_services_count,
"categories": data.get("categories", []),
"services_by_category": {
category: len([s for s in services.values() if s.get("category") == category])
for category in data.get("categories", [])
}
}
else:
return {
"success": False,
"error": f"HTTP {response.status}"
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
async def run_comprehensive_comparison(self) -> Dict[str, Any]:
"""Run comprehensive API vs Embedded comparison tests."""
logger.info("π§ͺ Starting API vs Embedded Services Comparison Test")
results = {
"timestamp": datetime.now().isoformat(),
"api_service_tests": [],
"embedded_service_tests": [],
"service_configuration_test": {},
"summary": {}
}
# Test service configuration endpoint
config_result = await self.test_service_configuration_endpoint()
results["service_configuration_test"] = config_result
# Test API services
logger.info("\nπ Testing API Services (Real Integration Expected)")
for service_name, service_config in self.api_services.items():
result = await self.test_api_service(service_name, service_config)
results["api_service_tests"].append(result)
status = "β
REAL API" if result.get("is_real_api") else "β MOCK/TEMPLATE"
logger.info(f" {service_name}: {status}")
# Test embedded services routing
logger.info("\nπ Testing Embedded Services (Routing Expected)")
for service_name, service_config in self.embedded_services.items():
result = await self.test_embedded_service_routing(service_name, service_config)
results["embedded_service_tests"].append(result)
status = "β
ROUTES TO EMBEDDED" if result.get("routes_to_embedded") else "β INCORRECT ROUTING"
logger.info(f" {service_name}: {status}")
# Generate summary
api_real_count = len([r for r in results["api_service_tests"] if r.get("is_real_api")])
embedded_routing_count = len([r for r in results["embedded_service_tests"] if r.get("routes_to_embedded")])
results["summary"] = {
"api_services_tested": len(self.api_services),
"api_services_real": api_real_count,
"api_real_percentage": (api_real_count / len(self.api_services) * 100) if self.api_services else 0,
"embedded_services_tested": len(self.embedded_services),
"embedded_services_routing_correctly": embedded_routing_count,
"embedded_routing_percentage": (embedded_routing_count / len(self.embedded_services) * 100) if self.embedded_services else 0,
"dual_architecture_working": api_real_count > 0 and embedded_routing_count > 0
}
return results
def generate_comparison_report(self, results: Dict[str, Any]) -> str:
"""Generate HTML comparison report."""
summary = results["summary"]
html_content = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AIRDOCS API vs Embedded Services Test Report</title>
<style>
body {{ font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif; margin: 0; padding: 2rem; background: #f8fafc; }}
.container {{ max-width: 1000px; margin: 0 auto; }}
.header {{ text-align: center; margin-bottom: 3rem; }}
.title {{ font-size: 2.5rem; font-weight: 700; color: #1e293b; margin-bottom: 1rem; }}
.subtitle {{ font-size: 1.2rem; color: #64748b; }}
.summary-grid {{ display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1.5rem; margin-bottom: 3rem; }}
.summary-card {{ background: white; padding: 1.5rem; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); text-align: center; }}
.summary-value {{ font-size: 2rem; font-weight: 700; color: #667eea; }}
.summary-label {{ font-size: 0.9rem; color: #6b7280; margin-top: 0.5rem; }}
.test-section {{ background: white; border-radius: 12px; padding: 2rem; margin-bottom: 2rem; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }}
.section-title {{ font-size: 1.5rem; font-weight: 600; color: #1e293b; margin-bottom: 1.5rem; }}
.test-result {{ border: 1px solid #e5e7eb; border-radius: 8px; padding: 1rem; margin-bottom: 1rem; }}
.test-result.success {{ border-left: 4px solid #10b981; }}
.test-result.failure {{ border-left: 4px solid #ef4444; }}
.test-name {{ font-weight: 600; color: #374151; }}
.test-details {{ font-size: 0.9rem; color: #6b7280; margin-top: 0.5rem; }}
.status-badge {{ padding: 0.25rem 0.75rem; border-radius: 12px; font-size: 0.8rem; font-weight: 500; }}
.status-success {{ background: #dcfce7; color: #166534; }}
.status-failure {{ background: #fee2e2; color: #991b1b; }}
.content-preview {{ background: #f8fafc; padding: 1rem; border-radius: 8px; font-size: 0.9rem; margin-top: 1rem; max-height: 200px; overflow-y: auto; }}
</style>
</head>
<body>
<div class="container">
<div class="header">
<div class="title">π API vs π Embedded Services Test</div>
<div class="subtitle">Validation of AIRDOCS Dual-Architecture Implementation</div>
<div class="subtitle">Generated: {results["timestamp"]}</div>
</div>
<div class="summary-grid">
<div class="summary-card">
<div class="summary-value">{summary["api_real_percentage"]:.0f}%</div>
<div class="summary-label">API Services with Real Integration</div>
</div>
<div class="summary-card">
<div class="summary-value">{summary["embedded_routing_percentage"]:.0f}%</div>
<div class="summary-label">Embedded Services Routing Correctly</div>
</div>
<div class="summary-card">
<div class="summary-value">{'β
' if summary["dual_architecture_working"] else 'β'}</div>
<div class="summary-label">Dual Architecture Status</div>
</div>
</div>
<div class="test-section">
<div class="section-title">π API Services Test Results</div>
"""
for test in results["api_service_tests"]:
status_class = "success" if test.get("is_real_api") else "failure"
status_text = "Real API Integration" if test.get("is_real_api") else "Mock/Template Content"
badge_class = "status-success" if test.get("is_real_api") else "status-failure"
html_content += f"""
<div class="test-result {status_class}">
<div class="test-name">{test["service_name"].replace('_', ' ').title()}</div>
<div class="test-details">
<span class="status-badge {badge_class}">{status_text}</span>
Category: {test["category"]} |
Words: {test.get("word_count", 0)} |
Time: {test.get("response_time", 0):.2f}s
</div>
{f'<div class="content-preview">{test.get("content_preview", "")}</div>' if test.get("content_preview") else ''}
</div>
"""
html_content += """
</div>
<div class="test-section">
<div class="section-title">π Embedded Services Test Results</div>
"""
for test in results["embedded_service_tests"]:
status_class = "success" if test.get("routes_to_embedded") else "failure"
status_text = "Routes to Embedded" if test.get("routes_to_embedded") else "Incorrect Routing"
badge_class = "status-success" if test.get("routes_to_embedded") else "status-failure"
html_content += f"""
<div class="test-result {status_class}">
<div class="test-name">{test["service_name"].replace('_', ' ').title()}</div>
<div class="test-details">
<span class="status-badge {badge_class}">{status_text}</span>
Category: {test["category"]} |
Selected: {test.get("selected_service", "N/A")} |
OAuth: {'Required' if test.get("oauth_required") else 'Not Required'}
</div>
</div>
"""
html_content += """
</div>
</div>
</body>
</html>
"""
return html_content
async def main():
"""Run the API vs Embedded comparison test."""
async with APIvsEmbeddedTester() as tester:
# Run comprehensive comparison
results = await tester.run_comprehensive_comparison()
# Save results
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
results_file = f"api_vs_embedded_test_{timestamp}.json"
report_file = f"api_vs_embedded_report_{timestamp}.html"
# Save JSON results
with open(results_file, 'w') as f:
json.dump(results, f, indent=2, default=str)
# Generate and save HTML report
html_report = tester.generate_comparison_report(results)
with open(report_file, 'w') as f:
f.write(html_report)
# Print summary
summary = results["summary"]
print(f"\nπ API vs π Embedded Services Test Results")
print(f"=" * 50)
print(f"API Services Real Integration: {summary['api_services_real']}/{summary['api_services_tested']} ({summary['api_real_percentage']:.1f}%)")
print(f"Embedded Services Routing: {summary['embedded_services_routing_correctly']}/{summary['embedded_services_tested']} ({summary['embedded_routing_percentage']:.1f}%)")
print(f"Dual Architecture Working: {'β
YES' if summary['dual_architecture_working'] else 'β NO'}")
print(f"\nπ Detailed report: {report_file}")
return results
if __name__ == "__main__":
asyncio.run(main())