UNPKG

@simonecoelhosfo/optimizely-mcp-server

Version:

Optimizely MCP Server for AI assistants with integrated CLI tools

168 lines 6.32 kB
/** * Test script for Intelligent Query Engine * Tests environment grouping and other complex queries */ import { IntelligentQueryEngine } from './IntelligentQueryEngine.js'; import { OptimizelyAdapter } from './adapters/OptimizelyAdapter.js'; import { getLogger } from '../../logging/Logger.js'; import Database from 'better-sqlite3'; const logger = getLogger(); async function runTests() { console.info('Starting Intelligent Query Engine tests'); try { // Open database directly const db = new Database('./data/optimizely-cache.db', { readonly: true }); // Initialize engine with full config const engine = new IntelligentQueryEngine({ discovery: { autoDiscover: true, discoveryInterval: 3600000, cacheTTL: 60000 }, execution: { defaultStrategy: 'hybrid-sql-first', maxQueryTime: 30000, enableParallel: false, parallelThreshold: 1000 } }); // Register Optimizely adapter const optimizelyAdapter = new OptimizelyAdapter({ database: db }); engine.registerAdapter(optimizelyAdapter); // Test queries const queries = [ { name: 'Basic Flag List', query: { find: 'flags', select: ['key', 'name', 'description'], limit: 5 } }, { name: 'Flags with Environments', query: { find: 'flags', select: ['key', 'name', 'environment'], limit: 10 } }, { name: 'Group by Environment', query: { find: 'flags', select: ['environment'], groupBy: ['environment'], aggregations: [{ field: '*', function: 'COUNT', alias: 'count' }] } }, { name: 'Filter by Archived Status', query: { find: 'flags', select: ['key', 'name', 'archived'], where: [{ field: 'archived', operator: '=', value: false }], limit: 5 } }, { name: 'Complex Multi-Environment Query', query: { find: 'flags', select: ['key', 'name', 'environment', 'created_time'], where: [{ field: 'environment', operator: 'IN', value: ['production', 'development'] }], orderBy: [{ field: 'created_time', direction: 'DESC' }], limit: 10 } } ]; // Run each query for (const testCase of queries) { console.log(`\n${'='.repeat(60)}`); console.log(`Test: ${testCase.name}`); console.log('='.repeat(60)); try { // Analyze query plan // First check the adapter const stats = engine.getStatistics(); console.log(`\nAdapter registered: ${stats.adapters.includes('optimizely')}`); const plan = await engine.analyze(testCase.query); console.log('\nExecution Plan:'); console.log(`- Strategy: ${plan.strategy}`); console.log(`- Phases: ${plan.phases.length}`); console.log(`- Estimated Cost: ${plan.estimatedCost}`); console.log(`- Estimated Rows: ${plan.estimatedRows}`); // Execute query const startTime = Date.now(); const result = await engine.query(testCase.query); const duration = Date.now() - startTime; console.log('\nResults:'); console.log(`- Status: SUCCESS`); console.log(`- Rows returned: ${result.data.length}`); console.log(`- Execution time: ${duration}ms`); console.log(`- Cache hit: ${result.metadata.cacheHit ? 'Yes' : 'No'}`); // Show sample data if (result.data.length > 0) { console.log('\nSample data (first 3 rows):'); result.data.slice(0, 3).forEach((row, i) => { console.log(` ${i + 1}. ${JSON.stringify(row)}`); }); } } catch (error) { console.log(`\nError: ${error.message}`); if (error.stack) { console.log('\nStack trace:'); console.log(error.stack.split('\n').slice(1, 4).join('\n')); } } } // Show engine statistics console.log(`\n${'='.repeat(60)}`); console.log('Engine Statistics'); console.log('='.repeat(60)); const stats = engine.getStatistics(); console.log(`- Adapters: ${stats.adapters.join(', ')}`); console.log(`- Query patterns tracked: ${stats.queryPatterns}`); console.log(`- Field catalog stats:`, stats.fieldCatalog); // Shutdown await engine.shutdown(); // Close database db.close(); } catch (error) { console.error('Test failed:', error); console.error('\nFATAL ERROR:', error.message); process.exit(1); } } // Run tests console.log('Intelligent Query Engine Test Suite'); console.log('==================================\n'); runTests() .then(() => { console.log('\nAll tests completed'); process.exit(0); }) .catch((error) => { console.error('\nTest suite failed:', error); process.exit(1); }); //# sourceMappingURL=test-query-engine.js.map