"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 perspective_js_1 = require("../perspective.js");
const clientId = 17;
describe("PriorPerspective", () => {
    const refSeq = 10;
    const perspective = new perspective_js_1.PriorPerspective(refSeq, clientId);
    it("sees operations from the same client", () => {
        const stamp = { clientId, seq: 1 };
        node_assert_1.strict.ok(perspective.hasOccurred(stamp));
    });
    it("sees operations at or below the refSeq", () => {
        for (let seq = 0; seq <= refSeq; seq++) {
            const stamp = { clientId, seq };
            node_assert_1.strict.ok(perspective.hasOccurred(stamp), `Failed for seq ${seq}`);
        }
    });
    it("Does not see operations from other clients above the refSeq", () => {
        const stamp = { clientId: clientId + 1, seq: refSeq + 1 };
        node_assert_1.strict.ok(!perspective.hasOccurred(stamp));
    });
    it("Uses operations to determine segment visibility", () => {
        const insert = { type: "insert", seq: 5, clientId };
        const remove1 = { type: "setRemove", seq: 10, clientId };
        const remove2 = { type: "sliceRemove", seq: 12, clientId };
        const seg1 = { insert };
        const seg2 = { insert, removes: [remove1] };
        const seg3 = {
            insert,
            removes: [remove1, remove2],
        };
        const seg4 = {
            insert,
            removes: [remove2],
        };
        const perspective1 = new perspective_js_1.PriorPerspective(4, clientId + 1);
        const perspective2 = new perspective_js_1.PriorPerspective(6, clientId + 1);
        const perspective3 = new perspective_js_1.PriorPerspective(10, clientId + 1);
        // Only perspectives 2 and 3 have seen the insert
        node_assert_1.strict.ok(!perspective1.isSegmentPresent(seg1));
        node_assert_1.strict.ok(perspective2.isSegmentPresent(seg1));
        node_assert_1.strict.ok(perspective3.isSegmentPresent(seg1));
        // Perspectives 2 and 3 have seen the insert, and perspective 3 has seen the remove
        node_assert_1.strict.ok(!perspective1.isSegmentPresent(seg2));
        node_assert_1.strict.ok(perspective2.isSegmentPresent(seg2));
        node_assert_1.strict.ok(!perspective3.isSegmentPresent(seg2));
        // Perspectives 2 and 3 have seen the insert, and perspective 3 has seen one of the removes
        node_assert_1.strict.ok(!perspective1.isSegmentPresent(seg3));
        node_assert_1.strict.ok(perspective2.isSegmentPresent(seg3));
        node_assert_1.strict.ok(!perspective3.isSegmentPresent(seg3));
        // Perspectives 2 and 3 have seen the insert, and none have seen the remove
        node_assert_1.strict.ok(!perspective1.isSegmentPresent(seg4));
        node_assert_1.strict.ok(perspective2.isSegmentPresent(seg4));
        node_assert_1.strict.ok(perspective3.isSegmentPresent(seg4));
    });
});
describe("LocalReconnectingPerspective", () => {
    const refSeq = 10;
    const localSeq = 20;
    const perspective = new perspective_js_1.LocalReconnectingPerspective(refSeq, clientId, localSeq);
    it("sees operations from the same client at or below localSeq", () => {
        for (let i = 0; i <= localSeq; i++) {
            const stamp = { seq: constants_js_1.UnassignedSequenceNumber, clientId, localSeq: i };
            node_assert_1.strict.ok(perspective.hasOccurred(stamp), `Failed for localSeq ${i}`);
        }
    });
    it("does not see operations from the same client above localSeq", () => {
        const stamp = {
            seq: constants_js_1.UnassignedSequenceNumber,
            clientId,
            localSeq: localSeq + 1,
        };
        node_assert_1.strict.ok(!perspective.hasOccurred(stamp));
    });
    it("sees operations at or below refSeq", () => {
        for (let seq = 0; seq <= refSeq; seq++) {
            const stamp = {
                seq,
                clientId: seq % 3 === 0 ? clientId : clientId + 1,
            };
            node_assert_1.strict.ok(perspective.hasOccurred(stamp), `Failed for seq ${seq}`);
        }
    });
    it("does not see operations from other clients above refSeq", () => {
        const stamp = { seq: refSeq + 1, clientId: clientId + 1 };
        node_assert_1.strict.ok(!perspective.hasOccurred(stamp));
    });
});
describe("LocalDefaultPerspective", () => {
    const perspective = new perspective_js_1.LocalDefaultPerspective(clientId);
    it("sees all operations", () => {
        for (const id of [0, 1, 2, 3, clientId]) {
            for (const refSeq of [0, 1, 5, 100, 1000]) {
                const stamp = { seq: 1, clientId: id };
                node_assert_1.strict.ok(perspective.hasOccurred(stamp), `Failed for clientId ${id} and refSeq ${refSeq}`);
            }
        }
        for (const localSeq of [0, 1, 5, 100, 1000]) {
            const stamp = { seq: constants_js_1.UnassignedSequenceNumber, clientId, localSeq };
            node_assert_1.strict.ok(perspective.hasOccurred(stamp), `Failed for localSeq ${localSeq}`);
        }
    });
});
describe("RemoteObliteratePerspective", () => {
    const perspective = new perspective_js_1.RemoteObliteratePerspective(clientId);
    it("Sees all inserts", () => {
        for (const id of [0, 1, 2, 3, clientId]) {
            for (const refSeq of [0, 1, 5, 100, 1000]) {
                const stamp = { type: "insert", seq: 1, clientId: id };
                node_assert_1.strict.ok(perspective.hasOccurred(stamp), `Failed for clientId ${id} and refSeq ${refSeq}`);
            }
        }
        for (const localSeq of [0, 1, 5, 100, 1000]) {
            const stamp = {
                type: "insert",
                seq: constants_js_1.UnassignedSequenceNumber,
                clientId,
                localSeq,
            };
            node_assert_1.strict.ok(perspective.hasOccurred(stamp), `Failed for localSeq ${localSeq}`);
        }
    });
    it("Sees remote removes", () => {
        for (const id of [0, 1, 2, 3, clientId]) {
            for (const refSeq of [0, 1, 5, 100, 1000]) {
                for (const type of ["setRemove", "sliceRemove"]) {
                    const stamp = { type, seq: 1, clientId: id };
                    node_assert_1.strict.ok(perspective.hasOccurred(stamp), `Failed for clientId ${id} and refSeq ${refSeq} with ${type}`);
                }
            }
        }
    });
    it("Does not see local removes", () => {
        for (const localSeq of [0, 1, 5, 100, 1000]) {
            for (const type of ["setRemove", "sliceRemove"]) {
                const stamp = {
                    type,
                    seq: constants_js_1.UnassignedSequenceNumber,
                    clientId,
                    localSeq,
                };
                node_assert_1.strict.ok(!perspective.hasOccurred(stamp), `Failed for localSeq ${localSeq}`);
            }
        }
    });
});
//# sourceMappingURL=perspective.spec.js.map