@@ -39,8 +39,16 @@ import {
39
39
ConnectionState ,
40
40
JitsiCall ,
41
41
ElementCall ,
42
+ ElementCallIntent ,
42
43
} from "../../../src/models/Call" ;
43
- import { stubClient , mkEvent , mkRoomMember , setupAsyncStoreWithClient , mockPlatformPeg } from "../../test-utils" ;
44
+ import {
45
+ stubClient ,
46
+ mkEvent ,
47
+ mkRoomMember ,
48
+ setupAsyncStoreWithClient ,
49
+ mockPlatformPeg ,
50
+ MockEventEmitter ,
51
+ } from "../../test-utils" ;
44
52
import { MatrixClientPeg } from "../../../src/MatrixClientPeg" ;
45
53
import WidgetStore from "../../../src/stores/WidgetStore" ;
46
54
import { WidgetMessagingStore } from "../../../src/stores/widgets/WidgetMessagingStore" ;
@@ -50,8 +58,6 @@ import SettingsStore from "../../../src/settings/SettingsStore";
50
58
import { Anonymity , PosthogAnalytics } from "../../../src/PosthogAnalytics" ;
51
59
import { type SettingKey } from "../../../src/settings/Settings.tsx" ;
52
60
import SdkConfig from "../../../src/SdkConfig.ts" ;
53
- import RoomListStore from "../../../src/stores/room-list/RoomListStore.ts" ;
54
- import { DefaultTagID } from "../../../src/stores/room-list/models.ts" ;
55
61
import DMRoomMap from "../../../src/utils/DMRoomMap.ts" ;
56
62
57
63
const enabledSettings = new Set ( [ "feature_group_calls" , "feature_video_rooms" , "feature_element_call_video_rooms" ] ) ;
@@ -65,6 +71,7 @@ const setUpClientRoomAndStores = (): {
65
71
alice : RoomMember ;
66
72
bob : RoomMember ;
67
73
carol : RoomMember ;
74
+ roomSession : Mocked < MatrixRTCSession > ;
68
75
} => {
69
76
stubClient ( ) ;
70
77
const client = mocked < MatrixClient > ( MatrixClientPeg . safeGet ( ) ) ;
@@ -93,12 +100,13 @@ const setUpClientRoomAndStores = (): {
93
100
jest . spyOn ( room , "getMyMembership" ) . mockReturnValue ( KnownMembership . Join ) ;
94
101
95
102
client . getRoom . mockImplementation ( ( roomId ) => ( roomId === room . roomId ? room : null ) ) ;
96
- client . getRoom . mockImplementation ( ( roomId ) => ( roomId === room . roomId ? room : null ) ) ;
97
- client . matrixRTC . getRoomSession . mockImplementation ( ( roomId ) => {
98
- const session = new EventEmitter ( ) as MatrixRTCSession ;
99
- session . memberships = [ ] ;
100
- return session ;
101
- } ) ;
103
+
104
+ const roomSession = new MockEventEmitter ( {
105
+ memberships : [ ] ,
106
+ getOldestMembership : jest . fn ( ) . mockReturnValue ( undefined ) ,
107
+ } ) as Mocked < MatrixRTCSession > ;
108
+
109
+ client . matrixRTC . getRoomSession . mockReturnValue ( roomSession ) ;
102
110
client . getRooms . mockReturnValue ( [ room ] ) ;
103
111
client . getUserId . mockReturnValue ( alice . userId ) ;
104
112
client . getDeviceId . mockReturnValue ( "alices_device" ) ;
@@ -120,7 +128,7 @@ const setUpClientRoomAndStores = (): {
120
128
setupAsyncStoreWithClient ( WidgetStore . instance , client ) ;
121
129
setupAsyncStoreWithClient ( WidgetMessagingStore . instance , client ) ;
122
130
123
- return { client, room, alice, bob, carol } ;
131
+ return { client, room, alice, bob, carol, roomSession } ;
124
132
} ;
125
133
126
134
const cleanUpClientRoomAndStores = ( client : MatrixClient , room : Room ) => {
@@ -553,14 +561,14 @@ describe("ElementCall", () => {
553
561
let client : Mocked < MatrixClient > ;
554
562
let room : Room ;
555
563
let alice : RoomMember ;
556
-
564
+ let roomSession : Mocked < MatrixRTCSession > ;
557
565
function setRoomMembers ( memberIds : string [ ] ) {
558
566
jest . spyOn ( room , "getJoinedMembers" ) . mockReturnValue ( memberIds . map ( ( id ) => ( { userId : id } ) as RoomMember ) ) ;
559
567
}
560
568
561
569
beforeEach ( ( ) => {
562
570
jest . useFakeTimers ( ) ;
563
- ( { client, room, alice } = setUpClientRoomAndStores ( ) ) ;
571
+ ( { client, room, alice, roomSession } = setUpClientRoomAndStores ( ) ) ;
564
572
SdkConfig . reset ( ) ;
565
573
} ) ;
566
574
@@ -571,7 +579,16 @@ describe("ElementCall", () => {
571
579
} ) ;
572
580
573
581
describe ( "get" , ( ) => {
574
- afterEach ( ( ) => Call . get ( room ) ?. destroy ( ) ) ;
582
+ let getUserIdForRoomIdSpy : jest . SpyInstance ;
583
+
584
+ beforeEach ( ( ) => {
585
+ getUserIdForRoomIdSpy = jest . spyOn ( DMRoomMap . shared ( ) , "getUserIdForRoomId" ) ;
586
+ } ) ;
587
+
588
+ afterEach ( ( ) => {
589
+ Call . get ( room ) ?. destroy ( ) ;
590
+ getUserIdForRoomIdSpy . mockRestore ( ) ;
591
+ } ) ;
575
592
576
593
it ( "finds no calls" , ( ) => {
577
594
expect ( Call . get ( room ) ) . toBeNull ( ) ;
@@ -600,11 +617,7 @@ describe("ElementCall", () => {
600
617
601
618
it ( "finds ongoing calls that are created by the session manager" , async ( ) => {
602
619
// There is an existing session created by another user in this room.
603
- client . matrixRTC . getRoomSession . mockReturnValue ( {
604
- on : ( ev : any , fn : any ) => { } ,
605
- off : ( ev : any , fn : any ) => { } ,
606
- memberships : [ { fakeVal : "fake membership" } ] ,
607
- } as unknown as MatrixRTCSession ) ;
620
+ roomSession . memberships . push ( { } as CallMembership ) ;
608
621
const call = Call . get ( room ) ;
609
622
if ( ! ( call instanceof ElementCall ) ) throw new Error ( "Failed to create call" ) ;
610
623
} ) ;
@@ -750,19 +763,50 @@ describe("ElementCall", () => {
750
763
expect ( urlParams . get ( "analyticsID" ) ) . toBeFalsy ( ) ;
751
764
} ) ;
752
765
753
- it ( "requests ringing notifications in DMs" , async ( ) => {
754
- const tagsSpy = jest . spyOn ( RoomListStore . instance , "getTagsForRoom" ) ;
755
- try {
756
- tagsSpy . mockReturnValue ( [ DefaultTagID . DM ] ) ;
757
- ElementCall . create ( room ) ;
758
- const call = Call . get ( room ) ;
759
- if ( ! ( call instanceof ElementCall ) ) throw new Error ( "Failed to create call" ) ;
766
+ it ( "requests ringing notifications and correct intent in DMs" , async ( ) => {
767
+ getUserIdForRoomIdSpy . mockImplementation ( ( roomId : string ) =>
768
+ room . roomId === roomId ? "any-user" : undefined ,
769
+ ) ;
770
+ ElementCall . create ( room ) ;
771
+ const call = Call . get ( room ) ;
772
+ if ( ! ( call instanceof ElementCall ) ) throw new Error ( "Failed to create call" ) ;
760
773
761
- const urlParams = new URLSearchParams ( new URL ( call . widget . url ) . hash . slice ( 1 ) ) ;
762
- expect ( urlParams . get ( "sendNotificationType" ) ) . toBe ( "ring" ) ;
763
- } finally {
764
- tagsSpy . mockRestore ( ) ;
765
- }
774
+ const urlParams = new URLSearchParams ( new URL ( call . widget . url ) . hash . slice ( 1 ) ) ;
775
+ expect ( urlParams . get ( "sendNotificationType" ) ) . toBe ( "ring" ) ;
776
+ expect ( urlParams . get ( "intent" ) ) . toBe ( ElementCallIntent . StartCallDM ) ;
777
+ } ) ;
778
+
779
+ it ( "requests correct intent when answering DMs" , async ( ) => {
780
+ roomSession . getOldestMembership . mockReturnValue ( { } as CallMembership ) ;
781
+ getUserIdForRoomIdSpy . mockImplementation ( ( roomId : string ) =>
782
+ room . roomId === roomId ? "any-user" : undefined ,
783
+ ) ;
784
+ ElementCall . create ( room ) ;
785
+ const call = Call . get ( room ) ;
786
+ if ( ! ( call instanceof ElementCall ) ) throw new Error ( "Failed to create call" ) ;
787
+
788
+ const urlParams = new URLSearchParams ( new URL ( call . widget . url ) . hash . slice ( 1 ) ) ;
789
+ expect ( urlParams . get ( "intent" ) ) . toBe ( ElementCallIntent . JoinExistingDM ) ;
790
+ } ) ;
791
+
792
+ it ( "requests correct intent when creating a non-DM call" , async ( ) => {
793
+ roomSession . getOldestMembership . mockReturnValue ( undefined ) ;
794
+ ElementCall . create ( room ) ;
795
+ const call = Call . get ( room ) ;
796
+ if ( ! ( call instanceof ElementCall ) ) throw new Error ( "Failed to create call" ) ;
797
+
798
+ const urlParams = new URLSearchParams ( new URL ( call . widget . url ) . hash . slice ( 1 ) ) ;
799
+ expect ( urlParams . get ( "intent" ) ) . toBe ( ElementCallIntent . StartCall ) ;
800
+ } ) ;
801
+
802
+ it ( "requests correct intent when joining a non-DM call" , async ( ) => {
803
+ roomSession . getOldestMembership . mockReturnValue ( { } as CallMembership ) ;
804
+ ElementCall . create ( room ) ;
805
+ const call = Call . get ( room ) ;
806
+ if ( ! ( call instanceof ElementCall ) ) throw new Error ( "Failed to create call" ) ;
807
+
808
+ const urlParams = new URLSearchParams ( new URL ( call . widget . url ) . hash . slice ( 1 ) ) ;
809
+ expect ( urlParams . get ( "intent" ) ) . toBe ( ElementCallIntent . JoinExisting ) ;
766
810
} ) ;
767
811
768
812
it ( "requests visual notifications in non-DMs" , async ( ) => {
0 commit comments