"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 constants_js_1 = require("../constants.js");
const properties_js_1 = require("../properties.js");
const segmentPropertiesManager_js_1 = require("../segmentPropertiesManager.js");
describe("PropertiesManager", () => {
    describe("handleProperties", () => {
        it("should handle properties without collaboration", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const seg = {
                properties: { key: "value" },
            };
            const op = { props: { key: "newValue" } };
            const deltas = propertiesManager.handleProperties(op, seg, 1, 0, false);
            node_assert_1.strict.deepEqual(deltas, { key: "value" });
            node_assert_1.strict.deepEqual(seg.properties, { key: "newValue" });
        });
        it("should handle properties with collaboration", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const seg = {
                properties: { key: "value" },
            };
            const op = { props: { key: "newValue" } };
            const deltas = propertiesManager.handleProperties(op, seg, constants_js_1.UnassignedSequenceNumber, 0, true);
            node_assert_1.strict.deepEqual(deltas, { key: "value" });
            node_assert_1.strict.deepEqual(seg.properties, { key: "newValue" });
        });
        it("should handle properties with rollback", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const seg = {
                properties: { key: "value" },
            };
            const op = { props: { key: "newValue" } };
            // Simulate pending state for rollback
            propertiesManager.handleProperties(op, seg, constants_js_1.UnassignedSequenceNumber, 0, true);
            const deltas = propertiesManager.handleProperties(op, seg, constants_js_1.UnassignedSequenceNumber, 0, true, segmentPropertiesManager_js_1.PropertiesRollback.Rollback);
            node_assert_1.strict.deepEqual(deltas, { key: "newValue" });
            node_assert_1.strict.deepEqual(seg.properties, { key: "value" });
        });
        it("should handle properties with seq as a number and collaborating true", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const seg = {
                properties: { key: "value" },
            };
            const op = { props: { key: "newValue" } };
            const deltas = propertiesManager.handleProperties(op, seg, 2, 1, true);
            node_assert_1.strict.deepEqual(deltas, { key: "value" });
            node_assert_1.strict.deepEqual(seg.properties, { key: "newValue" });
        });
        it("should handle properties with seq as a number and collaborating false", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const seg = {
                properties: { key: "value" },
            };
            const op = { props: { key: "newValue" } };
            const deltas = propertiesManager.handleProperties(op, seg, 2, 1, false);
            node_assert_1.strict.deepEqual(deltas, { key: "value" });
            node_assert_1.strict.deepEqual(seg.properties, { key: "newValue" });
        });
        it("should handle properties with adjusts", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const seg = {
                properties: { key: 1 },
            };
            const op = { adjust: { key: { delta: 1 } } };
            const deltas = propertiesManager.handleProperties(op, seg, 2, 1, true);
            node_assert_1.strict.deepEqual(deltas, { key: 1 });
            node_assert_1.strict.deepEqual(seg.properties, { key: 2 });
        });
        it("should handle properties with props and adjusts interleaved", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const seg = {
                properties: { key: 1, otherKey: "value" },
            };
            const op1 = { props: { otherKey: "newValue" } };
            const op2 = { adjust: { key: { delta: 1 } } };
            propertiesManager.handleProperties(op1, seg, 2, 1, true);
            const deltas = propertiesManager.handleProperties(op2, seg, 3, 2, true);
            node_assert_1.strict.deepEqual(deltas, { key: 1 });
            node_assert_1.strict.deepEqual(seg.properties, { key: 2, otherKey: "newValue" });
        });
    });
    describe("rollbackProperties", () => {
        it("should rollback properties when collaborating is true", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const seg = {
                properties: { key: "value" },
            };
            const op = { props: { key: "newValue" } };
            const rollbackDeltas = propertiesManager.handleProperties(op, seg, constants_js_1.UnassignedSequenceNumber, 0, true);
            const deltas = propertiesManager.rollbackProperties({ props: rollbackDeltas }, seg, true);
            node_assert_1.strict.deepEqual(deltas, { key: "newValue" });
            node_assert_1.strict.deepEqual(seg.properties, { key: "value" });
        });
        it("should rollback properties when collaborating is false", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const seg = {
                properties: { key: "value" },
            };
            const op = { props: { key: "newValue" } };
            const rollbackDeltas = propertiesManager.handleProperties(op, seg, constants_js_1.UnassignedSequenceNumber, 0, false);
            const deltas = propertiesManager.rollbackProperties({ props: rollbackDeltas }, seg, false);
            node_assert_1.strict.deepEqual(deltas, { key: "newValue" });
            node_assert_1.strict.deepEqual(seg.properties, { key: "value" });
        });
    });
    describe("ack", () => {
        it("should acknowledge property changes", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const op = { props: { key: "value" } };
            const seg = { properties: {} };
            propertiesManager.handleProperties(op, seg, constants_js_1.UnassignedSequenceNumber, 1, true);
            (0, node_assert_1.strict)(propertiesManager.hasPendingProperties({ key: "value" }));
            propertiesManager.ack(1, 0, op);
        });
    });
    describe("copyTo", () => {
        it("should copy properties and manager state", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const op = { props: { key: "value" } };
            const seg = { properties: {} };
            propertiesManager.handleProperties(op, seg, constants_js_1.UnassignedSequenceNumber, 1, true);
            (0, node_assert_1.strict)(propertiesManager.hasPendingProperties({ key: "value" }));
            const dest = {};
            propertiesManager.copyTo({ key: "value" }, dest);
            (0, node_assert_1.strict)(dest.propertyManager instanceof segmentPropertiesManager_js_1.PropertiesManager);
            (0, node_assert_1.strict)(dest.propertyManager.hasPendingProperties({ key: "value" }));
        });
    });
    describe("getAtSeq", () => {
        it("should retrieve properties at a specific sequence number", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const op = { adjust: { key: { delta: 5 } } };
            const seg = { properties: {} };
            propertiesManager.handleProperties(op, seg, 1, 0, true);
            const properties = propertiesManager.getAtSeq(seg.properties, 0);
            (0, node_assert_1.strict)((0, properties_js_1.matchProperties)(properties, {}));
        });
    });
    describe("hasPendingProperties", () => {
        it("should check for pending properties", () => {
            const propertiesManager = new segmentPropertiesManager_js_1.PropertiesManager();
            const op = { props: { key: "value" } };
            const seg = { properties: {} };
            propertiesManager.handleProperties(op, seg, constants_js_1.UnassignedSequenceNumber, 1, true);
            (0, node_assert_1.strict)(propertiesManager.hasPendingProperties({ key: "value" }));
            (0, node_assert_1.strict)(!propertiesManager.hasPendingProperties({ otherKey: "otherValue" }));
        });
    });
});
//# sourceMappingURL=propertyManager.spec.js.map