"use strict";
/**
 * E2E Test: Enhanced API Configuration Generator
 *
 * Verifies that the API generator writes all expected 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");
const generation_orchestrator_1 = require("../generators/generation-orchestrator");
describe('E2E: Enhanced API Configuration Generator', () => {
    const testOutputDir = path.join(__dirname, '../../.e2e-output/enhanced-api');
    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 OpenAPI documentation files', async () => {
        const workflow = {
            workflow: { id: 'api-test', version: '1.0.0', name: 'API Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {
                documentation: {
                    format: 'openapi',
                    auto_generate: true,
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const docsPath = path.join(testOutputDir, 'api/src/config/api-docs.ts');
        expect(fs.existsSync(docsPath)).toBe(true);
        const docs = fs.readFileSync(docsPath, 'utf-8');
        expect(docs).toContain('apiDocsConfig');
        expect(docs).toContain('openapi');
    });
    it('should generate Swagger middleware', async () => {
        const workflow = {
            workflow: { id: 'swagger-test', version: '1.0.0', name: 'Swagger Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {
                documentation: {
                    format: 'swagger',
                    auto_generate: false,
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const swaggerPath = path.join(testOutputDir, 'api/src/middleware/swagger.ts');
        expect(fs.existsSync(swaggerPath)).toBe(true);
        const swagger = fs.readFileSync(swaggerPath, 'utf-8');
        expect(swagger).toContain('setupSwagger');
        expect(swagger).toContain('swagger-ui-express');
    });
    it('should generate URL path versioning middleware', async () => {
        const workflow = {
            workflow: { id: 'versioning-test', version: '1.0.0', name: 'Versioning Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {
                versioning: {
                    strategy: 'url_path',
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const versioningPath = path.join(testOutputDir, 'api/src/middleware/versioning.ts');
        expect(fs.existsSync(versioningPath)).toBe(true);
        const versioning = fs.readFileSync(versioningPath, 'utf-8');
        expect(versioning).toContain('versioningMiddleware');
        expect(versioning).toContain('requireVersion');
    });
    it('should generate header-based versioning', async () => {
        const workflow = {
            workflow: { id: 'header-test', version: '1.0.0', name: 'Header Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {
                versioning: {
                    strategy: 'header',
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const versioningPath = path.join(testOutputDir, 'api/src/middleware/versioning.ts');
        const versioning = fs.readFileSync(versioningPath, 'utf-8');
        expect(versioning).toContain("req.headers['x-api-version']");
    });
    it('should generate deprecation middleware with sunset headers', async () => {
        const workflow = {
            workflow: { id: 'deprecation-test', version: '1.0.0', name: 'Deprecation Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {
                versioning: {
                    strategy: 'url_path',
                    deprecation_policy: {
                        notice_period: '90_days',
                        sunset_headers: true,
                    },
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const deprecationPath = path.join(testOutputDir, 'api/src/middleware/deprecation.ts');
        expect(fs.existsSync(deprecationPath)).toBe(true);
        const deprecation = fs.readFileSync(deprecationPath, 'utf-8');
        expect(deprecation).toContain('deprecate');
        expect(deprecation).toContain("res.setHeader('Sunset'");
    });
    it('should generate version router', async () => {
        const workflow = {
            workflow: { id: 'router-test', version: '1.0.0', name: 'Router Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {
                versioning: {
                    strategy: 'url_path',
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const routerPath = path.join(testOutputDir, 'api/src/utils/version-router.ts');
        expect(fs.existsSync(routerPath)).toBe(true);
        const router = fs.readFileSync(routerPath, 'utf-8');
        expect(router).toContain('VersionRouter');
        expect(router).toContain('getRouter');
    });
    it('should generate CORS configuration', async () => {
        const workflow = {
            workflow: { id: 'cors-test', version: '1.0.0', name: 'CORS Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {
                cors: {
                    enabled: true,
                    origins: ['https://example.com'],
                    methods: ['GET', 'POST'],
                    credentials: true,
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const corsPath = path.join(testOutputDir, 'api/src/config/cors.ts');
        expect(fs.existsSync(corsPath)).toBe(true);
        const cors = fs.readFileSync(corsPath, 'utf-8');
        expect(cors).toContain('corsConfig');
        expect(cors).toContain('https://example.com');
        expect(cors).toContain('credentials: true');
    });
    it('should generate CORS middleware', async () => {
        const workflow = {
            workflow: { id: 'cors-middleware-test', version: '1.0.0', name: 'CORS Middleware Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {
                cors: {
                    enabled: true,
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const middlewarePath = path.join(testOutputDir, 'api/src/middleware/cors.ts');
        expect(fs.existsSync(middlewarePath)).toBe(true);
        const middleware = fs.readFileSync(middlewarePath, 'utf-8');
        expect(middleware).toContain('corsMiddleware');
    });
    it('should generate validation middleware', async () => {
        const workflow = {
            workflow: { id: 'validation-test', version: '1.0.0', name: 'Validation Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {},
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const validationPath = path.join(testOutputDir, 'api/src/middleware/validation.ts');
        expect(fs.existsSync(validationPath)).toBe(true);
        const validation = fs.readFileSync(validationPath, 'utf-8');
        expect(validation).toContain('validate');
        expect(validation).toContain('VALIDATION_ERROR');
    });
    it('should generate response formatter', async () => {
        const workflow = {
            workflow: { id: 'formatter-test', version: '1.0.0', name: 'Formatter Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {},
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const formatterPath = path.join(testOutputDir, 'api/src/middleware/response-formatter.ts');
        expect(fs.existsSync(formatterPath)).toBe(true);
        const formatter = fs.readFileSync(formatterPath, 'utf-8');
        expect(formatter).toContain('responseFormatter');
        expect(formatter).toContain('res.success');
        expect(formatter).toContain('res.error');
    });
    it('should generate API types', async () => {
        const workflow = {
            workflow: { id: 'types-test', version: '1.0.0', name: 'Types Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {},
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const typesPath = path.join(testOutputDir, 'api/src/types/api.ts');
        expect(fs.existsSync(typesPath)).toBe(true);
        const types = fs.readFileSync(typesPath, 'utf-8');
        expect(types).toContain('ApiException');
        expect(types).toContain('ValidationException');
        expect(types).toContain('PaginatedResponse');
    });
    it('should generate app setup with health check', async () => {
        const workflow = {
            workflow: { id: 'app-test', version: '1.0.0', name: 'App Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {},
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const appPath = path.join(testOutputDir, 'api/src/app.ts');
        expect(fs.existsSync(appPath)).toBe(true);
        const app = fs.readFileSync(appPath, 'utf-8');
        expect(app).toContain('createApp');
        expect(app).toContain('/health');
        expect(app).toContain('helmet()');
    });
    it('should generate complete API configuration', async () => {
        const workflow = {
            workflow: { id: 'complete-test', version: '1.0.0', name: 'Complete Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            api: {
                base_path: '/api',
                documentation: {
                    format: 'openapi',
                    auto_generate: true,
                },
                versioning: {
                    strategy: 'url_path',
                    deprecation_policy: {
                        notice_period: '90_days',
                        sunset_headers: true,
                    },
                },
                cors: {
                    enabled: true,
                    origins: ['https://example.com'],
                },
                middleware: ['logger'],
            },
            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, 'api/src/config/api-docs.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/middleware/swagger.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/utils/swagger-generator.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/middleware/versioning.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/utils/version-router.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/middleware/deprecation.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/config/cors.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/middleware/cors.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/middleware/index.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/middleware/validation.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/middleware/response-formatter.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/types/api.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/app.ts'))).toBe(true);
    });
});
//# sourceMappingURL=enhanced-api.e2e.test.js.map