"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-non-null-assertion */
const stochastic_test_utils_1 = require("@fluid-private/stochastic-test-utils");
const mergeTreeOperationRunner_js_1 = require("./mergeTreeOperationRunner.js");
const testClientLogger_js_1 = require("./testClientLogger.js");
const allOperations = [mergeTreeOperationRunner_js_1.removeRange, mergeTreeOperationRunner_js_1.annotateRange, mergeTreeOperationRunner_js_1.insertAtRefPos];
const defaultOptions = {
    minLength: { min: 1, max: 32 },
    opsPerRollbackRange: { min: 1, max: 32 },
    rounds: 10,
    opsPerRound: 10,
    operations: allOperations,
    growthFunc: (input) => input * 2,
};
describe("MergeTree.Client", () => {
    (0, mergeTreeOperationRunner_js_1.doOverRanges)(defaultOptions, ({ minLength, opsPerRollbackRange: opsPerRollback }) => {
        it(`RollbackFarm_${minLength} OpsPerRollback: ${opsPerRollback}`, async () => {
            const random = (0, stochastic_test_utils_1.makeRandom)(0xdeadbeef, 0xfeedbed, minLength, opsPerRollback);
            // A: readonly, B: rollback, C: rollback + edit, D: edit
            const clients = (0, testClientLogger_js_1.createClientsAtInitialState)({ initialState: "", options: { mergeTreeEnableAnnotateAdjust: true } }, "A", "B", "C", "D");
            let seq = 0;
            for (let round = 0; round < defaultOptions.rounds; round++) {
                for (const c of clients.all)
                    c.updateMinSeq(seq);
                const logger = new testClientLogger_js_1.TestClientLogger(clients.all, `Round ${round}`);
                // initialize and ack 10 random actions on either C or D
                const initialMsgs = (0, mergeTreeOperationRunner_js_1.generateOperationMessagesForClients)(random, seq, [clients.A, clients.C, clients.D], logger, defaultOptions.opsPerRound, minLength, defaultOptions.operations);
                seq = (0, mergeTreeOperationRunner_js_1.applyMessages)(seq, initialMsgs, clients.all, logger);
                logger.validate();
                // generate messages to rollback on B or C, then rollback
                const rollbackMsgs = (0, mergeTreeOperationRunner_js_1.generateOperationMessagesForClients)(random, seq, [clients.A, clients.B, clients.C], logger, opsPerRollback, minLength, defaultOptions.operations);
                while (rollbackMsgs.length > 0) {
                    const msg = rollbackMsgs.pop();
                    // TODO: The type here is probably MergeTreeDeltaType but
                    // omitting GROUP, given the typing of the rollback method.
                    // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
                    clients[msg[0].clientId].rollback({ type: msg[0].contents.type }, msg[1]);
                }
                logger.validate();
                logger.dispose();
            }
        }).timeout(30 * 10000);
    });
});
//# sourceMappingURL=client.rollbackFarm.spec.js.map