"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const nock_1 = __importDefault(require("nock"));
const index_1 = __importDefault(require("../index"));
beforeEach(() => {
    nock_1.default.cleanAll();
});
const settings = {
    projectToken: 'test-api-key',
    apiSecret: 'test-proj-token',
    apiRegion: 'US'
};
const END_POINT = 'https://api.mixpanel.com';
const timestamp = '2024-10-25T15:21:15.449Z';
const testDestination = (0, actions_core_1.createTestIntegration)(index_1.default);
describe('MultiStatus', () => {
    describe('trackEvent', () => {
        it('should successfully handle a batch of events with complete success response from Mixpanel API', async () => {
            (0, nock_1.default)(END_POINT).post('/import?strict=1').reply(200, {
                code: 200,
                status: 'Ok',
                num_records_imported: 2
            });
            const mapping = {
                event: {
                    '@path': '$.event'
                }
            };
            const events = [
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' }),
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' })
            ];
            const features = { 'mixpanel-multistatus': true };
            const response = await testDestination.executeBatch('trackEvent', {
                events,
                mapping,
                settings,
                features
            });
            expect(response[0]).toMatchObject({
                status: 200,
                body: 'Ok'
            });
            expect(response[1]).toMatchObject({
                status: 200,
                body: 'Ok'
            });
        });
        it('should successfully handle a batch of events with partial success response from Mixpanel API', async () => {
            (0, nock_1.default)(END_POINT)
                .post('/import?strict=1')
                .reply(200, {
                code: 200,
                status: 'Ok',
                num_records_imported: 1,
                failed_records: [
                    {
                        index: 1,
                        insert_id: '13c0b661-f48b-51cd-ba54-97c5999169c0',
                        field: 'properties.time',
                        message: 'Payload validation error'
                    }
                ]
            });
            const mapping = {
                event: {
                    '@path': '$.event'
                }
            };
            const events = [
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' }),
                (0, actions_core_1.createTestEvent)({ timestamp })
            ];
            delete events[1].event;
            const features = { 'mixpanel-multistatus': true };
            const response = await testDestination.executeBatch('trackEvent', {
                events,
                mapping,
                settings,
                features
            });
            expect(response[0]).toMatchObject({
                status: 200,
                body: {}
            });
            expect(response[1]).toMatchObject({
                status: 400,
                errortype: 'PAYLOAD_VALIDATION_FAILED',
                errormessage: "The root value is missing the required field 'event'.",
                errorreporter: 'INTEGRATIONS'
            });
        });
        it('should successfully handle a batch of events with complete error response from Mixpanel API', async () => {
            (0, nock_1.default)(END_POINT).post('/import?strict=1').reply(200, {});
            const mapping = {
                event: {
                    '@path': '$.event'
                }
            };
            const events = [
                (0, actions_core_1.createTestEvent)({ timestamp }),
                (0, actions_core_1.createTestEvent)({ timestamp })
            ];
            delete events[0].event;
            delete events[1].event;
            const features = { 'mixpanel-multistatus': true };
            const response = await testDestination.executeBatch('trackEvent', {
                events,
                mapping,
                settings,
                features
            });
            expect(response[0]).toMatchObject({
                status: 400,
                errortype: 'PAYLOAD_VALIDATION_FAILED',
                errormessage: "The root value is missing the required field 'event'.",
                errorreporter: 'INTEGRATIONS'
            });
            expect(response[1]).toMatchObject({
                status: 400,
                errortype: 'PAYLOAD_VALIDATION_FAILED',
                errormessage: "The root value is missing the required field 'event'.",
                errorreporter: 'INTEGRATIONS'
            });
        });
        it('should successfully handle a batch of events with fatal error response from Mixpanel API', async () => {
            const mockResponse = {
                code: 400,
                status: 'Bad Request',
                num_records_imported: 1,
                failed_records: [
                    {
                        index: 1,
                        insert_id: '13c0b661-f48b-51cd-ba54-97c5999169c0',
                        field: 'properties.time',
                        message: 'Payload validation error'
                    }
                ]
            };
            (0, nock_1.default)(END_POINT).post('/import?strict=1').reply(400, mockResponse);
            const mapping = {
                event: {
                    '@path': '$.event'
                }
            };
            const events = [
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' }),
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' })
            ];
            const features = { 'mixpanel-multistatus': true };
            const response = await testDestination.executeBatch('trackEvent', {
                events,
                mapping,
                settings,
                features
            });
            expect(response[0]).toMatchObject({
                status: 200,
                body: 'Bad Request',
                sent: {
                    event: 'Test event'
                }
            });
            expect(response[1]).toMatchObject({
                status: 400,
                errormessage: 'Payload validation error',
                errortype: 'BAD_REQUEST',
                errorreporter: 'DESTINATION'
            });
        });
        it('should successfully handle a batch of events with fatal error 401 response from Mixpanel API', async () => {
            const mockResponse = {
                code: 401,
                status: 'Invalid credentials',
                error: 'Unauthorized'
            };
            (0, nock_1.default)(END_POINT).post('/import?strict=1').reply(401, mockResponse);
            const mapping = {
                event: {
                    '@path': '$.event'
                }
            };
            const events = [
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' }),
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' })
            ];
            const features = { 'mixpanel-multistatus': true };
            const response = await testDestination.executeBatch('trackEvent', {
                events,
                mapping,
                settings,
                features
            });
            expect(response).toMatchObject([
                {
                    status: 401,
                    errormessage: 'Unauthorized',
                    errorreporter: 'DESTINATION'
                },
                {
                    status: 401,
                    errormessage: 'Unauthorized',
                    errorreporter: 'DESTINATION'
                }
            ]);
        });
    });
    describe('trackPurchase', () => {
        it('should successfully handle a batch of events with complete success response from Mixpanel API', async () => {
            (0, nock_1.default)(END_POINT).post('/import?strict=1').reply(200, {
                code: 200,
                status: 'Ok',
                num_records_imported: 2
            });
            const mapping = {
                event: {
                    '@path': '$.event'
                }
            };
            const events = [
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' }),
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' })
            ];
            const features = { 'mixpanel-multistatus': true };
            const response = await testDestination.executeBatch('trackPurchase', {
                events,
                mapping,
                settings,
                features
            });
            expect(response[0]).toMatchObject({
                status: 200,
                body: 'Ok'
            });
            expect(response[1]).toMatchObject({
                status: 200,
                body: 'Ok'
            });
        });
        it('should successfully handle a batch of events with partial success response from Mixpanel API', async () => {
            (0, nock_1.default)(END_POINT).post('/import?strict=1').reply(200, {
                code: 200,
                status: 'Ok',
                num_records_imported: 1
            });
            const mapping = {
                event: {
                    '@path': '$.event'
                }
            };
            const events = [
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' }),
                (0, actions_core_1.createTestEvent)({ timestamp })
            ];
            delete events[1].event;
            const features = { 'mixpanel-multistatus': true };
            const response = await testDestination.executeBatch('trackPurchase', {
                events,
                mapping,
                settings,
                features
            });
            expect(response[0]).toMatchObject({
                status: 200,
                body: 'Ok'
            });
            expect(response[1]).toMatchObject({
                status: 400,
                errortype: 'PAYLOAD_VALIDATION_FAILED',
                errormessage: "The root value is missing the required field 'event'.",
                errorreporter: 'INTEGRATIONS'
            });
        });
        it('should successfully handle a batch of events with complete error response from Mixpanel API', async () => {
            (0, nock_1.default)(END_POINT).post('/import?strict=1').reply(200, {});
            const mapping = {
                event: {
                    '@path': '$.event'
                }
            };
            const events = [
                (0, actions_core_1.createTestEvent)({ timestamp }),
                (0, actions_core_1.createTestEvent)({ timestamp })
            ];
            delete events[0].event;
            delete events[1].event;
            const features = { 'mixpanel-multistatus': true };
            const response = await testDestination.executeBatch('trackPurchase', {
                events,
                mapping,
                settings,
                features
            });
            expect(response[0]).toMatchObject({
                status: 400,
                errormessage: "The root value is missing the required field 'event'.",
                errorreporter: 'INTEGRATIONS'
            });
            expect(response[1]).toMatchObject({
                status: 400,
                errormessage: "The root value is missing the required field 'event'.",
                errorreporter: 'INTEGRATIONS'
            });
        });
        it('should successfully handle a batch of events with fatal error response from Mixpanel API', async () => {
            const mockResponse = {
                code: 400,
                status: 'Bad Request',
                num_records_imported: 0,
                failed_records: [
                    {
                        index: 0,
                        insert_id: '13c0b661-f48b-51cd-ba54-97c5999169c0',
                        field: 'properties.time',
                        message: 'Payload validation error'
                    },
                    {
                        index: 1,
                        insert_id: '13c0b661-f48b-51cd-ba54-97c5999169c0',
                        field: 'properties.time',
                        message: 'Payload validation error'
                    }
                ]
            };
            (0, nock_1.default)(END_POINT).post('/import?strict=1').reply(400, mockResponse);
            const mapping = {
                event: {
                    '@path': '$.event'
                }
            };
            const events = [
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' }),
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' })
            ];
            const features = { 'mixpanel-multistatus': true };
            const response = await testDestination.executeBatch('trackPurchase', {
                events,
                mapping,
                settings,
                features
            });
            expect(response).toMatchObject([
                {
                    status: 400,
                    errormessage: 'Payload validation error',
                    errorreporter: 'DESTINATION'
                },
                {
                    status: 400,
                    errormessage: 'Payload validation error',
                    errorreporter: 'DESTINATION'
                }
            ]);
        });
        it('should successfully handle a batch of events with fatal error 401 response from Mixpanel API', async () => {
            const mockResponse = {
                code: 401,
                status: 'Invalid credentials',
                error: 'Unauthorized'
            };
            (0, nock_1.default)(END_POINT).post('/import?strict=1').reply(401, mockResponse);
            const mapping = {
                event: {
                    '@path': '$.event'
                }
            };
            const events = [
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' }),
                (0, actions_core_1.createTestEvent)({ timestamp, event: 'Test event' })
            ];
            const features = { 'mixpanel-multistatus': true };
            const response = await testDestination.executeBatch('trackPurchase', {
                events,
                mapping,
                settings,
                features
            });
            expect(response).toMatchObject([
                {
                    status: 401,
                    errormessage: 'Unauthorized',
                    errorreporter: 'DESTINATION'
                },
                {
                    status: 401,
                    errormessage: 'Unauthorized',
                    errorreporter: 'DESTINATION'
                }
            ]);
        });
    });
});
//# sourceMappingURL=multistatus.test.js.map