@simonecoelhosfo/optimizely-mcp-server
Version:
Optimizely MCP Server for AI assistants with integrated CLI tools
116 lines • 4.81 kB
JavaScript
/**
* Test script for Intelligent Query Engine Cache Integration
*
* This script verifies that the caching layer is properly integrated
* with the IntelligentQueryEngine.
*/
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 testCacheIntegration() {
console.info('Starting cache integration test');
try {
// Initialize in-memory database for testing
const db = new Database(':memory:');
// Create engine with cache enabled
const engine = new IntelligentQueryEngine({
cache: {
enabled: true,
warmupOnStart: false,
statsInterval: 10000, // 10 seconds for testing
defaultTTL: 60000, // 1 minute for testing
}
});
// Register Optimizely adapter
const adapter = new OptimizelyAdapter({ database: db });
engine.registerAdapter(adapter);
// Test queries
const testQueries = [
{
find: 'flags',
select: ['key', 'name', 'enabled'],
where: [{ field: 'enabled', operator: '=', value: true }],
limit: 10,
},
{
find: 'experiments',
select: ['key', 'name', 'status'],
groupBy: ['status'],
aggregations: [{ field: 'key', function: 'COUNT', alias: 'count' }],
},
{
find: 'audiences',
select: ['id', 'name', 'conditions'],
limit: 5,
}
];
console.info('=== Testing Cache Performance ===');
// Run each query twice to test cache
for (const query of testQueries) {
console.info(`\nTesting query: ${query.find}`);
// First execution (cache miss)
const start1 = Date.now();
const result1 = await engine.query(query);
const time1 = Date.now() - start1;
console.info(`First execution: ${time1}ms (cached: ${result1.metadata.cached})`);
console.info(`Results: ${result1.data.length} rows`);
// Second execution (cache hit)
const start2 = Date.now();
const result2 = await engine.query(query);
const time2 = Date.now() - start2;
console.info(`Second execution: ${time2}ms (cached: ${result2.metadata.cached})`);
console.info(`Cache speedup: ${Math.round((time1 - time2) / time1 * 100)}%`);
// Verify results are the same
if (JSON.stringify(result1.data) !== JSON.stringify(result2.data)) {
console.error('ERROR: Cached results differ from original!');
}
}
// Test cache statistics
console.info('\n=== Cache Statistics ===');
const stats = engine.getCacheMetrics();
console.info(JSON.stringify(stats, null, 2));
// Test cache invalidation
console.info('\n=== Testing Cache Invalidation ===');
const invalidated = await engine.invalidateCache('flags');
console.info(`Invalidated ${invalidated} cache entries for flags`);
// Run query again to verify invalidation
const start3 = Date.now();
const result3 = await engine.query(testQueries[0]);
const time3 = Date.now() - start3;
console.info(`After invalidation: ${time3}ms (cached: ${result3.metadata.cached})`);
// Test cache preloading
console.info('\n=== Testing Cache Preload ===');
await engine.preloadCache([
{
find: 'flags',
select: ['key', 'name', 'description'],
where: [{ field: 'archived', operator: '=', value: false }],
},
{
find: 'experiments',
select: ['key', 'name', 'status'],
where: [{ field: 'status', operator: '=', value: 'running' }],
}
]);
console.info('Cache preload complete');
// Final statistics
console.info('\n=== Final Statistics ===');
const finalStats = engine.getStatistics();
console.info(JSON.stringify(finalStats, null, 2));
// Shutdown
await engine.shutdown();
console.info('\nCache integration test complete!');
}
catch (error) {
console.error('Test failed: ' + String(error));
throw error;
}
}
// Run the test
testCacheIntegration().catch(error => {
console.error('Fatal error: ' + String(error));
process.exit(1);
});
//# sourceMappingURL=test-cache-integration.js.map