"use strict";
/**
 * E2E Test: Enhanced Deployment Generator
 *
 * Verifies that the deployment generator writes all expected infrastructure files to disk.
 */
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");
const generation_orchestrator_1 = require("../generators/generation-orchestrator");
describe('E2E: Enhanced Deployment Generator', () => {
    const testOutputDir = path.join(__dirname, '../../.e2e-output/enhanced-deployment');
    let orchestrator;
    beforeEach(() => {
        if (fs.existsSync(testOutputDir)) {
            fs.rmSync(testOutputDir, { recursive: true, force: true });
        }
        orchestrator = new generation_orchestrator_1.GenerationOrchestrator();
    });
    afterEach(() => {
        if (fs.existsSync(testOutputDir)) {
            fs.rmSync(testOutputDir, { recursive: true, force: true });
        }
    });
    it('should generate multi-region configuration', async () => {
        const workflow = {
            workflow: { id: 'multi-region-test', version: '1.0.0', name: 'Multi-Region Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [
                    { name: 'us-east-1', primary: true, services: ['api', 'web'], database_replica: false },
                    { name: 'eu-west-1', services: ['api'], database_replica: true },
                ],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const regionConfigPath = path.join(testOutputDir, 'infrastructure/config/regions.ts');
        expect(fs.existsSync(regionConfigPath)).toBe(true);
        const regionConfig = fs.readFileSync(regionConfigPath, 'utf-8');
        expect(regionConfig).toContain('us-east-1');
        expect(regionConfig).toContain('eu-west-1');
        expect(regionConfig).toContain('getPrimaryRegion');
        expect(regionConfig).toContain('getActiveRegions');
    });
    it('should generate multi-region Terraform configuration', async () => {
        const workflow = {
            workflow: { id: 'terraform-test', version: '1.0.0', name: 'Terraform Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [
                    { name: 'us-east-1', primary: true, services: ['api'] },
                    { name: 'us-west-2', services: ['api'], database_replica: true },
                ],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const terraformPath = path.join(testOutputDir, 'infrastructure/terraform/multi-region.tf');
        expect(fs.existsSync(terraformPath)).toBe(true);
        const terraform = fs.readFileSync(terraformPath, 'utf-8');
        expect(terraform).toContain('aws_ecs_cluster');
        expect(terraform).toContain('us-east-1');
        expect(terraform).toContain('us-west-2');
        expect(terraform).toContain('aws_db_instance');
    });
    it('should generate deployment script', async () => {
        const workflow = {
            workflow: { id: 'script-test', version: '1.0.0', name: 'Script Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [{ name: 'us-east-1', primary: true, services: ['api'] }],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const scriptPath = path.join(testOutputDir, 'infrastructure/scripts/deploy-multi-region.sh');
        expect(fs.existsSync(scriptPath)).toBe(true);
        const script = fs.readFileSync(scriptPath, 'utf-8');
        expect(script).toContain('#!/bin/bash');
        expect(script).toContain('terraform');
        expect(script).toContain('PRIMARY_REGION');
    });
    it('should generate Cloudflare Workers', async () => {
        const workflow = {
            workflow: { id: 'edge-test', version: '1.0.0', name: 'Edge Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'edge',
                edge_functions: [
                    { name: 'geo-router', regions: 'all', provider: 'cloudflare_workers' },
                ],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const edgeFuncPath = path.join(testOutputDir, 'infrastructure/edge/geo-router.ts');
        expect(fs.existsSync(edgeFuncPath)).toBe(true);
        const edgeFunc = fs.readFileSync(edgeFuncPath, 'utf-8');
        expect(edgeFunc).toContain('addEventListener');
        expect(edgeFunc).toContain('handleRequest');
        expect(edgeFunc).toContain('CF-IPCountry');
    });
    it('should generate Wrangler configuration', async () => {
        const workflow = {
            workflow: { id: 'wrangler-test', version: '1.0.0', name: 'Wrangler Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'edge',
                edge_functions: [{ name: 'auth-edge', regions: 'all', provider: 'cloudflare_workers' }],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const wranglerPath = path.join(testOutputDir, 'infrastructure/edge/auth-edge.wrangler.toml');
        expect(fs.existsSync(wranglerPath)).toBe(true);
        const wrangler = fs.readFileSync(wranglerPath, 'utf-8');
        expect(wrangler).toContain('name = "auth-edge"');
        expect(wrangler).toContain('type = "javascript"');
    });
    it('should generate Lambda@Edge functions', async () => {
        const workflow = {
            workflow: { id: 'lambda-test', version: '1.0.0', name: 'Lambda Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'edge',
                edge_functions: [{ name: 'header-transform', regions: 'all', provider: 'lambda_edge' }],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const lambdaPath = path.join(testOutputDir, 'infrastructure/edge/header-transform.ts');
        expect(fs.existsSync(lambdaPath)).toBe(true);
        const lambda = fs.readFileSync(lambdaPath, 'utf-8');
        expect(lambda).toContain('handler');
        expect(lambda).toContain('cloudfront-viewer-country');
    });
    it('should generate geo-routing logic', async () => {
        const workflow = {
            workflow: { id: 'geo-test', version: '1.0.0', name: 'Geo Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [{ name: 'us-east-1', primary: true, services: ['api'] }],
                geo_routing: { enabled: true, strategy: 'closest_region' },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const geoRouterPath = path.join(testOutputDir, 'infrastructure/routing/geo-router.ts');
        expect(fs.existsSync(geoRouterPath)).toBe(true);
        const geoRouter = fs.readFileSync(geoRouterPath, 'utf-8');
        expect(geoRouter).toContain('GeoRouter');
        expect(geoRouter).toContain('selectRegion');
        expect(geoRouter).toContain('getRegionHealth');
    });
    it('should generate geo-routing Terraform', async () => {
        const workflow = {
            workflow: { id: 'geo-terraform-test', version: '1.0.0', name: 'Geo Terraform Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [
                    { name: 'us-east-1', primary: true, services: ['api'] },
                    { name: 'eu-west-1', services: ['api'] },
                ],
                geo_routing: { enabled: true, strategy: 'lowest_latency' },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const terraformPath = path.join(testOutputDir, 'infrastructure/terraform/geo-routing.tf');
        expect(fs.existsSync(terraformPath)).toBe(true);
        const terraform = fs.readFileSync(terraformPath, 'utf-8');
        expect(terraform).toContain('aws_route53_health_check');
        expect(terraform).toContain('aws_route53_record');
        expect(terraform).toContain('geolocation_routing_policy');
    });
    it('should generate disaster recovery configuration', async () => {
        const workflow = {
            workflow: { id: 'dr-test', version: '1.0.0', name: 'DR Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [{ name: 'us-east-1', primary: true, services: ['api'] }],
                disaster_recovery: {
                    rpo: '1_hour',
                    rto: '4_hours',
                    replica_regions: ['us-west-2'],
                    failover: 'automatic',
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const drConfigPath = path.join(testOutputDir, 'infrastructure/dr/disaster-recovery.ts');
        expect(fs.existsSync(drConfigPath)).toBe(true);
        const drConfig = fs.readFileSync(drConfigPath, 'utf-8');
        expect(drConfig).toContain('DisasterRecoveryManager');
        expect(drConfig).toContain('checkRPO');
        expect(drConfig).toContain('triggerDR');
        expect(drConfig).toContain('1_hour');
    });
    it('should generate automatic failover logic', async () => {
        const workflow = {
            workflow: { id: 'auto-failover-test', version: '1.0.0', name: 'Auto Failover Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [{ name: 'us-east-1', primary: true, services: ['api'] }],
                disaster_recovery: {
                    rpo: '30_minutes',
                    rto: '2_hours',
                    failover: 'automatic',
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const failoverPath = path.join(testOutputDir, 'infrastructure/dr/failover.ts');
        expect(fs.existsSync(failoverPath)).toBe(true);
        const failover = fs.readFileSync(failoverPath, 'utf-8');
        expect(failover).toContain('FailoverManager');
        expect(failover).toContain('startMonitoring');
        expect(failover).toContain('triggerAutomaticFailover');
    });
    it('should generate manual failover logic', async () => {
        const workflow = {
            workflow: { id: 'manual-failover-test', version: '1.0.0', name: 'Manual Failover Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [{ name: 'us-east-1', primary: true, services: ['api'] }],
                disaster_recovery: {
                    rpo: '1_hour',
                    rto: '4_hours',
                    failover: 'manual',
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const failoverPath = path.join(testOutputDir, 'infrastructure/dr/failover.ts');
        const failover = fs.readFileSync(failoverPath, 'utf-8');
        expect(failover).toContain('initiateManualFailover');
    });
    it('should generate disaster recovery Terraform', async () => {
        const workflow = {
            workflow: { id: 'dr-terraform-test', version: '1.0.0', name: 'DR Terraform Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [{ name: 'us-east-1', primary: true, services: ['api'] }],
                disaster_recovery: {
                    rpo: '1_hour',
                    rto: '4_hours',
                    replica_regions: ['us-west-2', 'eu-west-1'],
                    failover: 'automatic',
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const terraformPath = path.join(testOutputDir, 'infrastructure/terraform/disaster-recovery.tf');
        expect(fs.existsSync(terraformPath)).toBe(true);
        const terraform = fs.readFileSync(terraformPath, 'utf-8');
        expect(terraform).toContain('aws_db_instance');
        expect(terraform).toContain('us_west_2_replica');
        expect(terraform).toContain('eu_west_1_replica');
    });
    it('should generate failover script', async () => {
        const workflow = {
            workflow: { id: 'failover-script-test', version: '1.0.0', name: 'Failover Script Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [{ name: 'us-east-1', primary: true, services: ['api'] }],
                disaster_recovery: {
                    rpo: '1_hour',
                    rto: '4_hours',
                    failover: 'manual',
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const scriptPath = path.join(testOutputDir, 'infrastructure/scripts/failover.sh');
        expect(fs.existsSync(scriptPath)).toBe(true);
        const script = fs.readFileSync(scriptPath, 'utf-8');
        expect(script).toContain('#!/bin/bash');
        expect(script).toContain('promote-read-replica');
        expect(script).toContain('TARGET_REGION');
    });
    it('should generate health check utilities', async () => {
        const workflow = {
            workflow: { id: 'health-test', version: '1.0.0', name: 'Health Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [{ name: 'us-east-1', primary: true, services: ['api'] }],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const healthCheckPath = path.join(testOutputDir, 'infrastructure/utils/health-check.ts');
        expect(fs.existsSync(healthCheckPath)).toBe(true);
        const healthCheck = fs.readFileSync(healthCheckPath, 'utf-8');
        expect(healthCheck).toContain('checkRegionHealth');
        expect(healthCheck).toContain('HealthStatus');
    });
    it('should generate region selector', async () => {
        const workflow = {
            workflow: { id: 'selector-test', version: '1.0.0', name: 'Selector Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [{ name: 'us-east-1', primary: true, services: ['api'] }],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const selectorPath = path.join(testOutputDir, 'infrastructure/utils/region-selector.ts');
        expect(fs.existsSync(selectorPath)).toBe(true);
        const selector = fs.readFileSync(selectorPath, 'utf-8');
        expect(selector).toContain('RegionSelector');
        expect(selector).toContain('selectForClient');
    });
    it('should generate deployment types', async () => {
        const workflow = {
            workflow: { id: 'types-test', version: '1.0.0', name: 'Types Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'single_region',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const typesPath = path.join(testOutputDir, 'infrastructure/types/deployment.ts');
        expect(fs.existsSync(typesPath)).toBe(true);
        const types = fs.readFileSync(typesPath, 'utf-8');
        expect(types).toContain('DeploymentStrategy');
        expect(types).toContain('Region');
        expect(types).toContain('EdgeFunction');
    });
    it('should generate main Terraform configuration', async () => {
        const workflow = {
            workflow: { id: 'main-terraform-test', version: '1.0.0', name: 'Main Terraform Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [{ name: 'us-east-1', primary: true, services: ['api'] }],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const terraformPath = path.join(testOutputDir, 'infrastructure/terraform/main.tf');
        expect(fs.existsSync(terraformPath)).toBe(true);
        const terraform = fs.readFileSync(terraformPath, 'utf-8');
        expect(terraform).toContain('terraform');
        expect(terraform).toContain('aws_vpc');
    });
    it('should generate deployment documentation', async () => {
        const workflow = {
            workflow: { id: 'docs-test', version: '1.0.0', name: 'Docs Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [{ name: 'us-east-1', primary: true, services: ['api'] }],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const docsPath = path.join(testOutputDir, 'infrastructure/README.md');
        expect(fs.existsSync(docsPath)).toBe(true);
        const docs = fs.readFileSync(docsPath, 'utf-8');
        expect(docs).toContain('# Deployment Infrastructure');
        expect(docs).toContain('multi_region');
    });
    it('should generate complete deployment infrastructure', async () => {
        const workflow = {
            workflow: { id: 'complete-test', version: '1.0.0', name: 'Complete Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            deployment: {
                strategy: 'multi_region',
                regions: [
                    { name: 'us-east-1', primary: true, services: ['api', 'web'], database_replica: false },
                    { name: 'eu-west-1', services: ['api'], database_replica: true },
                ],
                edge_functions: [
                    { name: 'cdn-router', regions: 'all', provider: 'cloudflare_workers' },
                ],
                geo_routing: { enabled: true, strategy: 'closest_region' },
                disaster_recovery: {
                    rpo: '1_hour',
                    rto: '4_hours',
                    replica_regions: ['us-west-2'],
                    failover: 'automatic',
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        // Verify all files exist
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/config/regions.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/terraform/multi-region.tf'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/scripts/deploy-multi-region.sh'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/edge/edge-functions.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/edge/cdn-router.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/routing/geo-router.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/terraform/geo-routing.tf'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/dr/disaster-recovery.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/dr/failover.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/scripts/failover.sh'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/terraform/disaster-recovery.tf'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/utils/health-check.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/utils/region-selector.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/types/deployment.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/terraform/main.tf'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/terraform/variables.tf'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'infrastructure/README.md'))).toBe(true);
    });
});
//# sourceMappingURL=enhanced-deployment.e2e.test.js.map