"use strict";
/**
 * Tests for EnhancedPerformanceGenerator
 */
Object.defineProperty(exports, "__esModule", { value: true });
const enhanced_performance_generator_1 = require("./enhanced-performance-generator");
const workflow_engine_1 = require("../../engine/workflow-engine");
describe('EnhancedPerformanceGenerator', () => {
    let generator;
    beforeEach(() => {
        generator = new enhanced_performance_generator_1.EnhancedPerformanceGenerator();
    });
    it('should have correct metadata', () => {
        expect(generator.id).toBe('enhanced-performance');
        expect(generator.name).toBe('Enhanced Performance Generator');
        expect(generator.description).toContain('performance optimization');
    });
    it('should return empty result when no performance config', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
            },
            stack: {
                api: {
                    type: 'backend',
                    technology: 'typescript/express',
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        expect(result.files).toHaveLength(0);
    });
    describe('Frontend Performance', () => {
        it('should generate bundler configuration with code splitting', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    web: {
                        type: 'frontend',
                        technology: 'react',
                    },
                },
                performance: {
                    frontend: {
                        code_splitting: 'route_based',
                        lazy_loading: true,
                        tree_shaking: true,
                        bundle_budget: {
                            max_initial: '200kb',
                            max_total: '1mb',
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const bundlerConfig = result.files.find(f => f.path.includes('vite.performance.config.ts'));
            expect(bundlerConfig).toBeDefined();
            expect(bundlerConfig?.content).toContain('codeSplitting: \'route_based\'');
            expect(bundlerConfig?.content).toContain('lazyLoading: true');
            expect(bundlerConfig?.content).toContain('treeShaking: true');
            expect(bundlerConfig?.content).toContain('Route-based code splitting');
        });
        it('should generate code splitting utilities', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    web: {
                        type: 'frontend',
                        technology: 'react',
                    },
                },
                performance: {
                    frontend: {
                        code_splitting: 'component_based',
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const codeSplitting = result.files.find(f => f.path.includes('code-splitting.tsx'));
            expect(codeSplitting).toBeDefined();
            expect(codeSplitting?.content).toContain('lazyWithRetry');
            expect(codeSplitting?.content).toContain('lazyWithPreload');
            expect(codeSplitting?.content).toContain('LoadingFallback');
        });
        it('should generate lazy loading utilities', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    web: {
                        type: 'frontend',
                        technology: 'react',
                    },
                },
                performance: {
                    frontend: {
                        lazy_loading: true,
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const lazyLoading = result.files.find(f => f.path.includes('lazy-loading.tsx'));
            expect(lazyLoading).toBeDefined();
            expect(lazyLoading?.content).toContain('useLazyImage');
            expect(lazyLoading?.content).toContain('LazyImage');
            expect(lazyLoading?.content).toContain('IntersectionObserver');
            expect(lazyLoading?.content).toContain('preloadResource');
        });
        it('should generate bundle analysis script', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    web: {
                        type: 'frontend',
                        technology: 'react',
                    },
                },
                performance: {
                    frontend: {
                        bundle_budget: {
                            max_initial: '300kb',
                            max_total: '2mb',
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const bundleAnalysis = result.files.find(f => f.path.includes('analyze-bundle.ts'));
            expect(bundleAnalysis).toBeDefined();
            expect(bundleAnalysis?.content).toContain('analyzeBundleSize');
            expect(bundleAnalysis?.content).toContain('300kb');
            expect(bundleAnalysis?.content).toContain('2mb');
        });
    });
    describe('Backend Performance', () => {
        it('should generate Redis caching configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                performance: {
                    backend: {
                        caching: {
                            strategy: 'redis',
                            ttl: 3600,
                            cache_keys: ['user', 'posts'],
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const caching = result.files.find(f => f.path.includes('caching.ts'));
            expect(caching).toBeDefined();
            expect(caching?.content).toContain('redis');
            expect(caching?.content).toContain('createClient');
            expect(caching?.content).toContain('ttl: 3600');
            expect(caching?.content).toContain('getCache');
            expect(caching?.content).toContain('setCache');
        });
        it('should generate Memcached caching configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                performance: {
                    backend: {
                        caching: {
                            strategy: 'memcached',
                            ttl: 1800,
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const caching = result.files.find(f => f.path.includes('caching.ts'));
            expect(caching).toBeDefined();
            expect(caching?.content).toContain('Memcached');
            expect(caching?.content).toContain('ttl: 1800');
        });
        it('should generate in-memory caching configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                performance: {
                    backend: {
                        caching: {
                            strategy: 'in_memory',
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const caching = result.files.find(f => f.path.includes('caching.ts'));
            expect(caching).toBeDefined();
            expect(caching?.content).toContain('new Map');
            expect(caching?.content).toContain('in_memory');
        });
        it('should generate connection pooling configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                performance: {
                    backend: {
                        connection_pooling: {
                            min: 5,
                            max: 20,
                            idle_timeout: 10000,
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const pooling = result.files.find(f => f.path.includes('database-pool.ts'));
            expect(pooling).toBeDefined();
            expect(pooling?.content).toContain('min: 5');
            expect(pooling?.content).toContain('max: 20');
            expect(pooling?.content).toContain('idleTimeoutMillis: 10000');
            expect(pooling?.content).toContain('Pool');
        });
        it('should generate query optimization utilities', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                performance: {
                    backend: {
                        query_optimization: true,
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const queryOpt = result.files.find(f => f.path.includes('query-optimization.ts'));
            expect(queryOpt).toBeDefined();
            expect(queryOpt?.content).toContain('QueryMonitor');
            expect(queryOpt?.content).toContain('OptimizedQueryBuilder');
            expect(queryOpt?.content).toContain('executeBatch');
            expect(queryOpt?.content).toContain('paginateWithCursor');
        });
    });
    describe('CDN Configuration', () => {
        it('should generate Cloudflare CDN configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    web: {
                        type: 'frontend',
                        technology: 'react',
                    },
                },
                performance: {
                    cdn: {
                        provider: 'cloudflare',
                        assets: ['*.js', '*.css', '*.png'],
                        cache_ttl: 86400,
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const cdn = result.files.find(f => f.path.includes('cdn.config.ts'));
            expect(cdn).toBeDefined();
            expect(cdn?.content).toContain('cloudflare');
            expect(cdn?.content).toContain('setCloudflareCacheHeaders');
            expect(cdn?.content).toContain('purgeCloudflarCache');
            expect(cdn?.content).toContain('cacheTTL: 86400');
        });
        it('should generate CloudFront CDN configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    web: {
                        type: 'frontend',
                        technology: 'react',
                    },
                },
                performance: {
                    cdn: {
                        provider: 'cloudfront',
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const cdn = result.files.find(f => f.path.includes('cdn.config.ts'));
            expect(cdn).toBeDefined();
            expect(cdn?.content).toContain('cloudfront');
            expect(cdn?.content).toContain('setCloudFrontCacheHeaders');
            expect(cdn?.content).toContain('invalidateCloudFrontCache');
        });
        it('should generate Fastly CDN configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    web: {
                        type: 'frontend',
                        technology: 'react',
                    },
                },
                performance: {
                    cdn: {
                        provider: 'fastly',
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const cdn = result.files.find(f => f.path.includes('cdn.config.ts'));
            expect(cdn).toBeDefined();
            expect(cdn?.content).toContain('fastly');
            expect(cdn?.content).toContain('setFastlyCacheHeaders');
            expect(cdn?.content).toContain('purgeFastlyCache');
        });
    });
    describe('Database Optimization', () => {
        it('should generate read replica configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                performance: {
                    database: {
                        read_replicas: 3,
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const replicas = result.files.find(f => f.path.includes('read-replicas.ts'));
            expect(replicas).toBeDefined();
            expect(replicas?.content).toContain('primaryPool');
            expect(replicas?.content).toContain('replicaPools');
            expect(replicas?.content).toContain('readQuery');
            expect(replicas?.content).toContain('writeQuery');
            expect(replicas?.content).toContain('DB_REPLICA_1_HOST');
            expect(replicas?.content).toContain('DB_REPLICA_2_HOST');
            expect(replicas?.content).toContain('DB_REPLICA_3_HOST');
        });
        it('should generate sharding configuration for tenant-based strategy', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                performance: {
                    database: {
                        sharding: {
                            strategy: 'by_tenant',
                            shard_count: 4,
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const sharding = result.files.find(f => f.path.includes('sharding.ts'));
            expect(sharding).toBeDefined();
            expect(sharding?.content).toContain('by_tenant');
            expect(sharding?.content).toContain('shardCount: 4');
            expect(sharding?.content).toContain('getShardByTenant');
            expect(sharding?.content).toContain('queryByTenant');
        });
        it('should generate sharding configuration for date-based strategy', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                performance: {
                    database: {
                        sharding: {
                            strategy: 'by_date',
                            shard_count: 12,
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const sharding = result.files.find(f => f.path.includes('sharding.ts'));
            expect(sharding).toBeDefined();
            expect(sharding?.content).toContain('by_date');
            expect(sharding?.content).toContain('getShardByDate');
            expect(sharding?.content).toContain('queryByDateRange');
        });
        it('should generate sharding configuration for hash-based strategy', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                performance: {
                    database: {
                        sharding: {
                            strategy: 'by_hash',
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const sharding = result.files.find(f => f.path.includes('sharding.ts'));
            expect(sharding).toBeDefined();
            expect(sharding?.content).toContain('by_hash');
            expect(sharding?.content).toContain('getShardByHash');
            expect(sharding?.content).toContain('queryAllShards');
        });
        it('should generate auto indexing utilities', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                performance: {
                    database: {
                        indexing_strategy: 'auto',
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const indexing = result.files.find(f => f.path.includes('indexing.ts'));
            expect(indexing).toBeDefined();
            expect(indexing?.content).toContain('IndexSuggester');
            expect(indexing?.content).toContain('suggestIndexes');
            expect(indexing?.content).toContain('logQuery');
        });
        it('should generate manual indexing utilities', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                performance: {
                    database: {
                        indexing_strategy: 'manual',
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const indexing = result.files.find(f => f.path.includes('indexing.ts'));
            expect(indexing).toBeDefined();
            expect(indexing?.content).toContain('indexDefinitions');
            expect(indexing?.content).toContain('generateIndexSQL');
            expect(indexing?.content).toContain('analyzeIndexUsage');
        });
    });
    describe('Complete Performance Schema', () => {
        it('should generate all performance files for complete schema', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test Complete Performance',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                    web: {
                        type: 'frontend',
                        technology: 'react',
                    },
                },
                performance: {
                    frontend: {
                        code_splitting: 'route_based',
                        lazy_loading: true,
                        bundle_budget: {
                            max_initial: '200kb',
                        },
                    },
                    backend: {
                        caching: {
                            strategy: 'redis',
                            ttl: 3600,
                        },
                        connection_pooling: {
                            min: 5,
                            max: 20,
                        },
                        query_optimization: true,
                    },
                    cdn: {
                        provider: 'cloudflare',
                    },
                    database: {
                        read_replicas: 2,
                        sharding: {
                            strategy: 'by_tenant',
                            shard_count: 4,
                        },
                        indexing_strategy: 'auto',
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            // Should generate multiple files
            expect(result.files.length).toBeGreaterThanOrEqual(11);
            // Frontend files
            expect(result.files.find(f => f.path.includes('web/vite.performance.config.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('web/src/utils/code-splitting.tsx'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('web/src/utils/lazy-loading.tsx'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('web/scripts/analyze-bundle.ts'))).toBeDefined();
            // Backend files
            expect(result.files.find(f => f.path.includes('api/src/config/caching.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('api/src/config/database-pool.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('api/src/utils/query-optimization.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('api/src/config/read-replicas.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('api/src/config/sharding.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('api/src/utils/indexing.ts'))).toBeDefined();
            // Shared files
            expect(result.files.find(f => f.path.includes('cdn.config.ts'))).toBeDefined();
            // All files should have absolute paths
            result.files.forEach(file => {
                expect(file.path.startsWith('/test/')).toBe(true);
            });
        });
    });
});
//# sourceMappingURL=enhanced-performance-generator.test.js.map