"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../constants.cjs");
const mergeTree_1 = require("../mergeTree.cjs");
const ops_1 = require("../ops.cjs");
const partialLengths_1 = require("../partialLengths.cjs");
const textSegment_1 = require("../textSegment.cjs");
const testUtils_1 = require("./testUtils.cjs");
describe("partial lengths", () => {
    let mergeTree;
    const localClientId = 17;
    const remoteClientId = 18;
    const refSeq = 0;
    beforeEach(() => {
        partialLengths_1.PartialSequenceLengths.options.verifier = partialLengths_1.verify;
        partialLengths_1.PartialSequenceLengths.options.verifyExpected = partialLengths_1.verifyExpected;
        mergeTree = new mergeTree_1.MergeTree();
        (0, testUtils_1.insertSegments)({
            mergeTree,
            pos: 0,
            segments: [textSegment_1.TextSegment.make("hello world!")],
            refSeq,
            clientId: localClientId,
            seq: 0,
            opArgs: undefined,
        });
        mergeTree.startCollaboration(localClientId, /* minSeq: */ 0, /* currentSeq: */ 0);
    });
    afterEach(() => {
        partialLengths_1.PartialSequenceLengths.options.verifier = undefined;
        partialLengths_1.PartialSequenceLengths.options.verifyExpected = undefined;
    });
    it("passes with no additional ops", () => {
        (0, testUtils_1.validatePartialLengths)(localClientId, mergeTree, [{ seq: refSeq, len: 12 }]);
    });
    describe("a single inserted element", () => {
        it("includes length of local insert for local view", () => {
            (0, testUtils_1.insertText)({
                mergeTree,
                pos: 0,
                refSeq,
                clientId: localClientId,
                seq: refSeq + 1,
                text: "more ",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            (0, testUtils_1.validatePartialLengths)(localClientId, mergeTree, [{ seq: 1, len: 17 }]);
        });
        it("includes length of local insert for remote view", () => {
            (0, testUtils_1.insertText)({
                mergeTree,
                pos: 0,
                refSeq,
                clientId: localClientId,
                seq: refSeq + 1,
                text: "more ",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            (0, testUtils_1.validatePartialLengths)(remoteClientId, mergeTree, [{ seq: 1, len: 17 }]);
        });
        it("includes length of remote insert for local view", () => {
            (0, testUtils_1.insertText)({
                mergeTree,
                pos: 0,
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 1,
                text: "more ",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            (0, testUtils_1.validatePartialLengths)(localClientId, mergeTree, [{ seq: 1, len: 17 }]);
        });
        it("includes length of remote insert for remote view", () => {
            (0, testUtils_1.insertText)({
                mergeTree,
                pos: 0,
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 1,
                text: "more ",
                props: undefined,
                opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
            });
            (0, testUtils_1.validatePartialLengths)(remoteClientId, mergeTree, [{ seq: 1, len: 17 }]);
        });
    });
    describe("a single removed segment", () => {
        it("includes result of local delete for local view", () => {
            (0, testUtils_1.markRangeRemoved)({
                mergeTree,
                start: 0,
                end: 12,
                refSeq,
                clientId: localClientId,
                seq: refSeq + 1,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.validatePartialLengths)(localClientId, mergeTree, [{ seq: 1, len: 0 }]);
        });
        it("includes result of local delete for remote view", () => {
            (0, testUtils_1.markRangeRemoved)({
                mergeTree,
                start: 0,
                end: 12,
                refSeq,
                clientId: localClientId,
                seq: refSeq + 1,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.validatePartialLengths)(remoteClientId, mergeTree, [{ seq: 1, len: 0 }]);
        });
        it("includes result of remote delete for local view", () => {
            (0, testUtils_1.markRangeRemoved)({
                mergeTree,
                start: 0,
                end: 12,
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 1,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.validatePartialLengths)(localClientId, mergeTree, [{ seq: 1, len: 0 }]);
        });
        it("includes result of remote delete for remote view", () => {
            (0, testUtils_1.markRangeRemoved)({
                mergeTree,
                start: 0,
                end: 12,
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 1,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.validatePartialLengths)(remoteClientId, mergeTree, [{ seq: 1, len: 0 }]);
        });
    });
    describe("aggregation", () => {
        it("includes lengths from multiple permutations in single tree", () => {
            (0, testUtils_1.insertSegments)({
                mergeTree,
                pos: 0,
                segments: [textSegment_1.TextSegment.make("1")],
                refSeq,
                clientId: localClientId,
                seq: refSeq + 1,
                opArgs: undefined,
            });
            (0, testUtils_1.insertSegments)({
                mergeTree,
                pos: 0,
                segments: [textSegment_1.TextSegment.make("2")],
                refSeq: refSeq + 1,
                clientId: remoteClientId,
                seq: refSeq + 2,
                opArgs: undefined,
            });
            (0, testUtils_1.insertSegments)({
                mergeTree,
                pos: 0,
                segments: [textSegment_1.TextSegment.make("3")],
                refSeq: refSeq + 2,
                clientId: localClientId,
                seq: refSeq + 3,
                opArgs: undefined,
            });
            (0, testUtils_1.insertSegments)({
                mergeTree,
                pos: 0,
                segments: [textSegment_1.TextSegment.make("4")],
                refSeq: refSeq + 3,
                clientId: remoteClientId,
                seq: refSeq + 4,
                opArgs: undefined,
            });
            (0, testUtils_1.validatePartialLengths)(localClientId, mergeTree, [{ seq: 4, len: 16 }]);
            (0, testUtils_1.validatePartialLengths)(remoteClientId, mergeTree, [{ seq: 4, len: 16 }]);
        });
        it("is correct for different heights", () => {
            for (let i = 0; i < 100; i++) {
                (0, testUtils_1.insertText)({
                    mergeTree,
                    pos: 0,
                    refSeq: i,
                    clientId: localClientId,
                    seq: i + 1,
                    text: "a",
                    props: undefined,
                    opArgs: { op: { type: ops_1.MergeTreeDeltaType.INSERT } },
                });
                (0, testUtils_1.validatePartialLengths)(localClientId, mergeTree, [{ seq: i + 1, len: i + 13 }]);
                (0, testUtils_1.validatePartialLengths)(remoteClientId, mergeTree, [{ seq: i + 1, len: i + 13 }]);
            }
            (0, testUtils_1.validatePartialLengths)(localClientId, mergeTree, [{ seq: 100, len: 112 }]);
            (0, testUtils_1.validatePartialLengths)(remoteClientId, mergeTree, [{ seq: 100, len: 112 }]);
        });
    });
    describe("concurrent, overlapping deletes", () => {
        it("concurrent remote changes are visible to local", () => {
            (0, testUtils_1.markRangeRemoved)({
                mergeTree,
                start: 0,
                end: 10,
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 1,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.markRangeRemoved)({
                mergeTree,
                start: 0,
                end: 10,
                refSeq,
                clientId: remoteClientId + 1,
                seq: refSeq + 2,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.validatePartialLengths)(localClientId, mergeTree, [{ seq: 1, len: 2 }]);
        });
        it("concurrent local and remote changes are visible", () => {
            (0, testUtils_1.markRangeRemoved)({
                mergeTree,
                start: 0,
                end: 10,
                refSeq,
                clientId: localClientId,
                seq: refSeq + 1,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.markRangeRemoved)({
                mergeTree,
                start: 0,
                end: 10,
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 2,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.validatePartialLengths)(localClientId, mergeTree, [{ seq: 1, len: 2 }]);
            (0, testUtils_1.validatePartialLengths)(remoteClientId, mergeTree, [{ seq: 1, len: 2 }]);
        });
        it("concurrent remote and unsequenced local changes are visible", () => {
            (0, testUtils_1.markRangeRemoved)({
                mergeTree,
                start: 0,
                end: 10,
                refSeq,
                clientId: localClientId,
                seq: constants_1.UnassignedSequenceNumber,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.markRangeRemoved)({
                mergeTree,
                start: 0,
                end: 10,
                refSeq,
                clientId: remoteClientId,
                seq: refSeq + 1,
                overwrite: false,
                opArgs: undefined,
            });
            (0, testUtils_1.validatePartialLengths)(localClientId, mergeTree, [{ seq: 1, len: 2 }]);
            (0, testUtils_1.validatePartialLengths)(remoteClientId, mergeTree, [{ seq: 1, len: 2 }]);
        });
    });
});
//# sourceMappingURL=partialLength.spec.cjs.map