"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const node_assert_1 = require("node:assert");
const constants_js_1 = require("../constants.js");
const mergeTree_js_1 = require("../mergeTree.js");
const mergeTreeDeltaCallback_js_1 = require("../mergeTreeDeltaCallback.js");
const ops_js_1 = require("../ops.js");
const textSegment_js_1 = require("../textSegment.js");
const testUtils_js_1 = require("./testUtils.js");
describe("MergeTree", () => {
    let mergeTree;
    const localClientId = 17;
    let currentSequenceNumber;
    beforeEach(() => {
        mergeTree = new mergeTree_js_1.MergeTree();
        (0, testUtils_js_1.insertSegments)({
            mergeTree,
            pos: 0,
            segments: [textSegment_js_1.TextSegment.make("hello world!")],
            refSeq: constants_js_1.UniversalSequenceNumber,
            clientId: constants_js_1.LocalClientId,
            seq: constants_js_1.UniversalSequenceNumber,
            opArgs: undefined,
        });
        currentSequenceNumber = 0;
        mergeTree.startCollaboration(localClientId, 
        /* minSeq: */ currentSequenceNumber, 
        /* currentSeq: */ currentSequenceNumber);
    });
    describe("markRangeRemoved", () => {
        it("Event on Removal", () => {
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            (0, testUtils_js_1.markRangeRemoved)({
                mergeTree,
                start: 4,
                end: 6,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_js_1.UnassignedSequenceNumber,
                overwrite: false,
                opArgs: undefined,
            });
            node_assert_1.strict.deepStrictEqual(count, {
                [ops_js_1.MergeTreeDeltaType.REMOVE]: 1,
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 2,
            });
        });
        // Verify that zamboni unlinks a removed segment and raises the appropriate maintenance event.
        it("Event on Unlink", () => {
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            const start = 4;
            const end = 6;
            (0, testUtils_js_1.markRangeRemoved)({
                mergeTree,
                start,
                end,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_js_1.UnassignedSequenceNumber,
                overwrite: false,
                opArgs: undefined,
            });
            // In order for the removed segment to unlinked by zamboni, we need to ACK the segment
            // and advance the collaboration window's minSeq past the removedSeq.
            mergeTree.ackPendingSegment({
                op: {
                    pos1: start,
                    pos2: end,
                    type: ops_js_1.MergeTreeDeltaType.REMOVE,
                },
                sequencedMessage: {
                    sequenceNumber: ++currentSequenceNumber,
                },
            });
            // Move currentSeq/minSeq past the seq# at which the removal was ACKed.
            mergeTree.collabWindow.currentSeq = currentSequenceNumber;
            mergeTree.setMinSeq(currentSequenceNumber);
            node_assert_1.strict.deepStrictEqual(count, {
                [ops_js_1.MergeTreeDeltaType.REMOVE]: 1,
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 2,
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.UNLINK]: 1,
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.ACKNOWLEDGED]: 1,
            });
        });
        it("Remote Before Local", () => {
            const remoteClientId = 35;
            let remoteSequenceNumber = currentSequenceNumber;
            (0, testUtils_js_1.markRangeRemoved)({
                mergeTree,
                start: 4,
                end: 6,
                refSeq: remoteSequenceNumber,
                clientId: remoteClientId,
                seq: ++remoteSequenceNumber,
                overwrite: false,
                opArgs: undefined,
            });
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            (0, testUtils_js_1.markRangeRemoved)({
                mergeTree,
                start: 3,
                end: 5,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_js_1.UnassignedSequenceNumber,
                overwrite: false,
                opArgs: undefined,
            });
            node_assert_1.strict.deepStrictEqual(count, {
                [ops_js_1.MergeTreeDeltaType.REMOVE]: 1,
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 2,
            });
        });
        it("Local Before Remote", () => {
            const remoteClientId = 35;
            let remoteSequenceNumber = currentSequenceNumber;
            (0, testUtils_js_1.markRangeRemoved)({
                mergeTree,
                start: 4,
                end: 6,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_js_1.UnassignedSequenceNumber,
                overwrite: false,
                opArgs: undefined,
            });
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            (0, testUtils_js_1.markRangeRemoved)({
                mergeTree,
                start: 3,
                end: 5,
                refSeq: remoteSequenceNumber,
                clientId: remoteClientId,
                seq: ++remoteSequenceNumber,
                overwrite: false,
                opArgs: undefined,
            });
            node_assert_1.strict.deepStrictEqual(count, {
                [ops_js_1.MergeTreeDeltaType.REMOVE]: 1,
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 2,
            });
        });
        it("Local delete shadows remote", () => {
            const remoteClientId = 35;
            let remoteSequenceNumber = currentSequenceNumber;
            (0, testUtils_js_1.markRangeRemoved)({
                mergeTree,
                start: 3,
                end: 6,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_js_1.UnassignedSequenceNumber,
                overwrite: false,
                opArgs: undefined,
            });
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            (0, testUtils_js_1.markRangeRemoved)({
                mergeTree,
                start: 4,
                end: 5,
                refSeq: remoteSequenceNumber,
                clientId: remoteClientId,
                seq: ++remoteSequenceNumber,
                overwrite: false,
                opArgs: undefined,
            });
            node_assert_1.strict.deepStrictEqual(count, {
                /* MergeTreeDeltaType.REMOVE is absent as it should not be fired. */
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 2,
            });
        });
    });
});
//# sourceMappingURL=mergeTree.markRangeRemoved.deltaCallback.spec.js.map