signal-desktop/ts/test-electron/state/selectors/calling_test.ts

258 lines
7.1 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2019 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { reducer as rootReducer } from '../../../state/reducer';
import { noopAction } from '../../../state/ducks/noop';
2022-07-08 20:46:25 +00:00
import { actions as userActions } from '../../../state/ducks/user';
2021-08-20 16:06:15 +00:00
import {
CallState,
CallViewMode,
2021-08-20 16:06:15 +00:00
GroupCallConnectionState,
GroupCallJoinState,
} from '../../../types/Calling';
import { CallMode } from '../../../types/CallDisposition';
import { generateAci } from '../../../types/ServiceId';
import {
getCallsByConversation,
getCallSelector,
2024-10-09 16:35:24 +00:00
getHasAnyAdminCallLinks,
getRingingCall,
2021-01-29 22:58:28 +00:00
isInCall,
} from '../../../state/selectors/calling';
import type {
2021-08-20 16:06:15 +00:00
CallingStateType,
DirectCallStateType,
GroupCallStateType,
} from '../../../state/ducks/calling';
import { getEmptyState } from '../../../state/ducks/calling';
2024-10-09 16:35:24 +00:00
import {
FAKE_CALL_LINK,
FAKE_CALL_LINK_WITH_ADMIN_KEY,
} from '../../../test-both/helpers/fakeCallLink';
const OUR_ACI = generateAci();
const ACI_1 = generateAci();
describe('state/selectors/calling', () => {
2022-07-08 20:46:25 +00:00
const getEmptyRootState = () => {
const initial = rootReducer(undefined, noopAction());
return rootReducer(
initial,
userActions.userChanged({
ourAci: OUR_ACI,
2022-07-08 20:46:25 +00:00
})
);
};
const getCallingState = (calling: CallingStateType) => ({
...getEmptyRootState(),
calling,
});
2020-11-13 19:57:55 +00:00
const stateWithDirectCall: CallingStateType = {
...getEmptyState(),
callsByConversation: {
'fake-direct-call-conversation-id': {
2020-11-13 19:57:55 +00:00
callMode: CallMode.Direct,
conversationId: 'fake-direct-call-conversation-id',
callState: CallState.Accepted,
isIncoming: false,
isVideoCall: false,
hasRemoteVideo: false,
},
},
};
2020-11-13 19:57:55 +00:00
const stateWithActiveDirectCall: CallingStateType = {
...stateWithDirectCall,
activeCallState: {
state: 'Active',
2024-02-22 21:19:50 +00:00
callMode: CallMode.Direct,
conversationId: 'fake-direct-call-conversation-id',
hasLocalAudio: true,
hasLocalVideo: false,
2022-05-19 03:28:51 +00:00
localAudioLevel: 0,
viewMode: CallViewMode.Paginated,
2020-11-17 15:07:53 +00:00
showParticipantsList: false,
outgoingRing: true,
pip: false,
settingsDialogOpen: false,
joinedAt: null,
},
};
2021-08-20 16:06:15 +00:00
const incomingDirectCall: DirectCallStateType = {
callMode: CallMode.Direct,
conversationId: 'fake-direct-call-conversation-id',
callState: CallState.Ringing,
isIncoming: true,
isVideoCall: false,
hasRemoteVideo: false,
};
2020-11-13 19:57:55 +00:00
const stateWithIncomingDirectCall: CallingStateType = {
...getEmptyState(),
callsByConversation: {
2021-08-20 16:06:15 +00:00
'fake-direct-call-conversation-id': incomingDirectCall,
},
};
const incomingGroupCall: GroupCallStateType = {
callMode: CallMode.Group,
conversationId: 'fake-group-call-conversation-id',
connectionState: GroupCallConnectionState.NotConnected,
joinState: GroupCallJoinState.NotJoined,
2023-11-16 19:55:35 +00:00
localDemuxId: undefined,
2021-08-20 16:06:15 +00:00
peekInfo: {
acis: [ACI_1],
pendingAcis: [],
creatorAci: ACI_1,
2021-08-20 16:06:15 +00:00
maxDevices: Infinity,
deviceCount: 1,
},
remoteParticipants: [],
ringId: BigInt(123),
ringerAci: ACI_1,
2021-08-20 16:06:15 +00:00
};
const stateWithIncomingGroupCall: CallingStateType = {
...getEmptyState(),
callsByConversation: {
'fake-group-call-conversation-id': incomingGroupCall,
},
};
2024-10-09 16:35:24 +00:00
const stateWithCallLink: CallingStateType = {
...getEmptyState(),
callLinks: {
[FAKE_CALL_LINK.roomId]: FAKE_CALL_LINK,
},
};
const stateWithAdminCallLink: CallingStateType = {
...getEmptyState(),
callLinks: {
[FAKE_CALL_LINK_WITH_ADMIN_KEY.roomId]: FAKE_CALL_LINK_WITH_ADMIN_KEY,
},
};
describe('getCallsByConversation', () => {
it('returns state.calling.callsByConversation', () => {
assert.deepEqual(getCallsByConversation(getEmptyRootState()), {});
assert.deepEqual(
getCallsByConversation(getCallingState(stateWithDirectCall)),
{
'fake-direct-call-conversation-id': {
callMode: CallMode.Direct,
conversationId: 'fake-direct-call-conversation-id',
callState: CallState.Accepted,
isIncoming: false,
isVideoCall: false,
hasRemoteVideo: false,
},
}
);
});
});
describe('getCallSelector', () => {
it('returns a selector that returns undefined if selecting a conversation with no call', () => {
assert.isUndefined(
getCallSelector(getEmptyRootState())('conversation-id')
);
});
it("returns a selector that returns a conversation's call", () => {
assert.deepEqual(
getCallSelector(getCallingState(stateWithDirectCall))(
'fake-direct-call-conversation-id'
),
{
callMode: CallMode.Direct,
conversationId: 'fake-direct-call-conversation-id',
callState: CallState.Accepted,
isIncoming: false,
isVideoCall: false,
hasRemoteVideo: false,
}
);
});
});
describe('getRingingCall', () => {
it('returns undefined if there are no calls', () => {
assert.isUndefined(getRingingCall(getEmptyRootState()));
});
it('returns undefined if there is no incoming call', () => {
assert.isUndefined(getRingingCall(getCallingState(stateWithDirectCall)));
assert.isUndefined(
getRingingCall(getCallingState(stateWithActiveDirectCall))
);
});
2021-08-20 16:06:15 +00:00
it('returns undefined if there is a group call with no peeked participants', () => {
const state = {
...stateWithIncomingGroupCall,
callsByConversation: {
'fake-group-call-conversation-id': {
...incomingGroupCall,
peekInfo: {
acis: [],
pendingAcis: [],
2021-08-20 16:06:15 +00:00
maxDevices: Infinity,
deviceCount: 1,
},
},
},
};
assert.isUndefined(getRingingCall(getCallingState(state)));
2021-08-20 16:06:15 +00:00
});
it('returns an incoming direct call', () => {
assert.deepEqual(
getRingingCall(getCallingState(stateWithIncomingDirectCall)),
2021-08-20 16:06:15 +00:00
incomingDirectCall
);
});
it('returns an incoming group call', () => {
assert.deepEqual(
getRingingCall(getCallingState(stateWithIncomingGroupCall)),
2021-08-20 16:06:15 +00:00
incomingGroupCall
);
});
});
2021-01-29 22:58:28 +00:00
describe('isInCall', () => {
it('returns should be false if we are not in a call', () => {
assert.isFalse(isInCall(getEmptyRootState()));
});
it('should be true if we are in a call', () => {
assert.isTrue(isInCall(getCallingState(stateWithActiveDirectCall)));
});
});
2024-10-09 16:35:24 +00:00
describe('getHasAnyAdminCallLinks', () => {
it('returns true with admin call links', () => {
assert.isTrue(
getHasAnyAdminCallLinks(getCallingState(stateWithAdminCallLink))
);
});
it('returns false with only non-admin call links', () => {
assert.isFalse(
getHasAnyAdminCallLinks(getCallingState(stateWithCallLink))
);
});
it('returns false without any call links', () => {
assert.isFalse(getHasAnyAdminCallLinks(getEmptyRootState()));
});
});
});