"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 mergeTreeNodes_1 = require("../mergeTreeNodes.cjs");
const textSegment_1 = require("../textSegment.cjs");
const constants_1 = require("../constants.cjs");
const mergeTree_1 = require("../mergeTree.cjs");
const mergeTreeNodeWalk_1 = require("../mergeTreeNodeWalk.cjs");
const testUtils_1 = require("./testUtils.cjs");
const localClientId = 17;
describe("MergeTree walks", () => {
    let mergeTree;
    beforeEach(() => {
        let initialText = "0";
        mergeTree = new mergeTree_1.MergeTree();
        mergeTree.insertSegments(0, [textSegment_1.TextSegment.make(initialText)], constants_1.UniversalSequenceNumber, constants_1.LocalClientId, constants_1.UniversalSequenceNumber, undefined);
        for (let i = 1; i < mergeTreeNodes_1.MaxNodesInBlock * mergeTreeNodes_1.MaxNodesInBlock; i++) {
            const text = i.toString();
            (0, testUtils_1.insertText)({
                mergeTree,
                pos: mergeTree.getLength(constants_1.UniversalSequenceNumber, localClientId),
                refSeq: constants_1.UniversalSequenceNumber,
                clientId: localClientId,
                seq: constants_1.UniversalSequenceNumber,
                text,
                props: undefined,
                opArgs: undefined,
            });
            initialText += text;
        }
    });
    describe("walkAllChildSegments", () => {
        function* getAllDescendantBlocks(block) {
            yield block;
            for (let i = 0; i < block.childCount; i++) {
                const child = block.children[i];
                if (!child.isLeaf()) {
                    yield* getAllDescendantBlocks(child);
                }
            }
        }
        it("visits only descendants", () => {
            for (const block of getAllDescendantBlocks(mergeTree.root)) {
                let walkedAnySegments = false;
                (0, mergeTreeNodeWalk_1.walkAllChildSegments)(block, (seg) => {
                    walkedAnySegments = true;
                    let current = seg.parent;
                    while (current !== block && current !== undefined) {
                        current = current.parent;
                    }
                    (0, assert_1.strict)(current === block, "Expected all visited segments to be descendants");
                    return true;
                });
                (0, assert_1.strict)(walkedAnySegments, "Walk should have hit segments");
            }
        });
    });
});
//# sourceMappingURL=mergeTree.walk.spec.cjs.map