"use strict";
/**
 * Tests for DockerComposeGenerator
 */
Object.defineProperty(exports, "__esModule", { value: true });
const docker_compose_generator_1 = require("./docker-compose-generator");
const workflow_engine_1 = require("../../engine/workflow-engine");
describe('DockerComposeGenerator', () => {
    let generator;
    beforeEach(() => {
        generator = new docker_compose_generator_1.DockerComposeGenerator();
    });
    it('should have correct metadata', () => {
        expect(generator.id).toBe('docker-compose');
        expect(generator.name).toBe('Docker Compose Generator');
    });
    it('should generate docker-compose.yml', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                'web-app': {
                    type: 'frontend',
                    technology: 'react',
                    dependsOn: ['api'],
                },
                api: {
                    type: 'backend',
                    technology: 'typescript/express',
                    dependsOn: ['database'],
                },
                database: {
                    type: 'database',
                    technology: 'postgresql',
                },
            },
            environment: {
                development: {
                    'web-app': {
                        VITE_API_URL: 'http://localhost:3000',
                    },
                    api: {
                        JWT_SECRET: 'test-secret',
                        DATABASE_URL: 'postgresql://localhost:5432/test',
                    },
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const composeFile = result.files.find(f => f.path.includes('docker-compose.yml'));
        expect(composeFile).toBeDefined();
        expect(composeFile?.content).toContain("version: '3.8'");
        expect(composeFile?.content).toContain('services:');
        expect(composeFile?.content).toContain('web-app:');
        expect(composeFile?.content).toContain('api:');
        expect(composeFile?.content).toContain('database:');
    });
    it('should generate Dockerfile for frontend', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                'web-app': {
                    type: 'frontend',
                    technology: 'react',
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const dockerfile = result.files.find(f => f.path.includes('web-app/Dockerfile'));
        expect(dockerfile).toBeDefined();
        expect(dockerfile?.content).toContain('FROM node:20-alpine AS builder');
        expect(dockerfile?.content).toContain('FROM nginx:alpine');
        expect(dockerfile?.content).toContain('npm ci');
        expect(dockerfile?.content).toContain('npm run build');
    });
    it('should generate Dockerfile for backend', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                api: {
                    type: 'backend',
                    technology: 'typescript/express',
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const dockerfile = result.files.find(f => f.path.includes('api/Dockerfile'));
        expect(dockerfile).toBeDefined();
        expect(dockerfile?.content).toContain('FROM node:20-alpine AS builder');
        expect(dockerfile?.content).toContain('npm ci');
        expect(dockerfile?.content).toContain('npm run build');
        expect(dockerfile?.content).toContain('EXPOSE 3000');
    });
    it('should generate nginx configuration', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                'web-app': {
                    type: 'frontend',
                    technology: 'react',
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const nginxConf = result.files.find(f => f.path.includes('nginx.conf'));
        expect(nginxConf).toBeDefined();
        expect(nginxConf?.content).toContain('server {');
        expect(nginxConf?.content).toContain('listen 80;');
        expect(nginxConf?.content).toContain('try_files $uri $uri/ /index.html');
        expect(nginxConf?.content).toContain('gzip on');
    });
    it('should generate .dockerignore files', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                'web-app': {
                    type: 'frontend',
                    technology: 'react',
                },
                api: {
                    type: 'backend',
                    technology: 'typescript/express',
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const frontendIgnore = result.files.find(f => f.path.includes('web-app/.dockerignore'));
        const backendIgnore = result.files.find(f => f.path.includes('api/.dockerignore'));
        expect(frontendIgnore).toBeDefined();
        expect(backendIgnore).toBeDefined();
        expect(frontendIgnore?.content).toContain('node_modules');
        expect(frontendIgnore?.content).toContain('.env');
    });
    it('should generate startup script', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                'web-app': {
                    type: 'frontend',
                    technology: 'react',
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const startScript = result.files.find(f => f.path.includes('start.sh'));
        expect(startScript).toBeDefined();
        expect(startScript?.content).toContain('#!/bin/bash');
        expect(startScript?.content).toContain('docker-compose');
        expect(startScript?.content).toContain('Starting services');
    });
    it('should configure database services correctly', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                database: {
                    type: 'database',
                    technology: 'postgresql',
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const composeFile = result.files.find(f => f.path.includes('docker-compose.yml'));
        expect(composeFile?.content).toContain('postgres:15-alpine');
        expect(composeFile?.content).toContain('POSTGRES_DB');
        expect(composeFile?.content).toContain('database_data');
        expect(composeFile?.content).toContain('healthcheck');
    });
    it('should not overwrite Dockerfiles', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                'web-app': {
                    type: 'frontend',
                    technology: 'react',
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const dockerfile = result.files.find(f => f.path.includes('Dockerfile'));
        expect(dockerfile?.overwrite).toBe(false);
    });
});
//# sourceMappingURL=docker-compose-generator.test.js.map