2022-05-06 09:09:28 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-07-10 08:01:03 +00:00
|
|
|
import { EventType, JoinRule, MatrixClient, MatrixEvent, Room, RoomMember } from "matrix-js-sdk/src/matrix";
|
2023-02-22 10:52:55 +00:00
|
|
|
import { render } from "@testing-library/react";
|
2022-04-08 17:32:35 +00:00
|
|
|
import { ReactElement } from "react";
|
2023-02-13 11:39:16 +00:00
|
|
|
import { Mocked, mocked } from "jest-mock";
|
2021-10-22 22:23:32 +00:00
|
|
|
|
2022-12-16 11:01:16 +00:00
|
|
|
import { textForEvent } from "../src/TextForEvent";
|
2021-07-26 16:41:39 +00:00
|
|
|
import SettingsStore from "../src/settings/SettingsStore";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { createTestClient, stubClient } from "./test-utils";
|
|
|
|
import { MatrixClientPeg } from "../src/MatrixClientPeg";
|
|
|
|
import UserIdentifierCustomisations from "../src/customisations/UserIdentifier";
|
2022-10-06 14:27:12 +00:00
|
|
|
import { ElementCall } from "../src/models/Call";
|
2022-12-16 11:01:16 +00:00
|
|
|
import { getSenderName } from "../src/utils/event/getSenderName";
|
2022-01-25 09:40:02 +00:00
|
|
|
|
|
|
|
jest.mock("../src/settings/SettingsStore");
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.mock("../src/customisations/UserIdentifier", () => ({
|
|
|
|
getDisplayUserIdentifier: jest.fn().mockImplementation((userId) => userId),
|
2022-01-25 09:40:02 +00:00
|
|
|
}));
|
2021-07-26 16:41:39 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
function mockPinnedEvent(pinnedMessageIds?: string[], prevPinnedMessageIds?: string[]): MatrixEvent {
|
2021-07-26 16:41:39 +00:00
|
|
|
return new MatrixEvent({
|
|
|
|
type: "m.room.pinned_events",
|
|
|
|
state_key: "",
|
|
|
|
sender: "@foo:example.com",
|
|
|
|
content: {
|
|
|
|
pinned: pinnedMessageIds,
|
|
|
|
},
|
|
|
|
prev_content: {
|
|
|
|
pinned: prevPinnedMessageIds,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("TextForEvent", () => {
|
2023-06-05 17:12:23 +00:00
|
|
|
const mockClient = createTestClient();
|
|
|
|
|
2022-01-19 00:58:31 +00:00
|
|
|
describe("getSenderName()", () => {
|
|
|
|
it("Prefers sender.name", () => {
|
|
|
|
expect(getSenderName({ sender: { name: "Alice" } } as MatrixEvent)).toBe("Alice");
|
|
|
|
});
|
|
|
|
it("Handles missing sender", () => {
|
|
|
|
expect(getSenderName({ getSender: () => "Alice" } as MatrixEvent)).toBe("Alice");
|
|
|
|
});
|
|
|
|
it("Handles missing sender and get sender", () => {
|
|
|
|
expect(getSenderName({ getSender: () => undefined } as MatrixEvent)).toBe("Someone");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-08-10 15:06:33 +00:00
|
|
|
describe("TextForPinnedEvent", () => {
|
2022-01-25 09:40:02 +00:00
|
|
|
beforeAll(() => {
|
|
|
|
// enable feature_pinning setting
|
2022-12-12 11:24:14 +00:00
|
|
|
(SettingsStore.getValue as jest.Mock).mockImplementation((feature) => feature === "feature_pinning");
|
2022-01-25 09:40:02 +00:00
|
|
|
});
|
2021-08-10 15:06:33 +00:00
|
|
|
|
|
|
|
it("mentions message when a single message was pinned, with no previously pinned messages", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const event = mockPinnedEvent(["message-1"]);
|
2023-06-05 17:12:23 +00:00
|
|
|
const plainText = textForEvent(event, mockClient);
|
|
|
|
const component = render(textForEvent(event, mockClient, true) as ReactElement);
|
2021-08-11 10:40:33 +00:00
|
|
|
|
|
|
|
const expectedText = "@foo:example.com pinned a message to this room. See all pinned messages.";
|
|
|
|
expect(plainText).toBe(expectedText);
|
2023-02-22 10:52:55 +00:00
|
|
|
expect(component.container).toHaveTextContent(expectedText);
|
2021-08-10 15:06:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("mentions message when a single message was pinned, with multiple previously pinned messages", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const event = mockPinnedEvent(["message-1", "message-2", "message-3"], ["message-1", "message-2"]);
|
2023-06-05 17:12:23 +00:00
|
|
|
const plainText = textForEvent(event, mockClient);
|
|
|
|
const component = render(textForEvent(event, mockClient, true) as ReactElement);
|
2021-08-11 10:40:33 +00:00
|
|
|
|
|
|
|
const expectedText = "@foo:example.com pinned a message to this room. See all pinned messages.";
|
|
|
|
expect(plainText).toBe(expectedText);
|
2023-02-22 10:52:55 +00:00
|
|
|
expect(component.container).toHaveTextContent(expectedText);
|
2021-08-10 15:06:33 +00:00
|
|
|
});
|
|
|
|
|
2021-08-11 13:51:36 +00:00
|
|
|
it("mentions message when a single message was unpinned, with a single message previously pinned", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const event = mockPinnedEvent([], ["message-1"]);
|
2023-06-05 17:12:23 +00:00
|
|
|
const plainText = textForEvent(event, mockClient);
|
|
|
|
const component = render(textForEvent(event, mockClient, true) as ReactElement);
|
2021-08-11 10:40:33 +00:00
|
|
|
|
2021-08-11 13:51:36 +00:00
|
|
|
const expectedText = "@foo:example.com unpinned a message from this room. See all pinned messages.";
|
2021-08-11 10:40:33 +00:00
|
|
|
expect(plainText).toBe(expectedText);
|
2023-02-22 10:52:55 +00:00
|
|
|
expect(component.container).toHaveTextContent(expectedText);
|
2021-08-10 15:06:33 +00:00
|
|
|
});
|
|
|
|
|
2021-08-11 13:51:36 +00:00
|
|
|
it("mentions message when a single message was unpinned, with multiple previously pinned messages", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const event = mockPinnedEvent(["message-2"], ["message-1", "message-2"]);
|
2023-06-05 17:12:23 +00:00
|
|
|
const plainText = textForEvent(event, mockClient);
|
|
|
|
const component = render(textForEvent(event, mockClient, true) as ReactElement);
|
2021-08-11 10:40:33 +00:00
|
|
|
|
|
|
|
const expectedText = "@foo:example.com unpinned a message from this room. See all pinned messages.";
|
|
|
|
expect(plainText).toBe(expectedText);
|
2023-02-22 10:52:55 +00:00
|
|
|
expect(component.container).toHaveTextContent(expectedText);
|
2021-08-10 15:06:33 +00:00
|
|
|
});
|
|
|
|
|
2021-08-11 13:51:36 +00:00
|
|
|
it("shows generic text when multiple messages were pinned", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const event = mockPinnedEvent(["message-1", "message-2", "message-3"], ["message-1"]);
|
2023-06-05 17:12:23 +00:00
|
|
|
const plainText = textForEvent(event, mockClient);
|
|
|
|
const component = render(textForEvent(event, mockClient, true) as ReactElement);
|
2021-08-11 10:40:33 +00:00
|
|
|
|
2021-08-11 13:51:36 +00:00
|
|
|
const expectedText = "@foo:example.com changed the pinned messages for the room.";
|
2021-08-11 10:40:33 +00:00
|
|
|
expect(plainText).toBe(expectedText);
|
2023-02-22 10:52:55 +00:00
|
|
|
expect(component.container).toHaveTextContent(expectedText);
|
2021-08-10 15:06:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shows generic text when multiple messages were unpinned", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const event = mockPinnedEvent(["message-3"], ["message-1", "message-2", "message-3"]);
|
2023-06-05 17:12:23 +00:00
|
|
|
const plainText = textForEvent(event, mockClient);
|
|
|
|
const component = render(textForEvent(event, mockClient, true) as ReactElement);
|
2021-08-11 10:40:33 +00:00
|
|
|
|
|
|
|
const expectedText = "@foo:example.com changed the pinned messages for the room.";
|
|
|
|
expect(plainText).toBe(expectedText);
|
2023-02-22 10:52:55 +00:00
|
|
|
expect(component.container).toHaveTextContent(expectedText);
|
2021-08-10 15:06:33 +00:00
|
|
|
});
|
2021-08-11 13:56:59 +00:00
|
|
|
|
|
|
|
it("shows generic text when one message was pinned, and another unpinned", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const event = mockPinnedEvent(["message-2"], ["message-1"]);
|
2023-06-05 17:12:23 +00:00
|
|
|
const plainText = textForEvent(event, mockClient);
|
|
|
|
const component = render(textForEvent(event, mockClient, true) as ReactElement);
|
2021-08-11 13:56:59 +00:00
|
|
|
|
|
|
|
const expectedText = "@foo:example.com changed the pinned messages for the room.";
|
|
|
|
expect(plainText).toBe(expectedText);
|
2023-02-22 10:52:55 +00:00
|
|
|
expect(component.container).toHaveTextContent(expectedText);
|
2021-08-11 13:56:59 +00:00
|
|
|
});
|
2021-07-26 16:41:39 +00:00
|
|
|
});
|
2022-01-20 19:10:56 +00:00
|
|
|
|
|
|
|
describe("textForPowerEvent()", () => {
|
2023-02-13 11:39:16 +00:00
|
|
|
let mockClient: Mocked<MatrixClient>;
|
2022-01-25 09:40:02 +00:00
|
|
|
const mockRoom = {
|
|
|
|
getMember: jest.fn(),
|
2023-02-13 11:39:16 +00:00
|
|
|
} as unknown as Mocked<Room>;
|
2022-01-25 09:40:02 +00:00
|
|
|
|
2022-01-20 19:10:56 +00:00
|
|
|
const userA = {
|
2023-02-13 11:39:16 +00:00
|
|
|
userId: "@a",
|
2022-12-12 11:24:14 +00:00
|
|
|
name: "Alice",
|
|
|
|
rawDisplayName: "Alice",
|
2023-02-13 11:39:16 +00:00
|
|
|
} as RoomMember;
|
2022-01-20 19:10:56 +00:00
|
|
|
const userB = {
|
2023-02-13 11:39:16 +00:00
|
|
|
userId: "@b",
|
2022-12-12 11:24:14 +00:00
|
|
|
name: "Bob (@b)",
|
|
|
|
rawDisplayName: "Bob",
|
2023-02-13 11:39:16 +00:00
|
|
|
} as RoomMember;
|
2022-01-20 19:10:56 +00:00
|
|
|
const userC = {
|
2023-02-13 11:39:16 +00:00
|
|
|
userId: "@c",
|
2022-12-12 11:24:14 +00:00
|
|
|
name: "Bob (@c)",
|
|
|
|
rawDisplayName: "Bob",
|
2023-02-13 11:39:16 +00:00
|
|
|
} as RoomMember;
|
2022-01-20 19:10:56 +00:00
|
|
|
interface PowerEventProps {
|
|
|
|
usersDefault?: number;
|
|
|
|
prevDefault?: number;
|
|
|
|
users: Record<string, number>;
|
|
|
|
prevUsers: Record<string, number>;
|
|
|
|
}
|
2022-12-12 11:24:14 +00:00
|
|
|
const mockPowerEvent = ({ usersDefault, prevDefault, users, prevUsers }: PowerEventProps): MatrixEvent => {
|
2022-09-22 07:42:07 +00:00
|
|
|
const mxEvent = new MatrixEvent({
|
|
|
|
type: EventType.RoomPowerLevels,
|
2023-02-13 11:39:16 +00:00
|
|
|
sender: userA.userId,
|
2022-09-22 07:42:07 +00:00
|
|
|
state_key: "",
|
|
|
|
content: {
|
|
|
|
users_default: usersDefault,
|
|
|
|
users,
|
|
|
|
},
|
|
|
|
prev_content: {
|
|
|
|
users: prevUsers,
|
|
|
|
users_default: prevDefault,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
mxEvent.sender = { name: userA.name } as RoomMember;
|
|
|
|
return mxEvent;
|
|
|
|
};
|
2022-01-20 19:10:56 +00:00
|
|
|
|
2022-01-25 09:40:02 +00:00
|
|
|
beforeAll(() => {
|
2023-02-13 17:01:43 +00:00
|
|
|
mockClient = createTestClient() as Mocked<MatrixClient>;
|
2022-01-25 09:40:02 +00:00
|
|
|
MatrixClientPeg.get = () => mockClient;
|
2023-06-05 17:12:23 +00:00
|
|
|
MatrixClientPeg.safeGet = () => mockClient;
|
2022-01-25 09:40:02 +00:00
|
|
|
mockClient.getRoom.mockClear().mockReturnValue(mockRoom);
|
2022-12-12 11:24:14 +00:00
|
|
|
mockRoom.getMember
|
|
|
|
.mockClear()
|
2023-02-13 17:01:43 +00:00
|
|
|
.mockImplementation((userId) => [userA, userB, userC].find((u) => u.userId === userId) || null);
|
2022-01-25 09:40:02 +00:00
|
|
|
(SettingsStore.getValue as jest.Mock).mockReturnValue(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
(UserIdentifierCustomisations.getDisplayUserIdentifier as jest.Mock)
|
|
|
|
.mockClear()
|
2022-12-12 11:24:14 +00:00
|
|
|
.mockImplementation((userId) => userId);
|
2022-01-25 09:40:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns falsy when no users have changed power level", () => {
|
2022-01-20 19:10:56 +00:00
|
|
|
const event = mockPowerEvent({
|
|
|
|
users: {
|
2023-02-13 11:39:16 +00:00
|
|
|
[userA.userId]: 100,
|
2022-01-20 19:10:56 +00:00
|
|
|
},
|
|
|
|
prevUsers: {
|
2023-02-13 11:39:16 +00:00
|
|
|
[userA.userId]: 100,
|
2022-01-20 19:10:56 +00:00
|
|
|
},
|
|
|
|
});
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(event, mockClient)).toBeFalsy();
|
2022-01-20 19:10:56 +00:00
|
|
|
});
|
|
|
|
|
2022-01-25 09:40:02 +00:00
|
|
|
it("returns false when users power levels have been changed by default settings", () => {
|
2022-01-20 19:10:56 +00:00
|
|
|
const event = mockPowerEvent({
|
|
|
|
usersDefault: 100,
|
|
|
|
prevDefault: 50,
|
|
|
|
users: {
|
2023-02-13 11:39:16 +00:00
|
|
|
[userA.userId]: 100,
|
2022-01-20 19:10:56 +00:00
|
|
|
},
|
|
|
|
prevUsers: {
|
2023-02-13 11:39:16 +00:00
|
|
|
[userA.userId]: 50,
|
2022-01-20 19:10:56 +00:00
|
|
|
},
|
|
|
|
});
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(event, mockClient)).toBeFalsy();
|
2022-01-20 19:10:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns correct message for a single user with changed power level", () => {
|
|
|
|
const event = mockPowerEvent({
|
|
|
|
users: {
|
2023-02-13 11:39:16 +00:00
|
|
|
[userB.userId]: 100,
|
2022-01-20 19:10:56 +00:00
|
|
|
},
|
|
|
|
prevUsers: {
|
2023-02-13 11:39:16 +00:00
|
|
|
[userB.userId]: 50,
|
2022-01-20 19:10:56 +00:00
|
|
|
},
|
|
|
|
});
|
2022-09-22 07:42:07 +00:00
|
|
|
const expectedText = "Alice changed the power level of Bob (@b) from Moderator to Admin.";
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(event, mockClient)).toEqual(expectedText);
|
2022-01-20 19:10:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns correct message for a single user with power level changed to the default", () => {
|
|
|
|
const event = mockPowerEvent({
|
|
|
|
usersDefault: 20,
|
|
|
|
prevDefault: 101,
|
|
|
|
users: {
|
2023-02-13 11:39:16 +00:00
|
|
|
[userB.userId]: 20,
|
2022-01-20 19:10:56 +00:00
|
|
|
},
|
|
|
|
prevUsers: {
|
2023-02-13 11:39:16 +00:00
|
|
|
[userB.userId]: 50,
|
2022-01-20 19:10:56 +00:00
|
|
|
},
|
|
|
|
});
|
2022-09-22 07:42:07 +00:00
|
|
|
const expectedText = "Alice changed the power level of Bob (@b) from Moderator to Default.";
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(event, mockClient)).toEqual(expectedText);
|
2022-01-20 19:10:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns correct message for a single user with power level changed to a custom level", () => {
|
|
|
|
const event = mockPowerEvent({
|
|
|
|
users: {
|
2023-02-13 11:39:16 +00:00
|
|
|
[userB.userId]: -1,
|
2022-01-20 19:10:56 +00:00
|
|
|
},
|
|
|
|
prevUsers: {
|
2023-02-13 11:39:16 +00:00
|
|
|
[userB.userId]: 50,
|
2022-01-20 19:10:56 +00:00
|
|
|
},
|
|
|
|
});
|
2022-09-22 07:42:07 +00:00
|
|
|
const expectedText = "Alice changed the power level of Bob (@b) from Moderator to Custom (-1).";
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(event, mockClient)).toEqual(expectedText);
|
2022-01-20 19:10:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns correct message for a multiple power level changes", () => {
|
|
|
|
const event = mockPowerEvent({
|
|
|
|
users: {
|
2023-02-13 11:39:16 +00:00
|
|
|
[userB.userId]: 100,
|
|
|
|
[userC.userId]: 50,
|
2022-01-20 19:10:56 +00:00
|
|
|
},
|
|
|
|
prevUsers: {
|
2023-02-13 11:39:16 +00:00
|
|
|
[userB.userId]: 50,
|
|
|
|
[userC.userId]: 101,
|
2022-01-20 19:10:56 +00:00
|
|
|
},
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
const expectedText =
|
|
|
|
"Alice changed the power level of Bob (@b) from Moderator to Admin," +
|
|
|
|
" Bob (@c) from Custom (101) to Moderator.";
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(event, mockClient)).toEqual(expectedText);
|
2022-01-25 09:40:02 +00:00
|
|
|
});
|
2022-01-20 19:10:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("textForCanonicalAliasEvent()", () => {
|
|
|
|
const userA = {
|
2023-02-13 11:39:16 +00:00
|
|
|
userId: "@a",
|
2022-12-12 11:24:14 +00:00
|
|
|
name: "Alice",
|
2022-01-20 19:10:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
interface AliasEventProps {
|
2022-12-12 11:24:14 +00:00
|
|
|
alias?: string;
|
|
|
|
prevAlias?: string;
|
|
|
|
altAliases?: string[];
|
|
|
|
prevAltAliases?: string[];
|
2022-01-20 19:10:56 +00:00
|
|
|
}
|
2022-12-12 11:24:14 +00:00
|
|
|
const mockEvent = ({ alias, prevAlias, altAliases, prevAltAliases }: AliasEventProps): MatrixEvent =>
|
|
|
|
new MatrixEvent({
|
|
|
|
type: EventType.RoomCanonicalAlias,
|
2023-02-13 11:39:16 +00:00
|
|
|
sender: userA.userId,
|
2022-12-12 11:24:14 +00:00
|
|
|
state_key: "",
|
|
|
|
content: {
|
|
|
|
alias,
|
|
|
|
alt_aliases: altAliases,
|
|
|
|
},
|
|
|
|
prev_content: {
|
|
|
|
alias: prevAlias,
|
|
|
|
alt_aliases: prevAltAliases,
|
|
|
|
},
|
|
|
|
});
|
2022-01-20 19:10:56 +00:00
|
|
|
|
|
|
|
type TestCase = [string, AliasEventProps & { result: string }];
|
|
|
|
const testCases: TestCase[] = [
|
2022-12-12 11:24:14 +00:00
|
|
|
[
|
|
|
|
"room alias didn't change",
|
|
|
|
{
|
|
|
|
result: "@a changed the addresses for this room.",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"room alias changed",
|
|
|
|
{
|
|
|
|
alias: "banana",
|
|
|
|
prevAlias: "apple",
|
|
|
|
result: "@a set the main address for this room to banana.",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"room alias was added",
|
|
|
|
{
|
|
|
|
alias: "banana",
|
|
|
|
result: "@a set the main address for this room to banana.",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"room alias was removed",
|
|
|
|
{
|
|
|
|
prevAlias: "apple",
|
|
|
|
result: "@a removed the main address for this room.",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"added an alt alias",
|
|
|
|
{
|
|
|
|
altAliases: ["canteloupe"],
|
|
|
|
result: "@a added alternative address canteloupe for this room.",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"added multiple alt aliases",
|
|
|
|
{
|
|
|
|
altAliases: ["canteloupe", "date"],
|
|
|
|
result: "@a added the alternative addresses canteloupe, date for this room.",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"removed an alt alias",
|
|
|
|
{
|
|
|
|
altAliases: ["canteloupe"],
|
|
|
|
prevAltAliases: ["canteloupe", "date"],
|
|
|
|
result: "@a removed alternative address date for this room.",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"added and removed an alt aliases",
|
|
|
|
{
|
|
|
|
altAliases: ["canteloupe", "elderberry"],
|
|
|
|
prevAltAliases: ["canteloupe", "date"],
|
|
|
|
result: "@a changed the alternative addresses for this room.",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"changed alias and added alt alias",
|
|
|
|
{
|
|
|
|
alias: "banana",
|
|
|
|
prevAlias: "apple",
|
|
|
|
altAliases: ["canteloupe"],
|
|
|
|
result: "@a changed the main and alternative addresses for this room.",
|
|
|
|
},
|
|
|
|
],
|
2022-01-20 19:10:56 +00:00
|
|
|
];
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it.each(testCases)("returns correct message when %s", (_d, { result, ...eventProps }) => {
|
2022-01-20 19:10:56 +00:00
|
|
|
const event = mockEvent(eventProps);
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(event, mockClient)).toEqual(result);
|
2022-01-20 19:10:56 +00:00
|
|
|
});
|
|
|
|
});
|
2022-04-11 09:10:16 +00:00
|
|
|
|
|
|
|
describe("textForPollStartEvent()", () => {
|
2023-02-13 11:39:16 +00:00
|
|
|
let pollEvent: MatrixEvent;
|
2022-04-11 09:10:16 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
pollEvent = new MatrixEvent({
|
2022-12-12 11:24:14 +00:00
|
|
|
type: "org.matrix.msc3381.poll.start",
|
|
|
|
sender: "@a",
|
2022-04-11 09:10:16 +00:00
|
|
|
content: {
|
2022-12-12 11:24:14 +00:00
|
|
|
"org.matrix.msc3381.poll.start": {
|
|
|
|
answers: [{ "org.matrix.msc1767.text": "option1" }, { "org.matrix.msc1767.text": "option2" }],
|
2022-04-11 09:10:16 +00:00
|
|
|
question: {
|
2022-12-12 11:24:14 +00:00
|
|
|
"body": "Test poll name",
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"org.matrix.msc1767.text": "Test poll name",
|
2022-04-11 09:10:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns correct message for redacted poll start", () => {
|
|
|
|
pollEvent.makeRedacted(pollEvent);
|
|
|
|
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(pollEvent, mockClient)).toEqual("@a: Message deleted");
|
2022-04-11 09:10:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns correct message for normal poll start", () => {
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(pollEvent, mockClient)).toEqual("@a has started a poll - ");
|
2022-04-11 09:10:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("textForMessageEvent()", () => {
|
2023-02-13 11:39:16 +00:00
|
|
|
let messageEvent: MatrixEvent;
|
2022-04-11 09:10:16 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
messageEvent = new MatrixEvent({
|
2022-12-12 11:24:14 +00:00
|
|
|
type: "m.room.message",
|
|
|
|
sender: "@a",
|
2022-04-11 09:10:16 +00:00
|
|
|
content: {
|
2022-12-12 11:24:14 +00:00
|
|
|
"body": "test message",
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"org.matrix.msc1767.text": "test message",
|
2022-04-11 09:10:16 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns correct message for redacted message", () => {
|
|
|
|
messageEvent.makeRedacted(messageEvent);
|
|
|
|
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(messageEvent, mockClient)).toEqual("@a: Message deleted");
|
2022-04-11 09:10:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns correct message for normal message", () => {
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(messageEvent, mockClient)).toEqual("@a: test message");
|
2022-04-11 09:10:16 +00:00
|
|
|
});
|
|
|
|
});
|
2022-10-06 14:27:12 +00:00
|
|
|
|
|
|
|
describe("textForCallEvent()", () => {
|
|
|
|
let mockClient: MatrixClient;
|
|
|
|
let callEvent: MatrixEvent;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
2023-06-05 17:12:23 +00:00
|
|
|
mockClient = MatrixClientPeg.safeGet();
|
2022-10-06 14:27:12 +00:00
|
|
|
|
|
|
|
mocked(mockClient.getRoom).mockReturnValue({
|
|
|
|
name: "Test room",
|
|
|
|
} as unknown as Room);
|
|
|
|
|
|
|
|
callEvent = {
|
|
|
|
getRoomId: jest.fn(),
|
|
|
|
getType: jest.fn(),
|
|
|
|
isState: jest.fn().mockReturnValue(true),
|
|
|
|
} as unknown as MatrixEvent;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each(ElementCall.CALL_EVENT_TYPE.names)("eventType=%s", (eventType: string) => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(callEvent).getType.mockReturnValue(eventType);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns correct message for call event when supported", () => {
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(callEvent, mockClient)).toEqual("Video call started in Test room.");
|
2022-10-06 14:27:12 +00:00
|
|
|
});
|
|
|
|
|
2023-03-01 15:23:35 +00:00
|
|
|
it("returns correct message for call event when not supported", () => {
|
2022-10-06 14:27:12 +00:00
|
|
|
mocked(mockClient).supportsVoip.mockReturnValue(false);
|
|
|
|
|
2023-06-05 17:12:23 +00:00
|
|
|
expect(textForEvent(callEvent, mockClient)).toEqual(
|
2022-12-12 11:24:14 +00:00
|
|
|
"Video call started in Test room. (not supported by this browser)",
|
2022-10-06 14:27:12 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-05-12 11:54:24 +00:00
|
|
|
|
|
|
|
describe("textForMemberEvent()", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle both displayname and avatar changing in one event", () => {
|
|
|
|
expect(
|
|
|
|
textForEvent(
|
|
|
|
new MatrixEvent({
|
|
|
|
type: "m.room.member",
|
|
|
|
sender: "@a:foo",
|
|
|
|
content: {
|
|
|
|
membership: "join",
|
|
|
|
avatar_url: "b",
|
|
|
|
displayname: "Bob",
|
|
|
|
},
|
|
|
|
prev_content: {
|
|
|
|
membership: "join",
|
|
|
|
avatar_url: "a",
|
|
|
|
displayname: "Andy",
|
|
|
|
},
|
|
|
|
state_key: "@a:foo",
|
|
|
|
}),
|
2023-06-05 17:12:23 +00:00
|
|
|
mockClient,
|
2023-05-12 11:54:24 +00:00
|
|
|
),
|
|
|
|
).toMatchInlineSnapshot(`"Andy changed their display name and profile picture"`);
|
|
|
|
});
|
|
|
|
});
|
2023-07-10 08:01:03 +00:00
|
|
|
|
|
|
|
describe("textForJoinRulesEvent()", () => {
|
|
|
|
type TestCase = [string, { result: string }];
|
|
|
|
const testCases: TestCase[] = [
|
|
|
|
[JoinRule.Public, { result: "@a made the room public to whoever knows the link." }],
|
|
|
|
[JoinRule.Invite, { result: "@a made the room invite only." }],
|
|
|
|
[JoinRule.Knock, { result: "@a changed the join rule to ask to join." }],
|
|
|
|
[JoinRule.Restricted, { result: "@a changed who can join this room." }],
|
|
|
|
];
|
|
|
|
|
|
|
|
it.each(testCases)("returns correct message when room join rule changed to %s", (joinRule, { result }) => {
|
|
|
|
expect(
|
|
|
|
textForEvent(
|
|
|
|
new MatrixEvent({
|
|
|
|
type: "m.room.join_rules",
|
|
|
|
sender: "@a",
|
|
|
|
content: {
|
|
|
|
join_rule: joinRule,
|
|
|
|
},
|
|
|
|
state_key: "",
|
|
|
|
}),
|
|
|
|
mockClient,
|
|
|
|
),
|
|
|
|
).toEqual(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`returns correct JSX message when room join rule changed to ${JoinRule.Restricted}`, () => {
|
|
|
|
expect(
|
|
|
|
textForEvent(
|
|
|
|
new MatrixEvent({
|
|
|
|
type: "m.room.join_rules",
|
|
|
|
sender: "@a",
|
|
|
|
content: {
|
|
|
|
join_rule: JoinRule.Restricted,
|
|
|
|
},
|
|
|
|
state_key: "",
|
|
|
|
}),
|
|
|
|
mockClient,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns correct default message", () => {
|
|
|
|
expect(
|
|
|
|
textForEvent(
|
|
|
|
new MatrixEvent({
|
|
|
|
type: "m.room.join_rules",
|
|
|
|
sender: "@a",
|
|
|
|
content: {
|
|
|
|
join_rule: "a not implemented one",
|
|
|
|
},
|
|
|
|
state_key: "",
|
|
|
|
}),
|
|
|
|
mockClient,
|
|
|
|
),
|
|
|
|
).toEqual("@a changed the join rule to a not implemented one");
|
|
|
|
});
|
|
|
|
});
|
2021-07-26 16:41:39 +00:00
|
|
|
});
|