@@ -6,7 +6,6 @@ const mockCallStoreReset = jest.fn();
66const mockUseCallStoreGetState = jest . fn ( ( ) => ( {
77 reset : mockCallStoreReset ,
88 setCall : jest . fn ( ) ,
9- setCallId : jest . fn ( ) ,
109 resetNativeCallId : jest . fn ( ) ,
1110 call : null as unknown ,
1211 callId : null as string | null ,
@@ -98,14 +97,17 @@ jest.mock('@rocket.chat/media-signaling', () => ({
9897 } )
9998} ) ) ;
10099
100+ const STREAM_NOTIFY_USER = 'stream-notify-user' ;
101+
101102function getStreamNotifyHandler ( ) : ( ddpMessage : IDDPMessage ) => void {
102103 const calls = mockOnStreamData . mock . calls as unknown as [ string , ( m : IDDPMessage ) => void ] [ ] ;
103- const last = calls [ calls . length - 1 ] ;
104- const handler = last ?. [ 1 ] ;
105- if ( typeof handler !== 'function' ) {
106- throw new Error ( 'stream-notify-user handler not registered' ) ;
104+ for ( let i = calls . length - 1 ; i >= 0 ; i -- ) {
105+ const [ eventName , handler ] = calls [ i ] ;
106+ if ( eventName === STREAM_NOTIFY_USER && typeof handler === 'function' ) {
107+ return handler ;
108+ }
107109 }
108- return handler ;
110+ throw new Error ( 'stream-notify-user handler not registered' ) ;
109111}
110112
111113describe ( 'MediaSessionInstance' , ( ) => {
@@ -115,7 +117,6 @@ describe('MediaSessionInstance', () => {
115117 mockUseCallStoreGetState . mockReturnValue ( {
116118 reset : mockCallStoreReset ,
117119 setCall : jest . fn ( ) ,
118- setCallId : jest . fn ( ) ,
119120 resetNativeCallId : jest . fn ( ) ,
120121 call : null ,
121122 callId : null ,
@@ -197,40 +198,8 @@ describe('MediaSessionInstance', () => {
197198 } ) ;
198199
199200 describe ( 'stream-notify-user (notification/accepted gated)' , ( ) => {
200- it ( 'does not call answerCall when store has no native-accepted callId' , async ( ) => {
201- const answerSpy = jest . spyOn ( mediaSessionInstance , 'answerCall' ) . mockResolvedValue ( undefined ) ;
202- mediaSessionInstance . init ( 'user-1' ) ;
203- const streamHandler = getStreamNotifyHandler ( ) ;
204- streamHandler ( {
205- msg : 'changed' ,
206- fields : {
207- eventName : 'uid/media-signal' ,
208- args : [
209- {
210- type : 'notification' ,
211- notification : 'accepted' ,
212- signedContractId : 'test-device-id' ,
213- callId : 'from-signal'
214- }
215- ]
216- }
217- } ) ;
218- await Promise . resolve ( ) ;
219- expect ( answerSpy ) . not . toHaveBeenCalled ( ) ;
220- answerSpy . mockRestore ( ) ;
221- } ) ;
222-
223- it ( 'does not call answerCall when transient callId matches signal but nativeAcceptedCallId does not' , async ( ) => {
201+ it ( 'does not call answerCall when nativeAcceptedCallId is null' , async ( ) => {
224202 const answerSpy = jest . spyOn ( mediaSessionInstance , 'answerCall' ) . mockResolvedValue ( undefined ) ;
225- mockUseCallStoreGetState . mockReturnValue ( {
226- reset : mockCallStoreReset ,
227- setCall : jest . fn ( ) ,
228- setCallId : jest . fn ( ) ,
229- resetNativeCallId : jest . fn ( ) ,
230- call : null ,
231- callId : 'from-signal' ,
232- nativeAcceptedCallId : null
233- } ) ;
234203 mediaSessionInstance . init ( 'user-1' ) ;
235204 const streamHandler = getStreamNotifyHandler ( ) ;
236205 streamHandler ( {
@@ -257,7 +226,6 @@ describe('MediaSessionInstance', () => {
257226 mockUseCallStoreGetState . mockReturnValue ( {
258227 reset : mockCallStoreReset ,
259228 setCall : jest . fn ( ) ,
260- setCallId : jest . fn ( ) ,
261229 resetNativeCallId : jest . fn ( ) ,
262230 call : null ,
263231 callId : null ,
@@ -289,7 +257,6 @@ describe('MediaSessionInstance', () => {
289257 mockUseCallStoreGetState . mockReturnValue ( {
290258 reset : mockCallStoreReset ,
291259 setCall : jest . fn ( ) ,
292- setCallId : jest . fn ( ) ,
293260 resetNativeCallId : jest . fn ( ) ,
294261 call : null ,
295262 callId : null ,
@@ -321,7 +288,6 @@ describe('MediaSessionInstance', () => {
321288 mockUseCallStoreGetState . mockReturnValue ( {
322289 reset : mockCallStoreReset ,
323290 setCall : jest . fn ( ) ,
324- setCallId : jest . fn ( ) ,
325291 resetNativeCallId : jest . fn ( ) ,
326292 call : { callId : 'from-signal' } as any ,
327293 callId : 'from-signal' ,
0 commit comments