"use strict";
/**
 * Tests for DrizzleGenerator
 */
Object.defineProperty(exports, "__esModule", { value: true });
const drizzle_generator_1 = require("./drizzle-generator");
const workflow_engine_1 = require("../../engine/workflow-engine");
describe('DrizzleGenerator', () => {
    let generator;
    beforeEach(() => {
        generator = new drizzle_generator_1.DrizzleGenerator();
    });
    it('should have correct metadata', () => {
        expect(generator.id).toBe('drizzle-database');
        expect(generator.name).toBe('Drizzle ORM Database Generator');
        expect(generator.database).toBe('drizzle');
    });
    it('should generate empty result for workflow without drizzle database', () => {
        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',
        });
        expect(result.files).toHaveLength(0);
    });
    it('should generate Drizzle schema with PostgreSQL', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                database: {
                    type: 'database',
                    technology: 'postgresql/drizzle',
                    tables: [
                        {
                            name: 'users',
                            required_fields: [
                                {
                                    name: 'id',
                                    type: 'uuid',
                                    primary_key: true,
                                },
                                {
                                    name: 'email',
                                    type: 'string',
                                    unique: true,
                                },
                                {
                                    name: 'name',
                                    type: 'string',
                                },
                            ],
                        },
                    ],
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        expect(result.files.length).toBeGreaterThan(0);
        // Check schema file
        const schemaFile = result.files.find(f => f.path.includes('schema.ts'));
        expect(schemaFile).toBeDefined();
        expect(schemaFile?.content).toContain("from 'drizzle-orm/pg-core'");
        expect(schemaFile?.content).toContain('export const users = pgTable');
        expect(schemaFile?.content).toContain("uuid('id').primaryKey()");
        expect(schemaFile?.content).toContain("varchar('email').unique()");
        expect(schemaFile?.content).toContain("varchar('name')");
    });
    it('should generate database client', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                database: {
                    type: 'database',
                    technology: 'postgresql/drizzle',
                    tables: [],
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const dbFile = result.files.find(f => f.path.includes('db.ts'));
        expect(dbFile).toBeDefined();
        expect(dbFile?.content).toContain("from 'drizzle-orm/node-postgres'");
        expect(dbFile?.content).toContain("from 'pg'");
        expect(dbFile?.content).toContain('export const db');
    });
    it('should generate package.json with Drizzle dependencies', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                database: {
                    type: 'database',
                    technology: 'postgresql/drizzle',
                    tables: [],
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const pkgFile = result.files.find(f => f.path.includes('package.json'));
        expect(pkgFile).toBeDefined();
        const pkg = JSON.parse(pkgFile?.content || '{}');
        expect(pkg.dependencies['drizzle-orm']).toBeDefined();
        expect(pkg.dependencies['pg']).toBeDefined();
        expect(pkg.devDependencies['drizzle-kit']).toBeDefined();
        expect(pkg.scripts['db:generate']).toBe('drizzle-kit generate:pg');
    });
    it('should generate Drizzle config file', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                database: {
                    type: 'database',
                    technology: 'postgresql/drizzle',
                    tables: [],
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const configFile = result.files.find(f => f.path.includes('drizzle.config.ts'));
        expect(configFile).toBeDefined();
        expect(configFile?.content).toContain('schema');
        expect(configFile?.content).toContain('driver');
    });
    it('should generate type exports', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                database: {
                    type: 'database',
                    technology: 'postgresql/drizzle',
                    tables: [
                        {
                            name: 'users',
                            required_fields: [
                                {
                                    name: 'id',
                                    type: 'uuid',
                                    primary_key: true,
                                },
                            ],
                        },
                    ],
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const schemaFile = result.files.find(f => f.path.includes('schema.ts'));
        expect(schemaFile?.content).toContain('export type Users');
        expect(schemaFile?.content).toContain('export type NewUsers');
        expect(schemaFile?.content).toContain('$inferSelect');
        expect(schemaFile?.content).toContain('$inferInsert');
    });
    it('should support MySQL', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                database: {
                    type: 'database',
                    technology: 'mysql/drizzle',
                    tables: [
                        {
                            name: 'users',
                            required_fields: [
                                {
                                    name: 'id',
                                    type: 'integer',
                                    primary_key: true,
                                },
                            ],
                        },
                    ],
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const schemaFile = result.files.find(f => f.path.includes('schema.ts'));
        expect(schemaFile?.content).toContain('mysql-core');
        expect(schemaFile?.content).toContain('mysqlTable');
        const pkg = result.files.find(f => f.path.includes('package.json'));
        const pkgJson = JSON.parse(pkg?.content || '{}');
        expect(pkgJson.dependencies['mysql2']).toBeDefined();
    });
    it('should not overwrite package.json', () => {
        const workflow = {
            workflow: {
                id: 'test',
                name: 'Test',
                version: '1.0.0',
                description: 'Test',
            },
            stack: {
                database: {
                    type: 'database',
                    technology: 'postgresql/drizzle',
                    tables: [],
                },
            },
        };
        const engine = new workflow_engine_1.WorkflowEngine();
        const executionPlan = engine.analyze(workflow);
        const result = generator.generate({
            workflow: workflow,
            executionPlan,
            outputDirectory: '/test',
        });
        const pkgFile = result.files.find(f => f.path.includes('package.json'));
        expect(pkgFile?.overwrite).toBe(false);
    });
});
//# sourceMappingURL=drizzle-generator.test.js.map