"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const node_assert_1 = require("node:assert");
const internal_1 = require("@fluidframework/telemetry-utils/internal");
const internal_2 = require("@fluidframework/test-runtime-utils/internal");
const index_js_1 = require("../../index.js");
const index_js_2 = require("../../opLifecycle/index.js");
describe("OpCompressor", () => {
    let compressor;
    const mockLogger = new internal_1.MockLogger();
    beforeEach(() => {
        compressor = new index_js_2.OpCompressor(mockLogger);
        mockLogger.clear();
    });
    const createSingletonBatch = (messageSize) => createBatch(1, messageSize);
    const createBatch = (length, messageSize) => {
        const messages = Array.from({ length }, () => createMessage(generateStringOfSize(messageSize)));
        return messagesToBatch(messages);
    };
    const messagesToBatch = (messages) => ({
        messages,
        contentSizeInBytes: messages
            .map((message) => JSON.stringify(message).length)
            .reduce((a, b) => a + b),
        referenceSequenceNumber: messages[0].referenceSequenceNumber,
    });
    const createMessage = (contents) => ({
        metadata: { flag: true },
        type: index_js_1.ContainerMessageType.FluidDataStoreOp,
        contents,
        referenceSequenceNumber: 0,
    });
    const generateStringOfSize = (sizeInBytes) => "0".repeat(sizeInBytes);
    const toMB = (bytes) => (bytes / (1024 * 1024)).toFixed(2);
    describe("Compressing batches", () => {
        for (const batch of [
            // batch with one small message
            createSingletonBatch(100 * 1024),
            // batch with one large message
            createSingletonBatch(100 * 100 * 1024),
        ]) {
            it(`Batch of ${batch.messages.length} ops of total size ${toMB(batch.contentSizeInBytes)} MB`, () => {
                const compressedBatch = compressor.compressBatch(batch);
                node_assert_1.strict.strictEqual(compressedBatch.messages.length, batch.messages.length);
                node_assert_1.strict.strictEqual(compressedBatch.messages[0].compression, "lz4");
                node_assert_1.strict.strictEqual(compressedBatch.messages[0].metadata?.flag, true);
            }).timeout(3000);
        }
    });
    describe("Unsupported batches", () => {
        for (const batch of [
            // large batch with small messages
            createBatch(6000, 100 * 1024),
            // small batch with large messages
            createBatch(6, 100 * 1024 * 1024),
        ]) {
            it(`Not compressing batch of ${batch.messages.length} ops of total size ${toMB(batch.contentSizeInBytes)} MB`, () => {
                node_assert_1.strict.throws(() => {
                    compressor.compressBatch(batch); // The need to cast indicates this is not going to work
                }, (0, internal_2.validateAssertionError)("Batch should not be empty and should contain a single message"), "Expected error was not thrown");
            });
        }
    });
});
//# sourceMappingURL=opCompressor.spec.js.map