/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { strict as assert } from "assert";
import { validateAssertionError } from "@fluidframework/test-runtime-utils";
import { rootFieldKey } from "../../../core/index.js";
import { TreeCompressionStrategy, cursorForJsonableTreeField, makeFieldBatchCodec, } from "../../../feature-libraries/index.js";
import { makeForestSummarizerCodec,
// eslint-disable-next-line import/no-internal-modules
 } from "../../../feature-libraries/forest-summary/codec.js";
import { emptySchema } from "../../cursorTestSuite.js";
// eslint-disable-next-line import/no-internal-modules
import { version } from "../../../feature-libraries/forest-summary/format.js";
import { chunkField, defaultChunkPolicy,
// eslint-disable-next-line import/no-internal-modules
 } from "../../../feature-libraries/chunked-forest/chunkTree.js";
import { brand } from "../../../util/index.js";
import { typeboxValidator } from "../../../external-utilities/index.js";
const codecOptions = { jsonValidator: typeboxValidator };
const fieldBatchCodec = makeFieldBatchCodec(codecOptions);
const context = {
    encodeType: TreeCompressionStrategy.Uncompressed,
};
const codec = makeForestSummarizerCodec(codecOptions, fieldBatchCodec);
const testFieldChunks = chunkField(cursorForJsonableTreeField([{ type: emptySchema.name }]), defaultChunkPolicy);
assert(testFieldChunks.length === 1);
const testFieldChunk = testFieldChunks[0];
const malformedData = [
    [
        "additional piece of data in entry",
        new Map([[rootFieldKey, [testFieldChunk.cursor(), "additional data"]]]),
    ],
    ["incorrect data type", ["incorrect data type"]],
];
const validData = [
    [
        "no entry",
        new Map(),
        {
            version,
            keys: [],
            fields: fieldBatchCodec.encode([], context),
        },
    ],
    [
        "single entry",
        new Map([[rootFieldKey, testFieldChunk.cursor()]]),
        {
            version,
            keys: [rootFieldKey],
            fields: fieldBatchCodec.encode([testFieldChunk.cursor()], context),
        },
    ],
    [
        "multiple entries",
        new Map([
            [rootFieldKey, testFieldChunk.cursor()],
            [brand("X"), testFieldChunk.cursor()],
        ]),
        undefined,
    ],
];
describe("ForestSummarizerCodec", () => {
    describe("encodes and decodes valid data.", () => {
        for (const [name, data, expected] of validData) {
            it(name, () => {
                const encodedData = codec.encode(data, context);
                if (expected !== undefined) {
                    assert.deepEqual(encodedData, expected);
                }
                const decodedData = codec.decode(encodedData, context);
                assert.deepEqual(decodedData, data);
            });
        }
    });
    describe("throws on receiving malformed data during encode.", () => {
        for (const [name, data] of malformedData) {
            it(name, () => {
                assert.throws(() => codec.encode(data, context), "malformed data");
            });
        }
    });
    describe("throws on receiving malformed data during decode.", () => {
        it("invalid version", () => {
            assert.throws(() => codec.decode({
                version: 2.0,
                fields: { version: 1 },
                keys: [],
            }, context), (e) => validateAssertionError(e, "version being decoded is not supported"));
        });
        it("invalid nested version", () => {
            assert.throws(() => codec.decode({
                version: 1.0,
                fields: { version: 2 },
                keys: [],
            }, context), (e) => validateAssertionError(e, "version being decoded is not supported"));
        });
        it("missing fields", () => {
            assert.throws(() => codec.decode({
                version: 1.0,
                keys: [],
            }, context), (e) => validateAssertionError(e, "Encoded schema should validate"));
        });
        it("extra field", () => {
            assert.throws(() => codec.decode({
                version: 1.0,
                fields: { version: 1 },
                keys: [],
                wrong: 5,
            }, context), (e) => validateAssertionError(e, "Encoded schema should validate"));
        });
    });
});
//# sourceMappingURL=forestSummarizerCodec.spec.js.map