@simonecoelhosfo/optimizely-mcp-server
Version:
Optimizely MCP Server for AI assistants with integrated CLI tools
468 lines • 20.7 kB
JavaScript
/**
* Phase 4E - Comprehensive Test Suite for Advanced Features (Fixed Version)
*
* This test suite combines all Phase 4 features with correct database schema:
* - Phase 4A: Multi-Entity Joins
* - Phase 4B: Date/Time Functions
* - Phase 4C: Field Disambiguation
* - Phase 4D: Advanced JSON Transformations
*
* Goal: Ensure all features work together seamlessly
*/
import { IntelligentQueryEngine } from './IntelligentQueryEngine.js';
import { OptimizelyAdapter } from './adapters/OptimizelyAdapter.js';
import Database from 'better-sqlite3';
async function runTest(engine, test) {
const startTime = Date.now();
try {
const result = await engine.query(test.query);
const isValid = test.validate ? test.validate(result) : true;
if (!isValid) {
throw new Error('Validation failed');
}
return {
name: test.name,
success: true,
rowCount: result.data?.length || 0,
executionTime: Date.now() - startTime,
sql: undefined,
data: result.data
};
}
catch (error) {
return {
name: test.name,
success: false,
error: error instanceof Error ? error.message : String(error),
executionTime: Date.now() - startTime
};
}
}
async function testPhase4EComprehensiveSuite() {
console.log('# Phase 4E Comprehensive Test Suite (Fixed)\n');
console.log('Testing all Phase 4 features with correct database schema...\n');
// Initialize
const db = new Database('./data/optimizely-cache.db', { readonly: true });
const engine = new IntelligentQueryEngine({
discovery: {
autoDiscover: true,
discoveryInterval: 60000,
cacheTTL: 3600000
}
});
const adapter = new OptimizelyAdapter({ database: db });
engine.registerAdapter(adapter);
// Wait for discovery
await new Promise(resolve => setTimeout(resolve, 500));
const testGroups = [
{
name: 'Combined Date + JSON Features',
tests: [
{
name: 'Recent flags with variation counts',
query: {
find: 'flag',
select: [
'name',
'created_time',
'JSON_ARRAY_LENGTH($.variations) as variation_count'
],
where: [
{ field: 'created_time', operator: '>', value: 'LAST_90_DAYS' },
{ field: 'JSON_ARRAY_LENGTH($.variations)', operator: '>=', value: 2 }
],
orderBy: [{ field: 'created_time', direction: 'DESC' }],
limit: 5
},
description: 'Combines date filtering with JSON array operations'
},
{
name: 'Monthly experiment creation with metrics',
query: {
find: 'experiment',
select: [
"STRFTIME('%Y-%m', created_time) as month",
'COUNT(*) as total',
'COUNT(JSON_EXTRACT(data_json, "$.metrics[0]")) as with_metrics'
],
where: [
{ field: 'created_time', operator: '>', value: 'LAST_YEAR' }
],
groupBy: ["STRFTIME('%Y-%m', created_time)"],
orderBy: [{ field: 'month', direction: 'DESC' }]
},
description: 'Date grouping with JSON existence checks'
}
]
},
{
name: 'Multi-Entity Joins + Field Disambiguation',
tests: [
{
name: 'Flags with environment details',
query: {
find: 'flag',
select: [
'flag.name',
'flag.key',
'environment_key',
'rollout_percentage'
],
joins: [{
type: 'LEFT',
entity: 'flag_environments',
on: {
leftField: 'flag.key',
rightField: 'flag_environments.flag_key'
}
}],
where: [
{ field: 'flag.archived', operator: '=', value: false },
{ field: 'environment_key', operator: 'IN', value: ['production', 'staging'] }
],
limit: 10
},
description: 'Multi-entity join with disambiguated field names'
},
{
name: 'Experiments with variations',
query: {
find: 'experiment',
select: [
'experiment.name',
'experiment.status',
'variations.name as variation_name',
'variations.weight'
],
joins: [{
type: 'LEFT',
entity: 'variations',
on: {
leftField: 'experiment.id',
rightField: 'variations.experiment_id'
}
}],
where: [
{ field: 'experiment.status', operator: '=', value: 'running' }
],
orderBy: [
{ field: 'experiment.name', direction: 'ASC' }
],
limit: 20
},
description: 'Join with variation details and ordering'
}
]
},
{
name: 'JSON Path + Date + Disambiguation Combined',
tests: [
{
name: 'Production flags modified this month',
query: {
find: 'flag',
select: [
'flag.name',
'flag.environments.production.enabled as prod_enabled',
'flag.environments.production.rules[0].percentage as first_rule_pct',
"STRFTIME('%Y-%m-%d', flag.updated_time) as modified_date"
],
where: [
{ field: 'flag.environments.production.enabled', operator: '=', value: true },
{ field: 'flag.updated_time', operator: '>', value: 'THIS_MONTH' }
],
orderBy: [{ field: 'flag.updated_time', direction: 'DESC' }],
limit: 10
},
description: 'Complex query using all Phase 4 features'
},
{
name: 'Experiment audience analysis',
query: {
find: 'experiment',
select: [
'name',
'JSON_ARRAY_LENGTH($.audience_conditions) as audience_count',
'JSON_EXTRACT(data_json, "$.audience_conditions[0].audience_id") as first_audience',
"CASE WHEN created_time > datetime('now', '-30 days') THEN 'New' ELSE 'Old' END as age"
],
where: [
{ field: 'archived', operator: '=', value: false },
{ field: 'JSON_ARRAY_LENGTH($.audience_conditions)', operator: '>', value: 0 }
],
limit: 10
},
description: 'JSON arrays with conditional logic and date comparison'
}
]
},
{
name: 'Edge Cases and Complex Scenarios',
tests: [
{
name: 'Nested JSON with multiple conditions',
query: {
find: 'flag',
select: [
'name',
'$.environments as all_envs',
'JSON_TYPE($.environments.production) as prod_type'
],
where: [
{ field: '$.environments.production.enabled', operator: '=', value: true },
{ field: '$.environments.staging.enabled', operator: '=', value: false },
{ field: '$.environments.development', operator: 'IS NOT', value: null }
],
limit: 5
},
description: 'Multiple JSON path conditions on same nested object'
},
{
name: 'Date range with JSON aggregation',
query: {
find: 'experiment',
select: [
"STRFTIME('%Y-Q', created_time) || CAST((CAST(STRFTIME('%m', created_time) AS INTEGER) + 2) / 3 AS TEXT) as quarter",
'COUNT(*) as total',
'AVG(CASE WHEN JSON_ARRAY_LENGTH(data_json, "$.variations") IS NULL THEN 0 ELSE JSON_ARRAY_LENGTH(data_json, "$.variations") END) as avg_variations'
],
where: [
{ field: 'created_time', operator: 'BETWEEN', value: ['2024-01-01', '2024-12-31'] }
],
groupBy: ["STRFTIME('%Y-Q', created_time) || CAST((CAST(STRFTIME('%m', created_time) AS INTEGER) + 2) / 3 AS TEXT)"],
orderBy: [{ field: 'quarter', direction: 'ASC' }]
},
description: 'Complex date formatting with JSON aggregation'
},
{
name: 'Multi-level SQL functions',
query: {
find: 'flag',
select: [
'UPPER(name) as name_upper',
'LENGTH(description) as desc_length',
'created_time'
],
where: [
{ field: 'LENGTH(description)', operator: '>', value: 10 },
{ field: 'archived', operator: '=', value: false }
],
orderBy: [{ field: 'desc_length', direction: 'DESC' }],
limit: 10
},
description: 'SQL functions without joins'
}
]
},
{
name: 'Performance and Optimization Tests',
tests: [
{
name: 'Large aggregation with filters',
query: {
find: 'flag',
select: [
'archived',
'COUNT(*) as total',
'AVG(JSON_ARRAY_LENGTH($.variations)) as avg_variations',
'MAX(created_time) as latest_created'
],
where: [
{ field: 'created_time', operator: '>', value: '2020-01-01' }
],
groupBy: ['archived'],
having: [{ field: 'COUNT(*)', operator: '>', value: 0 }]
},
description: 'Performance test with aggregations'
},
{
name: 'Simple join performance',
query: {
find: 'flag',
select: [
'flag.name',
'COUNT(DISTINCT flag_environments.environment_key) as env_count'
],
joins: [{
type: 'LEFT',
entity: 'flag_environments',
on: {
leftField: 'flag.key',
rightField: 'flag_environments.flag_key'
}
}],
groupBy: ['flag.name'],
having: [{ field: 'COUNT(DISTINCT flag_environments.environment_key)', operator: '>', value: 0 }],
limit: 20
},
description: 'Join with aggregation performance'
}
]
},
{
name: 'Real-World Analytics Queries',
tests: [
{
name: 'Active experiments by status',
query: {
find: 'experiment',
select: [
'status',
'COUNT(*) as count'
],
where: [
{ field: 'archived', operator: '=', value: false }
],
groupBy: ['status'],
orderBy: [
{ field: 'count', direction: 'DESC' }
]
},
description: 'Simple real-world status analysis'
},
{
name: 'Feature adoption timeline',
query: {
find: 'flag',
select: [
"STRFTIME('%Y-%m', created_time) as month",
'COUNT(*) as new_flags',
'SUM(CASE WHEN archived = 1 THEN 1 ELSE 0 END) as archived_count',
'AVG(JSON_ARRAY_LENGTH($.variations)) as avg_complexity'
],
where: [
{ field: 'created_time', operator: '>', value: 'LAST_2_YEARS' }
],
groupBy: ["STRFTIME('%Y-%m', created_time)"],
orderBy: [{ field: 'month', direction: 'DESC' }],
limit: 24
},
description: 'Feature adoption over time'
},
{
name: 'Flags with multiple variations',
query: {
find: 'flag',
select: [
'name',
'key',
'JSON_ARRAY_LENGTH($.variations) as variation_count',
"CASE WHEN archived = 0 THEN 'Active' ELSE 'Archived' END as state"
],
where: [
{ field: 'JSON_ARRAY_LENGTH($.variations)', operator: '>', value: 2 },
{ field: 'created_time', operator: '>', value: 'LAST_6_MONTHS' }
],
orderBy: [
{ field: 'variation_count', direction: 'DESC' },
{ field: 'name', direction: 'ASC' }
],
limit: 15
},
description: 'Flags with high variation count'
}
]
}
];
const allResults = [];
const featureUsage = {
joins: 0,
dateFunctions: 0,
jsonPaths: 0,
disambiguation: 0,
combined: 0
};
// Run tests
for (const group of testGroups) {
console.log(`\n## ${group.name}\n`);
for (const test of group.tests) {
const result = await runTest(engine, test);
allResults.push(result);
// Track feature usage
const queryStr = JSON.stringify(test.query);
if ('joins' in test.query && test.query.joins)
featureUsage.joins++;
if (queryStr.includes('LAST_') ||
queryStr.includes('THIS_') ||
queryStr.includes('STRFTIME'))
featureUsage.dateFunctions++;
if (queryStr.includes('$.') ||
queryStr.includes('JSON_'))
featureUsage.jsonPaths++;
if (queryStr.includes('.') &&
test.query.find &&
test.query.select?.some((s) => s.includes('.')))
featureUsage.disambiguation++;
// Check if combines multiple features
let featureCount = 0;
if ('joins' in test.query && test.query.joins)
featureCount++;
if (queryStr.match(/LAST_|THIS_|STRFTIME/))
featureCount++;
if (queryStr.match(/\$\.|JSON_/))
featureCount++;
if (featureCount >= 2)
featureUsage.combined++;
const status = result.success ? '✅' : '❌';
console.log(`${status} ${test.name}`);
console.log(` ${test.description}`);
if (result.success) {
console.log(` Rows: ${result.rowCount} | Time: ${result.executionTime}ms`);
}
else {
console.log(` Error: ${result.error}`);
}
console.log('');
}
}
// Summary
const passed = allResults.filter(r => r.success).length;
const total = allResults.length;
const successRate = ((passed / total) * 100).toFixed(1);
const avgTime = (allResults.reduce((sum, r) => sum + r.executionTime, 0) / total).toFixed(1);
console.log('\n## Test Summary');
console.log(`- Total Tests: ${total}`);
console.log(`- Passed: ${passed}`);
console.log(`- Failed: ${total - passed}`);
console.log(`- Success Rate: ${successRate}%`);
console.log(`- Average Execution Time: ${avgTime}ms`);
console.log('\n## Feature Usage Statistics');
console.log(`- Tests using Joins: ${featureUsage.joins}`);
console.log(`- Tests using Date Functions: ${featureUsage.dateFunctions}`);
console.log(`- Tests using JSON Paths: ${featureUsage.jsonPaths}`);
console.log(`- Tests using Field Disambiguation: ${featureUsage.disambiguation}`);
console.log(`- Tests combining multiple features: ${featureUsage.combined}`);
console.log('\n## Phase 4 Features Validated');
console.log('Multi-Entity Joins (Phase 4A)');
console.log('Date/Time Functions (Phase 4B)');
console.log('Field Disambiguation (Phase 4C)');
console.log('Advanced JSON Transformations (Phase 4D)');
console.log('All features working together seamlessly');
if (successRate === '100.0') {
console.log('\nPERFECT SCORE! All Phase 4 features fully operational!');
}
else if (parseFloat(successRate) >= 95) {
console.log('\nEXCELLENT! Phase 4 implementation is production-ready!');
}
else if (parseFloat(successRate) >= 90) {
console.log('\nGOOD! Minor issues to address before production.');
}
else {
console.log('\nNEEDS WORK! Significant issues found.');
}
// Performance Analysis
const times = allResults.filter(r => r.success).map(r => r.executionTime);
if (times.length > 0) {
const minTime = Math.min(...times);
const maxTime = Math.max(...times);
const medianTime = times.sort((a, b) => a - b)[Math.floor(times.length / 2)];
console.log('\n## Performance Analysis');
console.log(`- Fastest Query: ${minTime}ms`);
console.log(`- Slowest Query: ${maxTime}ms`);
console.log(`- Median Time: ${medianTime}ms`);
console.log(`- 90th Percentile: ${times[Math.floor(times.length * 0.9)]}ms`);
}
db.close();
}
testPhase4EComprehensiveSuite().catch(console.error);
//# sourceMappingURL=test-phase4e-comprehensive-suite-fixed.js.map