"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 mergeTreeNodeWalk_js_1 = require("../mergeTreeNodeWalk.js");
const mergeTreeNodes_js_1 = require("../mergeTreeNodes.js");
const textSegment_js_1 = require("../textSegment.js");
const testUtils_js_1 = require("./testUtils.js");
const localClientId = 17;
describe("MergeTree walks", () => {
    let mergeTree;
    beforeEach(() => {
        let initialText = "0";
        mergeTree = new mergeTree_js_1.MergeTree();
        mergeTree.insertSegments(0, [textSegment_js_1.TextSegment.make(initialText)], constants_js_1.UniversalSequenceNumber, constants_js_1.LocalClientId, constants_js_1.UniversalSequenceNumber, undefined);
        for (let i = 1; i < mergeTreeNodes_js_1.MaxNodesInBlock * mergeTreeNodes_js_1.MaxNodesInBlock; i++) {
            const text = i.toString();
            (0, testUtils_js_1.insertText)({
                mergeTree,
                pos: mergeTree.getLength(constants_js_1.UniversalSequenceNumber, localClientId),
                refSeq: constants_js_1.UniversalSequenceNumber,
                clientId: localClientId,
                seq: constants_js_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_js_1.walkAllChildSegments)(block, (seg) => {
                    walkedAnySegments = true;
                    let current = seg.parent;
                    while (current !== block && current !== undefined) {
                        current = current.parent;
                    }
                    (0, node_assert_1.strict)(current === block, "Expected all visited segments to be descendants");
                    return true;
                });
                (0, node_assert_1.strict)(walkedAnySegments, "Walk should have hit segments");
            }
        });
    });
});
//# sourceMappingURL=mergeTree.walk.spec.js.map