"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("annotateRange", () => {
        it("Event on annotation", () => {
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            mergeTree.annotateRange(4, 6, {
                props: { foo: "bar" },
            }, currentSequenceNumber, localClientId, constants_js_1.UnassignedSequenceNumber, undefined);
            node_assert_1.strict.deepStrictEqual(count, {
                [ops_js_1.MergeTreeDeltaType.ANNOTATE]: 1,
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 2,
            });
        });
        it("No event on annotation of empty range", () => {
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            mergeTree.annotateRange(3, 3, {
                props: { foo: "bar" },
            }, currentSequenceNumber, localClientId, ++currentSequenceNumber, undefined);
            node_assert_1.strict.deepStrictEqual(count, {
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 1,
            });
        });
        it("Annotate over local insertion", () => {
            (0, testUtils_js_1.insertText)({
                mergeTree,
                pos: 4,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_js_1.UnassignedSequenceNumber,
                text: "a",
                props: undefined,
                opArgs: undefined,
            });
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            mergeTree.annotateRange(3, 8, {
                props: { foo: "bar" },
            }, currentSequenceNumber, localClientId, constants_js_1.UnassignedSequenceNumber, undefined);
            node_assert_1.strict.deepStrictEqual(count, {
                [ops_js_1.MergeTreeDeltaType.ANNOTATE]: 1,
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 2,
            });
        });
        it("Annotate over remote insertion", () => {
            const remoteClientId = 35;
            let remoteSequenceNumber = currentSequenceNumber;
            (0, testUtils_js_1.insertText)({
                mergeTree,
                pos: 4,
                refSeq: remoteSequenceNumber,
                clientId: remoteClientId,
                seq: ++remoteSequenceNumber,
                text: "a",
                props: undefined,
                opArgs: undefined,
            });
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            mergeTree.annotateRange(3, 8, {
                props: { foo: "bar" },
            }, currentSequenceNumber, localClientId, constants_js_1.UnassignedSequenceNumber, undefined);
            node_assert_1.strict.deepStrictEqual(count, {
                [ops_js_1.MergeTreeDeltaType.ANNOTATE]: 1,
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 2,
            });
        });
        it("Annotate over remote deletion", () => {
            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);
            mergeTree.annotateRange(3, 8, {
                props: { foo: "bar" },
            }, currentSequenceNumber, localClientId, constants_js_1.UnassignedSequenceNumber, undefined);
            node_assert_1.strict.deepStrictEqual(count, {
                [ops_js_1.MergeTreeDeltaType.ANNOTATE]: 1,
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 2,
            });
        });
        it("Remote annotate within local deletion", () => {
            const remoteClientId = 35;
            let remoteSequenceNumber = currentSequenceNumber;
            (0, testUtils_js_1.markRangeRemoved)({
                mergeTree,
                start: 3,
                end: 8,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_js_1.UnassignedSequenceNumber,
                overwrite: false,
                opArgs: undefined,
            });
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            mergeTree.annotateRange(4, 6, {
                props: { foo: "bar" },
            }, remoteSequenceNumber, remoteClientId, ++remoteSequenceNumber, undefined);
            node_assert_1.strict.deepStrictEqual(count, {
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 2,
            });
        });
    });
});
//# sourceMappingURL=mergeTree.annotate.deltaCallback.spec.js.map