"use strict";
/**
 * Tests for Enhanced Migrations Generator
 */
Object.defineProperty(exports, "__esModule", { value: true });
const enhanced_migrations_generator_1 = require("./enhanced-migrations-generator");
describe('EnhancedMigrationsGenerator', () => {
    let generator;
    beforeEach(() => {
        generator = new enhanced_migrations_generator_1.EnhancedMigrationsGenerator();
    });
    describe('Configuration Generation', () => {
        it('should generate migration configuration', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'prisma_migrate',
                    auto_generate: true,
                    version_control: {
                        naming: 'timestamp',
                    },
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            expect(result.files.length).toBeGreaterThan(0);
            const configFile = result.files.find(f => f.path.includes('config/migrations.ts'));
            expect(configFile).toBeDefined();
            expect(configFile.content).toContain('strategy: \'prisma_migrate\'');
            expect(configFile.content).toContain('autoGenerate: true');
            expect(configFile.content).toContain('naming: \'timestamp\'');
        });
        it('should use default values when not specified', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {},
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const configFile = result.files.find(f => f.path.includes('config/migrations.ts'));
            expect(configFile).toBeDefined();
            expect(configFile.content).toContain('strategy: \'prisma_migrate\'');
            expect(configFile.content).toContain('naming: \'timestamp\'');
        });
    });
    describe('Prisma Migrations', () => {
        it('should generate Prisma migration files', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'prisma_migrate',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const prismaFile = result.files.find(f => f.path.includes('prisma-migrations.ts'));
            expect(prismaFile).toBeDefined();
            expect(prismaFile.content).toContain('PrismaMigrationRunner');
            expect(prismaFile.content).toContain('migrate');
            expect(prismaFile.content).toContain('generateMigration');
            expect(prismaFile.content).toContain('npx prisma migrate');
        });
    });
    describe('Drizzle Migrations', () => {
        it('should generate Drizzle migration files', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'drizzle',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const drizzleFile = result.files.find(f => f.path.includes('drizzle-migrations.ts'));
            expect(drizzleFile).toBeDefined();
            expect(drizzleFile.content).toContain('DrizzleMigrationRunner');
            expect(drizzleFile.content).toContain('drizzle-orm');
        });
        it('should generate drizzle config file', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'drizzle',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const configFile = result.files.find(f => f.path.includes('drizzle.config.ts'));
            expect(configFile).toBeDefined();
            expect(configFile.content).toContain('drizzle-kit');
        });
    });
    describe('TypeORM Migrations', () => {
        it('should generate TypeORM migration files', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'typeorm',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const typeormFile = result.files.find(f => f.path.includes('typeorm-migrations.ts'));
            expect(typeormFile).toBeDefined();
            expect(typeormFile.content).toContain('TypeORMMigrationRunner');
            expect(typeormFile.content).toContain('DataSource');
            expect(typeormFile.content).toContain('runMigrations');
            expect(typeormFile.content).toContain('undoLastMigration');
        });
    });
    describe('Knex Migrations', () => {
        it('should generate Knex migration files', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'knex',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const knexFile = result.files.find(f => f.path.includes('knex-migrations.ts'));
            expect(knexFile).toBeDefined();
            expect(knexFile.content).toContain('KnexMigrationRunner');
            expect(knexFile.content).toContain('migrate.latest');
            expect(knexFile.content).toContain('migrate.rollback');
        });
        it('should generate knexfile configuration', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'knex',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const knexfile = result.files.find(f => f.path.includes('knexfile.ts'));
            expect(knexfile).toBeDefined();
            expect(knexfile.content).toContain('development');
            expect(knexfile.content).toContain('staging');
            expect(knexfile.content).toContain('production');
        });
    });
    describe('Custom Migrations', () => {
        it('should generate custom migration framework', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'custom',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const customFile = result.files.find(f => f.path.includes('custom-migrations.ts'));
            expect(customFile).toBeDefined();
            expect(customFile.content).toContain('CustomMigrationRunner');
            expect(customFile.content).toContain('loadMigrations');
        });
        it('should generate migration template', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'custom',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const templateFile = result.files.find(f => f.path.includes('example_migration.ts.template'));
            expect(templateFile).toBeDefined();
            expect(templateFile.content).toContain('async up()');
            expect(templateFile.content).toContain('async down()');
        });
    });
    describe('Common Utilities', () => {
        it('should generate migration runner', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'prisma_migrate',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const runnerFile = result.files.find(f => f.path.includes('migration-runner.ts'));
            expect(runnerFile).toBeDefined();
            expect(runnerFile.content).toContain('MigrationRunner');
            expect(runnerFile.content).toContain('async run()');
            expect(runnerFile.content).toContain('async rollback()');
            expect(runnerFile.content).toContain('async status()');
        });
        it('should generate migration validator', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'prisma_migrate',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const validatorFile = result.files.find(f => f.path.includes('migration-validator.ts'));
            expect(validatorFile).toBeDefined();
            expect(validatorFile.content).toContain('MigrationValidator');
            expect(validatorFile.content).toContain('async validate()');
            expect(validatorFile.content).toContain('validateFileName');
        });
        it('should generate data migration template', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'prisma_migrate',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const dataFile = result.files.find(f => f.path.includes('data_migration_template'));
            expect(dataFile).toBeDefined();
            expect(dataFile.content).toContain('Data Migration Template');
        });
        it('should generate rollback script', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'prisma_migrate',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const rollbackFile = result.files.find(f => f.path.includes('scripts/rollback.ts'));
            expect(rollbackFile).toBeDefined();
            expect(rollbackFile.content).toContain('migrationRunner.rollback()');
            expect(rollbackFile.content).toContain('production');
        });
        it('should generate migration CLI', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'prisma_migrate',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const cliFile = result.files.find(f => f.path.includes('scripts/migrate.ts'));
            expect(cliFile).toBeDefined();
            expect(cliFile.content).toContain('#!/usr/bin/env node');
            expect(cliFile.content).toContain('commander');
            expect(cliFile.content).toContain('.command(\'up\')');
            expect(cliFile.content).toContain('.command(\'down\')');
            expect(cliFile.content).toContain('.command(\'status\')');
            expect(cliFile.content).toContain('.command(\'validate\')');
        });
    });
    describe('Edge Cases', () => {
        it('should return empty array when migrations not configured', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            expect(result.files).toEqual([]);
        });
        it('should handle all migration strategies', () => {
            const strategies = ['prisma_migrate', 'drizzle', 'typeorm', 'knex', 'custom'];
            strategies.forEach(strategy => {
                const workflow = {
                    workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                    stack: {},
                    migrations: {
                        strategy: strategy,
                    },
                };
                const context = {
                    workflow,
                    outputDirectory: '/tmp/test',
                    executionPlan: { components: [], services: [] },
                };
                const result = generator.generate(context);
                expect(result.files.length).toBeGreaterThan(0);
                const runnerFile = result.files.find(f => f.path.includes('migration-runner.ts'));
                expect(runnerFile).toBeDefined();
            });
        });
    });
    describe('File Generation', () => {
        it('should generate all expected files for Prisma', () => {
            const workflow = {
                workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
                stack: {},
                migrations: {
                    strategy: 'prisma_migrate',
                },
            };
            const context = {
                workflow,
                outputDirectory: '/tmp/test',
                executionPlan: { components: [], services: [] },
            };
            const result = generator.generate(context);
            const expectedFiles = [
                'config/migrations.ts',
                'prisma-migrations.ts',
                'migration-runner.ts',
                'migration-validator.ts',
                'data_migration_template',
                'scripts/rollback.ts',
                'scripts/migrate.ts',
            ];
            expectedFiles.forEach(file => {
                const found = result.files.find(f => f.path.includes(file));
                expect(found).toBeDefined();
            });
        });
    });
});
//# sourceMappingURL=enhanced-migrations-generator.test.js.map