"use strict";
/**
 * Tests for Event Types Generator
 */
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 path = __importStar(require("path"));
const event_types_generator_1 = require("./event-types-generator");
describe('Event Types Generator', () => {
    let baseWorkflow;
    let baseContext;
    const testOutputDir = '/tmp/test-output';
    beforeEach(() => {
        baseWorkflow = {
            workflow: {
                id: 'test-app',
                version: '1.0.0',
                name: 'Test Application',
                description: 'Test application for event types',
            },
            models: {},
            stack: {},
        };
        baseContext = {
            workflow: baseWorkflow,
            extendedWorkflow: undefined,
            outputDirectory: testOutputDir,
        };
    });
    // ========================================================================
    // Generator Metadata Tests
    // ========================================================================
    describe('Generator Metadata', () => {
        it('should have correct generator properties', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                        orm: 'prisma',
                    },
                },
                events: {
                    user_created: {
                        description: 'User account created',
                        payload: {
                            user_id: 'string',
                            email: 'string',
                        },
                    },
                },
            };
            const context = {
                ...baseContext,
                extendedWorkflow,
            };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result).toBeDefined();
            expect(result.files).toBeDefined();
            expect(Array.isArray(result.files)).toBe(true);
        });
    });
    // ========================================================================
    // Configuration Detection Tests
    // ========================================================================
    describe('Configuration Detection', () => {
        it('should skip generation when no extended workflow', async () => {
            const result = await (0, event_types_generator_1.generateEventTypes)(baseContext);
            expect(result.files).toHaveLength(0);
        });
        it('should skip generation when no events defined', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
            };
            const context = {
                ...baseContext,
                extendedWorkflow,
            };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files).toHaveLength(0);
        });
        it('should skip generation when events is empty object', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {},
            };
            const context = {
                ...baseContext,
                extendedWorkflow,
            };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files).toHaveLength(0);
        });
        it('should generate types when events are defined', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_created: {
                        description: 'User account created',
                        payload: {
                            user_id: 'string',
                            email: 'string',
                        },
                    },
                },
            };
            const context = {
                ...baseContext,
                extendedWorkflow,
            };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files).toHaveLength(1);
            expect(path.isAbsolute(result.files[0].path)).toBe(true);
            expect(result.files[0].path).toBe(path.join(testOutputDir, 'backend', 'src', 'types', 'events.ts'));
        });
    });
    // ========================================================================
    // File Generation Tests
    // ========================================================================
    describe('File Generation', () => {
        it('should generate events.ts with correct path', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_created: {
                        description: 'User created event',
                        payload: {
                            user_id: 'string',
                        },
                    },
                },
            };
            const context = {
                ...baseContext,
                extendedWorkflow,
            };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files).toHaveLength(1);
            expect(path.isAbsolute(result.files[0].path)).toBe(true);
            expect(result.files[0].path).toBe(path.join(testOutputDir, 'backend', 'src', 'types', 'events.ts'));
        });
        it('should include DO NOT EDIT warning', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_created: {
                        description: 'User created',
                        payload: {
                            user_id: 'string',
                        },
                    },
                },
            };
            const context = {
                ...baseContext,
                extendedWorkflow,
            };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files[0].content).toContain('DO NOT EDIT MANUALLY');
            expect(result.files[0].content).toContain('Auto-generated from workflow events');
        });
        it('should generate payload type interfaces', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_created: {
                        description: 'User created',
                        payload: {
                            user_id: 'string',
                            email: 'string',
                            name: 'string',
                        },
                    },
                },
            };
            const context = {
                ...baseContext,
                extendedWorkflow,
            };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            const content = result.files[0].content;
            expect(content).toContain('export interface UserCreatedPayload');
            expect(content).toContain('user_id: string;');
            expect(content).toContain('email: string;');
            expect(content).toContain('name: string;');
        });
        it('should generate event interfaces with metadata fields', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_created: {
                        description: 'User created event',
                        payload: {
                            user_id: 'string',
                        },
                    },
                },
            };
            const context = {
                ...baseContext,
                extendedWorkflow,
            };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            const content = result.files[0].content;
            expect(content).toContain('export interface UserCreatedEvent');
            expect(content).toContain("type: 'user_created';");
            expect(content).toContain('payload: UserCreatedPayload;');
            expect(content).toContain('timestamp: Date | string;');
            expect(content).toContain('correlationId?: string;');
            expect(content).toContain('userId?: string;');
        });
        it('should generate event type union', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_created: {
                        description: 'User created',
                        payload: { user_id: 'string' },
                    },
                    user_updated: {
                        description: 'User updated',
                        payload: { user_id: 'string' },
                    },
                },
            };
            const context = {
                ...baseContext,
                extendedWorkflow,
            };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            const content = result.files[0].content;
            expect(content).toContain('export type Event =');
            expect(content).toContain('UserCreatedEvent | UserUpdatedEvent');
            expect(content).toContain('export type EventType =');
            expect(content).toContain("'user_created' | 'user_updated'");
        });
        it('should generate event payload map', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_created: {
                        description: 'User created',
                        payload: { user_id: 'string' },
                    },
                    task_assigned: {
                        description: 'Task assigned',
                        payload: { task_id: 'string', user_id: 'string' },
                    },
                },
            };
            const context = {
                ...baseContext,
                extendedWorkflow,
            };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            const content = result.files[0].content;
            expect(content).toContain('export type EventPayloadMap =');
            expect(content).toContain("'user_created': UserCreatedPayload;");
            expect(content).toContain("'task_assigned': TaskAssignedPayload;");
        });
        it('should generate event registry for runtime validation', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_created: {
                        description: 'User account created',
                        payload: {
                            user_id: 'string',
                            email: 'string',
                        },
                    },
                },
            };
            const context = {
                ...baseContext,
                extendedWorkflow,
            };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            const content = result.files[0].content;
            expect(content).toContain('export const EVENT_REGISTRY =');
            expect(content).toContain("'user_created':");
            expect(content).toContain("description: 'User account created'");
            expect(content).toContain('payload:');
            expect(content).toContain("user_id: 'string'");
            expect(content).toContain("email: 'string'");
        });
        it('should generate type guard functions', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_created: {
                        description: 'User created',
                        payload: { user_id: 'string' },
                    },
                },
            };
            const context = {
                ...baseContext,
                extendedWorkflow,
            };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            const content = result.files[0].content;
            expect(content).toContain('export function isValidEventType');
            expect(content).toContain('eventType is EventType');
            expect(content).toContain('export function validateEventPayload');
        });
    });
    // ========================================================================
    // Type Mapping Tests
    // ========================================================================
    describe('Type Mapping', () => {
        it('should map string type correctly', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    test_event: {
                        description: 'Test event',
                        payload: {
                            text_field: 'string',
                        },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files[0].content).toContain('text_field: string;');
        });
        it('should map number type correctly', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    test_event: {
                        description: 'Test event',
                        payload: {
                            count: 'number',
                        },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files[0].content).toContain('count: number;');
        });
        it('should map boolean type correctly', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    test_event: {
                        description: 'Test event',
                        payload: {
                            is_active: 'boolean',
                        },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files[0].content).toContain('is_active: boolean;');
        });
        it('should map date type correctly', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    test_event: {
                        description: 'Test event',
                        payload: {
                            created_at: 'date',
                        },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files[0].content).toContain('created_at: Date | string;');
        });
        it('should map unknown type for unsupported types', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    test_event: {
                        description: 'Test event',
                        payload: {
                            custom_field: 'custom_type',
                        },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files[0].content).toContain('custom_field: unknown;');
        });
    });
    // ========================================================================
    // Naming Convention Tests
    // ========================================================================
    describe('Naming Conventions', () => {
        it('should convert snake_case event names to PascalCase', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_account_created: {
                        description: 'User account created',
                        payload: { user_id: 'string' },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files[0].content).toContain('UserAccountCreatedPayload');
            expect(result.files[0].content).toContain('UserAccountCreatedEvent');
        });
        it('should convert kebab-case event names to PascalCase', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    'task-status-changed': {
                        description: 'Task status changed',
                        payload: { task_id: 'string' },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files[0].content).toContain('TaskStatusChangedPayload');
            expect(result.files[0].content).toContain('TaskStatusChangedEvent');
        });
    });
    // ========================================================================
    // Multiple Events Tests
    // ========================================================================
    describe('Multiple Events', () => {
        it('should handle multiple event definitions', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_created: {
                        description: 'User created',
                        payload: { user_id: 'string', email: 'string' },
                    },
                    user_updated: {
                        description: 'User updated',
                        payload: { user_id: 'string' },
                    },
                    user_deleted: {
                        description: 'User deleted',
                        payload: { user_id: 'string' },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            const content = result.files[0].content;
            expect(content).toContain('UserCreatedPayload');
            expect(content).toContain('UserUpdatedPayload');
            expect(content).toContain('UserDeletedPayload');
            expect(content).toContain('UserCreatedEvent');
            expect(content).toContain('UserUpdatedEvent');
            expect(content).toContain('UserDeletedEvent');
        });
        it('should generate complete type union for multiple events', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    project_created: {
                        description: 'Project created',
                        payload: { project_id: 'string' },
                    },
                    task_assigned: {
                        description: 'Task assigned',
                        payload: { task_id: 'string', user_id: 'string' },
                    },
                    comment_added: {
                        description: 'Comment added',
                        payload: { comment_id: 'string', task_id: 'string' },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            const content = result.files[0].content;
            expect(content).toContain('ProjectCreatedEvent | TaskAssignedEvent | CommentAddedEvent');
            expect(content).toContain("'project_created' | 'task_assigned' | 'comment_added'");
        });
    });
    // ========================================================================
    // Code Quality Tests
    // ========================================================================
    describe('Code Quality', () => {
        it('should generate syntactically valid TypeScript', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_created: {
                        description: 'User created',
                        payload: {
                            user_id: 'string',
                            email: 'string',
                            name: 'string',
                        },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            // Check for basic TypeScript syntax
            expect(result.files[0].content).toContain('export interface');
            expect(result.files[0].content).toContain('export type');
            expect(result.files[0].content).toContain('export const');
            expect(result.files[0].content).toContain('export function');
            // No syntax errors that would be obvious
            expect(result.files[0].content).not.toContain('undefined;');
            expect(result.files[0].content).not.toContain('[object Object]');
        });
        it('should include event descriptions as JSDoc comments', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    task_completed: {
                        description: 'Task marked as completed by user',
                        payload: {
                            task_id: 'string',
                            completed_by: 'string',
                        },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files[0].content).toContain('/**');
            expect(result.files[0].content).toContain('* Task marked as completed by user');
            expect(result.files[0].content).toContain('*/');
        });
        it('should organize code into logical sections', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_created: {
                        description: 'User created',
                        payload: { user_id: 'string' },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            const content = result.files[0].content;
            expect(content).toContain('// Event Payload Type Mapping');
            expect(content).toContain('// Event Interfaces');
            expect(content).toContain('// Event Type Union');
            expect(content).toContain('// Event Registry');
            expect(content).toContain('// Type Guards');
        });
    });
    // ========================================================================
    // Edge Cases
    // ========================================================================
    describe('Edge Cases', () => {
        it('should handle event with single field payload', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    ping: {
                        description: 'Ping event',
                        payload: {
                            timestamp: 'string',
                        },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files).toHaveLength(1);
            expect(result.files[0].content).toContain('PingPayload');
            expect(result.files[0].content).toContain('timestamp: string;');
        });
        it('should handle event with many fields', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    user_profile_updated: {
                        description: 'User profile updated',
                        payload: {
                            user_id: 'string',
                            name: 'string',
                            email: 'string',
                            phone: 'string',
                            address: 'string',
                            city: 'string',
                            country: 'string',
                            postal_code: 'string',
                        },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files[0].content).toContain('user_id: string;');
            expect(result.files[0].content).toContain('name: string;');
            expect(result.files[0].content).toContain('email: string;');
            expect(result.files[0].content).toContain('postal_code: string;');
        });
        it('should handle event names with special characters', async () => {
            const extendedWorkflow = {
                ...baseWorkflow.workflow,
                technology: {
                    backend: {
                        framework: 'express',
                        language: 'typescript',
                    },
                },
                events: {
                    'user-account_created': {
                        description: 'Mixed naming convention',
                        payload: { user_id: 'string' },
                    },
                },
            };
            const context = { ...baseContext, extendedWorkflow };
            const result = await (0, event_types_generator_1.generateEventTypes)(context);
            expect(result.files[0].content).toContain('UserAccountCreatedPayload');
            expect(result.files[0].content).toContain('UserAccountCreatedEvent');
        });
    });
});
//# sourceMappingURL=event-types-generator.test.js.map