(function () {
    'use strict';
    describe('Console Service', function () {
        var ufConsole, ufConsoleInternal;
        testHarness.beforeEachCreateMocks();
        beforeEach(inject(function (_ufConsole_, _ufConsoleInternal_) {
            ufConsole = _ufConsole_;
            ufConsoleInternal = _ufConsoleInternal_;
        }));
        afterEach(function () {
            ufConsoleInternal.getMessages().length = 0;
        });
        it('should have message types', function () {
            expect(_(ufConsole.msgTypes).size()).toBe(4);
            expect(ufConsole.msgTypes.INSTALL).toBeDefined();
            expect(ufConsole.msgTypes.STATUS).toBeDefined();
            expect(ufConsole.msgTypes.GEL).toBeDefined();
            expect(ufConsole.msgTypes.UPDATE).toBeDefined();
        });
        it('should add a message', function (done) {
            expect(ufConsoleInternal.getMessages().length).toBe(0);
            ufConsoleInternal.addNewMessageListener(function () {
                expect(ufConsoleInternal.getMessages().length).toBe(1);
                done();
            });
            ufConsole.addMessage(ufConsole.msgTypes.STATUS, 'Test message text');
        });
        it('should add message even when there are no listeners', function () {
            ufConsoleInternal.addNewMessageListener();
            ufConsole.addMessage(ufConsole.msgTypes.STATUS, 'Test message text');
            expect(ufConsoleInternal.getMessages().length).toBe(1);
        });
        // the console directive is bound to the object obtained from getMessages()
        // we need to ensure the reference is not changed between calls
        it('should verify messages object is not the same between messages', function (done) {
            var messages = ufConsoleInternal.getMessages();
            ufConsoleInternal.addNewMessageListener(function () {
                expect(ufConsoleInternal.getMessages()).toBe(messages);
                done();
            });
            ufConsole.addMessage(ufConsole.msgTypes.STATUS, 'Test message text');
        });
        it('should verify that we can add a large number of messages', function (done) {
            var testSize = 10000;
            var timesCalled = 0;
            var testMessagePrefix = 'Test message text';
            ufConsoleInternal.addNewMessageListener(function () {
                timesCalled++;
                if (timesCalled === testSize) {
                    var messages = ufConsoleInternal.getMessages();
                    var lastMessage = messages[messages.length - 1];
                    expect(lastMessage.text).toBe(testMessagePrefix + (timesCalled - 1));
                    done();
                }
            });
            for (var i = 0; i < testSize; i++) {
                ufConsole.addMessage(ufConsole.msgTypes.STATUS, testMessagePrefix + i);
            }
        });
        it('should verify that the message buffer size is limited and wraps', function (done) {
            var testSize = 3000;
            var timesCalled = 0;
            var maxBufferSize = 1000;
            var testMessagePrefix = 'Test message text';
            ufConsoleInternal.addNewMessageListener(function () {
                timesCalled++;
                var messages = ufConsoleInternal.getMessages();
                var bufferSize = messages.length;
                expect(bufferSize <= maxBufferSize).toBe(true);
                var firstMessageInBuffer = testMessagePrefix + (timesCalled - bufferSize);
                var lastMessageInBuffer = testMessagePrefix + (timesCalled - 1);
                expect(messages[0].text).toBe(firstMessageInBuffer);
                expect(messages[messages.length - 1].text).toBe(lastMessageInBuffer);
                if (timesCalled === testSize) {
                    done();
                }
            });
            for (var i = 0; i < testSize; i++) {
                ufConsole.addMessage(ufConsole.msgTypes.STATUS, testMessagePrefix + i);
            }
        });
    });
})();
