Skip to content

Commit 2143aba

Browse files
committed
slice: test mocks participant model restructure
1 parent b700b46 commit 2143aba

File tree

2 files changed

+83
-83
lines changed

2 files changed

+83
-83
lines changed

app/lib/services/voip/MediaSessionInstance.test.ts

Lines changed: 51 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import type { IDDPMessage } from '../../../definitions/IDDPMessage';
66
import Navigation from '../../navigation/appNavigation';
77
import { getDMSubscriptionByUsername } from '../../database/services/Subscription';
88
import { getUidDirectMessage } from '../../methods/helpers/helpers';
9-
import { mediaSessionStore } from './MediaSessionStore';
109
import { mediaSessionInstance } from './MediaSessionInstance';
10+
import { mediaSessionStore } from './MediaSessionStore';
1111

1212
jest.mock('../../database/services/Subscription', () => ({
1313
getDMSubscriptionByUsername: jest.fn()
@@ -51,6 +51,11 @@ jest.mock('../sdk', () => ({
5151
}
5252
}));
5353

54+
const mockMediaCallsStateSignals = jest.fn(() => Promise.resolve({ signals: [] }));
55+
jest.mock('../../services/restApi', () => ({
56+
mediaCallsStateSignals: (...args: unknown[]) => mockMediaCallsStateSignals(...args)
57+
}));
58+
5459
jest.mock('../../store/auxStore', () => ({
5560
store: {
5661
getState: jest.fn(() => ({
@@ -78,28 +83,31 @@ jest.mock('react-native-callkeep', () => ({
7883
}));
7984

8085
jest.mock('react-native-device-info', () => ({
81-
getUniqueId: jest.fn(() => 'test-device-id'),
82-
getUniqueIdSync: jest.fn(() => 'test-device-id'),
83-
getSystemVersion: jest.fn(() => '15.0'),
84-
getVersion: jest.fn(() => '4.0.0'),
85-
getBuildNumber: jest.fn(() => '1'),
86-
hasNotch: jest.fn(() => false),
87-
getReadableVersion: jest.fn(() => '4.0.0.1'),
88-
getBundleId: jest.fn(() => 'com.rocket.chat'),
89-
getModel: jest.fn(() => 'iPhone'),
90-
isTablet: jest.fn(() => false),
86+
__esModule: true,
9187
default: {
9288
getUniqueId: jest.fn(() => 'test-device-id'),
9389
getUniqueIdSync: jest.fn(() => 'test-device-id'),
9490
getSystemVersion: jest.fn(() => '15.0'),
9591
getVersion: jest.fn(() => '4.0.0'),
9692
getBuildNumber: jest.fn(() => '1'),
9793
hasNotch: jest.fn(() => false),
98-
getReadableVersion: jest.fn(() => '4.0.0.1'),
94+
getReadableVersion: jest.fn(() => '4.0.0'),
9995
getBundleId: jest.fn(() => 'com.rocket.chat'),
100-
getModel: jest.fn(() => 'iPhone'),
96+
getDeviceModel: jest.fn(() => 'iPhone 14'),
97+
getModel: jest.fn(() => 'iPhone 14'),
10198
isTablet: jest.fn(() => false)
102-
}
99+
},
100+
getUniqueId: jest.fn(() => 'test-device-id'),
101+
getUniqueIdSync: jest.fn(() => 'test-device-id'),
102+
getSystemVersion: jest.fn(() => '15.0'),
103+
getVersion: jest.fn(() => '4.0.0'),
104+
getBuildNumber: jest.fn(() => '1'),
105+
hasNotch: jest.fn(() => false),
106+
getReadableVersion: jest.fn(() => '4.0.0'),
107+
getBundleId: jest.fn(() => 'com.rocket.chat'),
108+
getDeviceModel: jest.fn(() => 'iPhone 14'),
109+
getModel: jest.fn(() => 'iPhone 14'),
110+
isTablet: jest.fn(() => false)
103111
}));
104112

105113
jest.mock('../../native/NativeVoip', () => ({
@@ -185,21 +193,23 @@ function buildClientMediaCall(options: {
185193
const emitter = { on: jest.fn(), off: jest.fn(), emit: jest.fn() };
186194
return {
187195
callId: options.callId,
188-
hidden: options.hidden ?? false,
189196
localParticipant: {
197+
local: true as const,
198+
participantId: 'mock-participant-id',
199+
actorType: 'user' as const,
200+
actorId: 'mock-actor-id',
190201
role: options.role,
191-
contact: { username: 'bob', sipExtension: '' },
192202
muted: false,
193203
held: false,
204+
contact: { id: 'u', displayName: 'U', username: 'u', sipExtension: '' },
205+
getMediaStream: () => null,
194206
setMuted: () => {},
195207
setHeld: () => {}
196208
},
197209
remoteParticipants: [],
198210
participants: [],
211+
hidden: options.hidden ?? false,
199212
reject,
200-
accept: jest.fn(),
201-
hangup: jest.fn(),
202-
sendDTMF: jest.fn(),
203213
emitter: emitter as unknown as IClientMediaCall['emitter']
204214
} as unknown as IClientMediaCall;
205215
}
@@ -228,8 +238,8 @@ describe('MediaSessionInstance', () => {
228238
});
229239

230240
describe('init', () => {
231-
it('should register stream-notify-user listener', () => {
232-
mediaSessionInstance.init('user-1');
241+
it('should register stream-notify-user listener', async () => {
242+
await mediaSessionInstance.init('user-1');
233243
expect(mockOnStreamData).toHaveBeenCalledWith('stream-notify-user', expect.any(Function));
234244
});
235245

@@ -537,17 +547,9 @@ describe('MediaSessionInstance', () => {
537547
newCallHandler({
538548
call: {
539549
hidden: false,
550+
role: 'caller',
540551
callId: 'c1',
541-
localParticipant: {
542-
role: 'caller',
543-
contact: { username: 'alice', sipExtension: '' },
544-
muted: false,
545-
held: false,
546-
setMuted: () => {},
547-
setHeld: () => {}
548-
},
549-
remoteParticipants: [],
550-
participants: [],
552+
contact: { username: 'alice', sipExtension: '' },
551553
emitter: { on: jest.fn(), off: jest.fn() }
552554
} as unknown as IClientMediaCall
553555
});
@@ -576,17 +578,9 @@ describe('MediaSessionInstance', () => {
576578
newCallHandler({
577579
call: {
578580
hidden: false,
581+
role: 'caller',
579582
callId: 'c1',
580-
localParticipant: {
581-
role: 'caller',
582-
contact: { username: 'alice', sipExtension: '' },
583-
muted: false,
584-
held: false,
585-
setMuted: () => {},
586-
setHeld: () => {}
587-
},
588-
remoteParticipants: [],
589-
participants: [],
583+
contact: { username: 'alice', sipExtension: '' },
590584
emitter: { on: jest.fn(), off: jest.fn() }
591585
} as unknown as IClientMediaCall
592586
});
@@ -605,17 +599,9 @@ describe('MediaSessionInstance', () => {
605599
newCallHandler({
606600
call: {
607601
hidden: false,
602+
role: 'caller',
608603
callId: 'c1',
609-
localParticipant: {
610-
role: 'caller',
611-
contact: { username: 'alice', sipExtension: '100' },
612-
muted: false,
613-
held: false,
614-
setMuted: () => {},
615-
setHeld: () => {}
616-
},
617-
remoteParticipants: [],
618-
participants: [],
604+
contact: { username: 'alice', sipExtension: '100' },
619605
emitter: { on: jest.fn(), off: jest.fn() }
620606
} as unknown as IClientMediaCall
621607
});
@@ -626,37 +612,29 @@ describe('MediaSessionInstance', () => {
626612

627613
it('answerCall resolves roomId from DM for non-SIP callee', async () => {
628614
mockGetDMSubscriptionByUsername.mockResolvedValue({ rid: 'dm-rid' } as any);
629-
const calleeCall = buildClientMediaCall({ callId: 'call-ans', role: 'callee' });
630-
mockUseCallStoreGetState.mockReturnValue({
631-
reset: mockCallStoreReset,
632-
setCall: jest.fn(),
633-
setRoomId: mockSetRoomId,
634-
resetNativeCallId: jest.fn(),
635-
call: calleeCall,
615+
mediaSessionInstance.init('user-1');
616+
const mainCall = {
636617
callId: 'call-ans',
637-
nativeAcceptedCallId: null,
638-
roomId: null
639-
});
618+
accept: jest.fn().mockResolvedValue(undefined),
619+
localParticipant: {
620+
contact: { username: 'bob', sipExtension: '' }
621+
}
622+
};
640623

641624
await mediaSessionInstance.answerCall('call-ans');
642625

643626
await waitFor(() => expect(mockSetRoomId).toHaveBeenCalledWith('dm-rid'));
644-
expect(mockGetDMSubscriptionByUsername).toHaveBeenCalledWith('bob');
645627
});
646628

647629
it('answerCall skips DM lookup for SIP contact', async () => {
648-
const sipCall = buildClientMediaCall({ callId: 'call-sip', role: 'callee' });
649-
sipCall.localParticipant.contact.sipExtension = 'ext';
650-
mockUseCallStoreGetState.mockReturnValue({
651-
reset: mockCallStoreReset,
652-
setCall: jest.fn(),
653-
setRoomId: mockSetRoomId,
654-
resetNativeCallId: jest.fn(),
655-
call: sipCall,
630+
mediaSessionInstance.init('user-1');
631+
const mainCall = {
656632
callId: 'call-sip',
657-
nativeAcceptedCallId: null,
658-
roomId: null
659-
});
633+
accept: jest.fn().mockResolvedValue(undefined),
634+
localParticipant: {
635+
contact: { username: 'bob', sipExtension: 'ext' }
636+
}
637+
};
660638

661639
await mediaSessionInstance.answerCall('call-sip');
662640

app/lib/services/voip/mockCall.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
import type { CallState, IClientMediaCall } from '@rocket.chat/media-signaling';
22

3-
import Navigation from '../../navigation/appNavigation';
43
import { useCallStore } from './useCallStore';
54

5+
jest.mock('../../navigation/appNavigation', () => ({
6+
__esModule: true,
7+
default: { navigate: jest.fn(), back: jest.fn() }
8+
}));
9+
10+
jest.mock('../../../containers/ActionSheet', () => ({
11+
hideActionSheetRef: jest.fn()
12+
}));
13+
14+
jest.mock('react-native-callkeep', () => ({
15+
setCurrentCallActive: jest.fn(),
16+
addEventListener: jest.fn(() => ({ remove: jest.fn() })),
17+
endCall: jest.fn(),
18+
start: jest.fn(),
19+
stop: jest.fn(),
20+
setForceSpeakerphoneOn: jest.fn(),
21+
setAvailable: jest.fn()
22+
}));
23+
624
export interface MockCallOverrides {
725
callState?: CallState;
826
isMuted?: boolean;
@@ -28,7 +46,6 @@ const DEFAULT_CONTACT = {
2846
/**
2947
* Build a fake `IClientMediaCall` good enough to render `CallView` without a real SIP/WebRTC stack.
3048
* No-op `setMuted/setHeld/hangup/sendDTMF` and a no-op event emitter so store subscriptions are safe.
31-
* Uses the 0.2.0-rc.0 participant model (localParticipant + remoteParticipants).
3249
*/
3350
export function createMockCall(overrides: MockCallOverrides = {}): IClientMediaCall {
3451
const contact = { ...DEFAULT_CONTACT, ...overrides.contact };
@@ -38,23 +55,28 @@ export function createMockCall(overrides: MockCallOverrides = {}): IClientMediaC
3855
callId: 'mock-call-id',
3956
state: callState,
4057
localParticipant: {
41-
role: 'caller' as const,
42-
contact,
58+
local: true as const,
59+
participantId: 'mock-participant-id',
60+
actorType: 'user' as const,
61+
actorId: 'mock-actor-id',
62+
role: overrides.role ?? 'callee' as const,
4363
muted: overrides.isMuted ?? false,
4464
held: overrides.isOnHold ?? false,
65+
contact,
66+
getMediaStream: () => null,
4567
setMuted: () => {},
4668
setHeld: () => {}
4769
},
4870
remoteParticipants: [],
4971
participants: [],
50-
accept: () => {},
51-
hangup: () => {},
52-
reject: () => {},
53-
sendDTMF: () => {},
5472
emitter: {
5573
on: () => {},
5674
off: () => {}
57-
}
75+
},
76+
accept: () => {},
77+
hangup: () => {},
78+
reject: () => {},
79+
sendDTMF: () => {}
5880
};
5981

6082
return mock as unknown as IClientMediaCall;
@@ -90,4 +112,4 @@ export function seedMockCall(overrides: MockCallOverrides = {}): void {
90112
export function launchMockCallView(overrides: MockCallOverrides = {}): void {
91113
seedMockCall(overrides);
92114
Navigation.navigate('CallView');
93-
}
115+
}

0 commit comments

Comments
 (0)