2022-11-22 23:05:09 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-11-22 23:05:09 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-11-22 23:05:09 +08:00
|
|
|
*/
|
|
|
|
|
2023-02-02 02:51:37 +08:00
|
|
|
import React from "react";
|
2023-03-10 17:41:14 +08:00
|
|
|
import { mocked } from "jest-mock";
|
2024-10-15 00:11:58 +08:00
|
|
|
import { fireEvent, render, screen, waitFor, waitForElementToBeRemoved } from "jest-matrix-react";
|
2024-04-11 19:18:16 +08:00
|
|
|
import { HierarchyRoom, JoinRule, MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
2024-03-18 22:40:52 +08:00
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2022-11-22 23:05:09 +08:00
|
|
|
import { RoomHierarchy } from "matrix-js-sdk/src/room-hierarchy";
|
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import { mkStubRoom, stubClient } from "../../../test-utils";
|
|
|
|
import dispatcher from "../../../../src/dispatcher/dispatcher";
|
|
|
|
import SpaceHierarchy, { showRoom, toLocalRoom } from "../../../../src/components/structures/SpaceHierarchy";
|
|
|
|
import { Action } from "../../../../src/dispatcher/actions";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
2023-02-02 02:51:37 +08:00
|
|
|
|
2022-11-22 23:05:09 +08:00
|
|
|
describe("SpaceHierarchy", () => {
|
|
|
|
describe("showRoom", () => {
|
|
|
|
let client: MatrixClient;
|
|
|
|
let hierarchy: RoomHierarchy;
|
|
|
|
let room: Room;
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
2023-06-06 01:12:23 +08:00
|
|
|
client = MatrixClientPeg.safeGet();
|
2022-11-22 23:05:09 +08:00
|
|
|
room = new Room("room-id", client, "@alice:example.com");
|
|
|
|
hierarchy = new RoomHierarchy(room);
|
|
|
|
|
|
|
|
jest.spyOn(client, "isGuest").mockReturnValue(false);
|
|
|
|
|
|
|
|
jest.spyOn(hierarchy.roomMap, "get").mockReturnValue({
|
|
|
|
children_state: [],
|
|
|
|
room_id: "room-id2",
|
|
|
|
canonical_alias: "canonical-alias",
|
|
|
|
aliases: ["uncanonical-alias", "canonical-alias"],
|
|
|
|
world_readable: true,
|
|
|
|
guest_can_join: false,
|
|
|
|
num_joined_members: 35,
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.spyOn(dispatcher, "dispatch");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shows room", () => {
|
|
|
|
showRoom(client, hierarchy, "room-id2");
|
|
|
|
expect(dispatcher.dispatch).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
should_peek: true,
|
|
|
|
room_alias: "canonical-alias",
|
|
|
|
room_id: "room-id2",
|
|
|
|
via_servers: [],
|
|
|
|
oob_data: {
|
|
|
|
avatarUrl: undefined,
|
|
|
|
name: "canonical-alias",
|
|
|
|
},
|
|
|
|
roomType: undefined,
|
|
|
|
metricsTrigger: "RoomDirectory",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-01-28 03:20:01 +08:00
|
|
|
|
|
|
|
describe("toLocalRoom", () => {
|
2023-02-03 03:20:57 +08:00
|
|
|
stubClient();
|
2023-06-06 01:12:23 +08:00
|
|
|
const client = MatrixClientPeg.safeGet();
|
2023-02-03 03:38:42 +08:00
|
|
|
const roomV1 = mkStubRoom("room-id-1", "Room V1", client);
|
|
|
|
const roomV2 = mkStubRoom("room-id-2", "Room V2", client);
|
|
|
|
const roomV3 = mkStubRoom("room-id-3", "Room V3", client);
|
2023-02-03 03:20:57 +08:00
|
|
|
jest.spyOn(client, "getRoomUpgradeHistory").mockReturnValue([roomV1, roomV2, roomV3]);
|
2023-01-28 03:20:01 +08:00
|
|
|
|
|
|
|
it("grabs last room that is in hierarchy when latest version is in hierarchy", () => {
|
2023-01-28 03:29:50 +08:00
|
|
|
const hierarchy = {
|
|
|
|
roomMap: new Map([
|
2023-08-15 23:00:17 +08:00
|
|
|
[roomV1.roomId, { room_id: roomV1.roomId } as HierarchyRoom],
|
|
|
|
[roomV2.roomId, { room_id: roomV2.roomId } as HierarchyRoom],
|
|
|
|
[roomV3.roomId, { room_id: roomV3.roomId } as HierarchyRoom],
|
2023-01-28 03:29:50 +08:00
|
|
|
]),
|
|
|
|
} as RoomHierarchy;
|
2023-08-15 23:00:17 +08:00
|
|
|
const localRoomV1 = toLocalRoom(client, { room_id: roomV1.roomId } as HierarchyRoom, hierarchy);
|
2023-01-28 03:20:01 +08:00
|
|
|
expect(localRoomV1.room_id).toEqual(roomV3.roomId);
|
2023-08-15 23:00:17 +08:00
|
|
|
const localRoomV2 = toLocalRoom(client, { room_id: roomV2.roomId } as HierarchyRoom, hierarchy);
|
2023-01-28 03:20:01 +08:00
|
|
|
expect(localRoomV2.room_id).toEqual(roomV3.roomId);
|
2023-08-15 23:00:17 +08:00
|
|
|
const localRoomV3 = toLocalRoom(client, { room_id: roomV3.roomId } as HierarchyRoom, hierarchy);
|
2023-01-28 03:20:01 +08:00
|
|
|
expect(localRoomV3.room_id).toEqual(roomV3.roomId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("grabs last room that is in hierarchy when latest version is *not* in hierarchy", () => {
|
2023-01-28 03:29:50 +08:00
|
|
|
const hierarchy = {
|
|
|
|
roomMap: new Map([
|
2023-08-15 23:00:17 +08:00
|
|
|
[roomV1.roomId, { room_id: roomV1.roomId } as HierarchyRoom],
|
|
|
|
[roomV2.roomId, { room_id: roomV2.roomId } as HierarchyRoom],
|
2023-01-28 03:29:50 +08:00
|
|
|
]),
|
|
|
|
} as RoomHierarchy;
|
2023-08-15 23:00:17 +08:00
|
|
|
const localRoomV1 = toLocalRoom(client, { room_id: roomV1.roomId } as HierarchyRoom, hierarchy);
|
2023-01-28 03:20:01 +08:00
|
|
|
expect(localRoomV1.room_id).toEqual(roomV2.roomId);
|
2023-08-15 23:00:17 +08:00
|
|
|
const localRoomV2 = toLocalRoom(client, { room_id: roomV2.roomId } as HierarchyRoom, hierarchy);
|
2023-01-28 03:20:01 +08:00
|
|
|
expect(localRoomV2.room_id).toEqual(roomV2.roomId);
|
2023-08-15 23:00:17 +08:00
|
|
|
const localRoomV3 = toLocalRoom(client, { room_id: roomV3.roomId } as HierarchyRoom, hierarchy);
|
2023-01-28 03:20:01 +08:00
|
|
|
expect(localRoomV3.room_id).toEqual(roomV2.roomId);
|
|
|
|
});
|
2023-01-30 16:53:41 +08:00
|
|
|
|
|
|
|
it("returns specified room when none of the versions is in hierarchy", () => {
|
|
|
|
const hierarchy = { roomMap: new Map([]) } as RoomHierarchy;
|
2023-08-15 23:00:17 +08:00
|
|
|
const localRoomV1 = toLocalRoom(client, { room_id: roomV1.roomId } as HierarchyRoom, hierarchy);
|
2023-01-30 16:53:41 +08:00
|
|
|
expect(localRoomV1.room_id).toEqual(roomV1.roomId);
|
2023-08-15 23:00:17 +08:00
|
|
|
const localRoomV2 = toLocalRoom(client, { room_id: roomV2.roomId } as HierarchyRoom, hierarchy);
|
2023-01-30 16:53:41 +08:00
|
|
|
expect(localRoomV2.room_id).toEqual(roomV2.roomId);
|
2023-08-15 23:00:17 +08:00
|
|
|
const localRoomV3 = toLocalRoom(client, { room_id: roomV3.roomId } as HierarchyRoom, hierarchy);
|
2023-01-30 16:53:41 +08:00
|
|
|
expect(localRoomV3.room_id).toEqual(roomV3.roomId);
|
|
|
|
});
|
2023-03-10 17:41:14 +08:00
|
|
|
|
|
|
|
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 () => {
|
|
|
|
mocked(client.getRoomUpgradeHistory).mockClear();
|
|
|
|
const hierarchy = { roomMap: new Map([]) } as RoomHierarchy;
|
2023-08-15 23:00:17 +08:00
|
|
|
toLocalRoom(client, { room_id: roomV1.roomId } as HierarchyRoom, hierarchy);
|
2023-03-10 17:41:14 +08:00
|
|
|
expect(client.getRoomUpgradeHistory).toHaveBeenCalledWith(roomV1.roomId, true, 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 () => {
|
|
|
|
mocked(client.getRoomUpgradeHistory).mockClear();
|
|
|
|
const hierarchy = { roomMap: new Map([]) } as RoomHierarchy;
|
2023-08-15 23:00:17 +08:00
|
|
|
toLocalRoom(client, { room_id: roomV1.roomId } as HierarchyRoom, hierarchy);
|
2023-03-10 17:41:14 +08:00
|
|
|
expect(client.getRoomUpgradeHistory).toHaveBeenCalledWith(roomV1.roomId, true, true);
|
|
|
|
});
|
|
|
|
});
|
2023-01-28 03:20:01 +08:00
|
|
|
});
|
2023-02-02 02:51:37 +08:00
|
|
|
|
2023-06-01 20:35:47 +08:00
|
|
|
describe("<SpaceHierarchy />", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
// IntersectionObserver isn't available in test environment
|
|
|
|
const mockIntersectionObserver = jest.fn();
|
|
|
|
mockIntersectionObserver.mockReturnValue({
|
|
|
|
observe: () => null,
|
|
|
|
unobserve: () => null,
|
|
|
|
disconnect: () => null,
|
2023-06-28 21:05:36 +08:00
|
|
|
} as ResizeObserver);
|
2023-06-01 20:35:47 +08:00
|
|
|
window.IntersectionObserver = mockIntersectionObserver;
|
|
|
|
});
|
|
|
|
|
2023-02-03 03:20:57 +08:00
|
|
|
stubClient();
|
2023-06-06 01:12:23 +08:00
|
|
|
const client = MatrixClientPeg.safeGet();
|
2023-02-03 03:20:57 +08:00
|
|
|
|
2023-02-03 03:38:42 +08:00
|
|
|
const dmRoomMap = {
|
2023-02-03 03:20:57 +08:00
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap;
|
|
|
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
|
|
|
|
|
2023-06-01 20:35:47 +08:00
|
|
|
const root = mkStubRoom("space-id-1", "Space 1", client);
|
|
|
|
const room1 = mkStubRoom("room-id-2", "Room 1", client);
|
|
|
|
const room2 = mkStubRoom("room-id-3", "Room 2", client);
|
|
|
|
const space1 = mkStubRoom("space-id-4", "Space 2", client);
|
|
|
|
const room3 = mkStubRoom("room-id-5", "Room 3", client);
|
|
|
|
mocked(client.getRooms).mockReturnValue([root]);
|
|
|
|
mocked(client.getRoom).mockImplementation(
|
|
|
|
(roomId) => client.getRooms().find((room) => room.roomId === roomId) ?? null,
|
|
|
|
);
|
2024-03-12 22:52:54 +08:00
|
|
|
[room1, room2, space1, room3].forEach((r) => mocked(r.getMyMembership).mockReturnValue(KnownMembership.Leave));
|
2023-02-03 03:20:57 +08:00
|
|
|
|
2023-08-15 23:00:17 +08:00
|
|
|
const hierarchyRoot: HierarchyRoom = {
|
2023-02-03 03:20:57 +08:00
|
|
|
room_id: root.roomId,
|
|
|
|
num_joined_members: 1,
|
2023-06-01 20:35:47 +08:00
|
|
|
room_type: "m.space",
|
2023-02-03 03:20:57 +08:00
|
|
|
children_state: [
|
|
|
|
{
|
|
|
|
state_key: room1.roomId,
|
|
|
|
content: { order: "1" },
|
2023-06-01 20:35:47 +08:00
|
|
|
origin_server_ts: 111,
|
|
|
|
type: "m.space.child",
|
|
|
|
sender: "@other:server",
|
2023-02-03 03:20:57 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
state_key: room2.roomId,
|
|
|
|
content: { order: "2" },
|
2023-06-01 20:35:47 +08:00
|
|
|
origin_server_ts: 111,
|
|
|
|
type: "m.space.child",
|
|
|
|
sender: "@other:server",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
state_key: space1.roomId,
|
|
|
|
content: { order: "3" },
|
|
|
|
origin_server_ts: 111,
|
|
|
|
type: "m.space.child",
|
|
|
|
sender: "@other:server",
|
2023-02-03 03:20:57 +08:00
|
|
|
},
|
2024-04-11 19:18:16 +08:00
|
|
|
{
|
|
|
|
state_key: "!knock1:server",
|
|
|
|
content: { order: "4" },
|
|
|
|
origin_server_ts: 111,
|
|
|
|
type: "m.space.child",
|
|
|
|
sender: "@other:server",
|
|
|
|
},
|
2023-02-03 03:20:57 +08:00
|
|
|
],
|
2023-06-01 20:35:47 +08:00
|
|
|
world_readable: true,
|
|
|
|
guest_can_join: true,
|
|
|
|
};
|
2023-08-15 23:00:17 +08:00
|
|
|
const hierarchyRoom1: HierarchyRoom = {
|
2023-06-01 20:35:47 +08:00
|
|
|
room_id: room1.roomId,
|
|
|
|
num_joined_members: 2,
|
|
|
|
children_state: [],
|
|
|
|
world_readable: true,
|
|
|
|
guest_can_join: true,
|
|
|
|
};
|
2023-08-15 23:00:17 +08:00
|
|
|
const hierarchyRoom2: HierarchyRoom = {
|
2023-06-01 20:35:47 +08:00
|
|
|
room_id: room2.roomId,
|
|
|
|
num_joined_members: 3,
|
|
|
|
children_state: [],
|
|
|
|
world_readable: true,
|
|
|
|
guest_can_join: true,
|
|
|
|
};
|
2023-08-15 23:00:17 +08:00
|
|
|
const hierarchyRoom3: HierarchyRoom = {
|
2023-06-01 20:35:47 +08:00
|
|
|
name: "Nested room",
|
|
|
|
room_id: room3.roomId,
|
|
|
|
num_joined_members: 3,
|
|
|
|
children_state: [],
|
|
|
|
world_readable: true,
|
|
|
|
guest_can_join: true,
|
|
|
|
};
|
2023-08-15 23:00:17 +08:00
|
|
|
const hierarchySpace1: HierarchyRoom = {
|
2023-06-01 20:35:47 +08:00
|
|
|
room_id: space1.roomId,
|
|
|
|
name: "Nested space",
|
|
|
|
num_joined_members: 1,
|
|
|
|
room_type: "m.space",
|
|
|
|
children_state: [
|
|
|
|
{
|
|
|
|
state_key: room3.roomId,
|
|
|
|
content: { order: "1" },
|
|
|
|
origin_server_ts: 111,
|
|
|
|
type: "m.space.child",
|
|
|
|
sender: "@other:server",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
world_readable: true,
|
|
|
|
guest_can_join: true,
|
|
|
|
};
|
2024-04-11 19:18:16 +08:00
|
|
|
const hierarchyKnockRoom1: HierarchyRoom = {
|
|
|
|
room_id: "!knock1:server",
|
|
|
|
name: "Knock room",
|
|
|
|
num_joined_members: 3,
|
|
|
|
children_state: [],
|
|
|
|
world_readable: true,
|
|
|
|
guest_can_join: true,
|
|
|
|
join_rule: JoinRule.Knock,
|
|
|
|
};
|
2023-06-01 20:35:47 +08:00
|
|
|
|
|
|
|
mocked(client.getRoomHierarchy).mockResolvedValue({
|
2024-04-11 19:18:16 +08:00
|
|
|
rooms: [
|
|
|
|
hierarchyRoot,
|
|
|
|
hierarchyRoom1,
|
|
|
|
hierarchyRoom2,
|
|
|
|
hierarchySpace1,
|
|
|
|
hierarchyRoom3,
|
|
|
|
hierarchyKnockRoom1,
|
|
|
|
],
|
2023-06-01 20:35:47 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
space: root,
|
|
|
|
showRoom: jest.fn(),
|
|
|
|
};
|
|
|
|
const getComponent = (props = {}): React.ReactElement => (
|
|
|
|
<MatrixClientContext.Provider value={client}>
|
2024-04-12 20:56:23 +08:00
|
|
|
<SpaceHierarchy {...defaultProps} {...props} />
|
2023-06-01 20:35:47 +08:00
|
|
|
</MatrixClientContext.Provider>
|
|
|
|
);
|
|
|
|
|
|
|
|
it("renders", async () => {
|
|
|
|
const { asFragment } = render(getComponent());
|
|
|
|
// Wait for spinners to go away
|
|
|
|
await waitForElementToBeRemoved(screen.getAllByRole("progressbar"));
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should join subspace when joining nested room", async () => {
|
|
|
|
mocked(client.joinRoom).mockResolvedValue({} as Room);
|
|
|
|
|
|
|
|
const { getByText } = render(getComponent());
|
|
|
|
// Wait for spinners to go away
|
|
|
|
await waitForElementToBeRemoved(screen.getAllByRole("progressbar"));
|
|
|
|
const button = getByText("Nested room")!.closest("li")!.querySelector(".mx_AccessibleButton_kind_primary")!;
|
|
|
|
fireEvent.click(button);
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(client.joinRoom).toHaveBeenCalledTimes(2);
|
|
|
|
});
|
|
|
|
// Joins subspace
|
|
|
|
expect(client.joinRoom).toHaveBeenCalledWith(space1.roomId, expect.any(Object));
|
|
|
|
expect(client.joinRoom).toHaveBeenCalledWith(room3.roomId, expect.any(Object));
|
2023-02-02 02:51:37 +08:00
|
|
|
});
|
2024-04-11 19:18:16 +08:00
|
|
|
|
|
|
|
it("should take user to view room for unjoined knockable rooms", async () => {
|
|
|
|
jest.spyOn(dispatcher, "dispatch");
|
|
|
|
|
|
|
|
const { getByText } = render(getComponent());
|
|
|
|
// Wait for spinners to go away
|
|
|
|
await waitForElementToBeRemoved(screen.getAllByRole("progressbar"));
|
|
|
|
const button = getByText("Knock room")!
|
|
|
|
.closest("li")!
|
|
|
|
.querySelector(".mx_AccessibleButton_kind_primary_outline")!;
|
|
|
|
fireEvent.click(button);
|
|
|
|
|
|
|
|
expect(defaultProps.showRoom).toHaveBeenCalledWith(
|
|
|
|
expect.anything(),
|
|
|
|
expect.anything(),
|
|
|
|
hierarchyKnockRoom1.room_id,
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
});
|
2023-02-02 02:51:37 +08:00
|
|
|
});
|
2022-11-22 23:05:09 +08:00
|
|
|
});
|