@codai/cbd
Version:
Codai Better Database - High-Performance Vector Memory System with HPKV-inspired architecture and MCP server
363 lines (306 loc) ⢠13.7 kB
JavaScript
/**
* CBD AI Analytics Engine - Test Script
* Phase 4.3.2 Implementation Verification
*
* Tests all AI analytics functionalities including:
* - Predictive Analytics
* - Anomaly Detection
* - Pattern Recognition
* - NLP Analysis
* - Recommendations
* - Comprehensive Analysis
*/
const http = require('http');
const AI_ANALYTICS_URL = 'http://localhost:4700';
/**
* Test runner for AI Analytics Engine
*/
class AIAnalyticsTestRunner {
constructor() {
this.tests = [];
this.results = {
passed: 0,
failed: 0,
total: 0
};
}
async runAllTests() {
console.log('š§ CBD AI Analytics Engine - Comprehensive Test Suite');
console.log('='.repeat(60));
// Test service health
await this.testHealthCheck();
// Test predictive analytics
await this.testPredictiveAnalytics();
// Test anomaly detection
await this.testAnomalyDetection();
// Test pattern recognition
await this.testPatternRecognition();
// Test NLP analysis
await this.testNLPAnalysis();
// Test recommendations
await this.testRecommendations();
// Test comprehensive analysis
await this.testComprehensiveAnalysis();
// Test model management
await this.testModelManagement();
// Show final results
this.showResults();
}
async testHealthCheck() {
try {
console.log('\\nš Testing Health Check...');
const response = await this.makeRequest('/health', 'GET');
if (response.status === 'healthy' && response.service === 'CBD AI Analytics Engine') {
this.pass('Health check successful');
console.log(` ā
Service: ${response.service}`);
console.log(` ā
Version: ${response.version}`);
console.log(` ā
AI Capabilities: TensorFlow=${response.aiCapabilities?.tensorFlow}, NLP=${response.aiCapabilities?.naturalLanguage}`);
} else {
this.fail('Health check failed - invalid response');
}
} catch (error) {
this.fail(`Health check failed: ${error.message}`);
}
}
async testPredictiveAnalytics() {
try {
console.log('\\nš Testing Predictive Analytics...');
const testData = [10, 12, 15, 18, 22, 25, 30, 35, 40, 45];
const response = await this.makeRequest('/analyze/predict', 'POST', {
data: testData,
modelType: 'timeseries',
horizon: 5,
confidence: 0.95
});
if (response.success && response.predictions && response.predictions.forecast) {
this.pass('Predictive analytics successful');
console.log(` ā
Generated ${response.predictions.forecast.length} predictions`);
console.log(` ā
Detected trend: ${response.predictions.trend}`);
console.log(` ā
Processing time: ${response.metadata.processingTime}ms`);
} else {
this.fail('Predictive analytics failed - invalid response');
}
} catch (error) {
this.fail(`Predictive analytics failed: ${error.message}`);
}
}
async testAnomalyDetection() {
try {
console.log('\\nšØ Testing Anomaly Detection...');
// Data with obvious anomalies
const testData = [10, 12, 11, 13, 100, 14, 12, 11, 200, 13, 12];
const response = await this.makeRequest('/analyze/anomaly', 'POST', {
data: testData,
threshold: 2.0,
method: 'statistical'
});
if (response.success && response.anomalies) {
this.pass('Anomaly detection successful');
console.log(` ā
Detected ${response.anomalies.detected.length} anomalies`);
console.log(` ā
Method: ${response.anomalies.method}`);
console.log(` ā
Statistics available: ${!!response.anomalies.statistics}`);
} else {
this.fail('Anomaly detection failed - invalid response');
}
} catch (error) {
this.fail(`Anomaly detection failed: ${error.message}`);
}
}
async testPatternRecognition() {
try {
console.log('\\nš Testing Pattern Recognition...');
const testData = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5];
const response = await this.makeRequest('/analyze/pattern', 'POST', {
data: testData,
patternType: 'temporal',
minSupport: 0.1
});
if (response.success && response.patterns) {
this.pass('Pattern recognition successful');
console.log(` ā
Discovered ${response.patterns.discovered.length} patterns`);
console.log(` ā
Pattern type: ${response.patterns.type}`);
console.log(` ā
Confidence: ${response.patterns.confidence}`);
} else {
this.fail('Pattern recognition failed - invalid response');
}
} catch (error) {
this.fail(`Pattern recognition failed: ${error.message}`);
}
}
async testNLPAnalysis() {
try {
console.log('\\nš£ļø Testing NLP Analysis...');
const testText = "This is a great product! I love using it every day. The quality is excellent and the price is reasonable. However, the delivery was a bit slow.";
const response = await this.makeRequest('/analyze/nlp', 'POST', {
text: testText,
tasks: ['sentiment', 'entities', 'keywords']
});
if (response.success && response.analysis) {
this.pass('NLP analysis successful');
console.log(` ā
Sentiment: ${response.analysis.sentiment?.label} (${response.analysis.sentiment?.score})`);
console.log(` ā
Entities found: ${response.analysis.entities?.length || 0}`);
console.log(` ā
Keywords found: ${response.analysis.keywords?.length || 0}`);
console.log(` ā
Word count: ${response.analysis.statistics?.wordCount}`);
} else {
this.fail('NLP analysis failed - invalid response');
}
} catch (error) {
this.fail(`NLP analysis failed: ${error.message}`);
}
}
async testRecommendations() {
try {
console.log('\\nš” Testing Recommendations...');
const response = await this.makeRequest('/analyze/recommend', 'POST', {
userId: 'test_user_123',
context: { category: 'electronics', previousViews: ['laptop', 'mouse'] },
itemCount: 5,
algorithm: 'collaborative'
});
if (response.success && response.recommendations && response.recommendations.items) {
this.pass('Recommendations successful');
console.log(` ā
Generated ${response.recommendations.items.length} recommendations`);
console.log(` ā
Algorithm: ${response.recommendations.algorithm}`);
console.log(` ā
Confidence: ${response.recommendations.confidence}`);
} else {
this.fail('Recommendations failed - invalid response');
}
} catch (error) {
this.fail(`Recommendations failed: ${error.message}`);
}
}
async testComprehensiveAnalysis() {
try {
console.log('\\nš Testing Comprehensive Analysis...');
const testData = [10, 15, 12, 18, 25, 22, 30, 28, 35, 100, 32, 38];
const response = await this.makeRequest('/analyze/comprehensive', 'POST', {
data: testData,
options: {
prediction: { horizon: 3 },
anomaly: { threshold: 2.5 },
pattern: { minSupport: 0.2 }
}
});
if (response.success && response.results) {
this.pass('Comprehensive analysis successful');
console.log(` ā
Predictions: ${!!response.results.predictions}`);
console.log(` ā
Anomalies: ${!!response.results.anomalies}`);
console.log(` ā
Patterns: ${!!response.results.patterns}`);
console.log(` ā
Insights: ${!!response.results.insights}`);
console.log(` ā
Tasks completed: ${response.metadata.tasksCompleted}`);
} else {
this.fail('Comprehensive analysis failed - invalid response');
}
} catch (error) {
this.fail(`Comprehensive analysis failed: ${error.message}`);
}
}
async testModelManagement() {
try {
console.log('\\nš¤ Testing Model Management...');
// List models
const listResponse = await this.makeRequest('/models/list', 'GET');
if (listResponse.success && Array.isArray(listResponse.models)) {
this.pass('Model listing successful');
console.log(` ā
Found ${listResponse.models.length} models`);
// Train a new model
const trainResponse = await this.makeRequest('/models/train', 'POST', {
modelType: 'regression',
trainingData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
modelConfig: { epochs: 10, learningRate: 0.01 }
});
if (trainResponse.success && trainResponse.modelId) {
this.pass('Model training successful');
console.log(` ā
Trained model: ${trainResponse.modelId}`);
console.log(` ā
Accuracy: ${trainResponse.trainingResult.accuracy}`);
} else {
this.fail('Model training failed');
}
} else {
this.fail('Model listing failed - invalid response');
}
} catch (error) {
this.fail(`Model management failed: ${error.message}`);
}
}
async makeRequest(path, method = 'GET', data = null) {
return new Promise((resolve, reject) => {
const options = {
hostname: 'localhost',
port: 4700,
path: path,
method: method,
headers: {
'Content-Type': 'application/json'
}
};
const req = http.request(options, (res) => {
let responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
try {
const response = JSON.parse(responseData);
resolve(response);
} catch (error) {
reject(new Error(`Invalid JSON response: ${responseData}`));
}
});
});
req.on('error', (error) => {
reject(error);
});
req.setTimeout(30000, () => {
req.destroy();
reject(new Error('Request timeout'));
});
if (data) {
req.write(JSON.stringify(data));
}
req.end();
});
}
pass(message) {
console.log(` ā
${message}`);
this.results.passed++;
this.results.total++;
}
fail(message) {
console.log(` ā ${message}`);
this.results.failed++;
this.results.total++;
}
showResults() {
console.log('\\n' + '='.repeat(60));
console.log('š§ CBD AI Analytics Engine - Test Results');
console.log('='.repeat(60));
console.log(`ā
Passed: ${this.results.passed}`);
console.log(`ā Failed: ${this.results.failed}`);
console.log(`š Total: ${this.results.total}`);
console.log(`šÆ Success Rate: ${((this.results.passed / this.results.total) * 100).toFixed(1)}%`);
if (this.results.failed === 0) {
console.log('\\nš All tests passed! AI Analytics Engine is fully operational.');
} else {
console.log(`\\nā ļø ${this.results.failed} test(s) failed. Please check the AI Analytics Engine.`);
}
console.log('\\nš Service URL: http://localhost:4700');
console.log('š Health Check: http://localhost:4700/health');
console.log('š Statistics: http://localhost:4700/stats');
}
}
// Run tests if executed directly
if (require.main === module) {
const testRunner = new AIAnalyticsTestRunner();
// Wait for service to be ready
setTimeout(async () => {
try {
await testRunner.runAllTests();
} catch (error) {
console.error('ā Test suite failed:', error.message);
process.exit(1);
}
}, 2000);
}
module.exports = AIAnalyticsTestRunner;