"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("insertText", () => {
        it("Insert starting text", () => {
            let eventCalled = 0;
            mergeTree.mergeTreeDeltaCallback = (opArgs, deltaArgs) => {
                eventCalled++;
            };
            (0, testUtils_js_1.insertText)({
                mergeTree,
                pos: 0,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_js_1.UnassignedSequenceNumber,
                text: "more ",
                props: undefined,
                opArgs: { op: { type: ops_js_1.MergeTreeDeltaType.INSERT } },
            });
            node_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_js_1.insertText)({
                mergeTree,
                pos: textLength,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_js_1.UnassignedSequenceNumber,
                text: "more ",
                props: undefined,
                opArgs: { 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);
            (0, testUtils_js_1.insertText)({
                mergeTree,
                pos: 4,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_js_1.UnassignedSequenceNumber,
                text: "more ",
                props: undefined,
                opArgs: { 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 remoteClientId = 35;
            let remoteSequenceNumber = currentSequenceNumber;
            const count = (0, testUtils_js_1.countOperations)(mergeTree);
            (0, testUtils_js_1.insertText)({
                mergeTree,
                pos: 0,
                refSeq: currentSequenceNumber,
                clientId: remoteClientId,
                seq: ++remoteSequenceNumber,
                text: "more ",
                props: undefined,
                opArgs: { 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);
            (0, testUtils_js_1.insertMarker)({
                mergeTree,
                pos: 4,
                refSeq: currentSequenceNumber,
                clientId: localClientId,
                seq: constants_js_1.UnassignedSequenceNumber,
                behaviors: ops_js_1.ReferenceType.Simple,
                props: undefined,
                opArgs: { 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