"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const assert_1 = __importDefault(require("assert"));
const ops_1 = require("../ops.cjs");
const opBuilder_1 = require("../opBuilder.cjs");
const testClient_1 = require("./testClient.cjs");
const mergeTreeOperationRunner_1 = require("./mergeTreeOperationRunner.cjs");
const testClientLogger_1 = require("./testClientLogger.cjs");
describe("MergeTree.Client", () => {
    for (const filePath of fs.readdirSync(mergeTreeOperationRunner_1.replayResultsPath)) {
        it(`Replay ${filePath}`, async () => {
            const file = JSON.parse(fs.readFileSync(`${mergeTreeOperationRunner_1.replayResultsPath}/${filePath}`).toString());
            const msgClients = new Map();
            const originalClient = new testClient_1.TestClient();
            msgClients.set("A", { client: originalClient, msgs: [] });
            originalClient.insertTextLocal(0, file[0].initialText);
            originalClient.startOrUpdateCollaboration("A");
            for (const group of file) {
                for (const msg of group.msgs) {
                    (0, assert_1.default)(msg.clientId, "expected clientId to be defined");
                    if (!msgClients.has(msg.clientId)) {
                        const client = await testClient_1.TestClient.createFromClientSnapshot(originalClient, msg.clientId);
                        msgClients.set(msg.clientId, { client, msgs: [] });
                    }
                }
            }
            for (const group of file) {
                const logger = new testClientLogger_1.TestClientLogger([...msgClients.values()].map((mc) => mc.client));
                const initialText = logger.validate();
                assert_1.default.strictEqual(initialText, group.initialText, "Initial text not as expected");
                for (const msg of group.msgs) {
                    const msgClient = msgClients.get(msg.clientId);
                    while (msgClient.msgs.length > 0 &&
                        msg.referenceSequenceNumber > msgClient.client.getCurrentSeq()) {
                        msgClient.client.applyMsg(msgClient.msgs.shift());
                    }
                    const op = msg.contents;
                    msgClient.client.localTransaction(op.type === ops_1.MergeTreeDeltaType.GROUP ? op : (0, opBuilder_1.createGroupOp)(op));
                    msgClients.forEach((mc) => mc.msgs.push(msg));
                }
                msgClients.forEach((mc) => {
                    while (mc.msgs.length > 0) {
                        mc.client.applyMsg(mc.msgs.shift());
                    }
                });
                const result = logger.validate();
                assert_1.default.strictEqual(result, group.resultText, "Result text not as expected");
                logger.dispose();
            }
        }).timeout(30 * 10000);
    }
});
//# sourceMappingURL=client.replay.spec.cjs.map