"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.itCorrectlyObliterates = exports.useStrictPartialLengthChecks = exports.validateRefCount = exports.validatePartialLengths = exports.countOperations = exports.nodeOrdinalsHaveIntegrity = exports.makeRemoteClient = exports.loadTextFromFileWithMarkers = exports.loadTextFromFile = void 0;
const node_assert_1 = require("node:assert");
const node_fs_1 = __importDefault(require("node:fs"));
const mergeTreeNodeWalk_js_1 = require("../mergeTreeNodeWalk.js");
const partialLengths_js_1 = require("../partialLengths.js");
const perspective_js_1 = require("../perspective.js");
const clientTestHelper_js_1 = require("./clientTestHelper.js");
const text_js_1 = require("./text.js");
function loadTextFromFile(filename, mergeTree, segLimit = 0) {
    const content = node_fs_1.default.readFileSync(filename, "utf8");
    return (0, text_js_1.loadText)(content, mergeTree, segLimit);
}
exports.loadTextFromFile = loadTextFromFile;
function loadTextFromFileWithMarkers(filename, mergeTree, segLimit = 0) {
    const content = node_fs_1.default.readFileSync(filename, "utf8");
    return (0, text_js_1.loadText)(content, mergeTree, segLimit, true);
}
exports.loadTextFromFileWithMarkers = loadTextFromFileWithMarkers;
/**
 * Creates a "mock remote client" which allows ergonomically generating {@link Perspective}s and {@link OperationStamp}s
 * for use in tests that interact directly with a {@link MergeTree}.
 *
 * Example:
 *
 * ```typescript
 * let seq = 0;
 * const remoteClient = makeRemoteClient({ clientId: 18 });
 *
 * mergeTree.insertSegments(
 * 0,
 * [TextSegment.make("some text")],
 * remoteClient.perspectiveAt({ refSeq: seq }),
 * remoteClient.stampAt({ seq: ++seq }),
 * undefined
 * );
 * ```
 */
function makeRemoteClient({ clientId }) {
    return {
        perspectiveAt({ refSeq }) {
            return new perspective_js_1.PriorPerspective(refSeq, clientId);
        },
        stampAt({ seq }) {
            return { seq, clientId };
        },
        id: clientId,
    };
}
exports.makeRemoteClient = makeRemoteClient;
function nodeOrdinalsHaveIntegrity(block) {
    const olen = block.ordinal.length;
    for (let i = 0; i < block.childCount; i++) {
        if (block.children[i].ordinal) {
            if (olen !== block.children[i].ordinal.length - 1) {
                console.log("node integrity issue");
                return false;
            }
            if (i > 0 && block.children[i].ordinal <= block.children[i - 1].ordinal) {
                console.log("node sib integrity issue");
                return false;
            }
            if (!block.children[i].isLeaf()) {
                return nodeOrdinalsHaveIntegrity(block.children[i]);
            }
        }
        else {
            console.log(`node child ordinal not set ${i}`);
            return false;
        }
    }
    return true;
}
exports.nodeOrdinalsHaveIntegrity = nodeOrdinalsHaveIntegrity;
/**
 * Returns an object that tallies each delta and maintenance operation observed
 * for the given 'mergeTree'.
 */
