@@ -835,6 +835,7 @@ describe("MatrixRTCSession", () => {
835
835
it ( "rotates key if a member leaves" , async ( ) => {
836
836
jest . useFakeTimers ( ) ;
837
837
try {
838
+ const KEY_DALAY = 3000 ;
838
839
const member2 = Object . assign ( { } , membershipTemplate , {
839
840
device_id : "BBBBBBB" ,
840
841
} ) ;
@@ -855,7 +856,8 @@ describe("MatrixRTCSession", () => {
855
856
sendEventMock . mockImplementation ( ( _roomId , _evType , payload ) => resolve ( payload ) ) ;
856
857
} ) ;
857
858
858
- sess . joinRoomSession ( [ mockFocus ] , mockFocus , { manageMediaKeys : true } ) ;
859
+ sess . joinRoomSession ( [ mockFocus ] , mockFocus , { manageMediaKeys : true , makeKeyDelay : KEY_DALAY } ) ;
860
+ const sendKeySpy = jest . spyOn ( ( sess as unknown as any ) . encryptionManager . transport , "sendKey" ) ;
859
861
const firstKeysPayload = await keysSentPromise1 ;
860
862
expect ( firstKeysPayload . keys ) . toHaveLength ( 1 ) ;
861
863
expect ( firstKeysPayload . keys [ 0 ] . index ) . toEqual ( 0 ) ;
@@ -872,14 +874,24 @@ describe("MatrixRTCSession", () => {
872
874
. mockReturnValue ( makeMockRoomState ( [ membershipTemplate ] , mockRoom . roomId ) ) ;
873
875
sess . onRTCSessionMemberUpdate ( ) ;
874
876
875
- jest . advanceTimersByTime ( 10000 ) ;
877
+ jest . advanceTimersByTime ( 3000 ) ;
878
+ expect ( sendKeySpy ) . toHaveBeenCalledTimes ( 1 ) ;
879
+ // check that we send the key with index 1 even though the send gets delayed when leaving.
880
+ // this makes sure we do not use an index that is one too old.
881
+ expect ( sendKeySpy ) . toHaveBeenLastCalledWith ( expect . any ( String ) , 1 , sess . memberships ) ;
882
+ // fake a condition in which we send another encryption key event.
883
+ // this could happen do to someone joining the call.
884
+ ( sess as unknown as any ) . encryptionManager . sendEncryptionKeysEvent ( ) ;
885
+ expect ( sendKeySpy ) . toHaveBeenLastCalledWith ( expect . any ( String ) , 1 , sess . memberships ) ;
886
+ jest . advanceTimersByTime ( 7000 ) ;
876
887
877
888
const secondKeysPayload = await keysSentPromise2 ;
878
889
879
890
expect ( secondKeysPayload . keys ) . toHaveLength ( 1 ) ;
880
891
expect ( secondKeysPayload . keys [ 0 ] . index ) . toEqual ( 1 ) ;
881
892
expect ( onMyEncryptionKeyChanged ) . toHaveBeenCalledTimes ( 2 ) ;
882
- expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 2 ) ;
893
+ // initial, on leave and the fake one we do with: `(sess as unknown as any).encryptionManager.sendEncryptionKeysEvent();`
894
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 3 ) ;
883
895
} finally {
884
896
jest . useRealTimers ( ) ;
885
897
}
0 commit comments