<commit_before>import {AuthoringWorkspaceService} from 'apps/authoring/authoring/services/AuthoringWorkspaceService';
import {ISuperdeskGlobalConfig} from 'superdesk-api';
import {appConfig} from 'appConfig';

describe('monitoring', () => {
    beforeEach(window.module('superdesk.apps.monitoring'));
    beforeEach(window.module('superdesk.apps.vocabularies'));
    beforeEach(window.module('superdesk.apps.searchProviders'));
    beforeEach(window.module('superdesk.core.services.pageTitle'));
    beforeEach(window.module('superdesk.templates-cache'));
    beforeEach(window.module('superdesk.mocks'));

    beforeEach(inject(($templateCache) => {
        // change template not to require aggregate config but rather render single group
        $templateCache.put('scripts/apps/monitoring/views/monitoring-view.html',
            '<div id="group" sd-monitoring-group data-group="{type: \'stage\', _id: \'foo\'}"></div>');
    }));

    beforeEach(inject(($httpBackend) => {
        $httpBackend.whenGET(/api$/).respond({_links: {child: []}});
    }));

    it('can switch between list and swimlane view',
        inject(($controller, $rootScope, $q, preferencesService) => {
            const testConfig: Partial<ISuperdeskGlobalConfig> = {
                features: {
                    ...appConfig.features,
                    swimlane: {
                        defaultNumberOfColumns: 4,
                    },
                },
            };

            Object.assign(appConfig, testConfig);

            spyOn(preferencesService, 'update');
            spyOn(preferencesService, 'get').and.callFake((name) => {
                if (name === 'monitoring:view') {
                    return $q.when({view: 'list'});
                }

                return $q.when(null);
            });

            var scope = $rootScope.$new(),
                ctrl = $controller('Monitoring', {$scope: scope});

            expect(ctrl.hasSwimlaneView).toBe(1);
            $rootScope.$digest();

            expect(ctrl.viewColumn).toBe(false); // no swimlane
            // display all groups for list view, i.e. limitTo: null when no swimlane
            expect(ctrl.columnsLimit).toBe(null);

            // Switch to swimlane view, via switch view button or returning back to monitoring
            // view while swimlane view was already ON
            ctrl.switchViewColumn(true, true);

            expect(ctrl.viewColumn).toBe(true); // swimlane
            expect(ctrl.columnsLimit).toBe(4);
            expect(preferencesService.update).toHaveBeenCalledWith(
                {'monitoring:view:session': true},
                'monitoring:view:session',
            );

            // Switch back to list view
            ctrl.switchViewColumn(false);
            expect(ctrl.viewColumn).toBe(false);
            expect(ctrl.columnsLimit).toBe(null);
        }));

    it('can preview an item', inject(($controller, $rootScope) => {
        var scope = $rootScope.$new(),
            ctrl = $controller('Monitoring', {$scope: scope}),
            item = {};

        expect(ctrl.state['with-preview']).toBeFalsy();

        ctrl.preview(item);

        expect(ctrl.previewItem).toBe(item);
        expect(ctrl.state['with-preview']).toBeTruthy();

        ctrl.closePreview();
        expect(ctrl.previewItem).toBe(null);
        expect(ctrl.state['with-preview']).toBeFalsy();
    }));

    it('can hide history tabs when preview externalsource item', inject(($controller, $rootScope) => {
        var scope = $rootScope.$new(),
            ctrl = $controller('Monitoring', {$scope: scope}),
            item: any = {};

        expect(ctrl.state['with-preview']).toBeFalsy();

        ctrl.preview(item);

        expect(ctrl.previewItem).toBe(item);
        expect(ctrl.state['with-preview']).toBeTruthy();
        expect(ctrl.showHistoryTab).toBeTruthy();

        item._type = 'externalsource';
        ctrl.preview(item);

        expect(ctrl.previewItem).toBe(item);
        expect(ctrl.showHistoryTab).toBeFalsy();
    }));

    it('can edit item', inject(($controller, $rootScope, session) => {
        session.identity = {_id: 'foo'};
        var scope = $rootScope.$new(),
            ctrl = $controller('Monitoring', {$scope: scope}),
            item = {};

        expect(ctrl.state['with-authoring']).toBeFalsy();

        ctrl.edit(item);
        expect(ctrl.editItem).toBe(item);
        expect(ctrl.state['with-authoring']).toBeTruthy();
    }));

    describe('cards service', () => {
        beforeEach(inject((desks) => {
            desks.deskLookup = {1: {_id: 1, name: 'desk1', desk_type: 'authoring'}};
            desks.stageLookup = {123: {default_incoming: false}, 456: {default_incoming: true}};
        }));

        it('can get criteria for stage', inject((cards, session) => {
            session.identity = {_id: 'foo'};
            var card = {_id: '123'};
            var criteria = cards.criteria(card);

            expect(criteria.source.query.filtered.filter.and).toContain({
                term: {'task.stage': card._id},
            });

            criteria = cards.criteria(card, 'foo');
            expect(criteria.source.query.filtered.filter.and).toContain({
                query: {query_string: {query: 'foo', lenient: true}},
            });
        }));

        it('can get criteria for personal', inject((cards, session) => {
            var card = {type: 'personal'};

            session.identity = {_id: 'foo'};
            var criteria = cards.criteria(card);

            expect(criteria.source.query.filtered.filter.and).toContain({
                bool: {
                    must: {term: {original_creator: session.identity._id}},
                    must_not: {exists: {field: 'task.desk'}},
                },
            });
        }));

        it('can get criteria for saved search', inject((cards, session) => {
            session.identity = {_id: 'foo'};
            var card = {type: 'search', search: {filter: {query: {q: 'foo'}}}};
            var criteria = cards.criteria(card);

            expect(criteria.source.query.filtered.query.query_string.query).toBe('foo');
        }));

        it('can get criteria for spike desk', inject((cards, session) => {
            session.identity = {_id: 'foo'};
            var card: any = {type: 'spike'};
            var criteria = cards.criteria(card);

            expect(criteria.source.query.filtered.filter.and).toContain({
                term: {'task.desk': card._id},
            });
            expect(criteria.source.query.filtered.filter.and).toContain({
                term: {state: 'spiked'},
            });
        }));

        it('can get criteria for highlight', inject((cards, session) => {
            session.identity = {_id: 'foo'};
            var card = {type: 'highlights'};
            var queryParam = {highlight: '123'};
            var criteria = cards.criteria(card, null, queryParam);
<commit_msg>fix unit test broken by f0c23b9 (#3712)<commit_after>import {AuthoringWorkspaceService} from 'apps/authoring/authoring/services/AuthoringWorkspaceService';
import {ISuperdeskGlobalConfig} from 'superdesk-api';
import {appConfig} from 'appConfig';

describe('monitoring', () => {
    beforeEach(window.module('superdesk.apps.monitoring'));
    beforeEach(window.module('superdesk.apps.vocabularies'));
    beforeEach(window.module('superdesk.apps.searchProviders'));
    beforeEach(window.module('superdesk.core.services.pageTitle'));
    beforeEach(window.module('superdesk.templates-cache'));
    beforeEach(window.module('superdesk.mocks'));

    beforeEach(inject(($templateCache) => {
        // change template not to require aggregate config but rather render single group
        $templateCache.put('scripts/apps/monitoring/views/monitoring-view.html',
            '<div id="group" sd-monitoring-group data-group="{type: \'stage\', _id: \'foo\'}"></div>');
    }));

    beforeEach(inject(($httpBackend) => {
        $httpBackend.whenGET(/api$/).respond({_links: {child: []}});
    }));

    it('can switch between list and swimlane view',
        inject(($controller, $rootScope, $q, preferencesService) => {
            const testConfig: Partial<ISuperdeskGlobalConfig> = {
                features: {
                    ...appConfig.features,
                    swimlane: {
                        defaultNumberOfColumns: 4,
                    },
                },
            };

            Object.assign(appConfig, testConfig);

            spyOn(preferencesService, 'update');
            spyOn(preferencesService, 'get').and.callFake((name) => {
                if (name === 'monitoring:view') {
                    return $q.when({view: 'list'});
                }

                return $q.when(null);
            });

            var scope = $rootScope.$new(),
                ctrl = $controller('Monitoring', {$scope: scope});

            expect(ctrl.hasSwimlaneView).toBe(1);
            $rootScope.$digest();

            expect(ctrl.viewColumn).toBe(false); // no swimlane
            // display all groups for list view, i.e. limitTo: null when no swimlane
            expect(ctrl.columnsLimit).toBe(null);

            // Switch to swimlane view, via switch view button or returning back to monitoring
            // view while swimlane view was already ON
            ctrl.switchViewColumn(true, true);

            expect(ctrl.viewColumn).toBe(true); // swimlane
            expect(ctrl.columnsLimit).toBe(4);
            expect(preferencesService.update).toHaveBeenCalledWith(
                {'monitoring:view:session': true},
                'monitoring:view:session',
            );

            // Switch back to list view
            ctrl.switchViewColumn(false);
            expect(ctrl.viewColumn).toBe(false);
            expect(ctrl.columnsLimit).toBe(null);
        }));

    it('can preview an item', inject(($controller, $rootScope) => {
        var scope = $rootScope.$new(),
            ctrl = $controller('Monitoring', {$scope: scope}),
            item = {};

        expect(ctrl.state['with-preview']).toBeFalsy();

        ctrl.preview(item);

        expect(ctrl.previewItem).toBe(item);
        expect(ctrl.state['with-preview']).toBeTruthy();

        ctrl.closePreview();
        expect(ctrl.previewItem).toBe(null);
        expect(ctrl.state['with-preview']).toBeFalsy();
    }));

    it('can hide history tabs when preview externalsource item', inject(($controller, $rootScope) => {
        var scope = $rootScope.$new(),
            ctrl = $controller('Monitoring', {$scope: scope}),
            item: any = {};

        expect(ctrl.state['with-preview']).toBeFalsy();

        ctrl.preview(item);

        expect(ctrl.previewItem).toBe(item);
        expect(ctrl.state['with-preview']).toBeTruthy();
        expect(ctrl.showHistoryTab).toBeTruthy();

        item._type = 'externalsource';
        ctrl.preview(item);

        expect(ctrl.previewItem).toBe(item);
        expect(ctrl.showHistoryTab).toBeFalsy();
    }));

    it('can edit item', inject(($controller, $rootScope, session) => {
        session.identity = {_id: 'foo'};
        var scope = $rootScope.$new(),
            ctrl = $controller('Monitoring', {$scope: scope}),
            item = {};

        expect(ctrl.state['with-authoring']).toBeFalsy();

        ctrl.edit(item);
        expect(ctrl.editItem).toBe(item);
        expect(ctrl.state['with-authoring']).toBeTruthy();
    }));

    describe('cards service', () => {
        beforeEach(inject((desks) => {
            desks.deskLookup = {1: {_id: 1, name: 'desk1', desk_type: 'authoring'}};
            desks.stageLookup = {123: {default_incoming: false}, 456: {default_incoming: true}};
        }));

        it('can get criteria for stage', inject((cards, session) => {
            session.identity = {_id: 'foo'};
            var card = {_id: '123'};
            var criteria = cards.criteria(card);

            expect(criteria.source.query.filtered.filter.and).toContain({
                term: {'task.stage': card._id},
            });

            criteria = cards.criteria(card, 'foo');
            expect(criteria.source.query.filtered.filter.and).toContain({
                query: {query_string: {query: 'foo', lenient: true}},
            });
        }));

        it('can get criteria for personal', inject((cards, session) => {
            var card = {type: 'personal'};

            session.identity = {_id: 'foo'};
            var criteria = cards.criteria(card);

            expect(criteria.source.query.filtered.filter.and).toContain({
                bool: {
                    must_not: {exists: {field: 'task.desk'}},
                    should: [
                        {term: {'task.user': session.identity._id}},
                        {bool: {
                            must: {term: {original_creator: session.identity._id}},
                            must_not: {exists: {field: 'task.user'}},
                        }},
                    ],
                    minimum_should_match: 1,
                },
            });
        }));

        it('can get criteria for saved search', inject((cards, session) => {
            session.identity = {_id: 'foo'};
            var card = {type: 'search', search: {filter: {query: {q: 'foo'}}}};
            var criteria = cards.criteria(card);

            expect(criteria.source.query.filtered.query.query_string.query).toBe('foo');
        }));

        it('can get criteria for spike desk', inject((cards, session) => {
            session.identity = {_id: 'foo'};
            var card: any = {type: 'spike'};
            var criteria = cards.criteria(card);

            expect(criteria.source.query.filtered.filter.and).toContain({
                term: {'task.desk': card._id},
            });
            expect(criteria.source.query.filtered.filter.and).toContain({
                term: {state: 'spiked'},
            });
        }));

        it('can get criteria for highlight', inject((cards, session) => {
            session.identity = {_id: 'foo'};
            var card = {type: 'highlights'};
            var queryParam = {highlight: '123'};
            var criteria = cards.criteria(card, null, queryParam);

{'fraction_non_alphanumeric': 0.15368421052631578, 'fraction_numerical': 0.003157894736842105, 'mean_word_length': 2.1403702071397093, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3131275', 'n_tokens_mistral': 4043, 'n_tokens_neox': 3896, 'n_words': 735}