"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testing_1 = require("@nestjs/testing");
const webhooks_controller_1 = require("../webhooks.controller");
const services_1 = require("../services");
describe('WebhooksController', () => {
    let controller;
    let service;
    const mockRequestUser = {
        id: 'user-123',
        tenant_id: 'tenant-123',
        email: 'test@example.com',
        role: 'admin',
    };
    const mockWebhook = {
        id: 'webhook-123',
        tenant_id: 'tenant-123',
        name: 'Test Webhook',
        url: 'https://example.com/webhook',
        events: ['user.created', 'user.updated'],
        is_active: true,
        created_at: new Date('2026-01-01'),
    };
    const mockDelivery = {
        id: 'delivery-123',
        webhook_id: 'webhook-123',
        event: 'user.created',
        payload: { user_id: 'user-456' },
        response_status: 200,
        response_body: '{"status":"ok"}',
        status: 'success',
        delivered_at: new Date('2026-01-01'),
    };
    const availableEvents = [
        { name: 'user.created', description: 'A new user was created' },
        { name: 'user.updated', description: 'A user was updated' },
        { name: 'user.deleted', description: 'A user was deleted' },
        { name: 'subscription.created', description: 'A new subscription was created' },
        { name: 'subscription.updated', description: 'A subscription was updated' },
        { name: 'invoice.paid', description: 'An invoice was paid' },
    ];
    beforeEach(async () => {
        const module = await testing_1.Test.createTestingModule({
            controllers: [webhooks_controller_1.WebhooksController],
            providers: [
                {
                    provide: services_1.WebhookService,
                    useValue: {
                        getAvailableEvents: jest.fn(),
                        findAll: jest.fn(),
                        findOne: jest.fn(),
                        create: jest.fn(),
                        update: jest.fn(),
                        remove: jest.fn(),
                        regenerateSecret: jest.fn(),
                        testWebhook: jest.fn(),
                        getDeliveries: jest.fn(),
                        retryDelivery: jest.fn(),
                    },
                },
            ],
        }).compile();
        controller = module.get(webhooks_controller_1.WebhooksController);
        service = module.get(services_1.WebhookService);
    });
    afterEach(() => {
        jest.clearAllMocks();
    });
    describe('getAvailableEvents', () => {
        it('should return available events', () => {
            service.getAvailableEvents.mockReturnValue(availableEvents);
            const result = controller.getAvailableEvents();
            expect(result).toEqual({ events: availableEvents });
            expect(service.getAvailableEvents).toHaveBeenCalled();
        });
    });
    describe('findAll', () => {
        it('should return all webhooks for tenant', async () => {
            service.findAll.mockResolvedValue([mockWebhook]);
            const result = await controller.findAll(mockRequestUser);
            expect(result).toEqual([mockWebhook]);
            expect(service.findAll).toHaveBeenCalledWith('tenant-123');
        });
    });
    describe('findOne', () => {
        it('should return a webhook by id', async () => {
            service.findOne.mockResolvedValue(mockWebhook);
            const result = await controller.findOne(mockRequestUser, 'webhook-123');
            expect(result).toEqual(mockWebhook);
            expect(service.findOne).toHaveBeenCalledWith('tenant-123', 'webhook-123');
        });
    });
    describe('create', () => {
        it('should create a webhook', async () => {
            const dto = {
                name: 'New Webhook',
                url: 'https://example.com/webhook',
                events: ['user.created'],
            };
            service.create.mockResolvedValue(mockWebhook);
            const result = await controller.create(mockRequestUser, dto);
            expect(result).toEqual(mockWebhook);
            expect(service.create).toHaveBeenCalledWith('tenant-123', 'user-123', dto);
        });
    });
    describe('update', () => {
        it('should update a webhook', async () => {
            const dto = { name: 'Updated Webhook', is_active: false };
            const updatedWebhook = { ...mockWebhook, ...dto };
            service.update.mockResolvedValue(updatedWebhook);
            const result = await controller.update(mockRequestUser, 'webhook-123', dto);
            expect(result.name).toBe('Updated Webhook');
            expect(service.update).toHaveBeenCalledWith('tenant-123', 'webhook-123', dto);
        });
    });
    describe('remove', () => {
        it('should delete a webhook', async () => {
            service.remove.mockResolvedValue(undefined);
            const result = await controller.remove(mockRequestUser, 'webhook-123');
            expect(result.message).toBe('Webhook deleted successfully');
            expect(service.remove).toHaveBeenCalledWith('tenant-123', 'webhook-123');
        });
    });
    describe('regenerateSecret', () => {
        it('should regenerate webhook secret', async () => {
            const newSecret = { secret: 'new-secret-key' };
            service.regenerateSecret.mockResolvedValue(newSecret);
            const result = await controller.regenerateSecret(mockRequestUser, 'webhook-123');
            expect(result).toEqual(newSecret);
            expect(service.regenerateSecret).toHaveBeenCalledWith('tenant-123', 'webhook-123');
        });
    });
    describe('test', () => {
        it('should test a webhook', async () => {
            const dto = { event: 'user.created', payload: { test: true } };
            service.testWebhook.mockResolvedValue(mockDelivery);
            const result = await controller.test(mockRequestUser, 'webhook-123', dto);
            expect(result).toEqual(mockDelivery);
            expect(service.testWebhook).toHaveBeenCalledWith('tenant-123', 'webhook-123', dto);
        });
    });
    describe('getDeliveries', () => {
        it('should return paginated deliveries', async () => {
            const deliveriesResult = { data: [mockDelivery], total: 1, page: 1, limit: 20 };
            service.getDeliveries.mockResolvedValue(deliveriesResult);
            const query = { page: 1, limit: 20 };
            const result = await controller.getDeliveries(mockRequestUser, 'webhook-123', query);
            expect(result).toEqual(deliveriesResult);
            expect(service.getDeliveries).toHaveBeenCalledWith('tenant-123', 'webhook-123', query);
        });
    });
    describe('retryDelivery', () => {
        it('should retry a failed delivery', async () => {
            service.retryDelivery.mockResolvedValue(mockDelivery);
            const result = await controller.retryDelivery(mockRequestUser, 'webhook-123', 'delivery-123');
            expect(result).toEqual(mockDelivery);
            expect(service.retryDelivery).toHaveBeenCalledWith('tenant-123', 'webhook-123', 'delivery-123');
        });
    });
});
//# sourceMappingURL=webhooks.controller.spec.js.map