"use strict";
/**
 * Tests for EnhancedSecurityGenerator
 */
Object.defineProperty(exports, "__esModule", { value: true });
const enhanced_security_generator_1 = require("./enhanced-security-generator");
const workflow_engine_1 = require("../../engine/workflow-engine");
describe('EnhancedSecurityGenerator', () => {
    let generator;
    beforeEach(() => {
        generator = new enhanced_security_generator_1.EnhancedSecurityGenerator();
    });
    it('should have correct metadata', () => {
        expect(generator.id).toBe('enhanced-security');
        expect(generator.name).toBe('Enhanced Security Generator');
        expect(generator.description).toContain('security and compliance');
    });
    it('should return empty result when no security 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('Encryption', () => {
        it('should generate encryption configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                security: {
                    encryption: {
                        at_rest: true,
                        in_transit: true,
                        field_level: ['ssn', 'credit_card'],
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const encryptionConfig = result.files.find(f => f.path.includes('config/encryption.ts'));
            expect(encryptionConfig).toBeDefined();
            expect(encryptionConfig?.content).toContain('atRest: true');
            expect(encryptionConfig?.content).toContain('inTransit: true');
            expect(encryptionConfig?.content).toContain('ssn');
            expect(encryptionConfig?.content).toContain('credit_card');
            expect(encryptionConfig?.content).toContain('encrypt');
            expect(encryptionConfig?.content).toContain('decrypt');
            expect(encryptionConfig?.content).toContain('AES-256-GCM');
        });
        it('should generate field-level encryption helpers', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                security: {
                    encryption: {
                        at_rest: true,
                        in_transit: false,
                        field_level: ['email', 'phone'],
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const encryptionConfig = result.files.find(f => f.path.includes('config/encryption.ts'));
            expect(encryptionConfig?.content).toContain('encryptFields');
            expect(encryptionConfig?.content).toContain('decryptFields');
        });
    });
    describe('Audit Logging', () => {
        it('should generate audit logging configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                security: {
                    audit_logging: {
                        enabled: true,
                        retention: '7_years',
                        log_all_database_changes: true,
                        log_api_requests: true,
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const auditConfig = result.files.find(f => f.path.includes('audit-logging.ts'));
            expect(auditConfig).toBeDefined();
            expect(auditConfig?.content).toContain('7_years');
            expect(auditConfig?.content).toContain('logDatabaseChanges: true');
            expect(auditConfig?.content).toContain('logApiRequests: true');
            expect(auditConfig?.content).toContain('auditLogger');
            expect(auditConfig?.content).toContain('AuditEventType');
        });
        it('should include audit event types', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                security: {
                    audit_logging: {
                        enabled: true,
                        retention: '1_year',
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const auditConfig = result.files.find(f => f.path.includes('audit-logging.ts'));
            expect(auditConfig?.content).toContain('USER_LOGIN');
            expect(auditConfig?.content).toContain('USER_LOGOUT');
            expect(auditConfig?.content).toContain('DATA_ACCESSED');
            expect(auditConfig?.content).toContain('SENSITIVE_DATA_ACCESSED');
            expect(auditConfig?.content).toContain('SECURITY_VIOLATION');
        });
    });
    describe('Rate Limiting', () => {
        it('should generate global rate limiting configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                security: {
                    rate_limiting: {
                        global: {
                            requests_per_minute: 100,
                            burst: 20,
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const rateLimitConfig = result.files.find(f => f.path.includes('rate-limiting.ts'));
            expect(rateLimitConfig).toBeDefined();
            expect(rateLimitConfig?.content).toContain('"requests_per_minute": 100');
            expect(rateLimitConfig?.content).toContain('"burst": 20');
            expect(rateLimitConfig?.content).toContain('globalRateLimiter');
        });
        it('should generate endpoint-specific rate limits', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                security: {
                    rate_limiting: {
                        per_endpoint: [
                            { path: '/api/auth/login', limit: '5/minute' },
                            { path: '/api/search', limit: '50/minute' },
                        ],
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const rateLimitConfig = result.files.find(f => f.path.includes('rate-limiting.ts'));
            expect(rateLimitConfig?.content).toContain('/api/auth/login');
            expect(rateLimitConfig?.content).toContain('5/minute');
            expect(rateLimitConfig?.content).toContain('createEndpointRateLimiter');
        });
        it('should generate per-user tier rate limits', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                security: {
                    rate_limiting: {
                        per_user: {
                            free: '100/hour',
                            premium: '1000/hour',
                            enterprise: '10000/hour',
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const rateLimitConfig = result.files.find(f => f.path.includes('rate-limiting.ts'));
            expect(rateLimitConfig?.content).toContain('free');
            expect(rateLimitConfig?.content).toContain('premium');
            expect(rateLimitConfig?.content).toContain('enterprise');
            expect(rateLimitConfig?.content).toContain('createTierRateLimiter');
        });
    });
    describe('GDPR Compliance', () => {
        it('should generate GDPR backend utilities', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                security: {
                    compliance: {
                        regulations: ['gdpr'],
                        gdpr: {
                            right_to_access: true,
                            right_to_erasure: true,
                            data_portability: true,
                            consent_management: true,
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const gdprConfig = result.files.find(f => f.path.includes('api/') && f.path.includes('gdpr.ts'));
            expect(gdprConfig).toBeDefined();
            expect(gdprConfig?.content).toContain('rightToAccess: true');
            expect(gdprConfig?.content).toContain('rightToErasure: true');
            expect(gdprConfig?.content).toContain('dataPortability: true');
            expect(gdprConfig?.content).toContain('consentManagement: true');
            expect(gdprConfig?.content).toContain('exportUserData');
            expect(gdprConfig?.content).toContain('deleteUserData');
        });
        it('should generate GDPR frontend utilities', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    web: {
                        type: 'frontend',
                        technology: 'react',
                    },
                },
                security: {
                    compliance: {
                        regulations: ['gdpr'],
                        gdpr: {
                            consent_management: true,
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const gdprUtils = result.files.find(f => f.path.includes('web/') && f.path.includes('gdpr.ts'));
            expect(gdprUtils).toBeDefined();
            expect(gdprUtils?.content).toContain('useConsentManagement');
            expect(gdprUtils?.content).toContain('requestDataExport');
            expect(gdprUtils?.content).toContain('requestAccountDeletion');
            expect(gdprUtils?.content).toContain('ConsentBanner');
        });
    });
    describe('Data Retention', () => {
        it('should generate data retention configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                security: {
                    compliance: {
                        regulations: ['gdpr'],
                        data_retention: {
                            user_data: '7_years',
                            audit_logs: '10_years',
                            session_data: '30_days',
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const retentionConfig = result.files.find(f => f.path.includes('data-retention.ts'));
            expect(retentionConfig).toBeDefined();
            expect(retentionConfig?.content).toContain('user_data');
            expect(retentionConfig?.content).toContain('7_years');
            expect(retentionConfig?.content).toContain('audit_logs');
            expect(retentionConfig?.content).toContain('10_years');
            expect(retentionConfig?.content).toContain('calculateDeletionDate');
            expect(retentionConfig?.content).toContain('cleanupExpiredData');
        });
    });
    describe('HIPAA Compliance', () => {
        it('should generate HIPAA utilities', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                security: {
                    compliance: {
                        regulations: ['hipaa'],
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const hipaaConfig = result.files.find(f => f.path.includes('hipaa.ts'));
            expect(hipaaConfig).toBeDefined();
            expect(hipaaConfig?.content).toContain('containsPHI');
            expect(hipaaConfig?.content).toContain('encryptPHI');
            expect(hipaaConfig?.content).toContain('logPHIAccess');
            expect(hipaaConfig?.content).toContain('applyMinimumNecessary');
            expect(hipaaConfig?.content).toContain('Protected Health Information');
        });
    });
    describe('PCI DSS Compliance', () => {
        it('should generate PCI DSS utilities', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                security: {
                    compliance: {
                        regulations: ['pci_dss'],
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const pciConfig = result.files.find(f => f.path.includes('pci-dss.ts'));
            expect(pciConfig).toBeDefined();
            expect(pciConfig?.content).toContain('maskCardNumber');
            expect(pciConfig?.content).toContain('validateCardNumber');
            expect(pciConfig?.content).toContain('encryptCardData');
            expect(pciConfig?.content).toContain('Luhn algorithm');
            expect(pciConfig?.content).toContain('tokenizeCard');
        });
    });
    describe('Content Security Policy', () => {
        it('should generate CSP configuration', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    web: {
                        type: 'frontend',
                        technology: 'react',
                    },
                },
                security: {
                    csp: {
                        enabled: true,
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const cspConfig = result.files.find(f => f.path.includes('csp.ts'));
            expect(cspConfig).toBeDefined();
            expect(cspConfig?.content).toContain('cspDirectives');
            expect(cspConfig?.content).toContain('default-src');
            expect(cspConfig?.content).toContain('script-src');
            expect(cspConfig?.content).toContain('generateCSPHeader');
            expect(cspConfig?.content).toContain('cspMiddleware');
        });
        it('should use custom CSP directives', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    web: {
                        type: 'frontend',
                        technology: 'react',
                    },
                },
                security: {
                    csp: {
                        enabled: true,
                        directives: {
                            'default-src': ["'self'"],
                            'script-src': ["'self'", 'https://cdn.example.com'],
                            'img-src': ["'self'", 'data:', 'https:'],
                        },
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const cspConfig = result.files.find(f => f.path.includes('csp.ts'));
            expect(cspConfig?.content).toContain('https://cdn.example.com');
        });
    });
    describe('Security Policy Documentation', () => {
        it('should generate security policy documentation', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                },
                security: {
                    compliance: {
                        regulations: ['gdpr', 'hipaa', 'soc2'],
                    },
                    encryption: {
                        at_rest: true,
                        in_transit: true,
                    },
                    audit_logging: {
                        enabled: true,
                        retention: '7_years',
                    },
                },
            };
            const engine = new workflow_engine_1.WorkflowEngine();
            const executionPlan = engine.analyze(workflow);
            const result = generator.generate({
                workflow,
                executionPlan,
                outputDirectory: '/test',
            });
            const securityPolicy = result.files.find(f => f.path.includes('SECURITY.md'));
            expect(securityPolicy).toBeDefined();
            expect(securityPolicy?.content).toContain('Security & Compliance Policy');
            expect(securityPolicy?.content).toContain('GDPR');
            expect(securityPolicy?.content).toContain('HIPAA');
            expect(securityPolicy?.content).toContain('SOC2');
            expect(securityPolicy?.content).toContain('Encryption at Rest');
            expect(securityPolicy?.content).toContain('Audit logging');
        });
    });
    describe('Complete Security Schema', () => {
        it('should generate all security files for complete schema', () => {
            const workflow = {
                workflow: {
                    id: 'test',
                    name: 'Test Complete Security',
                    version: '1.0.0',
                },
                stack: {
                    api: {
                        type: 'backend',
                        technology: 'typescript/express',
                    },
                    web: {
                        type: 'frontend',
                        technology: 'react',
                    },
                },
                security: {
                    compliance: {
                        regulations: ['gdpr', 'hipaa', 'pci_dss'],
                        data_retention: {
                            user_data: '7_years',
                        },
                        gdpr: {
                            right_to_access: true,
                            right_to_erasure: true,
                            consent_management: true,
                        },
                    },
                    encryption: {
                        at_rest: true,
                        in_transit: true,
                        field_level: ['ssn', 'credit_card'],
                    },
                    audit_logging: {
                        enabled: true,
                        retention: '10_years',
                        log_all_database_changes: true,
                        log_api_requests: true,
                    },
                    csp: {
                        enabled: true,
                    },
                    rate_limiting: {
                        global: {
                            requests_per_minute: 100,
                        },
                    },
                },
            };
            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(9);
            // Backend files
            expect(result.files.find(f => f.path.includes('api/src/config/encryption.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('api/src/config/audit-logging.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('api/src/middleware/rate-limiting.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('api/src/utils/gdpr.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('api/src/utils/hipaa.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('api/src/utils/pci-dss.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('api/src/utils/data-retention.ts'))).toBeDefined();
            // Frontend files
            expect(result.files.find(f => f.path.includes('web/src/utils/csp.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('web/src/utils/gdpr.ts'))).toBeDefined();
            // Shared files
            expect(result.files.find(f => f.path.includes('SECURITY.md'))).toBeDefined();
            // All files should have absolute paths
            result.files.forEach(file => {
                expect(file.path.startsWith('/test/')).toBe(true);
            });
        });
    });
});
//# sourceMappingURL=enhanced-security-generator.test.js.map