"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-non-null-assertion */
const node_assert_1 = require("node:assert");
const internal_1 = require("@fluidframework/test-runtime-utils/internal");
const attributionPolicy_js_1 = require("../attributionPolicy.js");
const snapshotlegacy_js_1 = require("../snapshotlegacy.js");
const testClient_js_1 = require("./testClient.js");
const testClientLogger_js_1 = require("./testClientLogger.js");
const testSerializer_js_1 = require("./testSerializer.js");
describe("snapshot", () => {
    it("header only", async () => {
        const client1 = new testClient_js_1.TestClient();
        client1.startOrUpdateCollaboration("0");
        for (let i = 0; i < snapshotlegacy_js_1.SnapshotLegacy.sizeOfFirstChunk; i++) {
            const op = client1.insertTextLocal(client1.getLength(), `${i % 10}`, { segment: i });
            const msg = client1.makeOpMessage(op, i + 1);
            msg.minimumSequenceNumber = i + 1;
            client1.applyMsg(msg);
        }
        const serializer = new testSerializer_js_1.TestSerializer();
        const snapshot = new snapshotlegacy_js_1.SnapshotLegacy(client1.mergeTree, client1.logger);
        snapshot.extractSync();
        const summaryTree = snapshot.emit([], serializer, undefined);
        const services = internal_1.MockStorage.createFromSummary(summaryTree.summary);
        const client2 = new testClient_js_1.TestClient(undefined);
        const runtime = {
            logger: client2.logger,
            clientId: "1",
        };
        await client2.load(runtime, services, serializer);
        node_assert_1.strict.equal(client2.getLength(), client1.getLength());
        node_assert_1.strict.equal(client2.getText(), client1.getText());
    }).timeout(5000);
    it("header and body", async () => {
        const clients = [new testClient_js_1.TestClient(), new testClient_js_1.TestClient(), new testClient_js_1.TestClient()];
        clients[0].startOrUpdateCollaboration("0");
        for (let i = 0; i < snapshotlegacy_js_1.SnapshotLegacy.sizeOfFirstChunk + 10; i++) {
            const op = clients[0].insertTextLocal(clients[0].getLength(), `${i % 10}`, {
                segment: i,
            });
            const msg = clients[0].makeOpMessage(op, i + 1);
            msg.minimumSequenceNumber = i + 1;
            clients[0].applyMsg(msg);
        }
        const serializer = new testSerializer_js_1.TestSerializer();
        for (let i = 0; i < clients.length - 1; i++) {
            const client1 = clients[i];
            const client2 = clients[i + 1];
            const snapshot = new snapshotlegacy_js_1.SnapshotLegacy(client1.mergeTree, client1.logger);
            snapshot.extractSync();
            const summaryTree = snapshot.emit([], serializer, undefined);
            const services = internal_1.MockStorage.createFromSummary(summaryTree.summary);
            const runtime = {
                logger: client2.logger,
                clientId: (i + 1).toString(),
            };
            await client2.load(runtime, services, serializer);
            const client2Len = client2.getLength();
            node_assert_1.strict.equal(client2Len, client1.getLength(), `client${client2.longClientId} and client${client1.longClientId} lengths don't match`);
            node_assert_1.strict.equal(client2.getText(snapshotlegacy_js_1.SnapshotLegacy.sizeOfFirstChunk - 1), client1.getText(snapshotlegacy_js_1.SnapshotLegacy.sizeOfFirstChunk - 1));
        }
    }).timeout(5000);
    async function assertAttributionKeysMatch(client, expected) {
        node_assert_1.strict.deepEqual(client.getAllAttributionSeqs(), expected.root, "Keys don't match before round-tripping");
        for (const [channel, channelExpectation] of Object.entries(expected.channels ?? {})) {
            node_assert_1.strict.deepEqual(client.getAllAttributionSeqs(channel), channelExpectation, `Keys for channel ${channel} don't match before round-trip.`);
        }
        const serializer = new testSerializer_js_1.TestSerializer();
        // This avoids necessitating handling catchup ops.
        client.mergeTree.setMinSeq(client.mergeTree.collabWindow.currentSeq);
        const snapshot = new snapshotlegacy_js_1.SnapshotLegacy(client.mergeTree, client.logger);
        snapshot.extractSync();
        const summaryTree = snapshot.emit([], serializer, undefined);
        const services = internal_1.MockStorage.createFromSummary(summaryTree.summary);
        const roundTripClient = new testClient_js_1.TestClient({
            attribution: {
                track: true,
                policyFactory: attributionPolicy_js_1.createInsertOnlyAttributionPolicy,
            },
        });
        const runtime = {
            logger: roundTripClient.logger,
            clientId: "round-trips summary",
        };
        await roundTripClient.load(runtime, services, serializer);
        node_assert_1.strict.deepEqual(roundTripClient.getAllAttributionSeqs(), expected.root, "Keys don't match after round-tripping");
        for (const [channel, channelExpectation] of Object.entries(expected.channels ?? {})) {
            node_assert_1.strict.deepEqual(roundTripClient.getAllAttributionSeqs(channel), channelExpectation, `Keys for channel ${channel} don't match after round-trip.`);
        }
    }
    it("preserves attribution information", async () => {
        const clients = (0, testClientLogger_js_1.createClientsAtInitialState)({
            initialState: "",
            options: {
                attribution: {
                    track: true,
                    policyFactory: (0, attributionPolicy_js_1.createPropertyTrackingAndInsertionAttributionPolicyFactory)("foo"),
                },
            },
        }, "A", "B");
        const ops = [];
        const applyAllOps = () => {
            for (const op of ops.splice(0))
                clients.all.map((client) => client.applyMsg(op));
        };
        ops.push(clients.A.makeOpMessage(clients.A.insertTextLocal(0, "hello world"), /* seq */ 1));
        applyAllOps();
        ops.push(clients.B.makeOpMessage(clients.B.insertTextLocal(6, "new "), 
        /* seq */ 2, 
        /* refSeq */ 1), clients.B.makeOpMessage(clients.B.annotateRangeLocal(0, 14, { foo: "bar" }), 
        /* seq */ 3, 
        /* refSeq */ 2));
        applyAllOps();
        await assertAttributionKeysMatch(clients.A, {
            // "hello " has key 1 (i.e. seq 1), "new " has key 2 (i.e. seq 2), "world" has key 1.
            root: [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1],
            channels: {
                foo: Array.from({ length: "hello new world".length }, (_, i) => i < 14 ? 3 : undefined),
            },
        });
    });
    it("doesn't include attribution information when attribution tracking is false on doc creation", async () => {
        const clients = (0, testClientLogger_js_1.createClientsAtInitialState)({
            initialState: "",
            options: { attribution: { track: false } },
        }, "A");
        const ops = [];
        const applyAllOps = () => {
            for (const op of ops.splice(0))
                clients.all.map((client) => client.applyMsg(op));
        };
        ops.push(clients.A.makeOpMessage(clients.A.insertTextLocal(0, "hello world"), /* seq */ 1));
        applyAllOps();
        await assertAttributionKeysMatch(clients.A, {
            root: Array.from({ length: "hello world".length }, () => undefined),
        });
    });
});
//# sourceMappingURL=snapshotlegacy.spec.js.map