define([
    'bitbucket/internal/util/html'
], function(
    htmlUtil
) {

    'use strict';

    var lineStreamOpts;
    var testOne;

    module('util-html', {
        setup: function () {
            setupTestData();
        }
    });

    function testNodeStream(stream, expectedNodes) {
        var node;
        var i;
        for(i = 0; i < expectedNodes.length; i++) {
            node = expectedNodes[i];
            equal(stream.getNextTextPosition(), node.textPosition, 'text position should match expected for node ' + i);
            equal(stream.getNextType(), node.type, 'node type should match expected for node ' + i);
            deepEqual(stream.next(), node, 'node object should match expected for node ' + i);
        }
        equal(stream.getNextTextPosition(), null, 'text position should be null at end');
        equal(stream.getNextType(), null, 'node type should be null at end');
        equal(stream.next(), null, 'node object should be null at end');
    }

    function setupTestData() {
        var OPEN = htmlUtil.NodeType.OPEN;
        var CLOSE = htmlUtil.NodeType.CLOSE;

        lineStreamOpts = {
            lineStart : '<pre>',
            lineEnd : '</pre>'
        };

        testOne = {
            text : 'hihellohow are you?\nwhoaawesomestuff',
            lines : ['hihellohow are you?','whoaawesomestuff'],
            lineNodes : [{
                    type : OPEN,
                    value : '<pre>',
                    textPosition : 0,
                    close : {
                        type : CLOSE,
                        value : '</pre>'
                    }
                }, {
                    type : CLOSE,
                    value : '</pre>',
                    textPosition : 20
                }, {
                    type : OPEN,
                    value : '<pre>',
                    textPosition : 20,
                    close : {
                        type : CLOSE,
                        value : '</pre>'
                    }
                }, {
                    type : CLOSE,
                    value : '</pre>',
                    textPosition : 37
                }],
            html : 'hi<pre>hello</pre>how are you?\n<center><b>whoa<i><u>awe</u>some</i></b></center>stuff',
            htmlNodes : [{
                    type : OPEN,
                    value : '<pre>',
                    textPosition : 2,
                    close : {
                        type : CLOSE,
                        value : '</pre>'
                    }
                }, {
                    type : CLOSE,
                    value : '</pre>',
                    textPosition : 7
                }, {
                    type : OPEN,
                    value : '<center>',
                    textPosition : 20,
                    close : {
                        type : CLOSE,
                        value : '</center>'
                    }
                }, {
                    type : OPEN,
                    value : '<b>',
                    textPosition : 20,
                    close : {
                        type : CLOSE,
                        value : '</b>'
                    }
                }, {
                    type : OPEN,
                    value : '<i>',
                    textPosition : 24,
                    close : {
                        type : CLOSE,
                        value : '</i>'
                    }
                }, {
                    type : OPEN,
                    value : '<u>',
                    textPosition : 24,
                    close : {
                        type : CLOSE,
                        value : '</u>'
                    }
                }, {
                    type : CLOSE,
                    value : '</u>',
                    textPosition : 27
                }, {
                    type : CLOSE,
                    value : '</i>',
                    textPosition : 31
                }, {
                    type : CLOSE,
                    value : '</b>',
                    textPosition : 31
                }, {
                    type : CLOSE,
                    value : '</center>',
                    textPosition : 31
                }],
            mergedHtml : '<pre>hi<pre>hello</pre>how are you?\n</pre><pre><center><b>whoa<i><u>awe</u>some</i></b></center>stuff</pre>'
        };
    }

    test('htmlNodeStream works', function() {
        var stream = htmlUtil.htmlNodeStream(testOne.html, function(a) { return a; });
        testNodeStream(stream, testOne.htmlNodes);
    });

    test('lineNodeStream works', function() {
        var stream = htmlUtil.lineNodeStream(testOne.lines, function(a) { return a; }, lineStreamOpts);
        testNodeStream(stream, testOne.lineNodes);
    });

    test('mergeStreams works', function() {
        var lineStream = htmlUtil.lineNodeStream(testOne.lines, function(a) { return a; }, lineStreamOpts);
        var htmlStream = htmlUtil.htmlNodeStream(testOne.html, function(a) { return a; });

        var result = htmlUtil.mergeStreams(testOne.text, lineStream, htmlStream);

        equal(result, testOne.mergedHtml, 'MergeStreams result should be ' + testOne.mergedHtml);
    });

    test('sanitizeId works', function() {
        equal(htmlUtil.sanitizeId(''), '');
        equal(htmlUtil.sanitizeId('abc123456'), 'abc123456');
        equal(htmlUtil.sanitizeId('abc123:456-789.0123'), 'abc123\\:456-789\\.0123');
    });
});
