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", "salve", "wed/dloc", "wed/validator", "../util"], function (require, exports, salve_1, dloc_1, validator, util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    validator = __importStar(validator);
    const assert = chai.assert;
    function sameEvents(evs, expected) {
        assert.sameMembers(Array.from(evs).map((x) => x.toString()), expected.map((x) => x.toString()));
    }
    describe("validator", () => {
        let emptyTree;
        let emptyDataRoot;
        let grammar;
        let genericTree;
        before(() => {
            const frag = document.createDocumentFragment();
            emptyTree = document.createElement("div");
            frag.appendChild(emptyTree);
            emptyDataRoot = new dloc_1.DLocRoot(frag);
            const provider = new util_1.DataProvider("/base/build/");
            return Promise.all([
                provider.getText("schemas/simplified-rng.js").then((schema) => {
                    grammar = salve_1.readTreeFromJSON(schema);
                }),
                provider.getDoc("dist/dev/lib/tests/validator_test_data/to_parse_converted.xml")
                    .then((doc) => {
                    genericTree = doc;
                }),
            ]);
        });
        describe("possibleAt", () => {
            it("with DLoc", () => {
                const p = new validator.Validator(grammar, emptyTree, []);
                const evs = p.possibleAt(dloc_1.DLoc.mustMakeDLoc(emptyDataRoot, emptyTree, 0));
                sameEvents(evs, [new salve_1.Event("enterStartTag", new salve_1.Name("", "", "html"))]);
            });
        });
        // We test speculativelyValidateFragment through speculativelyValidate
        describe("speculativelyValidate", () => {
            it("with DLoc", () => {
                const tree = genericTree.cloneNode(true);
                const dataRoot = new dloc_1.DLocRoot(tree);
                const p = new validator.Validator(grammar, tree, []);
                const body = tree.getElementsByTagName("body")[0];
                const container = body.parentNode;
                const index = Array.prototype.indexOf.call(container.childNodes, body);
                const ret = p.speculativelyValidate(dloc_1.DLoc.mustMakeDLoc(dataRoot, container, index), body);
                assert.isFalse(ret);
            });
        });
        // speculativelyValidateFragment is largely tested through
        // speculativelyValidate above.
        describe("speculativelyValidateFragment", () => {
            it("throws an error if toParse is not an element", () => {
                const tree = genericTree.cloneNode(true);
                const dataRoot = new dloc_1.DLocRoot(tree);
                const p = new validator.Validator(grammar, tree, []);
                // tslint:disable-next-line:no-any
                p._maxTimespan = 0; // Work forever.
                const body = tree.getElementsByTagName("body")[0];
                const container = body.parentNode;
                const index = Array.prototype.indexOf.call(container.childNodes, body);
                // tslint:disable-next-line:no-any
                assert.throws(p.speculativelyValidateFragment.bind(p, dloc_1.DLoc.makeDLoc(dataRoot, container, index), document.createTextNode("blah")), Error, "toParse is not an element");
            });
        });
        describe("with a mode validator", () => {
            let p;
            let tree;
            let validationError;
            before(() => {
                validationError = new salve_1.ValidationError("Test");
                tree = genericTree.cloneNode(true);
            });
            beforeEach(() => {
                // tslint:disable-next-line:completed-docs
                class Validator {
                    validateDocument() {
                        return [{
                                error: validationError,
                                node: tree,
                                index: 0,
                            }];
                    }
                }
                p = new validator.Validator(grammar, tree, [new Validator()]);
                // tslint:disable-next-line:no-any
                p._maxTimespan = 0; // Work forever.
            });
            function onCompletion(cb) {
                p.events.addEventListener("state-update", (state) => {
                    if (!(state.state === validator.VALID ||
                        state.state === validator.INVALID)) {
                        return;
                    }
                    cb();
                });
            }
            it("records additional errors", (done) => {
                onCompletion(() => {
                    assert.deepEqual(p.errors, [{ error: validationError, node: tree,
                            index: 0 }]);
                    done();
                });
                p.start();
            });
            it("emits additional error events", (done) => {
                let seen = 0;
                p.events.addEventListener("error", (error) => {
                    assert.deepEqual(error, { error: validationError, node: tree,
                        index: 0 });
                    seen++;
                });
                onCompletion(() => {
                    assert.equal(seen, 1);
                    done();
                });
                p.start();
            });
        });
    });
});
//  LocalWords:  rng chai js validator Mangalam MPL Dubeau html enterStartTag
//# sourceMappingURL=validator-test.js.map