2021-04-22 13:45:13 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 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.
|
|
|
|
*/
|
|
|
|
|
2022-05-18 13:15:25 +00:00
|
|
|
import { EventEmitter } from "events";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { mocked } from "jest-mock";
|
2021-04-22 13:45:13 +00:00
|
|
|
import { EventType } from "matrix-js-sdk/src/@types/event";
|
2021-07-27 13:39:14 +00:00
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
2022-03-01 08:33:29 +00:00
|
|
|
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
|
|
|
|
import { defer } from "matrix-js-sdk/src/utils";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { ClientEvent, RoomEvent, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2021-04-22 13:45:13 +00:00
|
|
|
|
2021-11-11 13:07:41 +00:00
|
|
|
import SpaceStore from "../../src/stores/spaces/SpaceStore";
|
|
|
|
import {
|
|
|
|
MetaSpace,
|
2021-07-27 20:11:47 +00:00
|
|
|
UPDATE_HOME_BEHAVIOUR,
|
2021-04-25 08:24:00 +00:00
|
|
|
UPDATE_INVITED_SPACES,
|
|
|
|
UPDATE_SELECTED_SPACE,
|
2021-06-04 15:52:50 +00:00
|
|
|
UPDATE_TOP_LEVEL_SPACES,
|
2021-11-11 13:07:41 +00:00
|
|
|
} from "../../src/stores/spaces";
|
2022-02-23 11:21:11 +00:00
|
|
|
import * as testUtils from "../test-utils";
|
2021-07-27 19:15:40 +00:00
|
|
|
import { mkEvent, stubClient } from "../test-utils";
|
2021-04-23 11:19:38 +00:00
|
|
|
import DMRoomMap from "../../src/utils/DMRoomMap";
|
2021-04-23 13:45:22 +00:00
|
|
|
import defaultDispatcher from "../../src/dispatcher/dispatcher";
|
2021-07-27 20:11:47 +00:00
|
|
|
import SettingsStore from "../../src/settings/SettingsStore";
|
|
|
|
import { SettingLevel } from "../../src/settings/SettingLevel";
|
2022-01-12 20:12:28 +00:00
|
|
|
import { Action } from "../../src/dispatcher/actions";
|
2022-03-01 08:33:29 +00:00
|
|
|
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
|
2021-04-22 13:45:13 +00:00
|
|
|
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
const testUserId = "@test:user";
|
|
|
|
|
2021-04-23 15:14:55 +00:00
|
|
|
const fav1 = "!fav1:server";
|
|
|
|
const fav2 = "!fav2:server";
|
|
|
|
const fav3 = "!fav3:server";
|
|
|
|
const dm1 = "!dm1:server";
|
2021-07-27 13:39:14 +00:00
|
|
|
const dm1Partner = new RoomMember(dm1, "@dm1Partner:server");
|
|
|
|
dm1Partner.membership = "join";
|
2021-04-23 15:14:55 +00:00
|
|
|
const dm2 = "!dm2:server";
|
2021-07-27 13:39:14 +00:00
|
|
|
const dm2Partner = new RoomMember(dm2, "@dm2Partner:server");
|
|
|
|
dm2Partner.membership = "join";
|
2021-04-23 15:14:55 +00:00
|
|
|
const dm3 = "!dm3:server";
|
2021-07-27 13:39:14 +00:00
|
|
|
const dm3Partner = new RoomMember(dm3, "@dm3Partner:server");
|
|
|
|
dm3Partner.membership = "join";
|
2021-04-23 15:14:55 +00:00
|
|
|
const orphan1 = "!orphan1:server";
|
|
|
|
const orphan2 = "!orphan2:server";
|
|
|
|
const invite1 = "!invite1:server";
|
|
|
|
const invite2 = "!invite2:server";
|
|
|
|
const room1 = "!room1:server";
|
|
|
|
const room2 = "!room2:server";
|
2021-05-07 10:22:10 +00:00
|
|
|
const room3 = "!room3:server";
|
2022-02-17 20:24:05 +00:00
|
|
|
const room4 = "!room4:server";
|
2021-04-23 15:14:55 +00:00
|
|
|
const space1 = "!space1:server";
|
|
|
|
const space2 = "!space2:server";
|
|
|
|
const space3 = "!space3:server";
|
2022-02-17 20:24:05 +00:00
|
|
|
const space4 = "!space4:server";
|
2021-04-23 15:14:55 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const getUserIdForRoomId = jest.fn((roomId) => {
|
2021-12-17 09:26:32 +00:00
|
|
|
return {
|
|
|
|
[dm1]: dm1Partner.userId,
|
|
|
|
[dm2]: dm2Partner.userId,
|
|
|
|
[dm3]: dm3Partner.userId,
|
|
|
|
}[roomId];
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
const getDMRoomsForUserId = jest.fn((userId) => {
|
2021-12-17 09:26:32 +00:00
|
|
|
switch (userId) {
|
|
|
|
case dm1Partner.userId:
|
|
|
|
return [dm1];
|
|
|
|
case dm2Partner.userId:
|
|
|
|
return [dm2];
|
|
|
|
case dm3Partner.userId:
|
|
|
|
return [dm3];
|
|
|
|
default:
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// @ts-ignore
|
|
|
|
DMRoomMap.sharedInstance = { getUserIdForRoomId, getDMRoomsForUserId };
|
|
|
|
|
2021-04-22 13:45:13 +00:00
|
|
|
describe("SpaceStore", () => {
|
2021-04-23 11:19:38 +00:00
|
|
|
stubClient();
|
2021-04-22 13:45:13 +00:00
|
|
|
const store = SpaceStore.instance;
|
2021-04-23 11:19:38 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
2021-04-22 13:45:13 +00:00
|
|
|
|
2022-03-01 08:33:29 +00:00
|
|
|
const spyDispatcher = jest.spyOn(defaultDispatcher, "dispatch");
|
|
|
|
|
2021-07-06 10:35:56 +00:00
|
|
|
let rooms = [];
|
2021-07-27 19:15:40 +00:00
|
|
|
const mkRoom = (roomId: string) => testUtils.mkRoom(client, roomId, rooms);
|
|
|
|
const mkSpace = (spaceId: string, children: string[] = []) => testUtils.mkSpace(client, spaceId, rooms, children);
|
2022-12-12 11:24:14 +00:00
|
|
|
const viewRoom = (roomId) => defaultDispatcher.dispatch({ action: Action.ViewRoom, room_id: roomId }, true);
|
2021-04-23 13:45:22 +00:00
|
|
|
|
2021-04-22 13:45:13 +00:00
|
|
|
const run = async () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(client).getRoom.mockImplementation((roomId) => rooms.find((room) => room.roomId === roomId));
|
|
|
|
mocked(client).getRoomUpgradeHistory.mockImplementation((roomId) => [
|
|
|
|
rooms.find((room) => room.roomId === roomId),
|
|
|
|
]);
|
2021-07-27 19:15:40 +00:00
|
|
|
await testUtils.setupAsyncStoreWithClient(store, client);
|
2022-01-26 10:38:07 +00:00
|
|
|
jest.runOnlyPendingTimers();
|
2021-04-22 13:45:13 +00:00
|
|
|
};
|
|
|
|
|
2021-07-27 20:11:47 +00:00
|
|
|
const setShowAllRooms = async (value: boolean) => {
|
|
|
|
if (store.allRoomsInHome === value) return;
|
|
|
|
const emitProm = testUtils.emitPromise(store, UPDATE_HOME_BEHAVIOUR);
|
2021-07-30 11:20:02 +00:00
|
|
|
await SettingsStore.setValue("Spaces.allRoomsInHome", null, SettingLevel.DEVICE, value);
|
2022-01-26 10:38:07 +00:00
|
|
|
jest.runOnlyPendingTimers(); // run async dispatch
|
2021-07-27 20:11:47 +00:00
|
|
|
await emitProm;
|
|
|
|
};
|
|
|
|
|
2021-11-11 13:07:41 +00:00
|
|
|
beforeEach(async () => {
|
2022-01-26 10:38:07 +00:00
|
|
|
jest.runOnlyPendingTimers(); // run async dispatch
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(client).getVisibleRooms.mockReturnValue((rooms = []));
|
2021-11-11 13:07:41 +00:00
|
|
|
|
|
|
|
await SettingsStore.setValue("Spaces.enabledMetaSpaces", null, SettingLevel.DEVICE, {
|
|
|
|
[MetaSpace.Home]: true,
|
|
|
|
[MetaSpace.Favourites]: true,
|
|
|
|
[MetaSpace.People]: true,
|
|
|
|
[MetaSpace.Orphans]: true,
|
|
|
|
});
|
2022-03-01 08:33:29 +00:00
|
|
|
|
|
|
|
spyDispatcher.mockClear();
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|
2021-11-11 13:07:41 +00:00
|
|
|
|
2021-04-23 08:55:30 +00:00
|
|
|
afterEach(async () => {
|
2021-07-27 19:15:40 +00:00
|
|
|
await testUtils.resetAsyncStoreWithClient(store);
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("static hierarchy resolution tests", () => {
|
|
|
|
it("handles no spaces", async () => {
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([]);
|
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("handles 3 joined top level spaces", async () => {
|
|
|
|
mkSpace("!space1:server");
|
|
|
|
mkSpace("!space2:server");
|
|
|
|
mkSpace("!space3:server");
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(store.spacePanelSpaces.sort()).toStrictEqual(client.getVisibleRooms().sort());
|
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("handles a basic hierarchy", async () => {
|
|
|
|
mkSpace("!space1:server");
|
|
|
|
mkSpace("!space2:server");
|
|
|
|
mkSpace("!company:server", [
|
2022-12-12 11:24:14 +00:00
|
|
|
mkSpace("!company_dept1:server", [mkSpace("!company_dept1_group1:server").roomId]).roomId,
|
2021-04-22 13:45:13 +00:00
|
|
|
mkSpace("!company_dept2:server").roomId,
|
|
|
|
]);
|
|
|
|
await run();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(store.spacePanelSpaces.map((r) => r.roomId).sort()).toStrictEqual(
|
|
|
|
["!space1:server", "!space2:server", "!company:server"].sort(),
|
|
|
|
);
|
2021-04-22 13:45:13 +00:00
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
2021-04-23 10:20:26 +00:00
|
|
|
|
|
|
|
expect(store.getChildRooms("!space1:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!space1:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildRooms("!space2:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!space2:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildRooms("!company:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!company:server")).toStrictEqual([
|
|
|
|
client.getRoom("!company_dept1:server"),
|
|
|
|
client.getRoom("!company_dept2:server"),
|
|
|
|
]);
|
|
|
|
expect(store.getChildRooms("!company_dept1:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!company_dept1:server")).toStrictEqual([
|
|
|
|
client.getRoom("!company_dept1_group1:server"),
|
|
|
|
]);
|
|
|
|
expect(store.getChildRooms("!company_dept1_group1:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!company_dept1_group1:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildRooms("!company_dept2:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!company_dept2:server")).toStrictEqual([]);
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("handles a sub-space existing in multiple places in the space tree", async () => {
|
|
|
|
const subspace = mkSpace("!subspace:server");
|
|
|
|
mkSpace("!space1:server");
|
|
|
|
mkSpace("!space2:server");
|
|
|
|
mkSpace("!company:server", [
|
2022-12-12 11:24:14 +00:00
|
|
|
mkSpace("!company_dept1:server", [mkSpace("!company_dept1_group1:server", [subspace.roomId]).roomId])
|
|
|
|
.roomId,
|
2021-04-22 13:45:13 +00:00
|
|
|
mkSpace("!company_dept2:server", [subspace.roomId]).roomId,
|
|
|
|
subspace.roomId,
|
|
|
|
]);
|
|
|
|
await run();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(store.spacePanelSpaces.map((r) => r.roomId).sort()).toStrictEqual(
|
|
|
|
["!space1:server", "!space2:server", "!company:server"].sort(),
|
|
|
|
);
|
2021-04-22 13:45:13 +00:00
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
2021-04-23 10:20:26 +00:00
|
|
|
|
|
|
|
expect(store.getChildRooms("!space1:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!space1:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildRooms("!space2:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!space2:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildRooms("!company:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!company:server")).toStrictEqual([
|
|
|
|
client.getRoom("!company_dept1:server"),
|
|
|
|
client.getRoom("!company_dept2:server"),
|
|
|
|
subspace,
|
|
|
|
]);
|
|
|
|
expect(store.getChildRooms("!company_dept1:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!company_dept1:server")).toStrictEqual([
|
|
|
|
client.getRoom("!company_dept1_group1:server"),
|
|
|
|
]);
|
|
|
|
expect(store.getChildRooms("!company_dept1_group1:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!company_dept1_group1:server")).toStrictEqual([subspace]);
|
|
|
|
expect(store.getChildRooms("!company_dept2:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!company_dept2:server")).toStrictEqual([subspace]);
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|
|
|
|
|
2021-04-23 10:20:26 +00:00
|
|
|
it("handles full cycles", async () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
mkSpace("!a:server", [mkSpace("!b:server", [mkSpace("!c:server", ["!a:server"]).roomId]).roomId]);
|
2021-04-22 13:45:13 +00:00
|
|
|
await run();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(store.spacePanelSpaces.map((r) => r.roomId)).toStrictEqual(["!a:server"]);
|
2021-04-22 13:45:13 +00:00
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
2021-04-23 10:20:26 +00:00
|
|
|
|
|
|
|
expect(store.getChildRooms("!a:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!a:server")).toStrictEqual([client.getRoom("!b:server")]);
|
|
|
|
expect(store.getChildRooms("!b:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!b:server")).toStrictEqual([client.getRoom("!c:server")]);
|
|
|
|
expect(store.getChildRooms("!c:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!c:server")).toStrictEqual([client.getRoom("!a:server")]);
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|
|
|
|
|
2021-04-23 10:20:26 +00:00
|
|
|
it("handles partial cycles", async () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
mkSpace("!b:server", [mkSpace("!a:server", [mkSpace("!c:server", ["!a:server"]).roomId]).roomId]);
|
2021-04-22 13:45:13 +00:00
|
|
|
await run();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(store.spacePanelSpaces.map((r) => r.roomId)).toStrictEqual(["!b:server"]);
|
2021-04-22 13:45:13 +00:00
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
2021-04-23 10:20:26 +00:00
|
|
|
|
|
|
|
expect(store.getChildRooms("!b:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!b:server")).toStrictEqual([client.getRoom("!a:server")]);
|
|
|
|
expect(store.getChildRooms("!a:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!a:server")).toStrictEqual([client.getRoom("!c:server")]);
|
|
|
|
expect(store.getChildRooms("!c:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!c:server")).toStrictEqual([client.getRoom("!a:server")]);
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|
|
|
|
|
2021-04-23 10:20:26 +00:00
|
|
|
it("handles partial cycles with additional spaces coming off them", async () => {
|
|
|
|
// TODO this test should be failing right now
|
2021-04-22 13:45:13 +00:00
|
|
|
mkSpace("!a:server", [
|
2022-12-12 11:24:14 +00:00
|
|
|
mkSpace("!b:server", [mkSpace("!c:server", ["!a:server", mkSpace("!d:server").roomId]).roomId]).roomId,
|
2021-04-22 13:45:13 +00:00
|
|
|
]);
|
|
|
|
await run();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(store.spacePanelSpaces.map((r) => r.roomId)).toStrictEqual(["!a:server"]);
|
2021-04-22 13:45:13 +00:00
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
2021-04-23 10:20:26 +00:00
|
|
|
|
|
|
|
expect(store.getChildRooms("!a:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!a:server")).toStrictEqual([client.getRoom("!b:server")]);
|
|
|
|
expect(store.getChildRooms("!b:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!b:server")).toStrictEqual([client.getRoom("!c:server")]);
|
|
|
|
expect(store.getChildRooms("!c:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!c:server")).toStrictEqual([
|
|
|
|
client.getRoom("!a:server"),
|
|
|
|
client.getRoom("!d:server"),
|
|
|
|
]);
|
|
|
|
expect(store.getChildRooms("!d:server")).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces("!d:server")).toStrictEqual([]);
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|
|
|
|
|
2021-04-24 10:31:52 +00:00
|
|
|
it("invite to a subspace is only shown at the top level", async () => {
|
|
|
|
mkSpace(invite1).getMyMembership.mockReturnValue("invite");
|
|
|
|
mkSpace(space1, [invite1]);
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([client.getRoom(space1)]);
|
|
|
|
expect(store.getChildSpaces(space1)).toStrictEqual([]);
|
|
|
|
expect(store.getChildRooms(space1)).toStrictEqual([]);
|
|
|
|
expect(store.invitedSpaces).toStrictEqual([client.getRoom(invite1)]);
|
|
|
|
});
|
|
|
|
|
2021-04-23 12:40:16 +00:00
|
|
|
describe("test fixture 1", () => {
|
2021-04-23 11:19:38 +00:00
|
|
|
beforeEach(async () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
[
|
|
|
|
fav1,
|
|
|
|
fav2,
|
|
|
|
fav3,
|
|
|
|
dm1,
|
|
|
|
dm2,
|
|
|
|
dm3,
|
|
|
|
orphan1,
|
|
|
|
orphan2,
|
|
|
|
invite1,
|
|
|
|
invite2,
|
|
|
|
room1,
|
|
|
|
room2,
|
|
|
|
room3,
|
|
|
|
room4,
|
|
|
|
].forEach(mkRoom);
|
2021-04-23 15:14:55 +00:00
|
|
|
mkSpace(space1, [fav1, room1]);
|
|
|
|
mkSpace(space2, [fav1, fav2, fav3, room1]);
|
2021-04-23 11:19:38 +00:00
|
|
|
mkSpace(space3, [invite2]);
|
2022-02-17 20:24:05 +00:00
|
|
|
mkSpace(space4, [room4, fav2, space2, space3]);
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(client).getRoom.mockImplementation((roomId) => rooms.find((room) => room.roomId === roomId));
|
2021-04-23 11:19:38 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
[fav1, fav2, fav3].forEach((roomId) => {
|
2021-04-23 11:19:38 +00:00
|
|
|
client.getRoom(roomId).tags = {
|
|
|
|
"m.favourite": {
|
|
|
|
order: 0.5,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
[invite1, invite2].forEach((roomId) => {
|
2022-03-11 16:03:33 +00:00
|
|
|
mocked(client.getRoom(roomId)).getMyMembership.mockReturnValue("invite");
|
2021-04-23 11:19:38 +00:00
|
|
|
});
|
|
|
|
|
2021-07-27 13:39:14 +00:00
|
|
|
// have dmPartner1 be in space1 with you
|
|
|
|
const mySpace1Member = new RoomMember(space1, testUserId);
|
|
|
|
mySpace1Member.membership = "join";
|
2022-12-12 11:24:14 +00:00
|
|
|
(rooms.find((r) => r.roomId === space1).getMembers as jest.Mock).mockReturnValue([
|
2021-07-27 13:39:14 +00:00
|
|
|
mySpace1Member,
|
|
|
|
dm1Partner,
|
|
|
|
]);
|
|
|
|
// have dmPartner2 be in space2 with you
|
|
|
|
const mySpace2Member = new RoomMember(space2, testUserId);
|
|
|
|
mySpace2Member.membership = "join";
|
2022-12-12 11:24:14 +00:00
|
|
|
(rooms.find((r) => r.roomId === space2).getMembers as jest.Mock).mockReturnValue([
|
2021-07-27 13:39:14 +00:00
|
|
|
mySpace2Member,
|
|
|
|
dm2Partner,
|
|
|
|
]);
|
|
|
|
// dmPartner3 is not in any common spaces with you
|
|
|
|
|
2021-09-06 13:34:06 +00:00
|
|
|
// room 2 claims to be a child of space2 and is so via a valid m.space.parent
|
|
|
|
const cliRoom2 = client.getRoom(room2);
|
2022-03-11 16:03:33 +00:00
|
|
|
const room2MockStateEvents = testUtils.mockStateEventImplementation([
|
2021-09-06 13:34:06 +00:00
|
|
|
mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.SpaceParent,
|
|
|
|
room: room2,
|
|
|
|
user: client.getUserId(),
|
|
|
|
skey: space2,
|
|
|
|
content: { via: [], canonical: true },
|
|
|
|
ts: Date.now(),
|
2022-03-11 16:03:33 +00:00
|
|
|
}) as MatrixEvent,
|
|
|
|
]);
|
|
|
|
mocked(cliRoom2.currentState).getStateEvents.mockImplementation(room2MockStateEvents);
|
2021-09-06 13:34:06 +00:00
|
|
|
const cliSpace2 = client.getRoom(space2);
|
2022-03-11 16:03:33 +00:00
|
|
|
mocked(cliSpace2.currentState).maySendStateEvent.mockImplementation(
|
|
|
|
(evType: string, userId: string) => {
|
|
|
|
if (evType === EventType.SpaceChild) {
|
|
|
|
return userId === client.getUserId();
|
|
|
|
}
|
|
|
|
return true;
|
2022-12-12 11:24:14 +00:00
|
|
|
},
|
|
|
|
);
|
2021-09-06 13:34:06 +00:00
|
|
|
|
|
|
|
// room 3 claims to be a child of space3 but is not due to invalid m.space.parent (permissions)
|
|
|
|
const cliRoom3 = client.getRoom(room3);
|
2022-03-11 16:03:33 +00:00
|
|
|
mocked(cliRoom3.currentState).getStateEvents.mockImplementation(
|
|
|
|
testUtils.mockStateEventImplementation([
|
|
|
|
mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.SpaceParent,
|
|
|
|
room: room3,
|
|
|
|
user: client.getUserId(),
|
|
|
|
skey: space3,
|
|
|
|
content: { via: [], canonical: true },
|
|
|
|
ts: Date.now(),
|
|
|
|
}),
|
2022-12-12 11:24:14 +00:00
|
|
|
]),
|
|
|
|
);
|
2021-09-06 13:34:06 +00:00
|
|
|
const cliSpace3 = client.getRoom(space3);
|
2022-03-11 16:03:33 +00:00
|
|
|
mocked(cliSpace3.currentState).maySendStateEvent.mockImplementation(
|
|
|
|
(evType: string, userId: string) => {
|
|
|
|
if (evType === EventType.SpaceChild) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2022-12-12 11:24:14 +00:00
|
|
|
},
|
|
|
|
);
|
2021-09-06 13:34:06 +00:00
|
|
|
|
2021-04-23 11:19:38 +00:00
|
|
|
await run();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("isRoomInSpace()", () => {
|
2022-02-17 20:24:05 +00:00
|
|
|
it("home space contains orphaned rooms", () => {
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, orphan1)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, orphan2)).toBeTruthy();
|
|
|
|
});
|
2021-04-23 11:19:38 +00:00
|
|
|
|
2022-02-17 20:24:05 +00:00
|
|
|
it("home space does not contain all favourites", () => {
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, fav1)).toBeFalsy();
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, fav2)).toBeFalsy();
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, fav3)).toBeFalsy();
|
|
|
|
});
|
2021-04-23 11:19:38 +00:00
|
|
|
|
2022-02-17 20:24:05 +00:00
|
|
|
it("home space contains dm rooms", () => {
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, dm1)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, dm2)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, dm3)).toBeTruthy();
|
|
|
|
});
|
2021-04-23 11:19:38 +00:00
|
|
|
|
2022-02-17 20:24:05 +00:00
|
|
|
it("home space contains invites", () => {
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, invite1)).toBeTruthy();
|
|
|
|
});
|
2021-04-23 11:19:38 +00:00
|
|
|
|
2022-02-17 20:24:05 +00:00
|
|
|
it("home space contains invites even if they are also shown in a space", () => {
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, invite2)).toBeTruthy();
|
|
|
|
});
|
2021-04-23 11:19:38 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("all rooms space does contain rooms/low priority even if they are also shown in a space", async () => {
|
|
|
|
await setShowAllRooms(true);
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, room1)).toBeTruthy();
|
|
|
|
});
|
2021-11-11 13:07:41 +00:00
|
|
|
|
2022-02-17 20:24:05 +00:00
|
|
|
it("favourites space does contain favourites even if they are also shown in a space", async () => {
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Favourites, fav1)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Favourites, fav2)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Favourites, fav3)).toBeTruthy();
|
|
|
|
});
|
2021-11-11 13:07:41 +00:00
|
|
|
|
2022-02-17 20:24:05 +00:00
|
|
|
it("people space does contain people even if they are also shown in a space", async () => {
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.People, dm1)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.People, dm2)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.People, dm3)).toBeTruthy();
|
|
|
|
});
|
2021-11-11 13:07:41 +00:00
|
|
|
|
2022-02-17 20:24:05 +00:00
|
|
|
it("orphans space does contain orphans even if they are also shown in all rooms", async () => {
|
|
|
|
await setShowAllRooms(true);
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Orphans, orphan1)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Orphans, orphan2)).toBeTruthy();
|
|
|
|
});
|
2021-04-23 12:40:16 +00:00
|
|
|
|
2022-02-17 20:24:05 +00:00
|
|
|
it("home space doesn't contain rooms/low priority if they are also shown in a space", async () => {
|
|
|
|
await setShowAllRooms(false);
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, room1)).toBeFalsy();
|
|
|
|
});
|
2021-07-27 20:11:47 +00:00
|
|
|
|
2022-02-17 20:24:05 +00:00
|
|
|
it("space contains child rooms", () => {
|
|
|
|
expect(store.isRoomInSpace(space1, fav1)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space1, room1)).toBeTruthy();
|
|
|
|
});
|
2021-04-23 12:40:16 +00:00
|
|
|
|
2022-02-17 20:24:05 +00:00
|
|
|
it("returns true for all sub-space child rooms when includeSubSpaceRooms is undefined", () => {
|
|
|
|
expect(store.isRoomInSpace(space4, room4)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space4, fav2)).toBeTruthy();
|
|
|
|
// space2's rooms
|
|
|
|
expect(store.isRoomInSpace(space4, fav1)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space4, fav3)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space4, room1)).toBeTruthy();
|
|
|
|
// space3's rooms
|
|
|
|
expect(store.isRoomInSpace(space4, invite2)).toBeTruthy();
|
|
|
|
});
|
2021-04-23 12:40:16 +00:00
|
|
|
|
2022-02-17 20:24:05 +00:00
|
|
|
it("returns true for all sub-space child rooms when includeSubSpaceRooms is true", () => {
|
|
|
|
expect(store.isRoomInSpace(space4, room4, true)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space4, fav2, true)).toBeTruthy();
|
|
|
|
// space2's rooms
|
|
|
|
expect(store.isRoomInSpace(space4, fav1, true)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space4, fav3, true)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space4, room1, true)).toBeTruthy();
|
|
|
|
// space3's rooms
|
|
|
|
expect(store.isRoomInSpace(space4, invite2, true)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns false for all sub-space child rooms when includeSubSpaceRooms is false", () => {
|
|
|
|
// direct children
|
|
|
|
expect(store.isRoomInSpace(space4, room4, false)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space4, fav2, false)).toBeTruthy();
|
|
|
|
// space2's rooms
|
|
|
|
expect(store.isRoomInSpace(space4, fav1, false)).toBeFalsy();
|
|
|
|
expect(store.isRoomInSpace(space4, fav3, false)).toBeFalsy();
|
|
|
|
expect(store.isRoomInSpace(space4, room1, false)).toBeFalsy();
|
|
|
|
// space3's rooms
|
|
|
|
expect(store.isRoomInSpace(space4, invite2, false)).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("space contains all sub-space's child rooms", () => {
|
|
|
|
expect(store.isRoomInSpace(space4, room4)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space4, fav2)).toBeTruthy();
|
|
|
|
// space2's rooms
|
|
|
|
expect(store.isRoomInSpace(space4, fav1)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space4, fav3)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space4, room1)).toBeTruthy();
|
|
|
|
// space3's rooms
|
|
|
|
expect(store.isRoomInSpace(space4, invite2)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("space contains child favourites", () => {
|
|
|
|
expect(store.isRoomInSpace(space2, fav1)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space2, fav2)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space2, fav3)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space2, room1)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("space contains child invites", () => {
|
|
|
|
expect(store.isRoomInSpace(space3, invite2)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("spaces contain dms which you have with members of that space", () => {
|
|
|
|
expect(store.isRoomInSpace(space1, dm1)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space2, dm1)).toBeFalsy();
|
|
|
|
expect(store.isRoomInSpace(space3, dm1)).toBeFalsy();
|
|
|
|
expect(store.isRoomInSpace(space1, dm2)).toBeFalsy();
|
|
|
|
expect(store.isRoomInSpace(space2, dm2)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space3, dm2)).toBeFalsy();
|
|
|
|
expect(store.isRoomInSpace(space1, dm3)).toBeFalsy();
|
|
|
|
expect(store.isRoomInSpace(space2, dm3)).toBeFalsy();
|
|
|
|
expect(store.isRoomInSpace(space3, dm3)).toBeFalsy();
|
|
|
|
});
|
2021-07-27 13:39:14 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("uses cached aggregated rooms", () => {
|
2022-02-17 20:24:05 +00:00
|
|
|
const rooms = store.getSpaceFilteredRoomIds(space4, true);
|
|
|
|
expect(store.isRoomInSpace(space4, fav1)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space4, fav3)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(space4, room1)).toBeTruthy();
|
|
|
|
|
|
|
|
// isRoomInSpace calls didn't rebuild room set
|
|
|
|
expect(rooms).toStrictEqual(store.getSpaceFilteredRoomIds(space4, true));
|
|
|
|
});
|
2021-07-27 13:39:14 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 09:26:32 +00:00
|
|
|
it("dms are only added to Notification States for only the People Space", async () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
[dm1, dm2, dm3].forEach((d) => {
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(MetaSpace.People)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(d),
|
|
|
|
).toBeTruthy();
|
2021-12-17 09:26:32 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
[space1, space2, space3, MetaSpace.Home, MetaSpace.Orphans, MetaSpace.Favourites].forEach((s) => {
|
|
|
|
[dm1, dm2, dm3].forEach((d) => {
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(s)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(d),
|
|
|
|
).toBeFalsy();
|
2021-07-27 13:39:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-17 09:26:32 +00:00
|
|
|
it("orphan rooms are added to Notification States for only the Home Space", async () => {
|
|
|
|
await setShowAllRooms(false);
|
2022-12-12 11:24:14 +00:00
|
|
|
[orphan1, orphan2].forEach((d) => {
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(MetaSpace.Home)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(d),
|
|
|
|
).toBeTruthy();
|
2021-12-17 09:26:32 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
[space1, space2, space3].forEach((s) => {
|
|
|
|
[orphan1, orphan2].forEach((d) => {
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(s)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(d),
|
|
|
|
).toBeFalsy();
|
2021-07-27 13:39:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("favourites are added to Notification States for all spaces containing the room inc Home", () => {
|
|
|
|
// XXX: All rooms space is forcibly enabled, as part of a future PR test Home space better
|
|
|
|
// [fav1, fav2, fav3].forEach(d => {
|
|
|
|
// expect(store.getNotificationState(HOME_SPACE).rooms.map(r => r.roomId).includes(d)).toBeTruthy();
|
|
|
|
// });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(space1)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(fav1),
|
|
|
|
).toBeTruthy();
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(space1)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(fav2),
|
|
|
|
).toBeFalsy();
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(space1)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(fav3),
|
|
|
|
).toBeFalsy();
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(space2)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(fav1),
|
|
|
|
).toBeTruthy();
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(space2)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(fav2),
|
|
|
|
).toBeTruthy();
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(space2)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(fav3),
|
|
|
|
).toBeTruthy();
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(space3)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(fav1),
|
|
|
|
).toBeFalsy();
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(space3)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(fav2),
|
|
|
|
).toBeFalsy();
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(space3)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(fav3),
|
|
|
|
).toBeFalsy();
|
2021-07-27 13:39:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("other rooms are added to Notification States for all spaces containing the room exc Home", () => {
|
|
|
|
// XXX: All rooms space is forcibly enabled, as part of a future PR test Home space better
|
|
|
|
// expect(store.getNotificationState(HOME_SPACE).rooms.map(r => r.roomId).includes(room1)).toBeFalsy();
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(space1)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(room1),
|
|
|
|
).toBeTruthy();
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(space2)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(room1),
|
|
|
|
).toBeTruthy();
|
|
|
|
expect(
|
|
|
|
store
|
|
|
|
.getNotificationState(space3)
|
|
|
|
.rooms.map((r) => r.roomId)
|
|
|
|
.includes(room1),
|
|
|
|
).toBeFalsy();
|
2021-07-27 13:39:14 +00:00
|
|
|
});
|
2021-09-06 13:34:06 +00:00
|
|
|
|
|
|
|
it("honours m.space.parent if sender has permission in parent space", () => {
|
2021-12-17 09:26:32 +00:00
|
|
|
expect(store.isRoomInSpace(space2, room2)).toBeTruthy();
|
2021-09-06 13:34:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("does not honour m.space.parent if sender does not have permission in parent space", () => {
|
2021-12-17 09:26:32 +00:00
|
|
|
expect(store.isRoomInSpace(space3, room3)).toBeFalsy();
|
2021-09-06 13:34:06 +00:00
|
|
|
});
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("hierarchy resolution update tests", () => {
|
2021-04-25 08:24:00 +00:00
|
|
|
it("updates state when spaces are joined", async () => {
|
|
|
|
await run();
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([]);
|
|
|
|
const space = mkSpace(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
const prom = testUtils.emitPromise(store, UPDATE_TOP_LEVEL_SPACES);
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(ClientEvent.Room, space);
|
2021-04-25 08:24:00 +00:00
|
|
|
await prom;
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([space]);
|
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates state when spaces are left", async () => {
|
|
|
|
const space = mkSpace(space1);
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([space]);
|
|
|
|
space.getMyMembership.mockReturnValue("leave");
|
2021-07-27 19:15:40 +00:00
|
|
|
const prom = testUtils.emitPromise(store, UPDATE_TOP_LEVEL_SPACES);
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(RoomEvent.MyMembership, space, "leave", "join");
|
2021-04-25 08:24:00 +00:00
|
|
|
await prom;
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates state when space invite comes in", async () => {
|
|
|
|
await run();
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([]);
|
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
|
|
|
const space = mkSpace(space1);
|
|
|
|
space.getMyMembership.mockReturnValue("invite");
|
2021-07-27 19:15:40 +00:00
|
|
|
const prom = testUtils.emitPromise(store, UPDATE_INVITED_SPACES);
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(ClientEvent.Room, space);
|
2021-04-25 08:24:00 +00:00
|
|
|
await prom;
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([]);
|
|
|
|
expect(store.invitedSpaces).toStrictEqual([space]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates state when space invite is accepted", async () => {
|
|
|
|
const space = mkSpace(space1);
|
|
|
|
space.getMyMembership.mockReturnValue("invite");
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([]);
|
|
|
|
expect(store.invitedSpaces).toStrictEqual([space]);
|
|
|
|
space.getMyMembership.mockReturnValue("join");
|
2021-07-27 19:15:40 +00:00
|
|
|
const prom = testUtils.emitPromise(store, UPDATE_TOP_LEVEL_SPACES);
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(RoomEvent.MyMembership, space, "join", "invite");
|
2021-04-25 08:24:00 +00:00
|
|
|
await prom;
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([space]);
|
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates state when space invite is rejected", async () => {
|
|
|
|
const space = mkSpace(space1);
|
|
|
|
space.getMyMembership.mockReturnValue("invite");
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([]);
|
|
|
|
expect(store.invitedSpaces).toStrictEqual([space]);
|
|
|
|
space.getMyMembership.mockReturnValue("leave");
|
2021-07-27 19:15:40 +00:00
|
|
|
const prom = testUtils.emitPromise(store, UPDATE_INVITED_SPACES);
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(RoomEvent.MyMembership, space, "leave", "invite");
|
2021-04-25 08:24:00 +00:00
|
|
|
await prom;
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([]);
|
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
|
|
|
});
|
2021-04-26 07:37:45 +00:00
|
|
|
|
|
|
|
it("room invite gets added to relevant space filters", async () => {
|
|
|
|
const space = mkSpace(space1, [invite1]);
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([space]);
|
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces(space1)).toStrictEqual([]);
|
|
|
|
expect(store.getChildRooms(space1)).toStrictEqual([]);
|
2021-12-17 09:26:32 +00:00
|
|
|
expect(store.isRoomInSpace(space1, invite1)).toBeFalsy();
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, invite1)).toBeFalsy();
|
2021-04-26 07:37:45 +00:00
|
|
|
|
|
|
|
const invite = mkRoom(invite1);
|
|
|
|
invite.getMyMembership.mockReturnValue("invite");
|
2021-07-27 19:15:40 +00:00
|
|
|
const prom = testUtils.emitPromise(store, space1);
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(ClientEvent.Room, space);
|
2021-04-26 07:37:45 +00:00
|
|
|
await prom;
|
|
|
|
|
|
|
|
expect(store.spacePanelSpaces).toStrictEqual([space]);
|
|
|
|
expect(store.invitedSpaces).toStrictEqual([]);
|
|
|
|
expect(store.getChildSpaces(space1)).toStrictEqual([]);
|
|
|
|
expect(store.getChildRooms(space1)).toStrictEqual([invite]);
|
2021-12-17 09:26:32 +00:00
|
|
|
expect(store.isRoomInSpace(space1, invite1)).toBeTruthy();
|
|
|
|
expect(store.isRoomInSpace(MetaSpace.Home, invite1)).toBeTruthy();
|
2021-04-26 07:37:45 +00:00
|
|
|
});
|
2022-02-17 20:24:05 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("onRoomsUpdate()", () => {
|
2022-02-17 20:24:05 +00:00
|
|
|
beforeEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
[
|
|
|
|
fav1,
|
|
|
|
fav2,
|
|
|
|
fav3,
|
|
|
|
dm1,
|
|
|
|
dm2,
|
|
|
|
dm3,
|
|
|
|
orphan1,
|
|
|
|
orphan2,
|
|
|
|
invite1,
|
|
|
|
invite2,
|
|
|
|
room1,
|
|
|
|
room2,
|
|
|
|
room3,
|
|
|
|
room4,
|
|
|
|
].forEach(mkRoom);
|
2022-02-17 20:24:05 +00:00
|
|
|
mkSpace(space2, [fav1, fav2, fav3, room1]);
|
|
|
|
mkSpace(space3, [invite2]);
|
|
|
|
mkSpace(space4, [room4, fav2, space2, space3]);
|
|
|
|
mkSpace(space1, [fav1, room1, space4]);
|
|
|
|
});
|
|
|
|
|
|
|
|
const addChildRoom = (spaceId, childId) => {
|
|
|
|
const childEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.SpaceChild,
|
|
|
|
room: spaceId,
|
|
|
|
user: client.getUserId(),
|
|
|
|
skey: childId,
|
|
|
|
content: { via: [], canonical: true },
|
|
|
|
ts: Date.now(),
|
|
|
|
});
|
|
|
|
const spaceRoom = client.getRoom(spaceId);
|
2022-03-11 16:03:33 +00:00
|
|
|
mocked(spaceRoom.currentState).getStateEvents.mockImplementation(
|
2022-02-17 20:24:05 +00:00
|
|
|
testUtils.mockStateEventImplementation([childEvent]),
|
|
|
|
);
|
|
|
|
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(RoomStateEvent.Events, childEvent, spaceRoom.currentState, undefined);
|
2022-02-17 20:24:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const addMember = (spaceId, user: RoomMember) => {
|
|
|
|
const memberEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMember,
|
|
|
|
room: spaceId,
|
|
|
|
user: client.getUserId(),
|
|
|
|
skey: user.userId,
|
2022-12-12 11:24:14 +00:00
|
|
|
content: { membership: "join" },
|
2022-02-17 20:24:05 +00:00
|
|
|
ts: Date.now(),
|
|
|
|
});
|
|
|
|
const spaceRoom = client.getRoom(spaceId);
|
2022-03-11 16:03:33 +00:00
|
|
|
mocked(spaceRoom.currentState).getStateEvents.mockImplementation(
|
2022-02-17 20:24:05 +00:00
|
|
|
testUtils.mockStateEventImplementation([memberEvent]),
|
|
|
|
);
|
2022-03-11 16:03:33 +00:00
|
|
|
mocked(spaceRoom).getMember.mockReturnValue(user);
|
2022-02-17 20:24:05 +00:00
|
|
|
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(RoomStateEvent.Members, memberEvent, spaceRoom.currentState, user);
|
2022-02-17 20:24:05 +00:00
|
|
|
};
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("emits events for parent spaces when child room is added", async () => {
|
2022-02-17 20:24:05 +00:00
|
|
|
await run();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const room5 = mkRoom("!room5:server");
|
|
|
|
const emitSpy = jest.spyOn(store, "emit").mockClear();
|
2022-02-17 20:24:05 +00:00
|
|
|
// add room5 into space2
|
|
|
|
addChildRoom(space2, room5.roomId);
|
|
|
|
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith(space2);
|
|
|
|
// space2 is subspace of space4
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith(space4);
|
|
|
|
// space4 is a subspace of space1
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith(space1);
|
|
|
|
expect(emitSpy).not.toHaveBeenCalledWith(space3);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates rooms state when a child room is added", async () => {
|
2022-02-17 20:24:05 +00:00
|
|
|
await run();
|
2022-12-12 11:24:14 +00:00
|
|
|
const room5 = mkRoom("!room5:server");
|
2022-02-17 20:24:05 +00:00
|
|
|
|
|
|
|
expect(store.isRoomInSpace(space2, room5.roomId)).toBeFalsy();
|
|
|
|
expect(store.isRoomInSpace(space4, room5.roomId)).toBeFalsy();
|
|
|
|
|
|
|
|
// add room5 into space2
|
|
|
|
addChildRoom(space2, room5.roomId);
|
|
|
|
|
|
|
|
expect(store.isRoomInSpace(space2, room5.roomId)).toBeTruthy();
|
|
|
|
// space2 is subspace of space4
|
|
|
|
expect(store.isRoomInSpace(space4, room5.roomId)).toBeTruthy();
|
|
|
|
// space4 is subspace of space1
|
|
|
|
expect(store.isRoomInSpace(space1, room5.roomId)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("emits events for parent spaces when a member is added", async () => {
|
2022-02-17 20:24:05 +00:00
|
|
|
await run();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit").mockClear();
|
2022-02-17 20:24:05 +00:00
|
|
|
// add into space2
|
|
|
|
addMember(space2, dm1Partner);
|
|
|
|
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith(space2);
|
|
|
|
// space2 is subspace of space4
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith(space4);
|
|
|
|
// space4 is a subspace of space1
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith(space1);
|
|
|
|
expect(emitSpy).not.toHaveBeenCalledWith(space3);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates users state when a member is added", async () => {
|
2022-02-17 20:24:05 +00:00
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(store.getSpaceFilteredUserIds(space2)).toEqual(new Set([]));
|
|
|
|
|
|
|
|
// add into space2
|
|
|
|
addMember(space2, dm1Partner);
|
|
|
|
|
|
|
|
expect(store.getSpaceFilteredUserIds(space2)).toEqual(new Set([dm1Partner.userId]));
|
|
|
|
expect(store.getSpaceFilteredUserIds(space4)).toEqual(new Set([dm1Partner.userId]));
|
|
|
|
expect(store.getSpaceFilteredUserIds(space1)).toEqual(new Set([dm1Partner.userId]));
|
|
|
|
});
|
|
|
|
});
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|
|
|
|
|
2021-04-23 10:20:26 +00:00
|
|
|
describe("active space switching tests", () => {
|
2021-04-24 10:32:55 +00:00
|
|
|
const fn = jest.spyOn(store, "emit");
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
mkRoom(room1); // not a space
|
2022-12-12 11:24:14 +00:00
|
|
|
mkSpace(space1, [mkSpace(space2).roomId]);
|
2021-04-24 10:32:55 +00:00
|
|
|
mkSpace(space3).getMyMembership.mockReturnValue("invite");
|
|
|
|
await run();
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(MetaSpace.Home);
|
|
|
|
expect(store.activeSpace).toBe(MetaSpace.Home);
|
2021-04-24 10:32:55 +00:00
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
fn.mockClear();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("switch to home space", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1);
|
2021-04-24 10:32:55 +00:00
|
|
|
fn.mockClear();
|
|
|
|
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(MetaSpace.Home);
|
|
|
|
expect(fn).toHaveBeenCalledWith(UPDATE_SELECTED_SPACE, MetaSpace.Home);
|
|
|
|
expect(store.activeSpace).toBe(MetaSpace.Home);
|
2021-04-24 10:32:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("switch to invited space", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space3);
|
|
|
|
expect(fn).toHaveBeenCalledWith(UPDATE_SELECTED_SPACE, space3);
|
|
|
|
expect(store.activeSpace).toBe(space3);
|
2021-04-24 10:32:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("switch to top level space", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1);
|
|
|
|
expect(fn).toHaveBeenCalledWith(UPDATE_SELECTED_SPACE, space1);
|
|
|
|
expect(store.activeSpace).toBe(space1);
|
2021-04-24 10:32:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("switch to subspace", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space2);
|
|
|
|
expect(fn).toHaveBeenCalledWith(UPDATE_SELECTED_SPACE, space2);
|
|
|
|
expect(store.activeSpace).toBe(space2);
|
2021-04-24 10:32:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("switch to unknown space is a nop", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
expect(store.activeSpace).toBe(MetaSpace.Home);
|
2021-04-24 10:32:55 +00:00
|
|
|
const space = client.getRoom(room1); // not a space
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space.roomId);
|
|
|
|
expect(fn).not.toHaveBeenCalledWith(UPDATE_SELECTED_SPACE, space.roomId);
|
|
|
|
expect(store.activeSpace).toBe(MetaSpace.Home);
|
2021-04-24 10:32:55 +00:00
|
|
|
});
|
2021-04-23 10:20:26 +00:00
|
|
|
});
|
|
|
|
|
2022-03-01 08:33:29 +00:00
|
|
|
it("does not race with lazy loading", async () => {
|
|
|
|
store.setActiveSpace(MetaSpace.Home);
|
|
|
|
|
|
|
|
mkRoom(room1);
|
|
|
|
const space = mkSpace(space1, [room1]);
|
|
|
|
// seed the context for space1 to be room1
|
|
|
|
window.localStorage.setItem(`mx_space_context_${space1}`, room1);
|
|
|
|
|
|
|
|
await run();
|
|
|
|
|
2022-03-11 16:03:33 +00:00
|
|
|
const deferred = defer<boolean>();
|
|
|
|
space.loadMembersIfNeeded.mockImplementation(() => {
|
2022-03-01 08:33:29 +00:00
|
|
|
const event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMember,
|
|
|
|
content: { membership: "join" },
|
|
|
|
skey: dm1Partner.userId,
|
|
|
|
user: dm1Partner.userId,
|
|
|
|
room: space1,
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
space.getMember.mockImplementation((userId) => {
|
2022-03-03 21:42:18 +00:00
|
|
|
if (userId === dm1Partner.userId) {
|
|
|
|
const member = new RoomMember(space1, dm1Partner.userId);
|
|
|
|
member.membership = "join";
|
|
|
|
return member;
|
|
|
|
}
|
|
|
|
});
|
2022-03-01 08:33:29 +00:00
|
|
|
|
|
|
|
client.emit(RoomStateEvent.Members, event, null, null);
|
2022-03-11 16:03:33 +00:00
|
|
|
return deferred.resolve(true) as unknown as Promise<boolean>;
|
2022-03-01 08:33:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
spyDispatcher.mockClear();
|
|
|
|
const getCurrentRoom = () => {
|
|
|
|
for (let i = spyDispatcher.mock.calls.length - 1; i >= 0; i--) {
|
|
|
|
if (spyDispatcher.mock.calls[i][0].action === Action.ViewRoom) {
|
|
|
|
return spyDispatcher.mock.calls[i][0]["room_id"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// set up space with LL where loadMembersIfNeeded emits membership events which trip switchSpaceIfNeeded
|
|
|
|
expect(space.loadMembersIfNeeded).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
store.setActiveSpace(space1, true);
|
2022-03-03 21:42:18 +00:00
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
expect(space.loadMembersIfNeeded).toHaveBeenCalled();
|
|
|
|
jest.runAllTimers();
|
2022-03-01 08:33:29 +00:00
|
|
|
|
|
|
|
expect(store.activeSpace).toBe(space1);
|
|
|
|
expect(getCurrentRoom()).toBe(room1);
|
|
|
|
|
|
|
|
await deferred.promise;
|
|
|
|
expect(store.activeSpace).toBe(space1);
|
|
|
|
expect(getCurrentRoom()).toBe(room1);
|
|
|
|
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(store.activeSpace).toBe(space1);
|
|
|
|
expect(getCurrentRoom()).toBe(room1);
|
|
|
|
});
|
|
|
|
|
2021-04-22 13:45:13 +00:00
|
|
|
describe("context switching tests", () => {
|
2021-07-27 20:11:47 +00:00
|
|
|
let dispatcherRef;
|
|
|
|
let currentRoom = null;
|
2021-04-23 15:14:55 +00:00
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
[room1, room2, orphan1].forEach(mkRoom);
|
|
|
|
mkSpace(space1, [room1, room2]);
|
|
|
|
mkSpace(space2, [room2]);
|
|
|
|
await run();
|
2021-07-27 20:11:47 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
dispatcherRef = defaultDispatcher.register((payload) => {
|
2022-02-22 10:04:27 +00:00
|
|
|
if (payload.action === Action.ViewRoom || payload.action === Action.ViewHomePage) {
|
2021-07-27 20:11:47 +00:00
|
|
|
currentRoom = payload.room_id || null;
|
|
|
|
}
|
|
|
|
});
|
2021-04-23 15:14:55 +00:00
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
localStorage.clear();
|
2021-11-11 13:07:41 +00:00
|
|
|
localStorage.setItem("mx_labs_feature_feature_spaces_metaspaces", "true");
|
2021-07-27 20:11:47 +00:00
|
|
|
defaultDispatcher.unregister(dispatcherRef);
|
2021-04-23 15:14:55 +00:00
|
|
|
});
|
|
|
|
|
2021-07-27 20:11:47 +00:00
|
|
|
const getCurrentRoom = () => {
|
2022-01-26 10:38:07 +00:00
|
|
|
jest.runOnlyPendingTimers();
|
2021-07-27 20:11:47 +00:00
|
|
|
return currentRoom;
|
|
|
|
};
|
2021-04-23 15:14:55 +00:00
|
|
|
|
|
|
|
it("last viewed room in target space is the current viewed and in both spaces", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1);
|
2021-04-23 15:14:55 +00:00
|
|
|
viewRoom(room2);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space2);
|
2021-04-23 15:14:55 +00:00
|
|
|
viewRoom(room2);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1);
|
2021-04-23 15:14:55 +00:00
|
|
|
expect(getCurrentRoom()).toBe(room2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("last viewed room in target space is in the current space", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1);
|
2021-04-23 15:14:55 +00:00
|
|
|
viewRoom(room2);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space2);
|
2021-04-23 15:14:55 +00:00
|
|
|
expect(getCurrentRoom()).toBe(space2);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1);
|
2021-04-23 15:14:55 +00:00
|
|
|
expect(getCurrentRoom()).toBe(room2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("last viewed room in target space is not in the current space", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1);
|
2021-04-23 15:14:55 +00:00
|
|
|
viewRoom(room1);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space2);
|
2021-04-23 15:14:55 +00:00
|
|
|
viewRoom(room2);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1);
|
2021-04-23 15:14:55 +00:00
|
|
|
expect(getCurrentRoom()).toBe(room1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("last viewed room is target space is not known", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1);
|
2021-04-23 15:14:55 +00:00
|
|
|
viewRoom(room1);
|
|
|
|
localStorage.setItem(`mx_space_context_${space2}`, orphan2);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space2);
|
2021-04-23 15:14:55 +00:00
|
|
|
expect(getCurrentRoom()).toBe(space2);
|
|
|
|
});
|
|
|
|
|
2021-07-27 20:11:47 +00:00
|
|
|
it("last viewed room is target space is no longer in that space", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1);
|
2021-07-27 20:11:47 +00:00
|
|
|
viewRoom(room1);
|
|
|
|
localStorage.setItem(`mx_space_context_${space2}`, room1);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space2);
|
2021-07-27 20:11:47 +00:00
|
|
|
expect(getCurrentRoom()).toBe(space2); // Space home instead of room1
|
|
|
|
});
|
|
|
|
|
2021-04-23 15:14:55 +00:00
|
|
|
it("no last viewed room in target space", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1);
|
2021-04-23 15:14:55 +00:00
|
|
|
viewRoom(room1);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space2);
|
2021-04-23 15:14:55 +00:00
|
|
|
expect(getCurrentRoom()).toBe(space2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("no last viewed room in home space", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1);
|
2021-04-23 15:14:55 +00:00
|
|
|
viewRoom(room1);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(MetaSpace.Home);
|
2021-07-27 20:11:47 +00:00
|
|
|
expect(getCurrentRoom()).toBeNull(); // Home
|
2021-04-23 15:14:55 +00:00
|
|
|
});
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("space auto switching tests", () => {
|
2021-04-23 13:45:22 +00:00
|
|
|
beforeEach(async () => {
|
2021-05-07 10:22:10 +00:00
|
|
|
[room1, room2, room3, orphan1].forEach(mkRoom);
|
|
|
|
mkSpace(space1, [room1, room2, room3]);
|
2021-04-23 13:45:22 +00:00
|
|
|
mkSpace(space2, [room1, room2]);
|
|
|
|
|
2021-07-27 19:15:40 +00:00
|
|
|
const cliRoom2 = client.getRoom(room2);
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(cliRoom2.currentState).getStateEvents.mockImplementation(
|
|
|
|
testUtils.mockStateEventImplementation([
|
|
|
|
mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.SpaceParent,
|
|
|
|
room: room2,
|
|
|
|
user: testUserId,
|
|
|
|
skey: space2,
|
|
|
|
content: { via: [], canonical: true },
|
|
|
|
ts: Date.now(),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
2021-04-23 13:45:22 +00:00
|
|
|
await run();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("no switch required, room is in current space", async () => {
|
|
|
|
viewRoom(room1);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1, false);
|
2021-04-23 13:45:22 +00:00
|
|
|
viewRoom(room2);
|
2021-11-11 13:07:41 +00:00
|
|
|
expect(store.activeSpace).toBe(space1);
|
2021-04-23 13:45:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("switch to canonical parent space for room", async () => {
|
|
|
|
viewRoom(room1);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space2, false);
|
2021-04-23 13:45:22 +00:00
|
|
|
viewRoom(room2);
|
2021-11-11 13:07:41 +00:00
|
|
|
expect(store.activeSpace).toBe(space2);
|
2021-04-23 13:45:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("switch to first containing space for room", async () => {
|
|
|
|
viewRoom(room2);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space2, false);
|
2021-05-07 10:22:10 +00:00
|
|
|
viewRoom(room3);
|
2021-11-11 13:07:41 +00:00
|
|
|
expect(store.activeSpace).toBe(space1);
|
2021-04-23 13:45:22 +00:00
|
|
|
});
|
|
|
|
|
2021-12-03 11:17:51 +00:00
|
|
|
it("switch to other rooms for orphaned room", async () => {
|
2021-04-23 13:45:22 +00:00
|
|
|
viewRoom(room1);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(space1, false);
|
2021-04-23 13:45:22 +00:00
|
|
|
viewRoom(orphan1);
|
2021-12-03 11:17:51 +00:00
|
|
|
expect(store.activeSpace).toBe(MetaSpace.Orphans);
|
2021-11-11 13:07:41 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 09:26:32 +00:00
|
|
|
it("switch to first valid space when selected metaspace is disabled", async () => {
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(MetaSpace.People, false);
|
|
|
|
expect(store.activeSpace).toBe(MetaSpace.People);
|
|
|
|
await SettingsStore.setValue("Spaces.enabledMetaSpaces", null, SettingLevel.DEVICE, {
|
|
|
|
[MetaSpace.Home]: false,
|
|
|
|
[MetaSpace.Favourites]: true,
|
|
|
|
[MetaSpace.People]: false,
|
|
|
|
[MetaSpace.Orphans]: true,
|
|
|
|
});
|
|
|
|
jest.runAllTimers();
|
2021-12-17 09:26:32 +00:00
|
|
|
expect(store.activeSpace).toBe(MetaSpace.Orphans);
|
2021-04-23 13:45:22 +00:00
|
|
|
});
|
2021-05-07 10:08:18 +00:00
|
|
|
|
|
|
|
it("when switching rooms in the all rooms home space don't switch to related space", async () => {
|
2021-07-27 20:11:47 +00:00
|
|
|
await setShowAllRooms(true);
|
2021-05-07 10:08:18 +00:00
|
|
|
viewRoom(room2);
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(MetaSpace.Home, false);
|
2021-05-07 10:08:18 +00:00
|
|
|
viewRoom(room1);
|
2021-11-11 13:07:41 +00:00
|
|
|
expect(store.activeSpace).toBe(MetaSpace.Home);
|
2021-05-07 10:08:18 +00:00
|
|
|
});
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("traverseSpace", () => {
|
2021-04-23 08:55:30 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
mkSpace("!a:server", [
|
|
|
|
mkSpace("!b:server", [
|
|
|
|
mkSpace("!c:server", [
|
|
|
|
"!a:server",
|
|
|
|
mkRoom("!c-child:server").roomId,
|
|
|
|
mkRoom("!shared-child:server").roomId,
|
|
|
|
]).roomId,
|
|
|
|
mkRoom("!b-child:server").roomId,
|
|
|
|
]).roomId,
|
|
|
|
mkRoom("!a-child:server").roomId,
|
|
|
|
"!shared-child:server",
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("avoids cycles", () => {
|
2021-04-23 12:40:16 +00:00
|
|
|
const fn = jest.fn();
|
|
|
|
store.traverseSpace("!b:server", fn);
|
2021-04-23 08:55:30 +00:00
|
|
|
|
2021-04-23 12:40:16 +00:00
|
|
|
expect(fn).toBeCalledTimes(3);
|
|
|
|
expect(fn).toBeCalledWith("!a:server");
|
|
|
|
expect(fn).toBeCalledWith("!b:server");
|
|
|
|
expect(fn).toBeCalledWith("!c:server");
|
2021-04-23 08:55:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("including rooms", () => {
|
2021-04-23 12:40:16 +00:00
|
|
|
const fn = jest.fn();
|
|
|
|
store.traverseSpace("!b:server", fn, true);
|
|
|
|
|
|
|
|
expect(fn).toBeCalledTimes(8); // twice for shared-child
|
|
|
|
expect(fn).toBeCalledWith("!a:server");
|
|
|
|
expect(fn).toBeCalledWith("!a-child:server");
|
|
|
|
expect(fn).toBeCalledWith("!b:server");
|
|
|
|
expect(fn).toBeCalledWith("!b-child:server");
|
|
|
|
expect(fn).toBeCalledWith("!c:server");
|
|
|
|
expect(fn).toBeCalledWith("!c-child:server");
|
|
|
|
expect(fn).toBeCalledWith("!shared-child:server");
|
2021-04-23 08:55:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("excluding rooms", () => {
|
2021-04-23 12:40:16 +00:00
|
|
|
const fn = jest.fn();
|
|
|
|
store.traverseSpace("!b:server", fn, false);
|
|
|
|
|
|
|
|
expect(fn).toBeCalledTimes(3);
|
|
|
|
expect(fn).toBeCalledWith("!a:server");
|
|
|
|
expect(fn).toBeCalledWith("!b:server");
|
|
|
|
expect(fn).toBeCalledWith("!c:server");
|
2021-04-23 08:55:30 +00:00
|
|
|
});
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|
2021-12-17 09:26:32 +00:00
|
|
|
|
|
|
|
it("test user flow", async () => {
|
|
|
|
// init the store
|
|
|
|
await run();
|
|
|
|
await setShowAllRooms(false);
|
|
|
|
|
|
|
|
// receive invite to space
|
|
|
|
const rootSpace = mkSpace(space1, [room1, room2, space2]);
|
|
|
|
rootSpace.getMyMembership.mockReturnValue("invite");
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(ClientEvent.Room, rootSpace);
|
2022-01-26 10:38:07 +00:00
|
|
|
jest.runOnlyPendingTimers();
|
2021-12-17 09:26:32 +00:00
|
|
|
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([rootSpace]);
|
|
|
|
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([]);
|
|
|
|
|
|
|
|
// accept invite to space
|
|
|
|
rootSpace.getMyMembership.mockReturnValue("join");
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(RoomEvent.MyMembership, rootSpace, "join", "invite");
|
2022-01-26 10:38:07 +00:00
|
|
|
jest.runOnlyPendingTimers();
|
2021-12-17 09:26:32 +00:00
|
|
|
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
|
|
|
|
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]);
|
|
|
|
|
|
|
|
// join room in space
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(space1, room1)).toBeFalsy();
|
|
|
|
const rootSpaceRoom1 = mkRoom(room1);
|
|
|
|
rootSpaceRoom1.getMyMembership.mockReturnValue("join");
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(ClientEvent.Room, rootSpaceRoom1);
|
2022-01-26 10:38:07 +00:00
|
|
|
jest.runOnlyPendingTimers();
|
2021-12-17 09:26:32 +00:00
|
|
|
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
|
|
|
|
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]);
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(space1, room1)).toBeTruthy();
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(MetaSpace.Home, room1)).toBeFalsy();
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(MetaSpace.Favourites, room1)).toBeFalsy();
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(MetaSpace.People, room1)).toBeFalsy();
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(MetaSpace.Orphans, room1)).toBeFalsy();
|
|
|
|
|
|
|
|
// receive room invite
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(space1, room2)).toBeFalsy();
|
|
|
|
const rootSpaceRoom2 = mkRoom(room2);
|
|
|
|
rootSpaceRoom2.getMyMembership.mockReturnValue("invite");
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(ClientEvent.Room, rootSpaceRoom2);
|
2022-01-26 10:38:07 +00:00
|
|
|
jest.runOnlyPendingTimers();
|
2021-12-17 09:26:32 +00:00
|
|
|
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
|
|
|
|
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]);
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(space1, room2)).toBeTruthy();
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(MetaSpace.Home, room2)).toBeTruthy();
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(MetaSpace.Favourites, room2)).toBeFalsy();
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(MetaSpace.People, room2)).toBeFalsy();
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(MetaSpace.Orphans, room2)).toBeFalsy();
|
|
|
|
|
|
|
|
// start DM in space
|
|
|
|
const myRootSpaceMember = new RoomMember(space1, testUserId);
|
|
|
|
myRootSpaceMember.membership = "join";
|
|
|
|
const rootSpaceFriend = new RoomMember(space1, dm1Partner.userId);
|
|
|
|
rootSpaceFriend.membership = "join";
|
2022-12-12 11:24:14 +00:00
|
|
|
rootSpace.getMembers.mockReturnValue([myRootSpaceMember, rootSpaceFriend]);
|
|
|
|
rootSpace.getMember.mockImplementation((userId) => {
|
2021-12-17 09:26:32 +00:00
|
|
|
switch (userId) {
|
|
|
|
case testUserId:
|
|
|
|
return myRootSpaceMember;
|
|
|
|
case dm1Partner.userId:
|
|
|
|
return rootSpaceFriend;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(SpaceStore.instance.getSpaceFilteredUserIds(space1).has(dm1Partner.userId)).toBeFalsy();
|
2022-03-11 16:03:33 +00:00
|
|
|
const memberEvent = mkEvent({
|
2021-12-17 09:26:32 +00:00
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMember,
|
|
|
|
content: {
|
|
|
|
membership: "join",
|
|
|
|
},
|
|
|
|
skey: dm1Partner.userId,
|
|
|
|
user: dm1Partner.userId,
|
|
|
|
room: space1,
|
2022-03-11 16:03:33 +00:00
|
|
|
});
|
|
|
|
client.emit(RoomStateEvent.Members, memberEvent, undefined, undefined);
|
2022-01-26 10:38:07 +00:00
|
|
|
jest.runOnlyPendingTimers();
|
2021-12-17 09:26:32 +00:00
|
|
|
expect(SpaceStore.instance.getSpaceFilteredUserIds(space1).has(dm1Partner.userId)).toBeTruthy();
|
|
|
|
const dm1Room = mkRoom(dm1);
|
|
|
|
dm1Room.getMyMembership.mockReturnValue("join");
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(ClientEvent.Room, dm1Room);
|
2022-01-26 10:38:07 +00:00
|
|
|
jest.runOnlyPendingTimers();
|
2021-12-17 09:26:32 +00:00
|
|
|
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
|
|
|
|
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]);
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(space1, dm1)).toBeTruthy();
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(MetaSpace.Home, dm1)).toBeTruthy();
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(MetaSpace.Favourites, dm1)).toBeFalsy();
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(MetaSpace.People, dm1)).toBeTruthy();
|
|
|
|
expect(SpaceStore.instance.isRoomInSpace(MetaSpace.Orphans, dm1)).toBeFalsy();
|
|
|
|
|
|
|
|
// join subspace
|
|
|
|
const subspace = mkSpace(space2);
|
|
|
|
subspace.getMyMembership.mockReturnValue("join");
|
|
|
|
const prom = testUtils.emitPromise(SpaceStore.instance, space1);
|
2022-03-11 16:03:33 +00:00
|
|
|
client.emit(ClientEvent.Room, subspace);
|
2022-01-26 10:38:07 +00:00
|
|
|
jest.runOnlyPendingTimers();
|
2021-12-17 09:26:32 +00:00
|
|
|
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(SpaceStore.instance.spacePanelSpaces.map((r) => r.roomId)).toStrictEqual([rootSpace.roomId]);
|
2021-12-17 09:26:32 +00:00
|
|
|
await prom;
|
|
|
|
});
|
2022-05-18 13:15:25 +00:00
|
|
|
|
|
|
|
it("correctly emits events for metaspace changes during onReady", async () => {
|
|
|
|
// similar to useEventEmitterState, but for use inside of tests
|
|
|
|
function testEventEmitterState(
|
|
|
|
emitter: EventEmitter | undefined,
|
|
|
|
eventName: string | symbol,
|
|
|
|
callback: (...args: any[]) => void,
|
|
|
|
): () => void {
|
|
|
|
callback();
|
|
|
|
emitter.addListener(eventName, callback);
|
|
|
|
return () => emitter.removeListener(eventName, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
let metaSpaces;
|
|
|
|
const removeListener = testEventEmitterState(store, UPDATE_TOP_LEVEL_SPACES, () => {
|
|
|
|
metaSpaces = store.enabledMetaSpaces;
|
|
|
|
});
|
|
|
|
expect(metaSpaces).toEqual(store.enabledMetaSpaces);
|
|
|
|
await run();
|
|
|
|
expect(metaSpaces).toEqual(store.enabledMetaSpaces);
|
|
|
|
removeListener();
|
|
|
|
});
|
2021-04-22 13:45:13 +00:00
|
|
|
});
|