"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 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();
        mergeTree.insertSegments(0, [textSegment_js_1.TextSegment.make("hello world")], mergeTree.localPerspective, mergeTree.collabWindow.mintNextLocalOperationStamp(), 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" },
            }, mergeTree.localPerspective, mergeTree.collabWindow.mintNextLocalOperationStamp(), 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" },
            }, mergeTree.localPerspective, { seq: ++currentSequenceNumber, clientId: localClientId }, undefined);
            node_assert_1.strict.deepStrictEqual(count, {
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 1,
            });
        });
        it("Annotate over local insertion", () => {
            mergeTree.insertSegments(4, [textSegment_js_1.TextSegment.make("a")], mergeTree.localPerspective, mergeTree.collabWindow.mintNextLocalOperationStamp(), undefined);
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            mergeTree.annotateRange(3, 8, {
                props: { foo: "bar" },
            }, mergeTree.localPerspective, mergeTree.collabWindow.mintNextLocalOperationStamp(), 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 remoteClient = (0, testUtils_js_1.makeRemoteClient)({ clientId: 35 });
            let remoteSequenceNumber = currentSequenceNumber;
            mergeTree.insertSegments(4, [textSegment_js_1.TextSegment.make("a")], remoteClient.perspectiveAt({ refSeq: remoteSequenceNumber }), remoteClient.stampAt({ seq: ++remoteSequenceNumber }), undefined);
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            mergeTree.annotateRange(3, 8, {
                props: { foo: "bar" },
            }, mergeTree.localPerspective, mergeTree.collabWindow.mintNextLocalOperationStamp(), 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 remoteClient = (0, testUtils_js_1.makeRemoteClient)({ clientId: 35 });
            let remoteSequenceNumber = currentSequenceNumber;
            mergeTree.markRangeRemoved(4, 6, remoteClient.perspectiveAt({ refSeq: remoteSequenceNumber }), remoteClient.stampAt({ seq: ++remoteSequenceNumber }), undefined);
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            mergeTree.annotateRange(3, 8, {
                props: { foo: "bar" },
            }, mergeTree.localPerspective, mergeTree.collabWindow.mintNextLocalOperationStamp(), 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 remoteClient = (0, testUtils_js_1.makeRemoteClient)({ clientId: 35 });
            let remoteSequenceNumber = currentSequenceNumber;
            mergeTree.markRangeRemoved(3, 8, mergeTree.localPerspective, mergeTree.collabWindow.mintNextLocalOperationStamp(), undefined);
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            mergeTree.annotateRange(4, 6, {
                props: { foo: "bar" },
            }, remoteClient.perspectiveAt({ refSeq: remoteSequenceNumber }), remoteClient.stampAt({ seq: ++remoteSequenceNumber }), undefined);
            node_assert_1.strict.deepStrictEqual(count, {
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 2,
            });
        });
    });
});
//# sourceMappingURL=mergeTree.annotate.deltaCallback.spec.js.map