/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { strict as assert } from "assert";
import { makeMitigatedChangeFamily } from "../../feature-libraries/index.js";
const fallback = "Fallback";
const arg1 = "arg1";
const arg2 = "arg2";
const arg3 = "arg3";
const throwingFamily = {
    buildEditor: (changeReceiver) => {
        assert.equal(changeReceiver, arg1);
        throw new Error("buildEditor");
    },
    rebaser: {
        compose: (changes) => {
            assert.equal(changes, arg1);
            throw new Error("compose");
        },
        invert: (change, isRollback) => {
            assert.equal(change, arg1);
            assert.equal(isRollback, arg2);
            throw new Error("invert");
        },
        rebase: (change, over) => {
            assert.equal(change, arg1);
            assert.equal(over, arg2);
            throw new Error("rebase");
        },
    },
    codecs: {},
};
const returningFamily = {
    buildEditor: (changeReceiver) => {
        assert.equal(changeReceiver, arg1);
        return "buildEditor";
    },
    rebaser: {
        compose: (changes) => {
            assert.equal(changes, arg1);
            return "compose";
        },
        invert: (change, isRollback) => {
            assert.equal(change, arg1);
            assert.equal(isRollback, arg2);
            return "invert";
        },
        rebase: (change, over) => {
            assert.equal(change, arg1);
            assert.equal(over, arg2);
            return "rebase";
        },
    },
    codecs: {},
};
const errorLog = [];
const mitigatedThrowingFamily = makeMitigatedChangeFamily(throwingFamily, fallback, (error) => errorLog.push(error.message));
const mitigatedReturningFamily = makeMitigatedChangeFamily(returningFamily, fallback, () => assert.fail("Unexpected onError call"));
const mitigatedReturningRebaser = mitigatedReturningFamily.rebaser;
const mitigatedThrowingRebaser = mitigatedThrowingFamily.rebaser;
const returningRebaser = returningFamily.rebaser;
describe("makeMitigatedChangeFamily", () => {
    it("does not interfere so long as nothing is thrown", () => {
        assert.equal(mitigatedReturningFamily.buildEditor(arg1), returningFamily.buildEditor(arg1));
        assert.equal(mitigatedReturningRebaser.rebase(arg1, arg2, arg3), returningRebaser.rebase(arg1, arg2, arg3));
        assert.equal(mitigatedReturningRebaser.invert(arg1, arg2), returningRebaser.invert(arg1, arg2));
        assert.equal(mitigatedReturningRebaser.compose(arg1), returningRebaser.compose(arg1));
    });
    describe("catches errors from", () => {
        it("rebase", () => {
            errorLog.length = 0;
            assert.equal(mitigatedThrowingRebaser.rebase(arg1, arg2, arg3), fallback);
            assert.deepEqual(errorLog, ["rebase"]);
        });
        it("invert", () => {
            errorLog.length = 0;
            assert.equal(mitigatedThrowingRebaser.invert(arg1, arg2), fallback);
            assert.deepEqual(errorLog, ["invert"]);
        });
        it("compose", () => {
            errorLog.length = 0;
            assert.equal(mitigatedThrowingRebaser.compose(arg1), fallback);
            assert.deepEqual(errorLog, ["compose"]);
        });
    });
    it("does not catch errors from buildEditor", () => {
        errorLog.length = 0;
        assert.throws(() => mitigatedThrowingFamily.buildEditor(arg1), new Error("buildEditor"));
        assert.deepEqual(errorLog, []);
    });
    it("does affect codecs", () => {
        assert.equal(mitigatedReturningFamily.codecs, returningFamily.codecs);
    });
});
//# sourceMappingURL=mitigatedChangeFamily.spec.js.map