Prepare for repo merge

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2024-10-15 11:35:21 +01:00
parent 0f670b8dc0
commit b084ff2313
No known key found for this signature in database
GPG key ID: A2B008A5F49F5D0D
807 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,90 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2022 Ryan Browne <code@commonlawfeature.com>
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import EmojiProvider from "../../src/autocomplete/EmojiProvider";
import { mkStubRoom } from "../test-utils/test-utils";
import { add } from "../../src/emojipicker/recent";
import { stubClient } from "../test-utils";
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
const EMOJI_SHORTCODES = [
":+1",
":heart",
":grinning",
":hand",
":man",
":sweat",
":monkey",
":boat",
":mailbox",
":cop",
":bow",
":kiss",
":golf",
];
// Some emoji shortcodes are too short and do not actually trigger autocompletion until the ending `:`.
// This means that we cannot compare their autocompletion before and after the ending `:` and have
// to simply assert that the final completion with the colon is the exact emoji.
const TOO_SHORT_EMOJI_SHORTCODE = [{ emojiShortcode: ":o", expectedEmoji: "⭕️" }];
describe("EmojiProvider", function () {
const testRoom = mkStubRoom(undefined, undefined, undefined);
stubClient();
MatrixClientPeg.safeGet();
it.each(EMOJI_SHORTCODES)("Returns consistent results after final colon %s", async function (emojiShortcode) {
const ep = new EmojiProvider(testRoom);
const range = { beginning: true, start: 0, end: 3 };
const completionsBeforeColon = await ep.getCompletions(emojiShortcode, range);
const completionsAfterColon = await ep.getCompletions(emojiShortcode + ":", range);
const firstCompletionWithoutColon = completionsBeforeColon[0].completion;
const firstCompletionWithColon = completionsAfterColon[0].completion;
expect(firstCompletionWithoutColon).toEqual(firstCompletionWithColon);
});
it.each(TOO_SHORT_EMOJI_SHORTCODE)(
"Returns correct results after final colon $emojiShortcode",
async ({ emojiShortcode, expectedEmoji }) => {
const ep = new EmojiProvider(testRoom);
const range = { beginning: true, start: 0, end: 3 };
const completions = await ep.getCompletions(emojiShortcode + ":", range);
expect(completions[0].completion).toEqual(expectedEmoji);
},
);
it("Recently used emojis are correctly sorted", async function () {
add("😘"); //kissing_heart
add("💗"); //heartpulse
add("💗"); //heartpulse
add("😍"); //heart_eyes
const ep = new EmojiProvider(testRoom);
const completionsList = await ep.getCompletions(":heart", { beginning: true, start: 0, end: 6 });
expect(completionsList[0]?.component?.props.title).toEqual(":heartpulse:");
expect(completionsList[1]?.component?.props.title).toEqual(":heart_eyes:");
});
it("Exact match in recently used takes the lead", async function () {
add("😘"); //kissing_heart
add("💗"); //heartpulse
add("💗"); //heartpulse
add("😍"); //heart_eyes
add("❤️"); //heart
const ep = new EmojiProvider(testRoom);
const completionsList = await ep.getCompletions(":heart", { beginning: true, start: 0, end: 6 });
expect(completionsList[0]?.component?.props.title).toEqual(":heart:");
expect(completionsList[1]?.component?.props.title).toEqual(":heartpulse:");
expect(completionsList[2]?.component?.props.title).toEqual(":heart_eyes:");
});
});

View file

