"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/runtime-utils/internal");
const internal_2 = require("@fluidframework/test-runtime-utils/internal");
const messageTypes_js_1 = require("../../messageTypes.js");
const index_js_1 = require("../../opLifecycle/index.js");
describe("opSerialization", () => {
    describe("ensureContentsDeserialized", () => {
        it("should deserialize string contents into an object", () => {
            const message = {
                contents: '{"key":"value"}',
            };
            (0, index_js_1.ensureContentsDeserialized)(message);
            node_assert_1.strict.deepStrictEqual(message.contents, { key: "value" });
        });
        it("should not modify or replace contents if they are already deserialized", () => {
            const contents = { key: "value" };
            const message = {
                contents,
            };
            (0, index_js_1.ensureContentsDeserialized)(message);
            node_assert_1.strict.equal(message.contents, contents);
        });
        it("should not modify contents if they are an empty string", () => {
            const message = {
                contents: "",
            };
            (0, index_js_1.ensureContentsDeserialized)(message);
            node_assert_1.strict.strictEqual(message.contents, "");
        });
    });
    describe("serializeOp", () => {
        it("should serialize an op with no handles just like JSON.stringify", () => {
            const op = {
                type: messageTypes_js_1.ContainerMessageType.Alias,
                contents: { internalId: "123", alias: "testAlias" },
            };
            const serialized = (0, index_js_1.serializeOp)(op);
            node_assert_1.strict.strictEqual(serialized.content, JSON.stringify(op));
        });
        it("should replace Fluid handles with their encoded form", () => {
            const mockHandle = new internal_2.MockHandle({});
            Object.assign(mockHandle, { foo: "should not be serialized" });
            const op = {
                type: messageTypes_js_1.ContainerMessageType.FluidDataStoreOp,
                contents: {
                    address: "123",
                    contents: { type: "op", content: { address: "test", contents: mockHandle } },
                },
            };
            const serialized = (0, index_js_1.serializeOp)(op);
            (0, node_assert_1.strict)(!serialized.content.includes("foo"), "Serialized op should not include the handle's properties besides the path");
            (0, node_assert_1.strict)(serialized.content.includes("__fluid_handle__") &&
                serialized.content.includes(mockHandle.absolutePath), "Serialized op should include the handle's path and encoded form");
        });
        it("should encode an object with already-encoded handles equivalently to JSON.stringify", () => {
            const op = {
                type: messageTypes_js_1.ContainerMessageType.FluidDataStoreOp,
                contents: {
                    address: "123",
                    contents: {
                        type: "op",
                        content: {
                            address: "test",
                            contents: (0, internal_1.encodeHandleForSerialization)(new internal_2.MockHandle({})),
                        },
                    },
                },
            };
            const serializedWithFunction = (0, index_js_1.serializeOp)(op);
            const serializedWithJSONStringify = JSON.stringify(op);
            node_assert_1.strict.strictEqual(serializedWithFunction.content, serializedWithJSONStringify);
        });
    });
});
//# sourceMappingURL=opSerialization.spec.js.map