"use strict";
/**
 * Tests for Enhanced API Configuration Generator
 */
Object.defineProperty(exports, "__esModule", { value: true });
const enhanced_api_generator_1 = require("./enhanced-api-generator");
describe('EnhancedApiGenerator', () => {
    let generator;
    beforeEach(() => {
        generator = new enhanced_api_generator_1.EnhancedApiGenerator();
    });
    it('should have correct metadata', () => {
        expect(generator.id).toBe('enhanced-api');
        expect(generator.name).toBe('Enhanced API Configuration Generator');
        expect(generator.description).toContain('API configuration');
    });
    it('should return empty result when no api config', () => {
        const workflow = {
            workflow: { id: 'test', version: '1.0.0', name: 'Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
        expect(result.files).toHaveLength(0);
    });
    describe('API Documentation', () => {
        it('should generate OpenAPI documentation config', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    documentation: {
                        format: 'openapi',
                        auto_generate: true,
                    },
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const docsFile = result.files.find(f => f.path.includes('config/api-docs.ts'));
            expect(docsFile).toBeDefined();
            expect(docsFile.content).toContain('apiDocsConfig');
            expect(docsFile.content).toContain('openapi');
            expect(docsFile.content).toContain('info');
        });
        it('should generate Swagger middleware', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    documentation: {
                        format: 'swagger',
                        auto_generate: false,
                    },
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const swaggerFile = result.files.find(f => f.path.includes('middleware/swagger.ts'));
            expect(swaggerFile).toBeDefined();
            expect(swaggerFile.content).toContain('setupSwagger');
            expect(swaggerFile.content).toContain('swagger-ui-express');
        });
        it('should generate auto-generation when enabled', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    documentation: {
                        format: 'openapi',
                        auto_generate: true,
                    },
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const swaggerFile = result.files.find(f => f.path.includes('middleware/swagger.ts'));
            expect(swaggerFile.content).toContain('generateSwaggerDoc()');
        });
        it('should generate swagger generator utility', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    documentation: {
                        format: 'openapi',
                        auto_generate: true,
                    },
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const generatorFile = result.files.find(f => f.path.includes('utils/swagger-generator.ts'));
            expect(generatorFile).toBeDefined();
            expect(generatorFile.content).toContain('generateSwaggerDoc');
            expect(generatorFile.content).toContain('ApiEndpoint');
        });
    });
    describe('API Versioning', () => {
        it('should generate URL path versioning middleware', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    versioning: {
                        strategy: 'url_path',
                    },
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const versioningFile = result.files.find(f => f.path.includes('middleware/versioning.ts'));
            expect(versioningFile).toBeDefined();
            expect(versioningFile.content).toContain('versioningMiddleware');
            expect(versioningFile.content).toContain('req.path.match');
            expect(versioningFile.content).toContain('requireVersion');
        });
        it('should generate header versioning middleware', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    versioning: {
                        strategy: 'header',
                    },
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const versioningFile = result.files.find(f => f.path.includes('middleware/versioning.ts'));
            expect(versioningFile.content).toContain("req.headers['x-api-version']");
        });
        it('should generate query param versioning middleware', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    versioning: {
                        strategy: 'query_param',
                    },
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const versioningFile = result.files.find(f => f.path.includes('middleware/versioning.ts'));
            expect(versioningFile.content).toContain('req.query.version');
        });
        it('should generate version router', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    versioning: {
                        strategy: 'url_path',
                    },
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const routerFile = result.files.find(f => f.path.includes('utils/version-router.ts'));
            expect(routerFile).toBeDefined();
            expect(routerFile.content).toContain('VersionRouter');
            expect(routerFile.content).toContain('getRouter');
        });
        it('should generate deprecation middleware when policy defined', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: '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 = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const deprecationFile = result.files.find(f => f.path.includes('middleware/deprecation.ts'));
            expect(deprecationFile).toBeDefined();
            expect(deprecationFile.content).toContain('deprecate');
            expect(deprecationFile.content).toContain('checkDeprecation');
            expect(deprecationFile.content).toContain('Sunset');
        });
        it('should include sunset headers when enabled', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: '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 = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const deprecationFile = result.files.find(f => f.path.includes('middleware/deprecation.ts'));
            expect(deprecationFile.content).toContain("res.setHeader('Sunset'");
            expect(deprecationFile.content).toContain("res.setHeader('Deprecation'");
        });
    });
    describe('CORS Configuration', () => {
        it('should generate CORS config with origins', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    cors: {
                        enabled: true,
                        origins: ['https://example.com', 'https://app.example.com'],
                        methods: ['GET', 'POST', 'PUT'],
                        credentials: true,
                    },
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const corsFile = result.files.find(f => f.path.includes('config/cors.ts'));
            expect(corsFile).toBeDefined();
            expect(corsFile.content).toContain('corsConfig');
            expect(corsFile.content).toContain('https://example.com');
            expect(corsFile.content).toContain('https://app.example.com');
            expect(corsFile.content).toContain('credentials: true');
        });
        it('should generate CORS middleware', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    cors: {
                        enabled: true,
                    },
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const middlewareFile = result.files.find(f => f.path.includes('middleware/cors.ts'));
            expect(middlewareFile).toBeDefined();
            expect(middlewareFile.content).toContain('corsMiddleware');
            expect(middlewareFile.content).toContain('corsPreflightHandler');
        });
        it('should not generate CORS files when disabled', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    cors: {
                        enabled: false,
                    },
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const corsFile = result.files.find(f => f.path.includes('config/cors.ts'));
            expect(corsFile).toBeUndefined();
        });
    });
    describe('Middleware', () => {
        it('should generate validation middleware', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {},
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const validationFile = result.files.find(f => f.path.includes('middleware/validation.ts'));
            expect(validationFile).toBeDefined();
            expect(validationFile.content).toContain('validate');
            expect(validationFile.content).toContain('ZodSchema');
            expect(validationFile.content).toContain('VALIDATION_ERROR');
        });
        it('should generate response formatter', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {},
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const formatterFile = result.files.find(f => f.path.includes('middleware/response-formatter.ts'));
            expect(formatterFile).toBeDefined();
            expect(formatterFile.content).toContain('responseFormatter');
            expect(formatterFile.content).toContain('res.success');
            expect(formatterFile.content).toContain('res.error');
        });
        it('should generate middleware index when middleware defined', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    middleware: ['logger', 'requestId'],
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const indexFile = result.files.find(f => f.path.includes('middleware/index.ts'));
            expect(indexFile).toBeDefined();
            expect(indexFile.content).toContain('globalMiddleware');
        });
    });
    describe('API Types', () => {
        it('should generate comprehensive API types', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {},
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const typesFile = result.files.find(f => f.path.includes('types/api.ts'));
            expect(typesFile).toBeDefined();
            expect(typesFile.content).toContain('PaginationParams');
            expect(typesFile.content).toContain('ApiException');
            expect(typesFile.content).toContain('ValidationException');
            expect(typesFile.content).toContain('UnauthorizedException');
            expect(typesFile.content).toContain('NotFoundException');
        });
    });
    describe('App Setup', () => {
        it('should generate app setup with all features', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    base_path: '/api/v1',
                    documentation: {
                        format: 'openapi',
                        auto_generate: true,
                    },
                    versioning: {
                        strategy: 'url_path',
                    },
                    cors: {
                        enabled: true,
                    },
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const appFile = result.files.find(f => f.path.includes('app.ts'));
            expect(appFile).toBeDefined();
            expect(appFile.content).toContain('createApp');
            expect(appFile.content).toContain('setupSwagger');
            expect(appFile.content).toContain('corsMiddleware');
            expect(appFile.content).toContain('versioningMiddleware');
            expect(appFile.content).toContain('helmet()');
        });
        it('should include health check endpoint', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {},
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            const appFile = result.files.find(f => f.path.includes('app.ts'));
            expect(appFile.content).toContain("/health");
            expect(appFile.content).toContain('status: \'healthy\'');
        });
    });
    describe('Complete Configuration', () => {
        it('should generate all files for complete API configuration', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0.0', name: 'Test' },
                stack: { api: { type: 'backend', technology: 'typescript/express' } },
                api: {
                    base_path: '/api',
                    documentation: {
                        format: 'openapi',
                        auto_generate: true,
                        output: './docs/openapi.json',
                    },
                    versioning: {
                        strategy: 'url_path',
                        deprecation_policy: {
                            notice_period: '90_days',
                            sunset_headers: true,
                        },
                    },
                    cors: {
                        enabled: true,
                        origins: ['https://example.com'],
                        methods: ['GET', 'POST'],
                        credentials: true,
                    },
                    middleware: ['logger', 'requestId'],
                },
                stages: { local: { infrastructure: { databases: {}, services: {} } } },
            };
            const result = generator.generate({ workflow, outputDirectory: '/tmp/test' });
            // Check all expected files are generated
            expect(result.files.find(f => f.path.includes('config/api-docs.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('middleware/swagger.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('utils/swagger-generator.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('middleware/versioning.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('utils/version-router.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('middleware/deprecation.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('config/cors.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('middleware/cors.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('middleware/index.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('middleware/validation.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('middleware/response-formatter.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('types/api.ts'))).toBeDefined();
            expect(result.files.find(f => f.path.includes('app.ts'))).toBeDefined();
        });
    });
});
//# sourceMappingURL=enhanced-api-generator.test.js.map