define([
    'lodash',
    'bitbucket/internal/feature/commit/difftree/difftree-search',
    'bitbucket/internal/util/function'
], function (
    _,
    DiffTreeSearch,
    fn
) {

    'use strict';

    module('difftree-search', {
        setup: function () {
            fn.set('bitbucket.internal.feature.difftree.searchTreeNode', _.identity, true)(window);
        }
    });

    test("Decorating the title", function () {
        var filter = new DiffTreeSearch._Text('a');
        var decorateTitle = DiffTreeSearch._decorateTitle;
        equal(decorateTitle('', filter), '', "Empty string");
        equal(decorateTitle('a', filter), '<strong>a</strong>', "Simple");
        equal(decorateTitle('aA', filter), '<strong>a</strong><strong>A</strong>', "Case insensitive");
        equal(decorateTitle('aA', new DiffTreeSearch._Text('a', true)), '<strong>a</strong>A', "Case sensitive");
        equal(decorateTitle('aca', filter), '<strong>a</strong>c<strong>a</strong>', "Starting and ending with text");
        equal(decorateTitle('bacad', filter), 'b<strong>a</strong>c<strong>a</strong>d', "Not starting or ending with text");
        equal(decorateTitle('a<<>', new DiffTreeSearch._Text('a<')), '<strong>a&lt;</strong>&lt;&gt;', "Everything should be HTML escaped");
        equal(decorateTitle('a.b', new DiffTreeSearch._Text('.')), 'a<strong>.</strong>b', "We shouldn't be matching as regex");
    });

    test("Filter the diff tree", function () {
        var difftree = DiffTreeSearch._filterDiffTree( {metadata: {isDirectory: true}, children: [
            {metadata: {}, data: {attr: {href: '#a/b/c.txt'}}},
            {metadata: {isDirectory: true}, children: []},
            {metadata: {}, data: {attr: {href: '#d.txt'}}},
            {metadata: {isDirectory: true}, children: [
                {metadata: {}, data: {attr: {href: '#g.txt'}}},
                {metadata: {}, data: {attr: {href: '#e/f.txt'}}}
            ]}
        ]}, new DiffTreeSearch._Text('a'), {diffs: [{
            destination: 'a/b/c.txt',
            hunks: [
            {
                segments: [
                    {type: 'ADDED', lines: [{line: 'a', destination: 1}]},
                    {type: 'REMOVED', lines: [{line: 'b', source: 1}]}
                ]
            },
            {
                segments: [
                    {type: 'ADDED', lines: [{line: 'b', destination: 2}]},
                    {type: 'REMOVED', lines: [{line: 'a', source: 210}]}
                ]
            }
        ]}, {
            source: 'e/f.txt',
            hunks: [
                {
                    segments: [
                        {type: 'CONTEXT', lines: [{line: 'a', source: 10, destination: 11}]}
                    ]
                }
            ]
        }]});
        equal(difftree.children.length, 2);
        deepEqual(difftree.children[0].children[0].data.title, {changeType: 'ADDED', lineNumber: '1', padding: 3, titleContent: '<strong>a</strong>'});
        deepEqual(difftree.children[0].children[1].data.title, {changeType: 'REMOVED', lineNumber: '210', padding: 3, titleContent: '<strong>a</strong>'});
        deepEqual(difftree.children[1].children[0].children[0].data.title, {changeType: 'CONTEXT', lineNumber: '10', padding: 2, titleContent: '<strong>a</strong>'});
    });
});
