<commit_before>describe('templates', function() {
    'use strict';

    beforeEach(module('superdesk.templates'));

    describe('templates widget', function() {
        it('should create a template', inject(function($controller, api, desks, $q, $rootScope) {
            spyOn(desks, 'fetchCurrentUserDesks').and.returnValue($q.when({_items: []}));
            spyOn(api, 'save').and.returnValue($q.when({}));

            var item = _.create({slugline: 'FOO', headline: 'foo'}),
                ctrl = $controller('CreateTemplateController', {item: item});
            expect(ctrl.name).toBe('FOO');
            expect(ctrl.type).toBe('create');
            ctrl.name = 'test';
            ctrl.desk = 'news';
            ctrl.save();
            expect(api.save).toHaveBeenCalledWith('content_templates', {
                template_name: 'test',
                template_type: 'create',
                template_desk: 'news',
                headline: 'foo',
                slugline: 'FOO'
            });
        }));
    });

    describe('templates service', function() {
        beforeEach(inject(function($q, api) {
            spyOn(api.content_templates, 'query').and.returnValue($q.when());
        }));
        it('can fetch templates using default parameters', inject(function(api, templates) {
            templates.fetchTemplates();
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 10,
                page: 1
            });
        }));
        it('can fetch templates using page parameters', inject(function(api, templates) {
            templates.fetchTemplates(2, 25);
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 25,
                page: 2
            });
        }));
        it('can fetch templates using type parameter', inject(function(api, templates) {
            templates.fetchTemplates(undefined, undefined, 'create');
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 10,
                page: 1,
                where: '{"$and":[{"template_type":"create"}]}'
            });
        }));
        it('can fetch templates using desk parameter', inject(function(api, templates) {
            templates.fetchTemplates(undefined, undefined, undefined, 'desk1');
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 10,
                page: 1,
                where: '{"$and":[{"template_desk":"desk1"}]}'
            });
        }));
        it('can fetch templates using personal desk parameter', inject(function(api, templates) {
            templates.fetchTemplates(undefined, undefined, undefined, 'personal');
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 10,
                page: 1,
                where: '{"$and":[{"template_desk":null}]}'
            });
        }));
        it('can fetch templates using keyword parameter', inject(function(api, templates) {
            templates.fetchTemplates(undefined, undefined, undefined, undefined, 'keyword');
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 10,
                page: 1,
                where: '{"$and":[{"template_name":{"$regex":"keyword","$options":"-i"}}]}'
            });
        }));
        it('can fetch templates using all parameters', inject(function(api, templates) {
            templates.fetchTemplates(25, 2, 'create', 'desk1', 'keyword');
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 2,
                page: 25,
                where: '{"$and":[{"template_type":"create","template_desk":"desk1","template_name":{"$regex":"keyword","$options":"-i"}}]}'
            });
        }));
    });
});
<commit_msg>fix(tests): Added recent templates tests.<commit_after>describe('templates', function() {
    'use strict';

    beforeEach(module('superdesk.templates'));

    describe('templates widget', function() {
        it('should create a template', inject(function($controller, api, desks, $q, $rootScope) {
            spyOn(desks, 'fetchCurrentUserDesks').and.returnValue($q.when({_items: []}));
            spyOn(api, 'save').and.returnValue($q.when({}));

            var item = _.create({slugline: 'FOO', headline: 'foo'}),
                ctrl = $controller('CreateTemplateController', {item: item});
            expect(ctrl.name).toBe('FOO');
            expect(ctrl.type).toBe('create');
            ctrl.name = 'test';
            ctrl.desk = 'news';
            ctrl.save();
            expect(api.save).toHaveBeenCalledWith('content_templates', {
                template_name: 'test',
                template_type: 'create',
                template_desk: 'news',
                headline: 'foo',
                slugline: 'FOO'
            });
        }));
    });

    describe('templates service', function() {
        beforeEach(inject(function($q, api) {
            spyOn(api.content_templates, 'query').and.returnValue($q.when());
        }));
        it('can fetch templates using default parameters', inject(function(api, templates) {
            templates.fetchTemplates();
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 10,
                page: 1
            });
        }));
        it('can fetch templates using page parameters', inject(function(api, templates) {
            templates.fetchTemplates(2, 25);
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 25,
                page: 2
            });
        }));
        it('can fetch templates using type parameter', inject(function(api, templates) {
            templates.fetchTemplates(undefined, undefined, 'create');
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 10,
                page: 1,
                where: '{"$and":[{"template_type":"create"}]}'
            });
        }));
        it('can fetch templates using desk parameter', inject(function(api, templates) {
            templates.fetchTemplates(undefined, undefined, undefined, 'desk1');
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 10,
                page: 1,
                where: '{"$and":[{"template_desk":"desk1"}]}'
            });
        }));
        it('can fetch templates using personal desk parameter', inject(function(api, templates) {
            templates.fetchTemplates(undefined, undefined, undefined, 'personal');
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 10,
                page: 1,
                where: '{"$and":[{"template_desk":null}]}'
            });
        }));
        it('can fetch templates using keyword parameter', inject(function(api, templates) {
            templates.fetchTemplates(undefined, undefined, undefined, undefined, 'keyword');
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 10,
                page: 1,
                where: '{"$and":[{"template_name":{"$regex":"keyword","$options":"-i"}}]}'
            });
        }));
        it('can fetch templates using all parameters', inject(function(api, templates) {
            templates.fetchTemplates(25, 2, 'create', 'desk1', 'keyword');
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 2,
                page: 25,
                where: '{"$and":[{"template_type":"create","template_desk":"desk1","template_name":{"$regex":"keyword","$options":"-i"}}]}'
            });
        }));
        it('can fetch templates by id', inject(function(api, templates) {
            templates.fetchTemplatesByIds([123, 456]);
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 10,
                page: 1,
                where: '{"$or":[{"_id":123},{"_id":456}]}'
            });
        }));
        it('can add recent templates', inject(function(api, templates, preferencesService, $q, $rootScope) {
            spyOn(preferencesService, 'get').and.returnValue($q.when({}));
            spyOn(preferencesService, 'update').and.returnValue($q.when());
            templates.addRecentTemplate('desk1', 'template1');
            $rootScope.$digest();
            expect(preferencesService.update).toHaveBeenCalledWith({
                'templates:recent': {
                    'desk1': ['template1']
                }
            });
        }));
        it('can get recent templates', inject(function(api, templates, preferencesService, $q, $rootScope) {
            spyOn(preferencesService, 'get').and.returnValue($q.when({
                'templates:recent': {
                    'desk2': ['template2', 'template3']
                }
            }));
            templates.getRecentTemplates('desk2');
            $rootScope.$digest();
            expect(api.content_templates.query).toHaveBeenCalledWith({
                max_results: 10,
                page: 1,
                where: '{"$or":[{"_id":"template2"},{"_id":"template3"}]}'
            });
        }));
    });
});

{'fraction_non_alphanumeric': 0.1521389514313284, 'fraction_numerical': 0.00964940495336121, 'mean_word_length': 2.1260053619302948, 'pattern_counts': {'":': 38, '<': 3, '<?xml version=': 0, '>': 3, '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': '133645', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2426, 'n_words': 428}