"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/driver-definitions/internal");
const internal_2 = require("@fluidframework/telemetry-utils/internal");
const internal_3 = require("@fluidframework/test-runtime-utils/internal");
const index_js_1 = require("../../summary/index.js");
const summaryOp = {
    clientId: "cliendId",
    clientSequenceNumber: 5,
    minimumSequenceNumber: 5,
    referenceSequenceNumber: 5,
    sequenceNumber: 6,
    timestamp: 6,
    type: internal_1.MessageType.Summarize,
    contents: {
        handle: "OpHandle",
        head: "head",
        message: "message",
        parents: ["parents"],
    },
};
const summaryAckContents = {
    handle: "AckHandle",
    summaryProposal: { summarySequenceNumber: summaryOp.sequenceNumber },
};
const summaryAck = {
    clientId: "cliendId",
    clientSequenceNumber: summaryOp.clientSequenceNumber + 1,
    minimumSequenceNumber: summaryOp.sequenceNumber,
    referenceSequenceNumber: summaryOp.sequenceNumber,
    sequenceNumber: summaryOp.sequenceNumber + 1,
    timestamp: summaryOp.timestamp + 1,
    type: internal_1.MessageType.SummaryAck,
    contents: summaryAckContents,
    data: JSON.stringify(summaryAckContents),
};
const summaryNackContents = {
    message: "Nack",
    summaryProposal: { summarySequenceNumber: summaryOp.sequenceNumber },
};
const summaryNack = {
    clientId: "cliendId",
    clientSequenceNumber: summaryOp.clientSequenceNumber + 1,
    minimumSequenceNumber: summaryOp.sequenceNumber,
    referenceSequenceNumber: summaryOp.sequenceNumber,
    sequenceNumber: summaryOp.sequenceNumber + 1,
    timestamp: summaryOp.timestamp + 1,
    type: internal_1.MessageType.SummaryNack,
    contents: summaryNackContents,
    data: JSON.stringify(summaryNackContents),
};
describe("Summary Collection", () => {
    describe("latestAck", () => {
        it("Ack with op", () => {
            const dm = new internal_3.MockDeltaManager();
            const sc = new index_js_1.SummaryCollection(dm, new internal_2.MockLogger().toTelemetryLogger());
            node_assert_1.strict.equal(sc.latestAck, undefined, "last ack undefined");
            dm.emit("op", summaryOp);
            dm.emit("op", summaryAck);
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            const latestAck = sc.latestAck;
            (0, node_assert_1.strict)(latestAck !== undefined, "last ack undefined");
            node_assert_1.strict.deepEqual({
                summaryOp,
                summaryAck,
            }, sc.latestAck);
        });
        it("Ack without op", () => {
            const dm = new internal_3.MockDeltaManager();
            const sc = new index_js_1.SummaryCollection(dm, new internal_2.MockLogger().toTelemetryLogger());
            node_assert_1.strict.equal(sc.latestAck, undefined, "last ack undefined");
            dm.emit("op", summaryAck);
            node_assert_1.strict.equal(sc.latestAck, undefined, "last ack undefined");
        });
        it("Nack with op", () => {
            const dm = new internal_3.MockDeltaManager();
            const sc = new index_js_1.SummaryCollection(dm, new internal_2.MockLogger().toTelemetryLogger());
            node_assert_1.strict.equal(sc.latestAck, undefined, "last ack undefined");
            dm.emit("op", summaryAck);
            dm.emit("op", summaryNack);
            node_assert_1.strict.equal(sc.latestAck, undefined, "last ack undefined");
        });
    });
    describe("opsSinceLastAck", () => {
        it("Ack with op", () => {
            const dm = new internal_3.MockDeltaManager();
            dm.on("op", (op) => {
                dm.lastSequenceNumber = op.sequenceNumber;
            });
            const sc = new index_js_1.SummaryCollection(dm, new internal_2.MockLogger().toTelemetryLogger());
            node_assert_1.strict.equal(sc.opsSinceLastAck, 0);
            dm.emit("op", summaryOp);
            node_assert_1.strict.equal(sc.opsSinceLastAck, summaryOp.sequenceNumber);
            dm.emit("op", summaryAck);
            node_assert_1.strict.equal(sc.opsSinceLastAck, 0);
        });
        it("Nack with op", () => {
            const dm = new internal_3.MockDeltaManager();
            dm.on("op", (op) => {
                dm.lastSequenceNumber = op.sequenceNumber;
            });
            const sc = new index_js_1.SummaryCollection(dm, new internal_2.MockLogger().toTelemetryLogger());
            node_assert_1.strict.equal(sc.opsSinceLastAck, 0);
            dm.emit("op", summaryOp);
            node_assert_1.strict.equal(sc.opsSinceLastAck, summaryOp.sequenceNumber);
            dm.emit("op", summaryNack);
            node_assert_1.strict.equal(sc.opsSinceLastAck, summaryNack.sequenceNumber);
        });
        it("Ack after Nack with op", () => {
            const dm = new internal_3.MockDeltaManager();
            dm.on("op", (op) => {
                dm.lastSequenceNumber = op.sequenceNumber;
            });
            const sc = new index_js_1.SummaryCollection(dm, new internal_2.MockLogger().toTelemetryLogger());
            node_assert_1.strict.equal(sc.opsSinceLastAck, 0);
            dm.emit("op", summaryOp);
            dm.emit("op", summaryNack);
            dm.emit("op", summaryAck);
            node_assert_1.strict.equal(sc.opsSinceLastAck, summaryAck.sequenceNumber);
        });
    });
    describe("opActions", () => {
        function createSummaryCollection(deltaManager) {
            const summaryCollection = new index_js_1.SummaryCollection(deltaManager, new internal_2.MockLogger().toTelemetryLogger());
            const callCounts = {
                default: 0,
                summarize: 0,
                summaryAck: 0,
                summaryNack: 0,
            };
            summaryCollection.on("default", () => callCounts.default++);
            summaryCollection.on(internal_1.MessageType.Summarize, () => callCounts.summarize++);
            summaryCollection.on(internal_1.MessageType.SummaryAck, () => callCounts.summaryAck++);
            summaryCollection.on(internal_1.MessageType.SummaryNack, () => callCounts.summaryNack++);
            return { summaryCollection, callCounts };
        }
        it("Summary op", () => {
            const dm = new internal_3.MockDeltaManager();
            const { callCounts } = createSummaryCollection(dm);
            dm.emit("op", summaryOp);
            node_assert_1.strict.equal(callCounts.summarize, 1);
        });
        it("Summary Ack without op", () => {
            const dm = new internal_3.MockDeltaManager();
            const { callCounts } = createSummaryCollection(dm);
            dm.emit("op", summaryAck);
            node_assert_1.strict.equal(callCounts.summaryAck, 0);
        });
        it("Summary Ack with op", () => {
            const dm = new internal_3.MockDeltaManager();
            const { callCounts } = createSummaryCollection(dm);
            dm.emit("op", summaryOp);
            dm.emit("op", summaryAck);
            node_assert_1.strict.equal(callCounts.summarize, 1);
            node_assert_1.strict.equal(callCounts.summaryAck, 1);
        });
        it("Double Summary Ack with op", () => {
            const dm = new internal_3.MockDeltaManager();
            const { callCounts } = createSummaryCollection(dm);
            dm.emit("op", summaryOp);
            dm.emit("op", summaryAck);
            dm.emit("op", summaryAck);
            node_assert_1.strict.equal(callCounts.summarize, 1);
            node_assert_1.strict.equal(callCounts.summaryAck, 1);
        });
        it("Summary Nack without op", () => {
            const dm = new internal_3.MockDeltaManager();
            const { callCounts } = createSummaryCollection(dm);
            dm.emit("op", summaryNack);
            node_assert_1.strict.equal(callCounts.summaryNack, 0);
        });
        it("Summary Nack with op", () => {
            const dm = new internal_3.MockDeltaManager();
            const { callCounts } = createSummaryCollection(dm);
            dm.emit("op", summaryOp);
            dm.emit("op", summaryNack);
            node_assert_1.strict.equal(callCounts.summarize, 1);
            node_assert_1.strict.equal(callCounts.summaryNack, 1);
        });
        it("Double Summary Nack with op", () => {
            const dm = new internal_3.MockDeltaManager();
            const { callCounts } = createSummaryCollection(dm);
            dm.emit("op", summaryOp);
            dm.emit("op", summaryNack);
            dm.emit("op", summaryNack);
            node_assert_1.strict.equal(callCounts.summarize, 1);
            node_assert_1.strict.equal(callCounts.summaryNack, 1);
        });
        it("default", () => {
            const dm = new internal_3.MockDeltaManager();
            const { callCounts } = createSummaryCollection(dm);
            dm.emit("op", {});
            node_assert_1.strict.equal(callCounts.default, 1);
        });
    });
});
//# sourceMappingURL=summaryCollection.spec.js.map