"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = require("assert");
const constants_1 = require("../constants.cjs");
const mergeTreeNodes_1 = require("../mergeTreeNodes.cjs");
const ops_1 = require("../ops.cjs");
const referencePositions_1 = require("../referencePositions.cjs");
const textSegment_1 = require("../textSegment.cjs");
const testClient_1 = require("./testClient.cjs");
const testUtils_1 = require("./testUtils.cjs");
describe("TestClient", () => {
    const localUserLongId = "localUser";
    let client;
    beforeEach(() => {
        client = new testClient_1.TestClient();
        (0, testUtils_1.insertSegments)({
            mergeTree: client.mergeTree,
            pos: 0,
            segments: [textSegment_1.TextSegment.make("")],
            refSeq: constants_1.UniversalSequenceNumber,
            clientId: client.getClientId(),
            seq: constants_1.UniversalSequenceNumber,
            opArgs: undefined,
        });
        client.startOrUpdateCollaboration(localUserLongId);
    });
    describe(".findTile", () => {
        it("Should be able to find non preceding tile based on label", () => {
            const tileLabel = "EOP";
            client.insertMarkerLocal(0, ops_1.ReferenceType.Tile, {
                [referencePositions_1.reservedTileLabelsKey]: [tileLabel],
                [mergeTreeNodes_1.reservedMarkerIdKey]: "some-id",
            });
            client.insertTextLocal(0, "abc");
            console.log(client.getText());
            assert_1.strict.equal(client.getLength(), 4, "length not expected");
            const tile = client.findTile(0, tileLabel, false);
            (0, assert_1.strict)(tile, "Returned tile undefined.");
            assert_1.strict.equal(tile.pos, 3, "Tile with label not at expected position");
        });
        it("Should be able to find non preceding tile position based on label from client with single tile", () => {
            const tileLabel = "EOP";
            client.insertTextLocal(0, "abc d");
            client.insertMarkerLocal(0, ops_1.ReferenceType.Tile, {
                [referencePositions_1.reservedTileLabelsKey]: [tileLabel],
                [mergeTreeNodes_1.reservedMarkerIdKey]: "some-id",
            });
            console.log(client.getText());
            assert_1.strict.equal(client.getLength(), 6, "length not expected");
            const tile = client.findTile(0, tileLabel, false);
            (0, assert_1.strict)(tile, "Returned tile undefined.");
            assert_1.strict.equal(tile.pos, 0, "Tile with label not at expected position");
        });
        it("Should be able to find preceding tile position based on label from client with multiple tile", () => {
            const tileLabel = "EOP";
            client.insertMarkerLocal(0, ops_1.ReferenceType.Tile, {
                [referencePositions_1.reservedTileLabelsKey]: [tileLabel],
                [mergeTreeNodes_1.reservedMarkerIdKey]: "some-id",
            });
            client.insertTextLocal(0, "abc d");
            client.insertMarkerLocal(0, ops_1.ReferenceType.Tile, {
                [referencePositions_1.reservedTileLabelsKey]: [tileLabel],
                [mergeTreeNodes_1.reservedMarkerIdKey]: "some-id",
            });
            client.insertTextLocal(7, "ef");
            client.insertMarkerLocal(8, ops_1.ReferenceType.Tile, {
                [referencePositions_1.reservedTileLabelsKey]: [tileLabel],
                [mergeTreeNodes_1.reservedMarkerIdKey]: "some-id",
            });
            console.log(client.getText());
            assert_1.strict.equal(client.getLength(), 10, "length not expected");
            const tile = client.findTile(5, tileLabel);
            (0, assert_1.strict)(tile, "Returned tile undefined.");
            assert_1.strict.equal(tile.pos, 0, "Tile with label not at expected position");
        });
        it("Should be able to find non preceding tile position from client with multiple tile", () => {
            const tileLabel = "EOP";
            client.insertMarkerLocal(0, ops_1.ReferenceType.Tile, {
                [referencePositions_1.reservedTileLabelsKey]: [tileLabel],
                [mergeTreeNodes_1.reservedMarkerIdKey]: "some-id",
            });
            client.insertTextLocal(0, "abc d");
            client.insertMarkerLocal(0, ops_1.ReferenceType.Tile, {
                [referencePositions_1.reservedTileLabelsKey]: [tileLabel],
                [mergeTreeNodes_1.reservedMarkerIdKey]: "some-id",
            });
            client.insertTextLocal(7, "ef");
            client.insertMarkerLocal(8, ops_1.ReferenceType.Tile, {
                [referencePositions_1.reservedTileLabelsKey]: [tileLabel],
                [mergeTreeNodes_1.reservedMarkerIdKey]: "some-id",
            });
            console.log(client.getText());
            assert_1.strict.equal(client.getLength(), 10, "length not expected");
            const tile = client.findTile(5, tileLabel, false);
            (0, assert_1.strict)(tile, "Returned tile undefined.");
            assert_1.strict.equal(tile.pos, 6, "Tile with label not at expected position");
        });
        it("Should be able to find tile from client with text length 1", () => {
            const tileLabel = "EOP";
            client.insertMarkerLocal(0, ops_1.ReferenceType.Tile, {
                [referencePositions_1.reservedTileLabelsKey]: [tileLabel],
                [mergeTreeNodes_1.reservedMarkerIdKey]: "some-id",
            });
            console.log(client.getText());
            assert_1.strict.equal(client.getLength(), 1, "length not expected");
            const tile = client.findTile(client.getLength(), tileLabel);
            (0, assert_1.strict)(tile, "Returned tile undefined.");
            assert_1.strict.equal(tile.pos, 0, "Tile with label not at expected position");
            const tile1 = client.findTile(0, tileLabel, false);
            (0, assert_1.strict)(tile1, "Returned tile undefined.");
            assert_1.strict.equal(tile1.pos, 0, "Tile with label not at expected position");
        });
        it("Should be able to find only preceding but not non preceding tile with index out of bound", () => {
            const tileLabel = "EOP";
            client.insertMarkerLocal(0, ops_1.ReferenceType.Tile, {
                [referencePositions_1.reservedTileLabelsKey]: [tileLabel],
                [mergeTreeNodes_1.reservedMarkerIdKey]: "some-id",
            });
            client.insertTextLocal(0, "abc");
            console.log(client.getText());
            assert_1.strict.equal(client.getLength(), 4, "length not expected");
            const tile = client.findTile(5, tileLabel);
            (0, assert_1.strict)(tile, "Returned tile undefined.");
            assert_1.strict.equal(tile.pos, 3, "Tile with label not at expected position");
            const tile1 = client.findTile(5, tileLabel, false);
            assert_1.strict.equal(typeof tile1, "undefined", "Returned tile should be undefined.");
        });
        it("Should return undefined when trying to find tile from text without the specified tile", () => {
            const tileLabel = "EOP";
            client.insertTextLocal(0, "abc");
            console.log(client.getText());
            assert_1.strict.equal(client.getLength(), 3, "length not expected");
            const tile = client.findTile(1, tileLabel);
            assert_1.strict.equal(typeof tile, "undefined", "Returned tile should be undefined.");
            const tile1 = client.findTile(1, tileLabel, false);
            assert_1.strict.equal(typeof tile1, "undefined", "Returned tile should be undefined.");
        });
        it("Should return undefined when trying to find tile from null text", () => {
            const tileLabel = "EOP";
            const tile = client.findTile(1, tileLabel);
            assert_1.strict.equal(typeof tile, "undefined", "Returned tile should be undefined.");
            const tile1 = client.findTile(1, tileLabel, false);
            assert_1.strict.equal(typeof tile1, "undefined", "Returned tile should be undefined.");
        });
    });
    describe(".annotateMarker", () => {
        it("annotate valid marker", () => {
            const insertOp = client.insertMarkerLocal(0, ops_1.ReferenceType.Tile, {
                [mergeTreeNodes_1.reservedMarkerIdKey]: "123",
            });
            (0, assert_1.strict)(insertOp);
            const markerInfo = client.getContainingSegment(0);
            const marker = markerInfo.segment;
            const annotateOp = client.annotateMarker(marker, { foo: "bar" });
            (0, assert_1.strict)(annotateOp);
            (0, assert_1.strict)(marker.properties);
            (0, assert_1.strict)(marker.properties.foo, "bar");
        });
    });
});
//# sourceMappingURL=client.spec.cjs.map