"use strict";
/**
 * E2E Test: Enhanced Monitoring Generator
 *
 * This test verifies that the enhanced monitoring generator actually writes
 * all expected monitoring configuration files to disk with correct content.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
require("../generators/index"); // Ensure all generators are registered
const generation_orchestrator_1 = require("../generators/generation-orchestrator");
describe('E2E: Enhanced Monitoring Generator', () => {
    const testOutputDir = path.join(__dirname, '../../.e2e-output/enhanced-monitoring');
    let orchestrator;
    beforeEach(() => {
        // Clean up test directory before each test
        if (fs.existsSync(testOutputDir)) {
            fs.rmSync(testOutputDir, { recursive: true, force: true });
        }
        orchestrator = new generation_orchestrator_1.GenerationOrchestrator();
    });
    afterEach(() => {
        // Clean up test directory after each test
        if (fs.existsSync(testOutputDir)) {
            fs.rmSync(testOutputDir, { recursive: true, force: true });
        }
    });
    it('should generate all monitoring files for complete monitoring schema', async () => {
        const workflow = {
            workflow: {
                id: 'e2e-monitoring-test',
                version: '1.0.0',
                name: 'E2E Monitoring Test',
                description: 'Testing enhanced monitoring generator E2E',
            },
            stack: {
                api: {
                    type: 'backend',
                    technology: 'typescript/express',
                    config: { port: 3000 },
                },
                web: {
                    type: 'frontend',
                    technology: 'react',
                    config: { port: 5173 },
                },
            },
            monitoring: {
                apm: {
                    provider: 'datadog',
                },
                error_tracking: {
                    provider: 'sentry',
                    frontend: true,
                    backend: true,
                },
                logging: {
                    provider: 'winston',
                    level: 'info',
                    transports: [
                        { type: 'console' },
                        { type: 'file', config: { filename: 'app.log' } },
                    ],
                },
                tracing: {
                    provider: 'jaeger',
                    sampling_rate: 0.1,
                },
                metrics: {
                    tracked: ['requests', 'errors', 'latency'],
                    custom_metrics: [
                        {
                            name: 'user_signups',
                            type: 'counter',
                            description: 'Number of user signups',
                        },
                    ],
                },
                alerts: [
                    {
                        name: 'high-error-rate',
                        condition: 'error_rate > 0.05',
                        channels: ['slack', 'email'],
                        severity: 'critical',
                    },
                ],
            },
            stages: {
                local: {
                    infrastructure: {
                        databases: {},
                        services: {},
                    },
                },
            },
        };
        // Generate code
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        // Verify files were generated
        expect(_result.files.length).toBeGreaterThan(0);
        // Define expected files
        const expectedFiles = [
            'api/src/config/logging.ts',
            'api/src/utils/logger.ts',
            'api/src/config/error-tracking.ts',
            'api/src/config/apm.ts',
            'api/src/config/tracing.ts',
            'api/src/config/metrics.ts',
            'web/src/utils/error-tracking.ts',
            'web/src/utils/performance.ts',
            'alerts.config.ts',
        ];
        // Verify each expected file exists
        expectedFiles.forEach(expectedFile => {
            const fullPath = path.join(testOutputDir, expectedFile);
            expect(fs.existsSync(fullPath)).toBe(true);
        });
    });
    it('should generate Winston logging with correct configuration', async () => {
        const workflow = {
            workflow: {
                id: 'logging-test',
                version: '1.0.0',
                name: 'Logging Test',
            },
            stack: {
                api: {
                    type: 'backend',
                    technology: 'typescript/express',
                    config: { port: 3000 },
                },
            },
            monitoring: {
                logging: {
                    provider: 'winston',
                    level: 'debug',
                    transports: [
                        { type: 'console' },
                        { type: 'file', config: { filename: 'logs/app.log' } },
                    ],
                },
            },
            stages: {
                local: {
                    infrastructure: {
                        databases: {},
                        services: {},
                    },
                },
            },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        // Read the generated logging config
        const loggingConfigPath = path.join(testOutputDir, 'api/src/config/logging.ts');
        expect(fs.existsSync(loggingConfigPath)).toBe(true);
        const loggingConfig = fs.readFileSync(loggingConfigPath, 'utf-8');
        // Verify Winston configuration
        expect(loggingConfig).toContain('winston');
        expect(loggingConfig).toContain("level: 'debug'");
        expect(loggingConfig).toContain('winston.transports.Console');
        expect(loggingConfig).toContain('winston.transports.File');
        expect(loggingConfig).toContain('logs/app.log');
    });
    it('should generate Sentry error tracking configuration', async () => {
        const workflow = {
            workflow: {
                id: 'error-tracking-test',
                version: '1.0.0',
                name: 'Error Tracking Test',
            },
            stack: {
                api: {
                    type: 'backend',
                    technology: 'typescript/express',
                    config: { port: 3000 },
                },
            },
            monitoring: {
                error_tracking: {
                    provider: 'sentry',
                    backend: true,
                },
            },
            stages: {
                local: {
                    infrastructure: {
                        databases: {},
                        services: {},
                    },
                },
            },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        // Read the generated error tracking config
        const errorTrackingPath = path.join(testOutputDir, 'api/src/config/error-tracking.ts');
        expect(fs.existsSync(errorTrackingPath)).toBe(true);
        const errorTracking = fs.readFileSync(errorTrackingPath, 'utf-8');
        // Verify Sentry setup
        expect(errorTracking).toContain('@sentry/node');
        expect(errorTracking).toContain('Sentry.init');
        expect(errorTracking).toContain('captureException');
        expect(errorTracking).toContain('SENTRY_DSN');
    });
    it('should generate Datadog APM configuration', async () => {
        const workflow = {
            workflow: {
                id: 'apm-test',
                version: '1.0.0',
                name: 'APM Test',
            },
            stack: {
                api: {
                    type: 'backend',
                    technology: 'typescript/express',
                    config: { port: 3000 },
                },
            },
            monitoring: {
                apm: {
                    provider: 'datadog',
                },
            },
            stages: {
                local: {
                    infrastructure: {
                        databases: {},
                        services: {},
                    },
                },
            },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        // Read the generated APM config
        const apmConfigPath = path.join(testOutputDir, 'api/src/config/apm.ts');
        expect(fs.existsSync(apmConfigPath)).toBe(true);
        const apmConfig = fs.readFileSync(apmConfigPath, 'utf-8');
        // Verify Datadog APM configuration
        expect(apmConfig).toContain('dd-trace');
        expect(apmConfig).toContain('tracer.init');
        expect(apmConfig).toContain('DD_ENV');
        expect(apmConfig).toContain('DD_SERVICE');
    });
    it('should generate frontend monitoring when enabled', async () => {
        const workflow = {
            workflow: {
                id: 'frontend-monitoring-test',
                version: '1.0.0',
                name: 'Frontend Monitoring Test',
            },
            stack: {
                web: {
                    type: 'frontend',
                    technology: 'react',
                    config: { port: 5173 },
                },
            },
            monitoring: {
                error_tracking: {
                    provider: 'sentry',
                    frontend: true,
                },
                apm: {
                    provider: 'datadog',
                },
            },
            stages: {
                local: {
                    infrastructure: {
                        databases: {},
                        services: {},
                    },
                },
            },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        // Read frontend error tracking
        const frontendErrorTrackingPath = path.join(testOutputDir, 'web/src/utils/error-tracking.ts');
        expect(fs.existsSync(frontendErrorTrackingPath)).toBe(true);
        const frontendErrorTracking = fs.readFileSync(frontendErrorTrackingPath, 'utf-8');
        expect(frontendErrorTracking).toContain('@sentry/react');
        expect(frontendErrorTracking).toContain('BrowserTracing');
        expect(frontendErrorTracking).toContain('VITE_SENTRY_DSN');
        // Read frontend performance monitoring
        const frontendPerfPath = path.join(testOutputDir, 'web/src/utils/performance.ts');
        expect(fs.existsSync(frontendPerfPath)).toBe(true);
        const frontendPerf = fs.readFileSync(frontendPerfPath, 'utf-8');
        expect(frontendPerf).toContain('trackPageLoad');
        expect(frontendPerf).toContain('trackUserInteraction');
    });
    it('should generate alert configuration with all alerts', async () => {
        const workflow = {
            workflow: {
                id: 'alerts-test',
                version: '1.0.0',
                name: 'Alerts Test',
            },
            stack: {
                api: {
                    type: 'backend',
                    technology: 'typescript/express',
                    config: { port: 3000 },
                },
            },
            monitoring: {
                alerts: [
                    {
                        name: 'high-cpu',
                        condition: 'cpu > 80',
                        channels: ['slack'],
                        severity: 'warning',
                    },
                    {
                        name: 'database-down',
                        condition: 'db_health == false',
                        channels: ['pagerduty', 'slack'],
                        severity: 'critical',
                    },
                ],
            },
            stages: {
                local: {
                    infrastructure: {
                        databases: {},
                        services: {},
                    },
                },
            },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        // Read alerts config
        const alertsConfigPath = path.join(testOutputDir, 'alerts.config.ts');
        expect(fs.existsSync(alertsConfigPath)).toBe(true);
        const alertsConfig = fs.readFileSync(alertsConfigPath, 'utf-8');
        // Verify alerts are included
        expect(alertsConfig).toContain('high-cpu');
        expect(alertsConfig).toContain('cpu > 80');
        expect(alertsConfig).toContain('database-down');
        expect(alertsConfig).toContain('db_health == false');
        expect(alertsConfig).toContain('pagerduty');
    });
});
//# sourceMappingURL=enhanced-monitoring.e2e.test.js.map