2022-05-09 16:14:45 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 - 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { Room } from 'matrix-js-sdk/src/matrix';
|
|
|
|
|
|
|
|
import { RoomViewStore } from '../../src/stores/RoomViewStore';
|
|
|
|
import { Action } from '../../src/dispatcher/actions';
|
2022-09-20 18:32:39 +03:00
|
|
|
import { getMockClientWithEventEmitter, untilDispatch, untilEmission } from '../test-utils';
|
2022-09-12 13:55:46 +03:00
|
|
|
import SettingsStore from '../../src/settings/SettingsStore';
|
|
|
|
import { SlidingSyncManager } from '../../src/SlidingSyncManager';
|
2022-10-19 15:07:03 +03:00
|
|
|
import { PosthogAnalytics } from '../../src/PosthogAnalytics';
|
2022-09-15 15:09:16 +03:00
|
|
|
import { TimelineRenderingType } from '../../src/contexts/RoomContext';
|
2022-09-20 18:32:39 +03:00
|
|
|
import { MatrixDispatcher } from '../../src/dispatcher/dispatcher';
|
|
|
|
import { UPDATE_EVENT } from '../../src/stores/AsyncStore';
|
|
|
|
import { ActiveRoomChangedPayload } from '../../src/dispatcher/payloads/ActiveRoomChangedPayload';
|
2022-10-19 15:07:03 +03:00
|
|
|
import { SpaceStoreClass } from '../../src/stores/spaces/SpaceStore';
|
2022-10-19 16:14:14 +03:00
|
|
|
import { TestSdkContext } from '../TestSdkContext';
|
2022-10-19 15:07:03 +03:00
|
|
|
|
|
|
|
// mock out the injected classes
|
|
|
|
jest.mock('../../src/PosthogAnalytics');
|
|
|
|
const MockPosthogAnalytics = <jest.Mock<PosthogAnalytics>><unknown>PosthogAnalytics;
|
|
|
|
jest.mock('../../src/SlidingSyncManager');
|
|
|
|
const MockSlidingSyncManager = <jest.Mock<SlidingSyncManager>><unknown>SlidingSyncManager;
|
|
|
|
jest.mock('../../src/stores/spaces/SpaceStore');
|
|
|
|
const MockSpaceStore = <jest.Mock<SpaceStoreClass>><unknown>SpaceStoreClass;
|
2022-05-09 16:14:45 +03:00
|
|
|
|
|
|
|
jest.mock('../../src/utils/DMRoomMap', () => {
|
|
|
|
const mock = {
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
getDMRoomsForUserId: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
shared: jest.fn().mockReturnValue(mock),
|
|
|
|
sharedInstance: mock,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('RoomViewStore', function() {
|
|
|
|
const userId = '@alice:server';
|
2022-09-20 18:32:39 +03:00
|
|
|
const roomId = "!randomcharacters:aser.ver";
|
|
|
|
// we need to change the alias to ensure cache misses as the cache exists
|
|
|
|
// through all tests.
|
|
|
|
let alias = "#somealias2:aser.ver";
|
2022-05-09 16:14:45 +03:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
joinRoom: jest.fn(),
|
|
|
|
getRoom: jest.fn(),
|
|
|
|
getRoomIdForAlias: jest.fn(),
|
2022-09-07 18:42:39 +03:00
|
|
|
isGuest: jest.fn(),
|
2022-05-09 16:14:45 +03:00
|
|
|
});
|
2022-09-20 18:32:39 +03:00
|
|
|
const room = new Room(roomId, mockClient, userId);
|
2022-10-19 15:07:03 +03:00
|
|
|
|
|
|
|
let roomViewStore: RoomViewStore;
|
|
|
|
let slidingSyncManager: SlidingSyncManager;
|
2022-09-20 18:32:39 +03:00
|
|
|
let dis: MatrixDispatcher;
|
2022-05-09 16:14:45 +03:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
jest.clearAllMocks();
|
2022-09-12 13:55:46 +03:00
|
|
|
mockClient.credentials = { userId: userId };
|
2022-05-09 16:14:45 +03:00
|
|
|
mockClient.joinRoom.mockResolvedValue(room);
|
|
|
|
mockClient.getRoom.mockReturnValue(room);
|
2022-09-07 18:42:39 +03:00
|
|
|
mockClient.isGuest.mockReturnValue(false);
|
2022-05-09 16:14:45 +03:00
|
|
|
|
2022-10-19 15:07:03 +03:00
|
|
|
// Make the RVS to test
|
2022-09-20 18:32:39 +03:00
|
|
|
dis = new MatrixDispatcher();
|
2022-10-19 15:07:03 +03:00
|
|
|
slidingSyncManager = new MockSlidingSyncManager();
|
2022-10-19 16:14:14 +03:00
|
|
|
const stores = new TestSdkContext();
|
2022-10-19 15:07:03 +03:00
|
|
|
stores._SlidingSyncManager = slidingSyncManager;
|
|
|
|
stores._PosthogAnalytics = new MockPosthogAnalytics();
|
|
|
|
stores._SpaceStore = new MockSpaceStore();
|
|
|
|
roomViewStore = new RoomViewStore(
|
|
|
|
dis, stores,
|
|
|
|
);
|
|
|
|
stores._RoomViewStore = roomViewStore;
|
2022-05-09 16:14:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can be used to view a room by ID and join', async () => {
|
2022-09-20 18:32:39 +03:00
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
dis.dispatch({ action: Action.JoinRoom });
|
|
|
|
await untilDispatch(Action.JoinRoomReady, dis);
|
|
|
|
expect(mockClient.joinRoom).toHaveBeenCalledWith(roomId, { viaServers: [] });
|
2022-10-19 15:07:03 +03:00
|
|
|
expect(roomViewStore.isJoining()).toBe(true);
|
2022-05-09 16:14:45 +03:00
|
|
|
});
|
|
|
|
|
2022-09-20 18:32:39 +03:00
|
|
|
it('can auto-join a room', async () => {
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId, auto_join: true });
|
|
|
|
await untilDispatch(Action.JoinRoomReady, dis);
|
|
|
|
expect(mockClient.joinRoom).toHaveBeenCalledWith(roomId, { viaServers: [] });
|
2022-10-19 15:07:03 +03:00
|
|
|
expect(roomViewStore.isJoining()).toBe(true);
|
2022-09-20 18:32:39 +03:00
|
|
|
});
|
2022-05-09 16:14:45 +03:00
|
|
|
|
2022-09-20 18:32:39 +03:00
|
|
|
it('emits ActiveRoomChanged when the viewed room changes', async () => {
|
|
|
|
const roomId2 = "!roomid:2";
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
let payload = await untilDispatch(Action.ActiveRoomChanged, dis) as ActiveRoomChangedPayload;
|
|
|
|
expect(payload.newRoomId).toEqual(roomId);
|
|
|
|
expect(payload.oldRoomId).toEqual(null);
|
|
|
|
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId2 });
|
|
|
|
payload = await untilDispatch(Action.ActiveRoomChanged, dis) as ActiveRoomChangedPayload;
|
|
|
|
expect(payload.newRoomId).toEqual(roomId2);
|
|
|
|
expect(payload.oldRoomId).toEqual(roomId);
|
|
|
|
});
|
2022-05-09 16:14:45 +03:00
|
|
|
|
2022-09-20 18:32:39 +03:00
|
|
|
it('invokes room activity listeners when the viewed room changes', async () => {
|
|
|
|
const roomId2 = "!roomid:2";
|
|
|
|
const callback = jest.fn();
|
2022-10-19 15:07:03 +03:00
|
|
|
roomViewStore.addRoomListener(roomId, callback);
|
2022-09-20 18:32:39 +03:00
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis) as ActiveRoomChangedPayload;
|
|
|
|
expect(callback).toHaveBeenCalledWith(true);
|
|
|
|
expect(callback).not.toHaveBeenCalledWith(false);
|
|
|
|
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId2 });
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis) as ActiveRoomChangedPayload;
|
|
|
|
expect(callback).toHaveBeenCalledWith(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be used to view a room by alias and join', async () => {
|
|
|
|
mockClient.getRoomIdForAlias.mockResolvedValue({ room_id: roomId, servers: [] });
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_alias: alias });
|
|
|
|
await untilDispatch((p) => { // wait for the re-dispatch with the room ID
|
|
|
|
return p.action === Action.ViewRoom && p.room_id === roomId;
|
|
|
|
}, dis);
|
2022-05-09 16:14:45 +03:00
|
|
|
|
|
|
|
// roomId is set to id of the room alias
|
2022-10-19 15:07:03 +03:00
|
|
|
expect(roomViewStore.getRoomId()).toBe(roomId);
|
2022-05-09 16:14:45 +03:00
|
|
|
|
|
|
|
// join the room
|
2022-09-20 18:32:39 +03:00
|
|
|
dis.dispatch({ action: Action.JoinRoom }, true);
|
2022-05-09 16:14:45 +03:00
|
|
|
|
2022-09-20 18:32:39 +03:00
|
|
|
await untilDispatch(Action.JoinRoomReady, dis);
|
2022-05-09 16:14:45 +03:00
|
|
|
|
2022-10-19 15:07:03 +03:00
|
|
|
expect(roomViewStore.isJoining()).toBeTruthy();
|
2022-05-09 16:14:45 +03:00
|
|
|
expect(mockClient.joinRoom).toHaveBeenCalledWith(alias, { viaServers: [] });
|
|
|
|
});
|
2022-09-12 13:55:46 +03:00
|
|
|
|
2022-09-20 18:32:39 +03:00
|
|
|
it('emits ViewRoomError if the alias lookup fails', async () => {
|
|
|
|
alias = "#something-different:to-ensure-cache-miss";
|
|
|
|
mockClient.getRoomIdForAlias.mockRejectedValue(new Error("network error or something"));
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_alias: alias });
|
|
|
|
const payload = await untilDispatch(Action.ViewRoomError, dis);
|
|
|
|
expect(payload.room_id).toBeNull();
|
|
|
|
expect(payload.room_alias).toEqual(alias);
|
2022-10-19 15:07:03 +03:00
|
|
|
expect(roomViewStore.getRoomAlias()).toEqual(alias);
|
2022-09-20 18:32:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('emits JoinRoomError if joining the room fails', async () => {
|
|
|
|
const joinErr = new Error("network error or something");
|
|
|
|
mockClient.joinRoom.mockRejectedValue(joinErr);
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
dis.dispatch({ action: Action.JoinRoom });
|
|
|
|
await untilDispatch(Action.JoinRoomError, dis);
|
2022-10-19 15:07:03 +03:00
|
|
|
expect(roomViewStore.isJoining()).toBe(false);
|
|
|
|
expect(roomViewStore.getJoinError()).toEqual(joinErr);
|
2022-09-20 18:32:39 +03:00
|
|
|
});
|
|
|
|
|
2022-09-15 15:09:16 +03:00
|
|
|
it('remembers the event being replied to when swapping rooms', async () => {
|
2022-09-20 18:32:39 +03:00
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis);
|
2022-09-15 15:09:16 +03:00
|
|
|
const replyToEvent = {
|
2022-09-20 18:32:39 +03:00
|
|
|
getRoomId: () => roomId,
|
2022-09-15 15:09:16 +03:00
|
|
|
};
|
2022-09-20 18:32:39 +03:00
|
|
|
dis.dispatch({ action: 'reply_to_event', event: replyToEvent, context: TimelineRenderingType.Room });
|
2022-10-19 15:07:03 +03:00
|
|
|
await untilEmission(roomViewStore, UPDATE_EVENT);
|
|
|
|
expect(roomViewStore.getQuotingEvent()).toEqual(replyToEvent);
|
2022-09-15 15:09:16 +03:00
|
|
|
// view the same room, should remember the event.
|
2022-09-20 18:32:39 +03:00
|
|
|
// set the highlighed flag to make sure there is a state change so we get an update event
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId, highlighted: true });
|
2022-10-19 15:07:03 +03:00
|
|
|
await untilEmission(roomViewStore, UPDATE_EVENT);
|
|
|
|
expect(roomViewStore.getQuotingEvent()).toEqual(replyToEvent);
|
2022-09-15 15:09:16 +03:00
|
|
|
});
|
|
|
|
|
2022-09-20 18:32:39 +03:00
|
|
|
it('swaps to the replied event room if it is not the current room', async () => {
|
|
|
|
const roomId2 = "!room2:bar";
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis);
|
|
|
|
const replyToEvent = {
|
|
|
|
getRoomId: () => roomId2,
|
|
|
|
};
|
|
|
|
dis.dispatch({ action: 'reply_to_event', event: replyToEvent, context: TimelineRenderingType.Room });
|
|
|
|
await untilDispatch(Action.ViewRoom, dis);
|
2022-10-19 15:07:03 +03:00
|
|
|
expect(roomViewStore.getQuotingEvent()).toEqual(replyToEvent);
|
|
|
|
expect(roomViewStore.getRoomId()).toEqual(roomId2);
|
2022-09-20 18:32:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('removes the roomId on ViewHomePage', async () => {
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis);
|
2022-10-19 15:07:03 +03:00
|
|
|
expect(roomViewStore.getRoomId()).toEqual(roomId);
|
2022-09-20 18:32:39 +03:00
|
|
|
|
|
|
|
dis.dispatch({ action: Action.ViewHomePage });
|
2022-10-19 15:07:03 +03:00
|
|
|
await untilEmission(roomViewStore, UPDATE_EVENT);
|
|
|
|
expect(roomViewStore.getRoomId()).toBeNull();
|
2022-09-20 18:32:39 +03:00
|
|
|
});
|
|
|
|
|
2022-09-12 13:55:46 +03:00
|
|
|
describe('Sliding Sync', function() {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SettingsStore, 'getValue').mockImplementation((settingName, roomId, value) => {
|
|
|
|
return settingName === "feature_sliding_sync"; // this is enabled, everything else is disabled.
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("subscribes to the room", async () => {
|
2022-10-19 15:07:03 +03:00
|
|
|
const setRoomVisible = jest.spyOn(slidingSyncManager, "setRoomVisible").mockReturnValue(
|
2022-09-12 13:55:46 +03:00
|
|
|
Promise.resolve(""),
|
|
|
|
);
|
|
|
|
const subscribedRoomId = "!sub1:localhost";
|
2022-09-20 18:32:39 +03:00
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: subscribedRoomId });
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis);
|
2022-10-19 15:07:03 +03:00
|
|
|
expect(roomViewStore.getRoomId()).toBe(subscribedRoomId);
|
2022-09-12 13:55:46 +03:00
|
|
|
expect(setRoomVisible).toHaveBeenCalledWith(subscribedRoomId, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Regression test for an in-the-wild bug where rooms would rapidly switch forever in sliding sync mode
|
|
|
|
it("doesn't get stuck in a loop if you view rooms quickly", async () => {
|
2022-10-19 15:07:03 +03:00
|
|
|
const setRoomVisible = jest.spyOn(slidingSyncManager, "setRoomVisible").mockReturnValue(
|
2022-09-12 13:55:46 +03:00
|
|
|
Promise.resolve(""),
|
|
|
|
);
|
2022-09-20 18:32:39 +03:00
|
|
|
const subscribedRoomId = "!sub1:localhost";
|
|
|
|
const subscribedRoomId2 = "!sub2:localhost";
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: subscribedRoomId }, true);
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: subscribedRoomId2 }, true);
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis);
|
|
|
|
// sub(1) then unsub(1) sub(2), unsub(1)
|
|
|
|
const wantCalls = [
|
|
|
|
[subscribedRoomId, true],
|
|
|
|
[subscribedRoomId, false],
|
|
|
|
[subscribedRoomId2, true],
|
|
|
|
[subscribedRoomId, false],
|
|
|
|
];
|
|
|
|
expect(setRoomVisible).toHaveBeenCalledTimes(wantCalls.length);
|
|
|
|
wantCalls.forEach((v, i) => {
|
|
|
|
try {
|
|
|
|
expect(setRoomVisible.mock.calls[i][0]).toEqual(v[0]);
|
|
|
|
expect(setRoomVisible.mock.calls[i][1]).toEqual(v[1]);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`i=${i} got ${setRoomVisible.mock.calls[i]} want ${v}`);
|
|
|
|
}
|
|
|
|
});
|
2022-09-12 13:55:46 +03:00
|
|
|
});
|
|
|
|
});
|
2022-05-09 16:14:45 +03:00
|
|
|
});
|