function countOperations(mergeTree) {
    const counts = {};
    node_assert_1.strict.strictEqual(mergeTree.mergeTreeDeltaCallback, undefined);
    node_assert_1.strict.strictEqual(mergeTree.mergeTreeMaintenanceCallback, undefined);
    const fn = (deltaArgs) => {
        const previous = counts[deltaArgs.operation];
        counts[deltaArgs.operation] = previous === undefined ? 1 : previous + 1;
    };
    mergeTree.mergeTreeDeltaCallback = (opArgs, deltaArgs) => {
        fn(deltaArgs);
    };
    mergeTree.mergeTreeMaintenanceCallback = fn;
    return counts;
}
exports.countOperations = countOperations;
function getPartialLengths(clientId, seq, mergeTree, localSeq, mergeBlock = mergeTree.root) {
    const partialLen = mergeBlock.partialLengths?.getPartialLength(seq, clientId, localSeq);
    const perspective = localSeq === undefined
        ? new perspective_js_1.PriorPerspective(seq, clientId)
        : new perspective_js_1.LocalReconnectingPerspective(seq, clientId, localSeq);
    let actualLen = 0;
    (0, mergeTreeNodeWalk_js_1.walkAllChildSegments)(mergeBlock, (segment) => {
        if (perspective.isSegmentPresent(segment)) {
            actualLen += segment.cachedLength;
        }
        return true;
    });
    return {
        partialLen,
        actualLen,
    };
}
function validatePartialLengths(clientId, mergeTree, expectedValues = [], minRefSeqForLocalSeq = new Map(), mergeBlock = mergeTree.root) {
    function validatePartialLengthAt(seq, localSeq, len) {
        const { partialLen, actualLen } = getPartialLengths(clientId, seq, mergeTree, localSeq, mergeBlock);
        if (partialLen && partialLen < 0) {
            node_assert_1.strict.fail("Negative partial length returned");
        }
        node_assert_1.strict.equal(partialLen, actualLen, "Partial length did not match value obtained from walking all segments in the block.");
        if (len !== undefined) {
            node_assert_1.strict.equal(partialLen, len, "Partial length did not match expected value.");
        }
    }
    if (clientId === mergeTree.collabWindow.clientId) {
        mergeTree.computeLocalPartials(0);
        // We don't add entries to the local partial lengths entries that ensure that a query for a given localSeq includes any dependent removes.
        // For example, in a scenario where segments are inserted between seqs 1 and 10 causing a length increase of 10, but then this entire range
        // is removed locally at localSeq 5, computing the length of the block using partial lengths at (seq: 1, localSeq: 5) can yield a negative
        // result since the computation "sees" the removal of length 10 but only one of the inserts that this removal affected.
        //
        // In the production codepath, this doesn't matter because we only ever query for (refSeq, localSeq) for which the refSeq is at or above the original
        // context in which the edit was applied, which means this 'dependency' is always included in the query.
        // We could fix it if we wanted to by using a similar solution to what we do for non-local edits (add adjustments to the unsequenced lengths
        // to ensure whenever the removal of a segment applies, so does existence of that segment), at which point we could validate for a wider range
        // of local perspectives.
        for (const [localSeq, minRefSeq] of minRefSeqForLocalSeq.entries()) {
            for (let refSeq = minRefSeq; refSeq <= mergeTree.collabWindow.currentSeq; refSeq++) {
                validatePartialLengthAt(refSeq, localSeq);
            }
        }
    }
    else {
        // We don't use partial lengths for the local client unless it's a reconnecting perspective (we just use the mergeBlock's cachedLength field).
        for (let seq = mergeTree.collabWindow.minSeq + 1; seq <= mergeTree.collabWindow.currentSeq; seq++) {
            validatePartialLengthAt(seq);
        }
    }
    for (const { seq, len, localSeq } of expectedValues) {
        validatePartialLengthAt(seq, localSeq, len);
    }
}
exports.validatePartialLengths = validatePartialLengths;
function validateRefCount(collection) {
    if (!collection) {
        return;
    }
    const expectedLength = [...collection].length;
    // eslint-disable-next-line @typescript-eslint/dot-notation
    node_assert_1.strict.equal(collection["refCount"], expectedLength);
}
exports.validateRefCount = validateRefCount;
/**
 * Enable stricter partial length assertions inside tests
 *
 * Note that these assertions can be expensive, and so should not be enabled in
 * production code or tests that run through thousands of ops (e.g. the SharedString
 * fuzz tests).
 */
function useStrictPartialLengthChecks() {
    beforeEach("Enable strict partial lengths", () => {
        partialLengths_js_1.PartialSequenceLengths.options.verifier = partialLengths_js_1.verifyPartialLengths;
        partialLengths_js_1.PartialSequenceLengths.options.verifyExpected = partialLengths_js_1.verifyExpectedPartialLengths;
    });
    afterEach("Disable strict partial lengths", () => {
        partialLengths_js_1.PartialSequenceLengths.options.verifier = undefined;
        partialLengths_js_1.PartialSequenceLengths.options.verifyExpected = undefined;
    });
}
exports.useStrictPartialLengthChecks = useStrictPartialLengthChecks;
function createObliterateTestBody({ action, expectedText }) {
    return () => {
        const events = [];
        const helper = new clientTestHelper_js_1.ClientTestHelper({
            mergeTreeEnableSidedObliterate: true,
        });
        helper.clients.A.on("delta", (opArgs, deltaArgs) => {
            events.push(deltaArgs.operation);
        });
        action(helper);
        helper.processAllOps();
        helper.logger.validate({ baseText: expectedText });
    };
}
function itCorrectlyObliterates(args) {
    return it(args.title, createObliterateTestBody(args));
}
exports.itCorrectlyObliterates = itCorrectlyObliterates;
itCorrectlyObliterates.skip = (args) => it.skip(args.title, createObliterateTestBody(args));
itCorrectlyObliterates.only = (args) => it.only(args.title, createObliterateTestBody(args));
//# sourceMappingURL=testUtils.js.map