"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultOptions = void 0;
const stochastic_test_utils_1 = require("@fluid-private/stochastic-test-utils");
const mergeTreeOperationRunner_js_1 = require("./mergeTreeOperationRunner.js");
const obliterateOperations_js_1 = require("./obliterateOperations.js");
const testClient_js_1 = require("./testClient.js");
const allOperations = [
    // The test model is not currently stable enough to support removeWithField: logic to look for fields
    // relies on text inside fields being numeric and text outside of fields alphabetic, but if a removal
    // operation takes away a whole field, a concurrent field replace (obliterate + insert new field content)
    // can result in numeric characters outside of fields.
    // removeWithField,
    obliterateOperations_js_1.annotateWithField,
    obliterateOperations_js_1.insertAvoidField,
    obliterateOperations_js_1.insertField,
    obliterateOperations_js_1.obliterateField,
];
// Note: this test isn't totally stable for all options matrices. E.g. there appear to be some issues with
// adding clients mid-run, which is likely related to known issues with obliterate and summarization.
exports.defaultOptions = {
    minLength: { min: 1, max: 512 },
    clients: { min: 3, max: 3 },
    opsPerRoundRange: { min: 1, max: 128 },
    rounds: 8,
    operations: allOperations,
    growthFunc: (input) => input * 2,
    insertText: obliterateOperations_js_1.generateInsertWithField,
};
// Generate a list of single character client names, support up to 69 clients
const clientNames = (0, mergeTreeOperationRunner_js_1.generateClientNames)();
function runObliterateFarmTests(opts, extraSeed) {
    (0, mergeTreeOperationRunner_js_1.doOverRange)(opts.minLength, opts.growthFunc, (minLength) => {
        for (const { name, config } of [
            {
                name: "obliterate with exact range replacement",
                config: {
                    ...opts,
                    // TODO: ensure that obliterate and inserts are not separated before enabling this
                    // applyOpDuringGeneration: true,
                },
            },
            {
                name: "obliterate exact range allowing zero length",
                config: {
                    ...opts,
                    operations: [
                        obliterateOperations_js_1.annotateWithField,
                        obliterateOperations_js_1.insertAvoidField,
                        obliterateOperations_js_1.insertField,
                        obliterateOperations_js_1.obliterateFieldZeroLength,
                    ],
                },
            },
        ])
            it(`${name}: ObliterateFarm_${minLength}`, async () => {
                const random = (0, stochastic_test_utils_1.makeRandom)(0xdeadbeef, 0xfeedbed, minLength, extraSeed ?? 0);
                const clients = [
                    new testClient_js_1.TestClient({
                        mergeTreeEnableObliterate: true,
                        mergeTreeEnableSidedObliterate: true,
                        mergeTreeEnableAnnotateAdjust: true,
                    }),
                ];
                for (const [i, c] of clients.entries())
                    c.startOrUpdateCollaboration(clientNames[i]);
                let seq = 0;
                while (clients.length < config.clients.max) {
                    for (const c of clients)
                        c.updateMinSeq(seq);
                    // Add double the number of clients each iteration
                    const targetClients = Math.max(config.clients.min, config.growthFunc(clients.length));
                    for (let cc = clients.length; cc < targetClients; cc++) {
                        const newClient = await testClient_js_1.TestClient.createFromClientSnapshot(clients[0], clientNames[cc]);
                        clients.push(newClient);
                    }
                    seq = (0, mergeTreeOperationRunner_js_1.runMergeTreeOperationRunner)(random, seq, clients, minLength, config);
                }
            }).timeout(30 * 10000);
    });
}
// More tests pass, but due to how this farm selects ops, the tests take a while to run, and merge-tree
// already a test suite on the longer side.
const describeFuzz = (0, stochastic_test_utils_1.createFuzzDescribe)({ defaultTestCount: 1 });
describeFuzz("MergeTree.Client Obliterate", ({ testCount }) => {
    if (testCount > 1) {
        (0, mergeTreeOperationRunner_js_1.doOverRange)({ min: 0, max: testCount - 1 }, (x) => x + 1, (seed) => {
            describe(`with seed ${seed}`, () => {
                runObliterateFarmTests(exports.defaultOptions, seed);
            });
        });
    }
    else {
        runObliterateFarmTests(exports.defaultOptions);
    }
});
//# sourceMappingURL=client.obliterateFarm.spec.js.map