"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 mergeTreeNodes_js_1 = require("../mergeTreeNodes.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("insertText", () => {
        it("Insert starting text", () => {
            let eventCalled = 0;
            mergeTree.mergeTreeDeltaCallback = (opArgs, deltaArgs) => {
                eventCalled++;
            };
            mergeTree.insertSegments(0, [textSegment_js_1.TextSegment.make("more ")], mergeTree.localPerspective, mergeTree.collabWindow.mintNextLocalOperationStamp(), { op: { type: ops_js_1.MergeTreeDeltaType.INSERT } });
            node_assert_1.strict.equal(eventCalled, 1);
        });
        it("Insert ending text", () => {
            const textLength = mergeTree.getLength(mergeTree.localPerspective);
            let eventCalled = 0;
            mergeTree.mergeTreeDeltaCallback = (opArgs, deltaArgs) => {
                eventCalled++;
            };
            mergeTree.insertSegments(textLength, [textSegment_js_1.TextSegment.make("more ")], mergeTree.localPerspective, mergeTree.collabWindow.mintNextLocalOperationStamp(), { op: { type: ops_js_1.MergeTreeDeltaType.INSERT } });
            node_assert_1.strict.equal(eventCalled, 1);
        });
        it("Insert middle text", () => {
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            mergeTree.insertSegments(4, [textSegment_js_1.TextSegment.make("more ")], mergeTree.localPerspective, mergeTree.collabWindow.mintNextLocalOperationStamp(), { op: { type: ops_js_1.MergeTreeDeltaType.INSERT } });
            node_assert_1.strict.deepStrictEqual(count, {
                [ops_js_1.MergeTreeDeltaType.INSERT]: 1,
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 1,
            });
        });
        it("Insert text remote", () => {
            const remoteClient = (0, testUtils_js_1.makeRemoteClient)({ clientId: 35 });
            let remoteSequenceNumber = currentSequenceNumber;
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            mergeTree.insertSegments(0, [textSegment_js_1.TextSegment.make("more ")], remoteClient.perspectiveAt({ refSeq: currentSequenceNumber }), remoteClient.stampAt({ seq: ++remoteSequenceNumber }), { op: { type: ops_js_1.MergeTreeDeltaType.INSERT } });
            node_assert_1.strict.deepStrictEqual(count, {
                [ops_js_1.MergeTreeDeltaType.INSERT]: 1,
            });
        });
    });
    describe("insertMarker", () => {
        it("Insert marker", () => {
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            mergeTree.insertSegments(4, [mergeTreeNodes_js_1.Marker.make(ops_js_1.ReferenceType.Simple)], mergeTree.localPerspective, mergeTree.collabWindow.mintNextLocalOperationStamp(), { op: { type: ops_js_1.MergeTreeDeltaType.INSERT } });
            node_assert_1.strict.deepStrictEqual(count, {
                [ops_js_1.MergeTreeDeltaType.INSERT]: 1,
                [mergeTreeDeltaCallback_js_1.MergeTreeMaintenanceType.SPLIT]: 1,
            });
        });
    });
});
//# sourceMappingURL=mergeTree.insert.deltaCallback.spec.js.map