"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
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 });
/* eslint-disable @typescript-eslint/no-non-null-assertion */
const node_assert_1 = __importDefault(require("node:assert"));
const fs = __importStar(require("node:fs"));
const opBuilder_js_1 = require("../opBuilder.js");
const ops_js_1 = require("../ops.js");
const mergeTreeOperationRunner_js_1 = require("./mergeTreeOperationRunner.js");
const testClient_js_1 = require("./testClient.js");
const testClientLogger_js_1 = require("./testClientLogger.js");
describe("MergeTree.Client", () => {
    for (const filePath of fs.readdirSync(mergeTreeOperationRunner_js_1.replayResultsPath)) {
        it(`Replay ${filePath}`, async () => {
            const file = JSON.parse(fs.readFileSync(`${mergeTreeOperationRunner_js_1.replayResultsPath}/${filePath}`).toString());
            const msgClients = new Map();
            const originalClient = new testClient_js_1.TestClient({ mergeTreeEnableObliterate: true });
            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, node_assert_1.default)(msg.clientId, "expected clientId to be defined");
                    if (!msgClients.has(msg.clientId)) {
                        const client = await testClient_js_1.TestClient.createFromClientSnapshot(originalClient, msg.clientId);
                        msgClients.set(msg.clientId, { client, msgs: [] });
                    }
                }
            }
            for (const group of file) {
                const logger = new testClientLogger_js_1.TestClientLogger([...msgClients.values()].map((mc) => mc.client));
                const initialText = logger.validate();
                node_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_js_1.MergeTreeDeltaType.GROUP ? op : (0, opBuilder_js_1.createGroupOp)(op));
                    for (const mc of msgClients)
                        mc[1].msgs.push(msg);
                }
                for (const mc of msgClients) {
                    while (mc[1].msgs.length > 0) {
                        mc[1].client.applyMsg(mc[1].msgs.shift());
                    }
                }
                const result = logger.validate();
                node_assert_1.default.strictEqual(result, group.resultText, "Result text not as expected");
                logger.dispose();
            }
        }).timeout(30 * 10000);
    }
});
//# sourceMappingURL=client.replay.spec.js.map