2021-06-23 15:32:41 +03:00
|
|
|
/*
|
|
|
|
Copyright 2021 Šimon Brandner <simon.bra.ag@gmail.com>
|
2022-11-18 22:05:00 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
2021-06-23 15:32:41 +03:00
|
|
|
|
|
|
|
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-02-13 14:39:16 +03:00
|
|
|
import React, { Component } from "react";
|
2019-08-24 13:47:07 +03:00
|
|
|
import ReactTestUtils from "react-dom/test-utils";
|
2019-01-04 07:55:52 +03:00
|
|
|
import ReactDOM from "react-dom";
|
2021-06-23 15:32:41 +03:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
|
|
|
import { User } from "matrix-js-sdk/src/models/user";
|
2022-11-15 13:20:36 +03:00
|
|
|
import { compare } from "matrix-js-sdk/src/utils";
|
2023-02-13 14:39:16 +03:00
|
|
|
import { MatrixClient, RoomState } from "matrix-js-sdk/src/matrix";
|
2021-10-23 01:23:32 +03:00
|
|
|
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import * as TestUtils from "../../../test-utils";
|
2021-06-23 15:32:41 +03:00
|
|
|
import MemberList from "../../../../src/components/views/rooms/MemberList";
|
2021-07-02 19:15:05 +03:00
|
|
|
import MemberTile from "../../../../src/components/views/rooms/MemberTile";
|
2022-11-18 22:05:00 +03:00
|
|
|
import { SDKContext } from "../../../../src/contexts/SDKContext";
|
|
|
|
import { TestSdkContext } from "../../../TestSdkContext";
|
2021-06-02 12:42:17 +03:00
|
|
|
|
2019-01-04 07:55:52 +03:00
|
|
|
function generateRoomId() {
|
|
|
|
return "!" + Math.random().toString().slice(2, 10) + ":domain";
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("MemberList", () => {
|
2022-03-21 12:03:03 +03:00
|
|
|
function createRoom(opts = {}) {
|
2023-02-16 12:38:44 +03:00
|
|
|
const room = new Room(generateRoomId(), client, client.getUserId()!);
|
2019-01-04 07:55:52 +03:00
|
|
|
if (opts) {
|
|
|
|
Object.assign(room, opts);
|
|
|
|
}
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:36:22 +03:00
|
|
|
let parentDiv: HTMLDivElement;
|
|
|
|
let client: MatrixClient;
|
|
|
|
let root: Component;
|
2023-02-13 14:39:16 +03:00
|
|
|
let memberListRoom: Room;
|
2023-02-15 16:36:22 +03:00
|
|
|
let memberList: MemberList;
|
2019-01-04 07:55:52 +03:00
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
let adminUsers: RoomMember[] = [];
|
|
|
|
let moderatorUsers: RoomMember[] = [];
|
|
|
|
let defaultUsers: RoomMember[] = [];
|
2019-01-04 07:55:52 +03:00
|
|
|
|
2019-01-04 08:33:01 +03:00
|
|
|
beforeEach(function () {
|
2019-12-16 14:12:48 +03:00
|
|
|
TestUtils.stubClient();
|
2019-01-04 07:55:52 +03:00
|
|
|
client = MatrixClientPeg.get();
|
|
|
|
client.hasLazyLoadMembersEnabled = () => false;
|
|
|
|
|
|
|
|
parentDiv = document.createElement("div");
|
|
|
|
document.body.appendChild(parentDiv);
|
|
|
|
|
|
|
|
// Make room
|
|
|
|
memberListRoom = createRoom();
|
|
|
|
expect(memberListRoom.roomId).toBeTruthy();
|
|
|
|
|
|
|
|
// Make users
|
2019-01-04 08:07:09 +03:00
|
|
|
adminUsers = [];
|
|
|
|
moderatorUsers = [];
|
|
|
|
defaultUsers = [];
|
2019-01-04 07:55:52 +03:00
|
|
|
const usersPerLevel = 2;
|
|
|
|
for (let i = 0; i < usersPerLevel; i++) {
|
|
|
|
const adminUser = new RoomMember(memberListRoom.roomId, `@admin${i}:localhost`);
|
|
|
|
adminUser.membership = "join";
|
|
|
|
adminUser.powerLevel = 100;
|
|
|
|
adminUser.user = new User(adminUser.userId);
|
|
|
|
adminUser.user.currentlyActive = true;
|
|
|
|
adminUser.user.presence = "online";
|
|
|
|
adminUser.user.lastPresenceTs = 1000;
|
|
|
|
adminUser.user.lastActiveAgo = 10;
|
|
|
|
adminUsers.push(adminUser);
|
|
|
|
|
|
|
|
const moderatorUser = new RoomMember(memberListRoom.roomId, `@moderator${i}:localhost`);
|
|
|
|
moderatorUser.membership = "join";
|
|
|
|
moderatorUser.powerLevel = 50;
|
|
|
|
moderatorUser.user = new User(moderatorUser.userId);
|
|
|
|
moderatorUser.user.currentlyActive = true;
|
|
|
|
moderatorUser.user.presence = "online";
|
|
|
|
moderatorUser.user.lastPresenceTs = 1000;
|
|
|
|
moderatorUser.user.lastActiveAgo = 10;
|
|
|
|
moderatorUsers.push(moderatorUser);
|
|
|
|
|
|
|
|
const defaultUser = new RoomMember(memberListRoom.roomId, `@default${i}:localhost`);
|
|
|
|
defaultUser.membership = "join";
|
|
|
|
defaultUser.powerLevel = 0;
|
|
|
|
defaultUser.user = new User(defaultUser.userId);
|
|
|
|
defaultUser.user.currentlyActive = true;
|
|
|
|
defaultUser.user.presence = "online";
|
|
|
|
defaultUser.user.lastPresenceTs = 1000;
|
|
|
|
defaultUser.user.lastActiveAgo = 10;
|
|
|
|
defaultUsers.push(defaultUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
client.getRoom = (roomId) => {
|
|
|
|
if (roomId === memberListRoom.roomId) return memberListRoom;
|
|
|
|
else return null;
|
|
|
|
};
|
|
|
|
memberListRoom.currentState = {
|
|
|
|
members: {},
|
2021-05-19 12:31:05 +03:00
|
|
|
getMember: jest.fn(),
|
2023-02-13 14:39:16 +03:00
|
|
|
getStateEvents: ((eventType, stateKey) =>
|
|
|
|
stateKey === undefined ? [] : null) as RoomState["getStateEvents"], // ignore 3pid invites
|
|
|
|
} as unknown as RoomState;
|
2019-01-04 07:55:52 +03:00
|
|
|
for (const member of [...adminUsers, ...moderatorUsers, ...defaultUsers]) {
|
|
|
|
memberListRoom.currentState.members[member.userId] = member;
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
const gatherWrappedRef = (r: MemberList) => {
|
2019-01-04 07:55:52 +03:00
|
|
|
memberList = r;
|
|
|
|
};
|
2022-11-18 22:05:00 +03:00
|
|
|
const context = new TestSdkContext();
|
|
|
|
context.client = client;
|
2021-06-23 15:32:41 +03:00
|
|
|
root = ReactDOM.render(
|
2022-11-18 22:05:00 +03:00
|
|
|
<SDKContext.Provider value={context}>
|
2022-03-21 12:03:03 +03:00
|
|
|
<MemberList
|
|
|
|
searchQuery=""
|
|
|
|
onClose={jest.fn()}
|
|
|
|
onSearchQueryChanged={jest.fn()}
|
|
|
|
roomId={memberListRoom.roomId}
|
|
|
|
ref={gatherWrappedRef}
|
|
|
|
/>
|
2022-11-18 22:05:00 +03:00
|
|
|
</SDKContext.Provider>,
|
2021-06-23 15:32:41 +03:00
|
|
|
parentDiv,
|
2023-02-13 14:39:16 +03:00
|
|
|
) as unknown as Component;
|
2019-01-04 07:55:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach((done) => {
|
|
|
|
if (parentDiv) {
|
|
|
|
ReactDOM.unmountComponentAtNode(parentDiv);
|
|
|
|
parentDiv.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
function expectOrderedByPresenceAndPowerLevel(memberTiles: MemberTile[], isPresenceEnabled: boolean) {
|
2023-02-15 16:36:22 +03:00
|
|
|
let prevMember: RoomMember | undefined;
|
2019-01-04 07:55:52 +03:00
|
|
|
for (const tile of memberTiles) {
|
|
|
|
const memberA = prevMember;
|
|
|
|
const memberB = tile.props.member;
|
|
|
|
prevMember = memberB; // just in case an expect fails, set this early
|
|
|
|
if (!memberA) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("COMPARING A VS B:");
|
|
|
|
console.log(memberList.memberString(memberA));
|
|
|
|
console.log(memberList.memberString(memberB));
|
|
|
|
|
2023-02-15 16:36:22 +03:00
|
|
|
const userA = memberA.user!;
|
|
|
|
const userB = memberB.user!;
|
2019-01-04 07:55:52 +03:00
|
|
|
|
|
|
|
let groupChange = false;
|
|
|
|
|
|
|
|
if (isPresenceEnabled) {
|
2023-02-13 14:39:16 +03:00
|
|
|
const convertPresence = (p: string) => (p === "unavailable" ? "online" : p);
|
|
|
|
const presenceIndex = (p: string) => {
|
2019-01-04 07:55:52 +03:00
|
|
|
const order = ["active", "online", "offline"];
|
|
|
|
const idx = order.indexOf(convertPresence(p));
|
|
|
|
return idx === -1 ? order.length : idx; // unknown states at the end
|
|
|
|
};
|
|
|
|
|
|
|
|
const idxA = presenceIndex(userA.currentlyActive ? "active" : userA.presence);
|
|
|
|
const idxB = presenceIndex(userB.currentlyActive ? "active" : userB.presence);
|
|
|
|
console.log("Comparing presence groups...");
|
|
|
|
expect(idxB).toBeGreaterThanOrEqual(idxA);
|
|
|
|
groupChange = idxA !== idxB;
|
|
|
|
} else {
|
|
|
|
console.log("Skipped presence groups");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!groupChange) {
|
|
|
|
console.log("Comparing power levels...");
|
|
|
|
expect(memberA.powerLevel).toBeGreaterThanOrEqual(memberB.powerLevel);
|
|
|
|
groupChange = memberA.powerLevel !== memberB.powerLevel;
|
|
|
|
} else {
|
|
|
|
console.log("Skipping power level check due to group change");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!groupChange) {
|
|
|
|
if (isPresenceEnabled) {
|
|
|
|
console.log("Comparing last active timestamp...");
|
2019-01-04 08:24:24 +03:00
|
|
|
expect(userB.getLastActiveTs()).toBeLessThanOrEqual(userA.getLastActiveTs());
|
2019-01-04 07:55:52 +03:00
|
|
|
groupChange = userA.getLastActiveTs() !== userB.getLastActiveTs();
|
|
|
|
} else {
|
|
|
|
console.log("Skipping last active timestamp");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("Skipping last active timestamp check due to group change");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!groupChange) {
|
2022-04-14 10:52:42 +03:00
|
|
|
const nameA = memberA.name[0] === "@" ? memberA.name.slice(1) : memberA.name;
|
|
|
|
const nameB = memberB.name[0] === "@" ? memberB.name.slice(1) : memberB.name;
|
2021-06-02 12:42:17 +03:00
|
|
|
const nameCompare = compare(nameB, nameA);
|
2019-01-04 07:55:52 +03:00
|
|
|
console.log("Comparing name");
|
|
|
|
expect(nameCompare).toBeGreaterThanOrEqual(0);
|
|
|
|
} else {
|
|
|
|
console.log("Skipping name check due to group change");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
function itDoesOrderMembersCorrectly(enablePresence: boolean) {
|
2019-01-04 07:55:52 +03:00
|
|
|
describe("does order members correctly", () => {
|
|
|
|
// Note: even if presence is disabled, we still expect that the presence
|
|
|
|
// tests will pass. All expectOrderedByPresenceAndPowerLevel does is ensure
|
|
|
|
// the order is perceived correctly, regardless of what we did to the members.
|
|
|
|
|
|
|
|
// Each of the 4 tests here is done to prove that the member list can meet
|
|
|
|
// all 4 criteria independently. Together, they should work.
|
|
|
|
|
|
|
|
it("by presence state", () => {
|
|
|
|
// Intentionally pick users that will confuse the power level sorting
|
|
|
|
const activeUsers = [defaultUsers[0]];
|
|
|
|
const onlineUsers = [adminUsers[0]];
|
|
|
|
const offlineUsers = [...moderatorUsers, ...adminUsers.slice(1), ...defaultUsers.slice(1)];
|
|
|
|
activeUsers.forEach((u) => {
|
2023-02-15 16:36:22 +03:00
|
|
|
u.user!.currentlyActive = true;
|
|
|
|
u.user!.presence = "online";
|
2019-01-04 07:55:52 +03:00
|
|
|
});
|
|
|
|
onlineUsers.forEach((u) => {
|
2023-02-15 16:36:22 +03:00
|
|
|
u.user!.currentlyActive = false;
|
|
|
|
u.user!.presence = "online";
|
2019-01-04 07:55:52 +03:00
|
|
|
});
|
|
|
|
offlineUsers.forEach((u) => {
|
2023-02-15 16:36:22 +03:00
|
|
|
u.user!.currentlyActive = false;
|
|
|
|
u.user!.presence = "offline";
|
2019-01-04 07:55:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// Bypass all the event listeners and skip to the good part
|
2021-06-23 15:32:41 +03:00
|
|
|
memberList.showPresence = enablePresence;
|
|
|
|
memberList.updateListNow();
|
2019-01-04 07:55:52 +03:00
|
|
|
|
|
|
|
const tiles = ReactTestUtils.scryRenderedComponentsWithType(root, MemberTile);
|
|
|
|
expectOrderedByPresenceAndPowerLevel(tiles, enablePresence);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("by power level", () => {
|
|
|
|
// We already have admin, moderator, and default users so leave them alone
|
|
|
|
|
|
|
|
// Bypass all the event listeners and skip to the good part
|
2023-02-13 14:39:16 +03:00
|
|
|
memberList.showPresence = enablePresence;
|
2021-06-23 15:32:41 +03:00
|
|
|
memberList.updateListNow();
|
2019-01-04 07:55:52 +03:00
|
|
|
|
|
|
|
const tiles = ReactTestUtils.scryRenderedComponentsWithType(root, MemberTile);
|
|
|
|
expectOrderedByPresenceAndPowerLevel(tiles, enablePresence);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("by last active timestamp", () => {
|
|
|
|
// Intentionally pick users that will confuse the power level sorting
|
|
|
|
// lastActiveAgoTs == lastPresenceTs - lastActiveAgo
|
|
|
|
const activeUsers = [defaultUsers[0]];
|
|
|
|
const semiActiveUsers = [adminUsers[0]];
|
|
|
|
const inactiveUsers = [...moderatorUsers, ...adminUsers.slice(1), ...defaultUsers.slice(1)];
|
|
|
|
activeUsers.forEach((u) => {
|
2019-01-04 08:24:24 +03:00
|
|
|
u.powerLevel = 100; // set everyone to the same PL to avoid running that check
|
2023-02-15 16:36:22 +03:00
|
|
|
u.user!.lastPresenceTs = 1000;
|
|
|
|
u.user!.lastActiveAgo = 0;
|
2019-01-04 07:55:52 +03:00
|
|
|
});
|
|
|
|
semiActiveUsers.forEach((u) => {
|
2019-01-04 08:24:24 +03:00
|
|
|
u.powerLevel = 100;
|
2023-02-15 16:36:22 +03:00
|
|
|
u.user!.lastPresenceTs = 1000;
|
|
|
|
u.user!.lastActiveAgo = 50;
|
2019-01-04 07:55:52 +03:00
|
|
|
});
|
|
|
|
inactiveUsers.forEach((u) => {
|
2019-01-04 08:24:24 +03:00
|
|
|
u.powerLevel = 100;
|
2023-02-15 16:36:22 +03:00
|
|
|
u.user!.lastPresenceTs = 1000;
|
|
|
|
u.user!.lastActiveAgo = 100;
|
2019-01-04 07:55:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// Bypass all the event listeners and skip to the good part
|
2021-06-23 15:32:41 +03:00
|
|
|
memberList.showPresence = enablePresence;
|
|
|
|
memberList.updateListNow();
|
2019-01-04 07:55:52 +03:00
|
|
|
|
|
|
|
const tiles = ReactTestUtils.scryRenderedComponentsWithType(root, MemberTile);
|
|
|
|
expectOrderedByPresenceAndPowerLevel(tiles, enablePresence);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("by name", () => {
|
|
|
|
// Intentionally put everyone on the same level to force a name comparison
|
|
|
|
const allUsers = [...adminUsers, ...moderatorUsers, ...defaultUsers];
|
|
|
|
allUsers.forEach((u) => {
|
2023-02-15 16:36:22 +03:00
|
|
|
u.user!.currentlyActive = true;
|
|
|
|
u.user!.presence = "online";
|
|
|
|
u.user!.lastPresenceTs = 1000;
|
|
|
|
u.user!.lastActiveAgo = 0;
|
2019-01-04 07:55:52 +03:00
|
|
|
u.powerLevel = 100;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Bypass all the event listeners and skip to the good part
|
2021-06-23 15:32:41 +03:00
|
|
|
memberList.showPresence = enablePresence;
|
|
|
|
memberList.updateListNow();
|
2019-01-04 07:55:52 +03:00
|
|
|
|
|
|
|
const tiles = ReactTestUtils.scryRenderedComponentsWithType(root, MemberTile);
|
|
|
|
expectOrderedByPresenceAndPowerLevel(tiles, enablePresence);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("when presence is enabled", () => {
|
|
|
|
itDoesOrderMembersCorrectly(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when presence is not enabled", () => {
|
|
|
|
itDoesOrderMembersCorrectly(false);
|
|
|
|
});
|
|
|
|
});
|