mirror of
https://github.com/vector-im/element-web.git
synced 2025-12-27 04:01:10 +01:00
Co-authored-by: github-merge-queue <118344674+github-merge-queue@users.noreply.github.com> Co-authored-by: github-merge-queue <github-merge-queue@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Florian Duros <florian.duros@ormaz.fr> Co-authored-by: Kim Brose <kim.brose@nordeck.net> Co-authored-by: Florian Duros <florianduros@element.io> Co-authored-by: R Midhun Suresh <hi@midhun.dev> Co-authored-by: dbkr <986903+dbkr@users.noreply.github.com> Co-authored-by: ElementRobot <releases@riot.im> Co-authored-by: dbkr <dbkr@users.noreply.github.com> Co-authored-by: David Baker <dbkr@users.noreply.github.com> Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Co-authored-by: David Langley <davidl@element.io> Co-authored-by: Michael Weimann <michaelw@matrix.org> Co-authored-by: Timshel <Timshel@users.noreply.github.com> Co-authored-by: Sahil Silare <32628578+sahil9001@users.noreply.github.com> Co-authored-by: Will Hunt <will@half-shot.uk> Co-authored-by: Hubert Chathi <hubert@uhoreg.ca> Co-authored-by: Andrew Ferrazzutti <andrewf@element.io> Co-authored-by: Robin <robin@robin.town> Co-authored-by: Tulir Asokan <tulir@maunium.net>
189 lines
7.1 KiB
TypeScript
189 lines
7.1 KiB
TypeScript
/*
|
|
Copyright 2024 New Vector Ltd.
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
Please see LICENSE files in the repository root for full details.
|
|
*/
|
|
|
|
import { sortBy } from "lodash";
|
|
import { averageBetweenStrings, DEFAULT_ALPHABET } from "matrix-js-sdk/src/utils";
|
|
|
|
import { midPointsBetweenStrings, reorderLexicographically } from "../../../src/utils/stringOrderField";
|
|
|
|
const moveLexicographicallyTest = (
|
|
orders: Array<string | undefined>,
|
|
fromIndex: number,
|
|
toIndex: number,
|
|
expectedChanges: number,
|
|
maxLength?: number,
|
|
): void => {
|
|
const ops = reorderLexicographically(orders, fromIndex, toIndex, maxLength);
|
|
|
|
const zipped: Array<[number, string | undefined]> = orders.map((o, i) => [i, o]);
|
|
ops.forEach(({ index, order }) => {
|
|
zipped[index][1] = order;
|
|
});
|
|
|
|
const newOrders = sortBy(zipped, (i) => i[1]);
|
|
expect(newOrders[toIndex][0]).toBe(fromIndex);
|
|
expect(ops).toHaveLength(expectedChanges);
|
|
};
|
|
|
|
describe("stringOrderField", () => {
|
|
describe("midPointsBetweenStrings", () => {
|
|
it("should work", () => {
|
|
expect(averageBetweenStrings("!!", "##")).toBe('""');
|
|
const midpoints = ["a", ...midPointsBetweenStrings("a", "e", 3, 1), "e"].sort();
|
|
expect(midpoints[0]).toBe("a");
|
|
expect(midpoints[4]).toBe("e");
|
|
expect(midPointsBetweenStrings(" ", "!'Tu:}", 1, 50)).toStrictEqual([" S:J\\~"]);
|
|
});
|
|
|
|
it("should return empty array when the request is not possible", () => {
|
|
expect(midPointsBetweenStrings("a", "e", 0, 1)).toStrictEqual([]);
|
|
expect(midPointsBetweenStrings("a", "e", 4, 1)).toStrictEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("reorderLexicographically", () => {
|
|
it("should work when moving left", () => {
|
|
moveLexicographicallyTest(["a", "c", "e", "g", "i"], 2, 1, 1);
|
|
});
|
|
|
|
it("should work when moving right", () => {
|
|
moveLexicographicallyTest(["a", "c", "e", "g", "i"], 1, 2, 1);
|
|
});
|
|
|
|
it("should work when all orders are undefined", () => {
|
|
moveLexicographicallyTest([undefined, undefined, undefined, undefined, undefined, undefined], 4, 1, 2);
|
|
});
|
|
|
|
it("should work when moving to end and all orders are undefined", () => {
|
|
moveLexicographicallyTest([undefined, undefined, undefined, undefined, undefined, undefined], 1, 4, 5);
|
|
});
|
|
|
|
it("should work when moving left and some orders are undefined", () => {
|
|
moveLexicographicallyTest(["a", "c", "e", undefined, undefined, undefined], 5, 2, 1);
|
|
|
|
moveLexicographicallyTest(["a", "a", "e", undefined, undefined, undefined], 5, 1, 2);
|
|
});
|
|
|
|
it("should work moving to the start when all is undefined", () => {
|
|
moveLexicographicallyTest([undefined, undefined, undefined, undefined], 2, 0, 1);
|
|
});
|
|
|
|
it("should work moving to the end when all is undefined", () => {
|
|
moveLexicographicallyTest([undefined, undefined, undefined, undefined], 1, 3, 4);
|
|
});
|
|
|
|
it("should work moving left when all is undefined", () => {
|
|
moveLexicographicallyTest([undefined, undefined, undefined, undefined, undefined, undefined], 4, 1, 2);
|
|
});
|
|
|
|
it("should work moving right when all is undefined", () => {
|
|
moveLexicographicallyTest([undefined, undefined, undefined, undefined], 1, 2, 3);
|
|
});
|
|
|
|
it("should work moving more right when all is undefined", () => {
|
|
moveLexicographicallyTest(
|
|
[undefined, undefined, undefined, undefined, undefined, /**/ undefined, undefined],
|
|
1,
|
|
4,
|
|
5,
|
|
);
|
|
});
|
|
|
|
it("should work moving left when right is undefined", () => {
|
|
moveLexicographicallyTest(["20", undefined, undefined, undefined, undefined, undefined], 4, 2, 2);
|
|
});
|
|
|
|
it("should work moving right when right is undefined", () => {
|
|
moveLexicographicallyTest(
|
|
["50", undefined, undefined, undefined, undefined, /**/ undefined, undefined],
|
|
1,
|
|
4,
|
|
4,
|
|
);
|
|
});
|
|
|
|
it("should work moving left when right is defined", () => {
|
|
moveLexicographicallyTest(["10", "20", "30", "40", undefined, undefined], 3, 1, 1);
|
|
});
|
|
|
|
it("should work moving right when right is defined", () => {
|
|
moveLexicographicallyTest(["10", "20", "30", "40", "50", undefined], 1, 3, 1);
|
|
});
|
|
|
|
it("should work moving left when all is defined", () => {
|
|
moveLexicographicallyTest(["11", "13", "15", "17", "19"], 2, 1, 1);
|
|
});
|
|
|
|
it("should work moving right when all is defined", () => {
|
|
moveLexicographicallyTest(["11", "13", "15", "17", "19"], 1, 2, 1);
|
|
});
|
|
|
|
it("should work moving left into no left space", () => {
|
|
moveLexicographicallyTest(["11", "12", "13", "14", "19"], 3, 1, 2, 2);
|
|
|
|
moveLexicographicallyTest(
|
|
[
|
|
DEFAULT_ALPHABET.charAt(0),
|
|
// Target
|
|
DEFAULT_ALPHABET.charAt(1),
|
|
DEFAULT_ALPHABET.charAt(2),
|
|
DEFAULT_ALPHABET.charAt(3),
|
|
DEFAULT_ALPHABET.charAt(4),
|
|
DEFAULT_ALPHABET.charAt(5),
|
|
],
|
|
5,
|
|
1,
|
|
5,
|
|
1,
|
|
);
|
|
});
|
|
|
|
it("should work moving right into no right space", () => {
|
|
moveLexicographicallyTest(["15", "16", "17", "18", "19"], 1, 3, 3, 2);
|
|
|
|
moveLexicographicallyTest(
|
|
[
|
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 5),
|
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 4),
|
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 3),
|
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 2),
|
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 1),
|
|
],
|
|
1,
|
|
3,
|
|
3,
|
|
1,
|
|
);
|
|
});
|
|
|
|
it("should work moving right into no left space", () => {
|
|
moveLexicographicallyTest(["11", "12", "13", "14", "15", "16", undefined], 1, 3, 3);
|
|
|
|
moveLexicographicallyTest(["0", "1", "2", "3", "4", "5"], 1, 3, 3, 1);
|
|
});
|
|
|
|
it("should work moving left into no right space", () => {
|
|
moveLexicographicallyTest(["15", "16", "17", "18", "19"], 4, 3, 4, 2);
|
|
|
|
moveLexicographicallyTest(
|
|
[
|
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 5),
|
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 4),
|
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 3),
|
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 2),
|
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 1),
|
|
],
|
|
4,
|
|
3,
|
|
4,
|
|
1,
|
|
);
|
|
});
|
|
});
|
|
});
|