<commit_before><!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">

    <title>Test suite for reconnection</title>
    <!-- Importing Web Component's Polyfill -->
    <script src="../../../webcomponentsjs/webcomponents-lite.js"></script>

    <!-- mock server responses -->
    <script src="../../../sinonjs/sinon.js"></script>
    <script src="../mockServer.js"></script>
    <script src="../../../fast-json-patch/dist/fast-json-patch.min.js"></script>

    <link rel="import" href="../shared/helpers.html">

    <script src="../../../web-component-tester/browser.js"></script>

    <link rel="import" href="../../../polymer/polymer.html">

    <!-- Step 1: import the element to test -->
    <script src="../../../Palindrom/dist/palindrom-dom.min.js"></script>
    <link rel="import" href="../../palindrom-client.html">
</head>

<body>
    <test-fixture id="my-fixture">
        <template>
            <!-- workaround for test-fixture bug -->
            <div>
                <palindrom-client remote-url="/palindrom" ref="bind" ping-interval-s="0"></palindrom-client>
                <dom-bind id="bind">
                    <template>
                        <span id="name">{{model.name}}</span>
                    </template>
                </dom-bind>
            </div>
        </template>
    </test-fixture>
    <script>
        var model;
        // Do not take `console.error` as a reason to fail the test.
        WCT._config.trackConsoleError = false;
        describe('palindrom-client', function () {
            before(function initChai(done) {
                chai.use(palindromChaiPlugin);
                done();
            });
            var container, domBind, ppclient, palindrom, nameSpan, consoleErrorSpy;

            beforeEach(function (done) {
                patchAppliedEventCallback = sinon.spy();
                patchSentEventCallback = sinon.spy();
                consoleErrorSpy = sinon.spy(console, 'error');

                model = { "name": "oldname" };

                container = fixture('my-fixture');
                setTimeout(function () {
                    ppclient = container.querySelector("palindrom-client");
                    palindrom = ppclient.palindrom;
                    domBind = container.querySelector('dom-bind');
                    nameSpan = container.querySelector('#name');
                    initPalindrom(palindrom, model);
                    done();
                }, 100);

            });
            afterEach(function () {
                console.error.restore();
            });

            it('when connection is broken with a reason log error reason to the console', function () {
                var closeEvent = { reason: 'thereason', code: 1 };
                palindrom.network._ws.close(closeEvent);
                expect(consoleErrorSpy.getCall(0).args[0].message).to.contain(closeEvent.reason);
            });
            describe('when connection is broken', function () {
                it('it should dispatch an event when the connection dies', function () {
                    var closeEvent = { reason: 'thereason', code: 1 };
                    ppclient.addEventListener('connectionerror', event => {
                        expect(event.detail.palindromConnectionError.message).to.contain(closeEvent.reason);
                    });
                    palindrom.network._ws.close(closeEvent);               
                });

                it('it should dispatch an event after it reconnects', function (done) {
                    var closeEvent = { reason: undefined, code: 1 };
                    palindrom.network._ws.close(closeEvent);
                    ppclient.addEventListener('reconnection-end', () => done());
                });
            
                it('should reconnect after a second and use model from server', function (done) {
                    var closeEvent = { reason: undefined, code: 1 };
                    palindrom.network._ws.close(closeEvent);
                    var newName = 'newname';
                    setMockServerModel({ name: newName });
                    ppclient.addEventListener('reconnection-end', function () {
                        setTimeout(function () {
                            expect(nameSpan.innerText).to.equal(newName);
                            done();
                        }, 100);
                    });
                });

                it('and send pending patches when reconnecting', function (done) {
                    var closeEvent = { reason: undefined, code: 1 };
                    var newName = "new-name";
                    palindrom.network._ws.close(closeEvent);
                    domBind.set("model.name", newName);

                    ppclient.addEventListener('reconnection-end', function () {
                        expect(JSON.parse(lastRequestContent)[0][2]).to.deep.equal(
                            //            first patch ----^  ^---- first two operations are versions
                            operationObject('replace', '/name', newName));
                        done();
                    });
                });
            });
        });
    </script>

</body>

