<commit_before>'use strict';

(function(factory) {
    if (typeof define === 'function' && define.amd) {
        define(['chai', 'emailjs-imap-client-imap', 'emailjs-mime-codec'], factory.bind(null, sinon));
    } else if (typeof exports === 'object') {
        module.exports = factory(require('sinon'), require('chai'), require('../../src/emailjs-imap-client-imap'), require('emailjs-mime-codec'));
    }
}(function(sinon, chai, ImapClient, mimefuncs) {
    var expect = chai.expect;
    chai.config.includeStack = true;

    var host = 'localhost';
    var port = 10000;

    describe('browserbox imap unit tests', () => {
        var client, socketStub;

        /* jshint indent:false */

        beforeEach(() => {
            client = new ImapClient(host, port);
            expect(client).to.exist;

            client.logger = {
                debug: () => {},
                error: () => {}
            };

            var TCPSocket = function() {};
            TCPSocket.open = () => {};
            TCPSocket.prototype.close = () => {};
            TCPSocket.prototype.send = () => {};
            TCPSocket.prototype.suspend = () => {};
            TCPSocket.prototype.resume = () => {};
            TCPSocket.prototype.upgradeToSecure = () => {};

            socketStub = sinon.createStubInstance(TCPSocket);
            sinon.stub(TCPSocket, 'open').withArgs(host, port).returns(socketStub);

            var promise = client.connect(TCPSocket).then(() => {
                expect(TCPSocket.open.callCount).to.equal(1);

                expect(socketStub.onerror).to.exist;
                expect(socketStub.onopen).to.exist;
                expect(socketStub.onclose).to.exist;
                expect(socketStub.ondata).to.exist;
            });

            setTimeout(() => socketStub.onopen(), 0);

            return promise;
        });

        describe('#close', () => {
            it('should call socket.close', (done) => {
                client.socket.readyState = 'open';

                client.close().then(() => {
                    expect(socketStub.close.callCount).to.equal(1);
                }).then(done).catch(done);

                setTimeout(() => socketStub.onclose(), 0);
            });

            it('should call socket.close', (done) => {
                client.socket.readyState = 'not open. duh.';

                client.close().then(() => {
                    expect(socketStub.close.called).to.be.false;
                }).then(done).catch(done);

                setTimeout(() => socketStub.onclose(), 0);
            });<commit_msg>still some race condition in unit test, trying to fix it<commit_after>'use strict';

(function(factory) {
    if (typeof define === 'function' && define.amd) {
        define(['chai', 'emailjs-imap-client-imap', 'emailjs-mime-codec'], factory.bind(null, sinon));
    } else if (typeof exports === 'object') {
        module.exports = factory(require('sinon'), require('chai'), require('../../src/emailjs-imap-client-imap'), require('emailjs-mime-codec'));
    }
}(function(sinon, chai, ImapClient, mimefuncs) {
    var expect = chai.expect;
    chai.config.includeStack = true;

    var host = 'localhost';
    var port = 10000;

    describe('browserbox imap unit tests', () => {
        var client, socketStub;

        /* jshint indent:false */

        beforeEach(() => {
            client = new ImapClient(host, port);
            expect(client).to.exist;

            client.logger = {
                debug: () => {},
                error: () => {}
            };

            var TCPSocket = function() {};
            TCPSocket.open = () => {};
            TCPSocket.prototype.close = () => {};
            TCPSocket.prototype.send = () => {};
            TCPSocket.prototype.suspend = () => {};
            TCPSocket.prototype.resume = () => {};
            TCPSocket.prototype.upgradeToSecure = () => {};

            socketStub = sinon.createStubInstance(TCPSocket);
            sinon.stub(TCPSocket, 'open').withArgs(host, port).returns(socketStub);

            var promise = client.connect(TCPSocket).then(() => {
                expect(TCPSocket.open.callCount).to.equal(1);

                expect(socketStub.onerror).to.exist;
                expect(socketStub.onopen).to.exist;
                expect(socketStub.onclose).to.exist;
                expect(socketStub.ondata).to.exist;
            });

            setTimeout(() => socketStub.onopen(), 10);

            return promise;
        });

        describe('#close', () => {
            it('should call socket.close', (done) => {
                client.socket.readyState = 'open';

                client.close().then(() => {
                    expect(socketStub.close.callCount).to.equal(1);
                }).then(done).catch(done);

                setTimeout(() => socketStub.onclose(), 0);
            });

            it('should call socket.close', (done) => {
                client.socket.readyState = 'not open. duh.';

                client.close().then(() => {
                    expect(socketStub.close.called).to.be.false;
                }).then(done).catch(done);

                setTimeout(() => socketStub.onclose(), 0);
            });
{'fraction_non_alphanumeric': 0.16698546289211935, 'fraction_numerical': 0.004016832440703902, 'mean_word_length': 1.8341463414634147, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '6708737', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1443, 'n_words': 230}