"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 constants_1 = require("../constants.cjs");
const mergeTreeDeltaCallback_1 = require("../mergeTreeDeltaCallback.cjs");
const mergeTree_1 = require("../mergeTree.cjs");
const ops_1 = require("../ops.cjs");
const textSegment_1 = require("../textSegment.cjs");
const testUtils_1 = require("./testUtils.cjs");
describe("MergeTree", () => {
    let mergeTree;
    const localClientId = 17;
    let currentSequenceNumber;
    beforeEach(() => {
        mergeTree = new mergeTree_1.MergeTree();
        (0, testUtils_1.insertSegments)({
            mergeTree,
            pos: 0,
            segments: [textSegment_1.TextSegment.make("hello world!")],
            refSeq: constants_1.UniversalSequenceNumber,
            clientId: constants_1.LocalClientId,
            seq: constants_1.UniversalSequenceNumber,
            opArgs: undefined,
        });
        currentSequenceNumber = 0;
        mergeTree.startCollaboration(localClientId, 
        /* minSeq: */ currentSequenceNumber, 
        /* currentSeq: */ currentSequenceNumber);
    });
    describe("insertText", () => {
        it("Insert starting text", () => {
            let eventCalled = 0;
            mergeTree.mergeTreeDeltaCallback = (opArgs, deltaArgs) => {
                eventCalled++;
            };
            (0, testUtils_1.insertText)({
                mergeTree,
                pos: 0,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_1.UnassignedSequenceNumber,
                text: "more ",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            assert_1.strict.equal(eventCalled, 1);
        });
        it("Insert ending text", () => {
            const textLength = mergeTree.getLength(currentSequenceNumber, localClientId);
            let eventCalled = 0;
            mergeTree.mergeTreeDeltaCallback = (opArgs, deltaArgs) => {
                eventCalled++;
            };
            (0, testUtils_1.insertText)({
                mergeTree,
                pos: textLength,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_1.UnassignedSequenceNumber,
                text: "more ",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            assert_1.strict.equal(eventCalled, 1);
        });
        it("Insert middle text", () => {
            const count = (0, testUtils_1.countOperations)(mergeTree);
            (0, testUtils_1.insertText)({
                mergeTree,
                pos: 4,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_1.UnassignedSequenceNumber,
                text: "more ",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            assert_1.strict.deepStrictEqual(count, {
                [ops_1.MergeTreeDeltaType.INSERT]: 1,
                [mergeTreeDeltaCallback_1.MergeTreeMaintenanceType.SPLIT]: 1,
            });
        });
        it("Insert text remote", () => {
            const remoteClientId = 35;
            let remoteSequenceNumber = currentSequenceNumber;
            const count = (0, testUtils_1.countOperations)(mergeTree);
            (0, testUtils_1.insertText)({
                mergeTree,
                pos: 0,
                refSeq: currentSequenceNumber,
                clientId: remoteClientId,
                seq: ++remoteSequenceNumber,
                text: "more ",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            assert_1.strict.deepStrictEqual(count, {
                [ops_1.MergeTreeDeltaType.INSERT]: 1,
            });
        });
    });
    describe("insertMarker", () => {
        it("Insert marker", () => {
            const count = (0, testUtils_1.countOperations)(mergeTree);
            (0, testUtils_1.insertMarker)({
                mergeTree,
                pos: 4,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_1.UnassignedSequenceNumber,
                behaviors: ops_1.ReferenceType.Simple,
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            assert_1.strict.deepStrictEqual(count, {
                [ops_1.MergeTreeDeltaType.INSERT]: 1,
                [mergeTreeDeltaCallback_1.MergeTreeMaintenanceType.SPLIT]: 1,
            });
        });
    });
});
//# sourceMappingURL=mergeTree.insert.deltaCallback.spec.cjs.map