|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { |
| 18 | + EventType, |
| 19 | + fixNotificationCountOnDecryption, |
| 20 | + MatrixClient, |
| 21 | + MatrixEvent, |
| 22 | + MsgType, |
| 23 | + NotificationCountType, |
| 24 | + RelationType, |
| 25 | + Room, |
| 26 | +} from "../../src/matrix"; |
| 27 | +import { IActionsObject } from "../../src/pushprocessor"; |
| 28 | +import { ReEmitter } from "../../src/ReEmitter"; |
| 29 | +import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../test-utils/client"; |
| 30 | +import { mkEvent, mock } from "../test-utils/test-utils"; |
| 31 | + |
| 32 | +let mockClient: MatrixClient; |
| 33 | +let room: Room; |
| 34 | +let event: MatrixEvent; |
| 35 | +let threadEvent: MatrixEvent; |
| 36 | + |
| 37 | +const ROOM_ID = "!roomId:example.org"; |
| 38 | +let THREAD_ID; |
| 39 | + |
| 40 | +function mkPushAction(notify, highlight): IActionsObject { |
| 41 | + return { |
| 42 | + notify, |
| 43 | + tweaks: { |
| 44 | + highlight, |
| 45 | + }, |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +describe("fixNotificationCountOnDecryption", () => { |
| 50 | + beforeEach(() => { |
| 51 | + mockClient = getMockClientWithEventEmitter({ |
| 52 | + ...mockClientMethodsUser(), |
| 53 | + getPushActionsForEvent: jest.fn().mockReturnValue(mkPushAction(true, true)), |
| 54 | + getRoom: jest.fn().mockImplementation(() => room), |
| 55 | + decryptEventIfNeeded: jest.fn().mockResolvedValue(void 0), |
| 56 | + supportsExperimentalThreads: jest.fn().mockReturnValue(true), |
| 57 | + }); |
| 58 | + mockClient.reEmitter = mock(ReEmitter, 'ReEmitter'); |
| 59 | + |
| 60 | + room = new Room(ROOM_ID, mockClient, mockClient.getUserId()); |
| 61 | + room.setUnreadNotificationCount(NotificationCountType.Total, 1); |
| 62 | + room.setUnreadNotificationCount(NotificationCountType.Highlight, 0); |
| 63 | + |
| 64 | + event = mkEvent({ |
| 65 | + type: EventType.RoomMessage, |
| 66 | + content: { |
| 67 | + msgtype: MsgType.Text, |
| 68 | + body: "Hello world!", |
| 69 | + }, |
| 70 | + event: true, |
| 71 | + }, mockClient); |
| 72 | + |
| 73 | + THREAD_ID = event.getId(); |
| 74 | + threadEvent = mkEvent({ |
| 75 | + type: EventType.RoomMessage, |
| 76 | + content: { |
| 77 | + "m.relates_to": { |
| 78 | + rel_type: RelationType.Thread, |
| 79 | + event_id: THREAD_ID, |
| 80 | + }, |
| 81 | + "msgtype": MsgType.Text, |
| 82 | + "body": "Thread reply", |
| 83 | + }, |
| 84 | + event: true, |
| 85 | + }); |
| 86 | + room.createThread(THREAD_ID, event, [threadEvent], false); |
| 87 | + |
| 88 | + room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total, 1); |
| 89 | + room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight, 0); |
| 90 | + |
| 91 | + event.getPushActions = jest.fn().mockReturnValue(mkPushAction(false, false)); |
| 92 | + threadEvent.getPushActions = jest.fn().mockReturnValue(mkPushAction(false, false)); |
| 93 | + }); |
| 94 | + |
| 95 | + it("changes the room count to highlight on decryption", () => { |
| 96 | + expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(1); |
| 97 | + expect(room.getUnreadNotificationCount(NotificationCountType.Highlight)).toBe(0); |
| 98 | + |
| 99 | + fixNotificationCountOnDecryption(mockClient, event); |
| 100 | + |
| 101 | + expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(1); |
| 102 | + expect(room.getUnreadNotificationCount(NotificationCountType.Highlight)).toBe(1); |
| 103 | + }); |
| 104 | + |
| 105 | + it("changes the thread count to highlight on decryption", () => { |
| 106 | + expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total)).toBe(1); |
| 107 | + expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight)).toBe(0); |
| 108 | + |
| 109 | + fixNotificationCountOnDecryption(mockClient, threadEvent); |
| 110 | + |
| 111 | + expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total)).toBe(1); |
| 112 | + expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight)).toBe(1); |
| 113 | + }); |
| 114 | +}); |
0 commit comments