</html>
<commit_msg>Remove cnsl.error test (in fav of pal-error-catcher)<commit_after><!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">

    <title>Test suite for reconnection</title>
    <!-- Importing Web Component's Polyfill -->
    <script src="../../../webcomponentsjs/webcomponents-lite.js"></script>

    <!-- mock server responses -->
    <script src="../../../sinonjs/sinon.js"></script>
    <script src="../mockServer.js"></script>
    <script src="../../../fast-json-patch/dist/fast-json-patch.min.js"></script>

    <link rel="import" href="../shared/helpers.html">

    <script src="../../../web-component-tester/browser.js"></script>

    <link rel="import" href="../../../polymer/polymer.html">

    <!-- Step 1: import the element to test -->
    <script src="../../../Palindrom/dist/palindrom-dom.min.js"></script>
    <link rel="import" href="../../palindrom-client.html">
</head>

<body>
    <test-fixture id="my-fixture">
        <template>
            <!-- workaround for test-fixture bug -->
            <div>
                <palindrom-client remote-url="/palindrom" ref="bind" ping-interval-s="0"></palindrom-client>
                <dom-bind id="bind">
                    <template>
                        <span id="name">{{model.name}}</span>
                    </template>
                </dom-bind>
            </div>
        </template>
    </test-fixture>
    <script>
        var model;
        // Do not take `console.error` as a reason to fail the test.
        WCT._config.trackConsoleError = false;
        describe('palindrom-client', function () {
            before(function initChai(done) {
                chai.use(palindromChaiPlugin);
                done();
            });
            var container, domBind, ppclient, palindrom, nameSpan, consoleErrorSpy;

            beforeEach(function (done) {
                patchAppliedEventCallback = sinon.spy();
                patchSentEventCallback = sinon.spy();
                consoleErrorSpy = sinon.spy(console, 'error');

                model = { "name": "oldname" };

                container = fixture('my-fixture');
                setTimeout(function () {
                    ppclient = container.querySelector("palindrom-client");
                    palindrom = ppclient.palindrom;
                    domBind = container.querySelector('dom-bind');
                    nameSpan = container.querySelector('#name');
                    initPalindrom(palindrom, model);
                    done();
                }, 100);

            });
            afterEach(function () {
                console.error.restore();
            });

            describe('when connection is broken', function () {
                it('it should dispatch an event when the connection dies', function () {
                    var closeEvent = { reason: 'thereason', code: 1 };
                    ppclient.addEventListener('connectionerror', event => {
                        expect(event.detail.palindromConnectionError.message).to.contain(closeEvent.reason);
                    });
                    palindrom.network._ws.close(closeEvent);               
                });

                it('it should dispatch an event after it reconnects', function (done) {
                    var closeEvent = { reason: undefined, code: 1 };
                    palindrom.network._ws.close(closeEvent);
                    ppclient.addEventListener('reconnection-end', () => done());
                });
            
                it('should reconnect after a second and use model from server', function (done) {
                    var closeEvent = { reason: undefined, code: 1 };
                    palindrom.network._ws.close(closeEvent);
                    var newName = 'newname';
                    setMockServerModel({ name: newName });
                    ppclient.addEventListener('reconnection-end', function () {
                        setTimeout(function () {
                            expect(nameSpan.innerText).to.equal(newName);
                            done();
                        }, 100);
                    });
                });

                it('and send pending patches when reconnecting', function (done) {
                    var closeEvent = { reason: undefined, code: 1 };
                    var newName = "new-name";
                    palindrom.network._ws.close(closeEvent);
                    domBind.set("model.name", newName);

                    ppclient.addEventListener('reconnection-end', function () {
                        expect(JSON.parse(lastRequestContent)[0][2]).to.deep.equal(
                            //            first patch ----^  ^---- first two operations are versions
                            operationObject('replace', '/name', newName));
                        done();
                    });
                });
            });
        });
    </script>

</body>

</html>

{'fraction_non_alphanumeric': 0.14043700067378959, 'fraction_numerical': 0.003946481855809029, 'mean_word_length': 1.5175672401259994, 'pattern_counts': {'":': 2, '<': 95, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5881279', 'n_tokens_mistral': 2688, 'n_tokens_neox': 2502, 'n_words': 518}