"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = require("assert");
const ops_1 = require("../ops.cjs");
const testClient_1 = require("./testClient.cjs");
const testUtils_1 = require("./testUtils.cjs");
describe("obliterate", () => {
    let client;
    let refSeq;
    const localClientId = 17;
    const remoteClientId = localClientId + 1;
    beforeEach(() => {
        client = new testClient_1.TestClient({
            mergeTreeEnableObliterate: true,
        });
        client.startOrUpdateCollaboration("local");
        for (const char of "hello world") {
            client.applyMsg(client.makeOpMessage(client.insertTextLocal(client.getLength(), char), client.getCurrentSeq() + 1));
        }
        assert_1.strict.equal(client.getText(), "hello world");
        refSeq = client.getCurrentSeq();
    });
    it("removes text", () => {
        client.obliterateRangeLocal(0, client.getLength());
        assert_1.strict.equal(client.getText(), "");
    });
    describe("concurrent obliterate and insert", () => {
        it("removes text for obliterate then insert", () => {
            client.obliterateRange({
                start: 0,
                end: client.getLength(),
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 1,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.insertText)({
                mergeTree: client.mergeTree,
                pos: 1,
                refSeq,
                clientId: remoteClientId + 1,
                seq: refSeq + 2,
                text: "more ",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            assert_1.strict.equal(client.getText(), "");
        });
        it("removes text for insert then obliterate when deleting entire string", () => {
            (0, testUtils_1.insertText)({
                mergeTree: client.mergeTree,
                pos: 1,
                refSeq,
                clientId: remoteClientId + 1,
                seq: refSeq + 1,
                text: "more ",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            client.obliterateRange({
                start: 0,
                end: "hello world".length,
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 2,
                overwrite: false,
                opArgs: undefined,
            });
            assert_1.strict.equal(client.getText(), "");
        });
        it("removes text for insert then obliterate", () => {
            (0, testUtils_1.insertText)({
                mergeTree: client.mergeTree,
                pos: 5,
                refSeq,
                clientId: remoteClientId + 1,
                seq: refSeq + 1,
                text: "more ",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            client.obliterateRange({
                start: 1,
                end: "hello world".length,
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 2,
                overwrite: false,
                opArgs: undefined,
            });
            assert_1.strict.equal(client.getText(), "h");
        });
    });
    describe("endpoint behavior", () => {
        it("does not expand to include text inserted at start", () => {
            client.obliterateRange({
                start: 5,
                end: "hello world".length,
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 1,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.insertText)({
                mergeTree: client.mergeTree,
                pos: 5,
                refSeq,
                clientId: remoteClientId + 1,
                seq: refSeq + 2,
                text: "XXX",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            assert_1.strict.equal(client.getText(), "helloXXX");
        });
        it("does not expand to include text inserted at end", () => {
            client.obliterateRange({
                start: 0,
                end: "hello".length,
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 1,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.insertText)({
                mergeTree: client.mergeTree,
                pos: 5,
                refSeq,
                clientId: remoteClientId + 1,
                seq: refSeq + 2,
                text: "XXX",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            assert_1.strict.equal(client.getText(), "XXX world");
        });
    });
    describe("local obliterate with concurrent inserts", () => {
        it("removes range when pending local obliterate op", () => {
            client.obliterateRangeLocal(0, "hello world".length);
            (0, testUtils_1.insertText)({
                mergeTree: client.mergeTree,
                pos: 1,
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 2,
                text: "XXX",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            assert_1.strict.equal(client.getText(), "");
        });
    });
});
//# sourceMappingURL=obliterate.spec.cjs.map