2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2020-11-04 22:04:48 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
|
2023-09-14 17:04:48 +00:00
|
|
|
import { generateAci } from '../../types/ServiceId';
|
|
|
|
import { isAciString } from '../../util/isAciString';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationType } from '../../state/ducks/conversations';
|
2023-08-17 18:48:42 +00:00
|
|
|
import { MemberRepository, _toMembers } from '../../quill/memberRepository';
|
2023-08-16 20:54:39 +00:00
|
|
|
import { getDefaultConversationWithServiceId } from '../../test-both/helpers/getDefaultConversation';
|
2020-11-04 22:04:48 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const UNKNOWN_SERVICE_ID = generateAci();
|
|
|
|
|
|
|
|
const memberMahershala: ConversationType = getDefaultConversationWithServiceId({
|
2020-11-04 22:04:48 +00:00
|
|
|
id: '555444',
|
|
|
|
title: 'Pal',
|
|
|
|
firstName: 'Mahershala',
|
|
|
|
profileName: 'Mr Ali',
|
|
|
|
name: 'Friend',
|
|
|
|
type: 'direct',
|
|
|
|
lastUpdated: Date.now(),
|
|
|
|
markedUnread: false,
|
2020-11-11 17:36:05 +00:00
|
|
|
areWeAdmin: false,
|
2021-05-07 22:21:10 +00:00
|
|
|
});
|
2020-11-04 22:04:48 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const memberShia: ConversationType = getDefaultConversationWithServiceId({
|
2020-11-04 22:04:48 +00:00
|
|
|
id: '333222',
|
|
|
|
title: 'Buddy',
|
|
|
|
firstName: 'Shia',
|
|
|
|
profileName: 'Sr LaBeouf',
|
|
|
|
name: 'Duder',
|
|
|
|
type: 'direct',
|
|
|
|
lastUpdated: Date.now(),
|
|
|
|
markedUnread: false,
|
2020-11-11 17:36:05 +00:00
|
|
|
areWeAdmin: false,
|
2021-05-07 22:21:10 +00:00
|
|
|
});
|
2020-11-04 22:04:48 +00:00
|
|
|
|
2023-08-17 18:48:42 +00:00
|
|
|
const conversations: Array<ConversationType> = [memberMahershala, memberShia];
|
2020-11-04 22:04:48 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const singleMember: ConversationType = getDefaultConversationWithServiceId({
|
2020-11-04 22:04:48 +00:00
|
|
|
id: '666777',
|
|
|
|
title: 'The Guy',
|
|
|
|
firstName: 'Jeff',
|
|
|
|
profileName: 'Jr Klaus',
|
|
|
|
name: 'Him',
|
|
|
|
type: 'direct',
|
|
|
|
lastUpdated: Date.now(),
|
|
|
|
markedUnread: false,
|
2020-11-11 17:36:05 +00:00
|
|
|
areWeAdmin: false,
|
2021-05-07 22:21:10 +00:00
|
|
|
});
|
2020-11-04 22:04:48 +00:00
|
|
|
|
|
|
|
describe('MemberRepository', () => {
|
|
|
|
describe('#updateMembers', () => {
|
|
|
|
it('updates with given members', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
|
|
|
assert.deepEqual(
|
|
|
|
memberRepository.getMembers(),
|
|
|
|
_toMembers(conversations)
|
|
|
|
);
|
2020-11-04 22:04:48 +00:00
|
|
|
|
2023-08-17 18:48:42 +00:00
|
|
|
const updatedConversations = [...conversations, singleMember];
|
|
|
|
memberRepository.updateMembers(updatedConversations);
|
|
|
|
assert.deepEqual(
|
|
|
|
memberRepository.getMembers(),
|
|
|
|
_toMembers(updatedConversations)
|
|
|
|
);
|
2020-11-04 22:04:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#getMemberById', () => {
|
|
|
|
it('returns undefined when there is no search id', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
2020-11-04 22:04:48 +00:00
|
|
|
assert.isUndefined(memberRepository.getMemberById());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns a matched member', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
2020-11-04 22:04:48 +00:00
|
|
|
assert.isDefined(memberRepository.getMemberById('555444'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns undefined when it does not have the member', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
2023-08-16 20:54:39 +00:00
|
|
|
assert.isUndefined(memberRepository.getMemberById(UNKNOWN_SERVICE_ID));
|
2020-11-04 22:04:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-17 18:48:42 +00:00
|
|
|
describe('#getMemberByAci', () => {
|
2023-08-16 20:54:39 +00:00
|
|
|
it('returns undefined when there is no search serviceId', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
|
|
|
assert.isUndefined(memberRepository.getMemberByAci());
|
2020-11-04 22:04:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns a matched member', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
|
|
|
const aci = memberMahershala.serviceId;
|
|
|
|
if (!isAciString(aci)) {
|
|
|
|
throw new Error('Service id not ACI');
|
|
|
|
}
|
|
|
|
assert.isDefined(memberRepository.getMemberByAci(aci));
|
2020-11-04 22:04:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns undefined when it does not have the member', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
|
|
|
assert.isUndefined(memberRepository.getMemberByAci(UNKNOWN_SERVICE_ID));
|
2020-11-04 22:04:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#search', () => {
|
|
|
|
describe('given a prefix-matching string on last name', () => {
|
|
|
|
it('returns the match', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
2020-11-04 22:04:48 +00:00
|
|
|
const results = memberRepository.search('a');
|
2023-08-17 18:48:42 +00:00
|
|
|
assert.deepEqual(results, _toMembers([memberMahershala]));
|
2020-11-04 22:04:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('given a prefix-matching string on first name', () => {
|
|
|
|
it('returns the match', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
2020-11-04 22:04:48 +00:00
|
|
|
const results = memberRepository.search('ma');
|
2023-08-17 18:48:42 +00:00
|
|
|
assert.deepEqual(results, _toMembers([memberMahershala]));
|
2020-11-04 22:04:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('given a prefix-matching string on profile name', () => {
|
|
|
|
it('returns the match', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
2020-11-04 22:04:48 +00:00
|
|
|
const results = memberRepository.search('sr');
|
2023-08-17 18:48:42 +00:00
|
|
|
assert.deepEqual(results, _toMembers([memberShia]));
|
2020-11-04 22:04:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-08-17 18:55:25 +00:00
|
|
|
describe('given a prefix-matching string on name', () => {
|
|
|
|
it('returns the match', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
2021-08-17 18:55:25 +00:00
|
|
|
const results = memberRepository.search('dude');
|
2023-08-17 18:48:42 +00:00
|
|
|
assert.deepEqual(results, _toMembers([memberShia]));
|
2021-08-17 18:55:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-04 22:04:48 +00:00
|
|
|
describe('given a prefix-matching string on title', () => {
|
|
|
|
it('returns the match', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
2021-08-17 18:55:25 +00:00
|
|
|
const results = memberRepository.search('bud');
|
2023-08-17 18:48:42 +00:00
|
|
|
assert.deepEqual(results, _toMembers([memberShia]));
|
2020-11-04 22:04:48 +00:00
|
|
|
});
|
2021-08-17 18:55:25 +00:00
|
|
|
|
|
|
|
it('handles titles with Unicode bidi characters, which some contacts have', () => {
|
|
|
|
const memberShiaBidi: ConversationType = {
|
|
|
|
...memberShia,
|
|
|
|
title: '\u2086Buddyo\u2069',
|
|
|
|
};
|
|
|
|
const memberRepository = new MemberRepository([
|
|
|
|
memberMahershala,
|
|
|
|
memberShiaBidi,
|
|
|
|
]);
|
|
|
|
const results = memberRepository.search('bud');
|
2023-08-17 18:48:42 +00:00
|
|
|
assert.deepEqual(results, _toMembers([memberShiaBidi]));
|
2021-08-17 18:55:25 +00:00
|
|
|
});
|
2020-11-04 22:04:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('given a match in the middle of a name', () => {
|
|
|
|
it('returns zero matches', () => {
|
2023-08-17 18:48:42 +00:00
|
|
|
const memberRepository = new MemberRepository(conversations);
|
2020-11-04 22:04:48 +00:00
|
|
|
const results = memberRepository.search('e');
|
|
|
|
assert.deepEqual(results, []);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|