@ -0,0 +1,165 @@
/*
Copyright 2018-2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import QueryMatcher from "../../src/autocomplete/QueryMatcher";
const OBJECTS = [
{ name: "Mel B", nick: "Scary" },
{ name: "Mel C", nick: "Sporty" },
{ name: "Emma", nick: "Baby" },
{ name: "Geri", nick: "Ginger" },
{ name: "Victoria", nick: "Posh" },
];
const NONWORDOBJECTS = [{ name: "B.O.B" }, { name: "bob" }];
describe("QueryMatcher", function () {
it("Returns results by key", function () {
const qm = new QueryMatcher(OBJECTS, { keys: ["name"] });
const results = qm.match("Geri");
expect(results.length).toBe(1);
expect(results[0].name).toBe("Geri");
});
it("Returns results by prefix", function () {
const qm = new QueryMatcher(OBJECTS, { keys: ["name"] });
const results = qm.match("Ge");
expect(results.length).toBe(1);
expect(results[0].name).toBe("Geri");
});
it("Matches case-insensitive", function () {
const qm = new QueryMatcher(OBJECTS, { keys: ["name"] });
const results = qm.match("geri");
expect(results.length).toBe(1);
expect(results[0].name).toBe("Geri");
});
it("Matches ignoring accents", function () {
const qm = new QueryMatcher([{ name: "Gëri", foo: 46 }], { keys: ["name"] });
const results = qm.match("geri");
expect(results.length).toBe(1);
expect(results[0].foo).toBe(46);
});
it("Returns multiple results in order of search string appearance", function () {
const qm = new QueryMatcher(OBJECTS, { keys: ["name", "nick"] });
const results = qm.match("or");
expect(results.length).toBe(2);
expect(results[0].name).toBe("Mel C");
expect(results[1].name).toBe("Victoria");
qm.setObjects(OBJECTS.slice().reverse());
const reverseResults = qm.match("or");
// should still be in the same order: search string position
// takes precedence over input order
expect(reverseResults.length).toBe(2);
expect(reverseResults[0].name).toBe("Mel C");
expect(reverseResults[1].name).toBe("Victoria");
});
it("Returns results with search string in same place according to key index", function () {
const objects = [
{ name: "a", first: "hit", second: "miss", third: "miss" },
{ name: "b", first: "miss", second: "hit", third: "miss" },
{ name: "c", first: "miss", second: "miss", third: "hit" },
];
const qm = new QueryMatcher(objects, { keys: ["second", "first", "third"] });
const results = qm.match("hit");
expect(results.length).toBe(3);
expect(results[0].name).toBe("b");
expect(results[1].name).toBe("a");
expect(results[2].name).toBe("c");
qm.setObjects(objects.slice().reverse());
const reverseResults = qm.match("hit");
// should still be in the same order: key index
// takes precedence over input order
expect(reverseResults.length).toBe(3);
expect(reverseResults[0].name).toBe("b");
expect(reverseResults[1].name).toBe("a");
expect(reverseResults[2].name).toBe("c");
});
it("Returns results with search string in same place and key in same place in insertion order", function () {
const qm = new QueryMatcher(OBJECTS, { keys: ["name"] });
const results = qm.match("Mel");
expect(results.length).toBe(2);
expect(results[0].name).toBe("Mel B");
expect(results[1].name).toBe("Mel C");
qm.setObjects(OBJECTS.slice().reverse());
const reverseResults = qm.match("Mel");
expect(reverseResults.length).toBe(2);
expect(reverseResults[0].name).toBe("Mel C");
expect(reverseResults[1].name).toBe("Mel B");
});
it("Returns numeric results in correct order (input pos)", function () {
// regression test for depending on object iteration order
const qm = new QueryMatcher([{ name: "123456badger" }, { name: "123456" }], { keys: ["name"] });
const results = qm.match("123456");
expect(results.length).toBe(2);
expect(results[0].name).toBe("123456badger");
expect(results[1].name).toBe("123456");
});
it("Returns numeric results in correct order (query pos)", function () {
const qm = new QueryMatcher([{ name: "999999123456" }, { name: "123456badger" }], { keys: ["name"] });
const results = qm.match("123456");
expect(results.length).toBe(2);
expect(results[0].name).toBe("123456badger");
expect(results[1].name).toBe("999999123456");
});
it("Returns results by function", function () {
const qm = new QueryMatcher(OBJECTS, {
keys: ["name"],
funcs: [(x) => x.name.replace("Mel", "Emma")],
});
const results = qm.match("Emma");
expect(results.length).toBe(3);
expect(results[0].name).toBe("Emma");
expect(results[1].name).toBe("Mel B");
expect(results[2].name).toBe("Mel C");
});
it("Matches words only by default", function () {
const qm = new QueryMatcher(NONWORDOBJECTS, { keys: ["name"] });
const results = qm.match("bob");
expect(results.length).toBe(2);
expect(results[0].name).toBe("B.O.B");
expect(results[1].name).toBe("bob");
});
it("Matches all chars with words-only off", function () {
const qm = new QueryMatcher(NONWORDOBJECTS, {
keys: ["name"],
shouldMatchWordsOnly: false,
});
const results = qm.match("bob");
expect(results.length).toBe(1);
expect(results[0].name).toBe("bob");
});
});

View file

@ -0,0 +1,127 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2023 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 { mocked } from "jest-mock";
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import RoomProvider from "../../src/autocomplete/RoomProvider";
import SettingsStore from "../../src/settings/SettingsStore";
import { mkRoom, mkSpace, stubClient } from "../test-utils";
describe("RoomProvider", () => {
it("suggests a room whose alias matches a prefix", async () => {
// Given a room
const client = stubClient();
const room = makeRoom(client, "room:e.com");
mocked(client.getVisibleRooms).mockReturnValue([room]);
// When we search for rooms starting with its prefix
const roomProvider = new RoomProvider(room);
const completions = await roomProvider.getCompletions("#ro", { beginning: true, start: 0, end: 3 });
// Then we find it
expect(completions).toStrictEqual([
{
type: "room",
completion: room.getCanonicalAlias(),
completionId: room.roomId,
component: expect.anything(),
href: "https://matrix.to/#/#room:e.com",
range: { start: 0, end: 3 },
suffix: " ",
},
]);
});
it("suggests only rooms matching a prefix", async () => {
// Given some rooms with different names
const client = stubClient();
const room1 = makeRoom(client, "room1:e.com");
const room2 = makeRoom(client, "room2:e.com");
const other = makeRoom(client, "other:e.com");
const space = makeSpace(client, "room3:e.com");
mocked(client.getVisibleRooms).mockReturnValue([room1, room2, other, space]);
// When we search for rooms starting with a prefix
const roomProvider = new RoomProvider(room1);
const completions = await roomProvider.getCompletions("#ro", { beginning: true, start: 0, end: 3 });
// Then we find the two rooms with that prefix, but not the other one
expect(completions).toStrictEqual([
{
type: "room",
completion: room1.getCanonicalAlias(),
completionId: room1.roomId,
component: expect.anything(),
href: "https://matrix.to/#/#room1:e.com",
range: { start: 0, end: 3 },
suffix: " ",
},
{
type: "room",
completion: room2.getCanonicalAlias(),
completionId: room2.roomId,
component: expect.anything(),
href: "https://matrix.to/#/#room2:e.com",
range: { start: 0, end: 3 },
suffix: " ",
},
]);
});
describe("If the feature_dynamic_room_predecessors is not enabled", () => {
beforeEach(() => {
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
});
it("Passes through the dynamic predecessor setting", async () => {
const client = stubClient();
const room = makeRoom(client, "room:e.com");
mocked(client.getVisibleRooms).mockReturnValue([room]);
mocked(client.getVisibleRooms).mockClear();
const roomProvider = new RoomProvider(room);
await roomProvider.getCompletions("#ro", { beginning: true, start: 0, end: 3 });
expect(client.getVisibleRooms).toHaveBeenCalledWith(false);
});
});
describe("If the feature_dynamic_room_predecessors is enabled", () => {
beforeEach(() => {
// Turn on feature_dynamic_room_predecessors setting
jest.spyOn(SettingsStore, "getValue").mockImplementation(
(settingName) => settingName === "feature_dynamic_room_predecessors",
);
});
it("Passes through the dynamic predecessor setting", async () => {
const client = stubClient();
const room = makeRoom(client, "room:e.com");
mocked(client.getVisibleRooms).mockReturnValue([room]);
mocked(client.getVisibleRooms).mockClear();
const roomProvider = new RoomProvider(room);
await roomProvider.getCompletions("#ro", { beginning: true, start: 0, end: 3 });
expect(client.getVisibleRooms).toHaveBeenCalledWith(true);
});
});
});
function makeSpace(client: MatrixClient, name: string): Room {
const space = mkSpace(client, `!${name}`);
space.getCanonicalAlias.mockReturnValue(`#${name}`);
return space;
}
function makeRoom(client: MatrixClient, name: string): Room {
const room = mkRoom(client, `!${name}`);
room.getCanonicalAlias.mockReturnValue(`#${name}`);
return room;
}

View file

@ -0,0 +1,127 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2023 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 { mocked } from "jest-mock";
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import SpaceProvider from "../../src/autocomplete/SpaceProvider";
import SettingsStore from "../../src/settings/SettingsStore";
import { mkRoom, mkSpace, stubClient } from "../test-utils";
describe("SpaceProvider", () => {
it("suggests a space whose alias matches a prefix", async () => {
// Given a space
const client = stubClient();
const space = makeSpace(client, "space:e.com");
mocked(client.getVisibleRooms).mockReturnValue([space]);
// When we search for spaces starting with its prefix
const spaceProvider = new SpaceProvider(space);
const completions = await spaceProvider.getCompletions("#sp", { beginning: true, start: 0, end: 3 });
// Then we find it
expect(completions).toStrictEqual([
{
type: "room",
completion: space.getCanonicalAlias(),
completionId: space.roomId,
component: expect.anything(),
href: "https://matrix.to/#/#space:e.com",
range: { start: 0, end: 3 },
suffix: " ",
},
]);
});
it("suggests only spaces matching a prefix", async () => {
// Given some spaces with different names
const client = stubClient();
const space1 = makeSpace(client, "space1:e.com");
const space2 = makeSpace(client, "space2:e.com");
const other = makeSpace(client, "other:e.com");
const room = makeRoom(client, "space3:e.com");
mocked(client.getVisibleRooms).mockReturnValue([space1, space2, other, room]);
// When we search for spaces starting with a prefix
const spaceProvider = new SpaceProvider(space1);
const completions = await spaceProvider.getCompletions("#sp", { beginning: true, start: 0, end: 3 });
// Then we find the two spaces with that prefix, but not the other one
expect(completions).toStrictEqual([
{
type: "room",
completion: space1.getCanonicalAlias(),
completionId: space1.roomId,
component: expect.anything(),
href: "https://matrix.to/#/#space1:e.com",
range: { start: 0, end: 3 },
suffix: " ",
},
{
type: "room",
completion: space2.getCanonicalAlias(),
completionId: space2.roomId,
component: expect.anything(),
href: "https://matrix.to/#/#space2:e.com",
range: { start: 0, end: 3 },
suffix: " ",
},
]);
});
describe("If the feature_dynamic_room_predecessors is not enabled", () => {
beforeEach(() => {
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
});
it("Passes through the dynamic predecessor setting", async () => {
const client = stubClient();
const space = makeSpace(client, "space:e.com");
mocked(client.getVisibleRooms).mockReturnValue([space]);
mocked(client.getVisibleRooms).mockClear();
const spaceProvider = new SpaceProvider(space);
await spaceProvider.getCompletions("#ro", { beginning: true, start: 0, end: 3 });
expect(client.getVisibleRooms).toHaveBeenCalledWith(false);
});
});
describe("If the feature_dynamic_room_predecessors is enabled", () => {
beforeEach(() => {
// Turn on feature_dynamic_space_predecessors setting
jest.spyOn(SettingsStore, "getValue").mockImplementation(
(settingName) => settingName === "feature_dynamic_room_predecessors",
);
});
it("Passes through the dynamic predecessor setting", async () => {
const client = stubClient();
const space = makeSpace(client, "space:e.com");
mocked(client.getVisibleRooms).mockReturnValue([space]);
mocked(client.getVisibleRooms).mockClear();
const spaceProvider = new SpaceProvider(space);
await spaceProvider.getCompletions("#ro", { beginning: true, start: 0, end: 3 });
expect(client.getVisibleRooms).toHaveBeenCalledWith(true);
});
});
});
function makeSpace(client: MatrixClient, name: string): Room {
const space = mkSpace(client, `!${name}`);
space.getCanonicalAlias.mockReturnValue(`#${name}`);
return space;
}
function makeRoom(client: MatrixClient, name: string): Room {
const room = mkRoom(client, `!${name}`);
room.getCanonicalAlias.mockReturnValue(`#${name}`);
return room;
}