var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
define(["require", "exports", "rxjs/operators", "wed", "wed/key-constants", "wed/onerror", "../base-config", "../wed-test-util"], function (require, exports, operators_1, wed, keyConstants, onerror, globalConfig, wed_test_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    wed = __importStar(wed);
    keyConstants = __importStar(keyConstants);
    onerror = __importStar(onerror);
    globalConfig = __importStar(globalConfig);
    const assert = chai.assert;
    describe("wed error handling:", () => {
        let setup;
        let editor;
        let server;
        beforeEach(() => {
            setup = new wed_test_util_1.EditorSetup(`${wed_test_util_1.dataPath}/wed_test_data/server_interaction_converted.xml`, globalConfig.config, document);
            ({ editor, server } = setup);
            return setup.init();
        });
        afterEach(() => {
            setup.restore();
        });
        it("tell user to reload when save fails hard", (done) => {
            server.emptyResponseOnSave = true;
            const $modal = onerror.__test.$modal;
            $modal.on("shown.bs.modal", () => {
                // Reset the state so we don't get a failure in the after hook.
                onerror.__test.reset();
                done();
            });
            editor.type(keyConstants.SAVE);
        });
        it("warn of disconnection when server returns a bad status", (done) => {
            server.failOnSave = true;
            const $modal = editor.modals.getModal("disconnect").getTopLevel();
            $modal.on("shown.bs.modal", () => {
                editor.saver.events.pipe(operators_1.filter((ev) => ev.name === "Saved"), operators_1.first())
                    .subscribe(() => {
                    // Was saved on retry!
                    // This allows us to let the whole save process run its course before
                    // we declare it done.
                    setTimeout(done, 0);
                });
                server.reset();
                // This triggers a retry.
                $modal.modal("hide");
            });
            editor.type(keyConstants.SAVE);
        });
        it("bring up modal when document was edited by someone else", (done) => {
            server.preconditionFailOnSave = true;
            const $modal = editor.modals.getModal("editedByOther").getTopLevel();
            $modal.on("shown.bs.modal", () => {
                // Prevent a reload.
                $modal.off("hidden.bs.modal.modal");
                $modal.modal("hide");
                done();
            });
            editor.type(keyConstants.SAVE);
        });
        it("bring up modal when there is a new version of editor", (done) => {
            server.tooOldOnSave = true;
            const $modal = editor.modals.getModal("tooOld").getTopLevel();
            $modal.on("shown.bs.modal", () => {
                // Prevent a reload.
                $modal.off("hidden.bs.modal.modal");
                $modal.modal("hide");
                done();
            });
            editor.type(keyConstants.SAVE);
        });
        it("no recovery when save fails hard", (done) => {
            server.emptyResponseOnSave = true;
            const $modal = onerror.__test.$modal;
            $modal.on("shown.bs.modal", () => {
                // Reset the state so we don't get a failure in the after hook.
                onerror.__test.reset();
                // The data was saved even though the server replied badly.
                assert.deepEqual(server.lastSaveRequest, {
                    // It is important that this be "save" and not "recover".
                    command: "save",
                    version: wed.version,
                    data: "<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">\
<teiHeader><fileDesc><titleStmt><title>abcd</title></titleStmt>\
<publicationStmt><p/></publicationStmt><sourceDesc><p/></sourceDesc>\
</fileDesc></teiHeader><text><body><p>Blah blah <term>blah</term> blah.</p>\
<p><term>blah</term></p></body></text></TEI>",
                });
                done();
            });
            editor.type(keyConstants.SAVE);
        });
        it("recovery on uncaught exception", (done) => {
            // We can't just raise an exception because mocha will intercept it and it
            // will never get to the onerror handler. If we raise the error in a
            // timeout, it will go straight to onerror.
            setTimeout(() => {
                setTimeout(() => {
                    // Reset the state so we don't get a failure in the after hook.
                    onerror.__test.reset();
                    assert.deepEqual(server.lastSaveRequest, {
                        command: "recover",
                        version: wed.version,
                        data: "<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">\
<teiHeader><fileDesc><titleStmt><title>abcd</title></titleStmt>\
<publicationStmt><p/></publicationStmt><sourceDesc><p/></sourceDesc>\
</fileDesc></teiHeader><text><body><p>Blah blah <term>blah</term> blah.</p>\
<p><term>blah</term></p></body></text></TEI>",
                    });
                    done();
                }, 1000);
                throw new Error("I'm failing!");
            }, 5);
        });
    });
});
//# sourceMappingURL=wed-error-handling-test.js.map