"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var moment_1 = __importDefault(require("moment"));
var filters_1 = require("./filters");
var types_1 = require("../../activity-log/types");
var types_2 = require("../types");
describe('filters helpers tests', function () {
    describe('buildHistoryFilterString', function () {
        var defaultActivitiesFilterString = "equals(type, '".concat(types_1.ActivityTypes.ENTITY_CREATED, "') or") +
            " equals(type, '".concat(types_1.ActivityTypes.ENTITY_REMOVED, "') or") +
            " equals(type, '".concat(types_1.ActivityTypes.ENTITY_LOST_MERGE, "')");
        var defaultUserFilterString = "not equals(user, 'collaboration-service')";
        var defaultFilterString = "(".concat(defaultActivitiesFilterString, ") and (").concat(defaultUserFilterString, ")");
        afterEach(function () {
            jest.restoreAllMocks();
        });
        it('should return default string for empty filter', function () {
            expect((0, filters_1.buildHistoryFilterString)(null)).toBe(defaultFilterString);
            expect((0, filters_1.buildHistoryFilterString)({})).toBe(defaultFilterString);
            expect((0, filters_1.buildHistoryFilterString)({
                users: [],
                activities: [],
                attributes: [],
                dateRange: null
            })).toBe(defaultFilterString);
        });
        it('should build users filter correctly', function () {
            var filter = {
                users: ['polina.arsenteva', 'carina.alabanza', "lev.sok'olov"]
            };
            var expectedResult = "(equals(user, 'polina.arsenteva') or equals(user, 'carina.alabanza') or equals(user, 'lev.sok\\'olov'))";
            expect((0, filters_1.buildHistoryFilterString)(filter)).toEqual(expectedResult + ' and ' + defaultFilterString);
        });
        it('should build activities filter correctly', function () {
            var filter = {
                activities: [types_2.HistoryActivityType.MERGE, types_2.HistoryActivityType.UNMERGE, types_2.HistoryActivityType.UPDATE]
            };
            var activitiesFilterString = "equals(type, '".concat(types_1.ActivityTypes.ENTITIES_MERGED, "') or") +
                " equals(type, '".concat(types_1.ActivityTypes.ENTITIES_MERGED_MANUALLY, "') or") +
                " equals(type, '".concat(types_1.ActivityTypes.ENTITIES_MERGED_ON_THE_FLY, "') or") +
                " equals(type, '".concat(types_1.ActivityTypes.ENTITIES_SPLITTED, "') or") +
                " equals(type, '".concat(types_1.ActivityTypes.ENTITY_CHANGED, "')");
            var expectedResult = "(".concat(defaultActivitiesFilterString, " or ").concat(activitiesFilterString, ") and (").concat(defaultUserFilterString, ")");
            expect((0, filters_1.buildHistoryFilterString)(filter)).toEqual(expectedResult);
        });
        it('should build attributes filter correctly', function () {
            var filter = {
                attributes: [
                    {
                        label: 'Activity',
                        value: 'configuration/entityTypes/HCP/attributes/activitywith',
                        attrType: {
                            label: 'Activity',
                            name: 'activitywith',
                            description: 'Activity',
                            type: 'Reference',
                            hidden: false,
                            important: false,
                            system: false,
                            searchable: true,
                            relationshipLabelPattern: '{Name}',
                            uri: 'configuration/entityTypes/HCP/attributes/activitywith'
                        },
                        chipLabel: 'Activity',
                        level: 0
                    },
                    {
                        label: 'ActivityTest',
                        value: 'configuration/entityTypes/HCP/attributes/activitywithTest',
                        attrType: {
                            label: 'ActivityTest',
                            name: 'activitywithTest',
                            description: 'Activity',
                            type: 'Reference',
                            hidden: false,
                            important: false,
                            system: false,
                            searchable: true,
                            relationshipLabelPattern: '{Name}',
                            uri: 'configuration/entityTypes/HCP/attributes/activitywithTest'
                        },
                        chipLabel: 'Activity',
                        level: 0
                    }
                ]
            };
            var expectedResult = '(changes(configuration/entityTypes/HCP/attributes/activitywith)' +
                ' or changes(configuration/entityTypes/HCP/attributes/activitywithTest))';
            expect((0, filters_1.buildHistoryFilterString)(filter)).toEqual(expectedResult + ' and ' + defaultFilterString);
        });
        it('should build date range within filter correctly', function () {
            jest.spyOn(Date, 'now').mockReturnValue(1636703406900);
            var filter = {
                dateRange: {
                    type: 'within',
                    period: [4, 'months']
                }
            };
            var expectedTimestamp = moment_1.default.utc(1636703406900).subtract(4, 'months').valueOf();
            expect((0, filters_1.buildHistoryFilterString)(filter)).toEqual("(gte(timestamp, ".concat(expectedTimestamp, ")) and ").concat(defaultFilterString));
        });
        it('should build date range ago filter correctly', function () {
            jest.spyOn(Date, 'now').mockReturnValue(1636703406900);
            var filter = {
                dateRange: {
                    type: 'ago',
                    period: [4, 'months']
                }
            };
            var expectedTimestamp = moment_1.default.utc(1636703406900).subtract(4, 'months').valueOf();
            expect((0, filters_1.buildHistoryFilterString)(filter)).toEqual("(lte(timestamp, ".concat(expectedTimestamp, ")) and ").concat(defaultFilterString));
        });
        it('should build date range between filter correctly', function () {
            var filter = {
                dateRange: {
                    type: 'between',
                    period: [new Date(123), new Date(456)]
                }
            };
            var expectedResult = "(range(timestamp, 123, ".concat((0, moment_1.default)(filter.dateRange.period[1])
                .endOf('date')
                .valueOf(), "))");
            expect((0, filters_1.buildHistoryFilterString)(filter)).toEqual(expectedResult + ' and ' + defaultFilterString);
        });
        it('should build all filters correctly', function () {
            var filter = {
                users: ['polina.arsenteva'],
                activities: [types_2.HistoryActivityType.UNMERGE],
                attributes: [
                    {
                        label: 'Activity',
                        value: 'configuration/entityTypes/HCP/attributes/activitywith',
                        attrType: {
                            label: 'Activity',
                            name: 'activitywith',
                            description: 'Activity',
                            type: 'Reference',
                            hidden: false,
                            important: false,
                            system: false,
                            searchable: true,
                            relationshipLabelPattern: '{Name}',
                            uri: 'configuration/entityTypes/HCP/attributes/activitywith'
                        },
                        chipLabel: 'Activity',
                        level: 0
                    }
                ],
                dateRange: {
                    type: 'between',
                    period: [new Date(123), new Date(456)]
                }
            };
            var expectedResult = "(equals(user, 'polina.arsenteva'))" +
                " and (".concat(defaultActivitiesFilterString, " or equals(type, '").concat(types_1.ActivityTypes.ENTITIES_SPLITTED, "'))") +
                ' and (changes(configuration/entityTypes/HCP/attributes/activitywith))' +
                " and (range(timestamp, 123, ".concat((0, moment_1.default)(filter.dateRange.period[1]).endOf('date').valueOf(), "))") +
                " and (".concat(defaultUserFilterString, ")");
            expect((0, filters_1.buildHistoryFilterString)(filter)).toEqual(expectedResult);
        });
    });
    describe('test isDefaultFilter', function () {
        it('should return true if filter is default, but activities are in other order', function () {
            var filter = {
                activities: [types_2.HistoryActivityType.MERGE, types_2.HistoryActivityType.UPDATE, types_2.HistoryActivityType.UNMERGE]
            };
            expect((0, filters_1.isDefaultFilter)(filter)).toBeTruthy();
        });
        it('should return true if filter is default, but dateRange, users, attributes are empty', function () {
            var filter = {
                activities: [types_2.HistoryActivityType.MERGE, types_2.HistoryActivityType.UPDATE, types_2.HistoryActivityType.UNMERGE],
                users: [],
                attributes: []
            };
            expect((0, filters_1.isDefaultFilter)(filter)).toBeTruthy();
        });
        it('should return true if filter is default, but dateRange, users, attributes are null or undefined', function () {
            var filter = {
                dateRange: undefined,
                activities: [types_2.HistoryActivityType.MERGE, types_2.HistoryActivityType.UPDATE, types_2.HistoryActivityType.UNMERGE],
                users: undefined,
                attributes: undefined
            };
            expect((0, filters_1.isDefaultFilter)(filter)).toBeTruthy();
        });
        it('should return false if filter does not include all default activities', function () {
            var filter = {
                activities: [types_2.HistoryActivityType.MERGE, types_2.HistoryActivityType.UPDATE]
            };
            expect((0, filters_1.isDefaultFilter)(filter)).toBeFalsy();
        });
        it('should return false if filter include all default activities and other filter values', function () {
            var filter = {
                activities: [types_2.HistoryActivityType.MERGE, types_2.HistoryActivityType.UPDATE, types_2.HistoryActivityType.UNMERGE],
                users: ['user1']
            };
            expect((0, filters_1.isDefaultFilter)(filter)).toBeFalsy();
        });
    });
});
