diff --git a/ts/session/utils/libsession/libsession_utils_contacts.ts b/ts/session/utils/libsession/libsession_utils_contacts.ts index cfae3911f..072873572 100644 --- a/ts/session/utils/libsession/libsession_utils_contacts.ts +++ b/ts/session/utils/libsession/libsession_utils_contacts.ts @@ -1,4 +1,4 @@ -import { ContactInfo } from 'libsession_util_nodejs'; +import { ContactInfo, ContactInfoSet } from 'libsession_util_nodejs'; import { ConversationModel } from '../../../models/conversation'; import { getContactInfoFromDBValues } from '../../../types/sqlSharedTypes'; import { ContactsWrapperActions } from '../../../webworker/workers/browser/libsession_worker_interface'; @@ -41,14 +41,16 @@ function isContactToStoreInWrapper(convo: ConversationModel): boolean { * Fetches the specified convo and updates the required field in the wrapper. * If that contact does not exist in the wrapper, it is created before being updated. */ -async function insertContactFromDBIntoWrapperAndRefresh(id: string): Promise { +async function insertContactFromDBIntoWrapperAndRefresh( + id: string +): Promise { const foundConvo = getConversationController().get(id); if (!foundConvo) { - return; + return null; } - if (!isContactToStoreInWrapper(foundConvo)) { - return; + if (!SessionUtilContact.isContactToStoreInWrapper(foundConvo)) { + return null; } const dbName = foundConvo.get('displayNameInProfile') || undefined; @@ -59,6 +61,7 @@ async function insertContactFromDBIntoWrapperAndRefresh(id: string): Promise { - describe('filter contacts for wrapper', () => { - const ourNumber = '051234567890acbdef'; - const validArgs = { - id: '050123456789abcdef050123456789abcdef0123456789abcdef050123456789ab', - type: ConversationTypeEnum.PRIVATE, - isApproved: true, - active_at: 123, - didApproveMe: true, - }; - beforeEach(() => { - Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourNumber); - }); - afterEach(() => { - Sinon.restore(); - }); + stubWindowLog(); + + const getLatestTimestampOffset = 200000; + const ourNumber = '051234567890acbdef'; + const validArgs = { + id: '050123456789abcdef050123456789abcdef0123456789abcdef050123456789ab', + type: ConversationTypeEnum.PRIVATE, + isApproved: true, + active_at: 123, + didApproveMe: true, + }; + beforeEach(() => { + Sinon.stub(GetNetworkTime, 'getLatestTimestampOffset').returns(getLatestTimestampOffset); + Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourNumber); + }); + afterEach(() => { + Sinon.restore(); + }); + + describe('isContactToStoreInWrapper', () => { it('excludes ourselves', () => { expect( SessionUtilContact.isContactToStoreInWrapper( @@ -87,7 +96,7 @@ describe('libsession_contacts', () => { ).to.be.eq(false); }); - it('excludes non approved by us nor did approveme and not active', () => { + it('excludes non approved by us nor did approveMe and not active', () => { expect( SessionUtilContact.isContactToStoreInWrapper( new ConversationModel({ @@ -100,7 +109,7 @@ describe('libsession_contacts', () => { ).to.be.eq(false); }); - it('includes non approved by us nor did approveme but active', () => { + it('includes non approved by us nor did approveMe but active', () => { expect( SessionUtilContact.isContactToStoreInWrapper( new ConversationModel({ @@ -201,4 +210,116 @@ describe('libsession_contacts', () => { ).to.be.eq(true); }); }); + + describe('insertContactFromDBIntoWrapperAndRefresh', () => { + const contactArgs = { + displayNameInProfile: 'Tester', + nickname: 'Testie', + avatarPointer: 'http://filev2.abcdef.com/file/abcdefghijklmnop', + profileKey: 'profileKey', + isBlocked: () => false, + expirationMode: 'off', + expireTimer: 0, + }; + + const contact = new ConversationModel({ + ...validArgs, + ...contactArgs, + id: ourNumber, + } as any); + Sinon.stub(getConversationController(), 'get').returns(contact); + Sinon.stub(SessionUtilContact, 'isContactToStoreInWrapper').returns(true); + Sinon.stub(ContactsWrapperActions, 'set').resolves(); + + it('the returned wrapper values matche with the inputted contact', async () => { + const wrapperContact = await SessionUtilContact.insertContactFromDBIntoWrapperAndRefresh( + ourNumber + ); + + expect(wrapperContact, 'something should be returned from the wrapper').to.not.be.null; + if (!wrapperContact) { + throw Error('something should be returned from the wrapper'); + } + + expect(wrapperContact.id, 'id in the wrapper should match the inputted contact').to.equal( + contact.id + ); + expect( + wrapperContact.approved, + 'approved in the wrapper should match the inputted contact' + ).to.equal(contact.isApproved()); + expect( + wrapperContact.approvedMe, + 'approvedMe in the wrapper should match the inputted contact' + ).to.equal(contact.didApproveMe()); + expect( + wrapperContact.blocked, + 'blocked in the wrapper should match the inputted contact' + ).to.equal(contact.isBlocked()); + expect( + wrapperContact.priority, + 'priority in the wrapper should match the inputted contact' + ).to.equal(contact.get('priority')); + expect( + wrapperContact.nickname, + 'nickname in the wrapper should match the inputted contact' + ).to.equal(contact.get('nickname')); + expect(wrapperContact.name, 'name in the wrapper should match the inputted contact').to.equal( + contact.get('displayNameInProfile') + ); + expect( + wrapperContact.expirationMode, + 'expirationMode in the wrapper should match the inputted contact' + ).to.equal(contact.get('expirationMode')); + expect( + wrapperContact.expirationTimerSeconds, + 'expirationTimerSeconds in the wrapper should match the inputted contact' + ).to.equal(contact.get('expireTimer')); + }); + it('if disappearing messages is on then the wrapper returned values should match the inputted contact', async () => { + const wrapperContact = await SessionUtilContact.insertContactFromDBIntoWrapperAndRefresh( + ourNumber + ); + + expect(wrapperContact, 'something should be returned from the wrapper').to.not.be.null; + if (!wrapperContact) { + throw Error('something should be returned from the wrapper'); + } + + expect(wrapperContact.id, 'id in the wrapper should match the inputted contact').to.equal( + contact.id + ); + expect( + wrapperContact.approved, + 'approved in the wrapper should match the inputted contact' + ).to.equal(contact.isApproved()); + expect( + wrapperContact.approvedMe, + 'approvedMe in the wrapper should match the inputted contact' + ).to.equal(contact.didApproveMe()); + expect( + wrapperContact.blocked, + 'blocked in the wrapper should match the inputted contact' + ).to.equal(contact.isBlocked()); + expect( + wrapperContact.priority, + 'priority in the wrapper should match the inputted contact' + ).to.equal(contact.get('priority')); + expect( + wrapperContact.nickname, + 'nickname in the wrapper should match the inputted contact' + ).to.equal(contact.get('nickname')); + expect(wrapperContact.name, 'name in the wrapper should match the inputted contact').to.equal( + contact.get('displayNameInProfile') + ); + expect( + wrapperContact.expirationMode, + 'expirationMode in the wrapper should match the inputted contact' + ).to.equal(contact.get('expirationMode')); + expect( + wrapperContact.expirationTimerSeconds, + 'expirationTimerSeconds in the wrapper should match the inputted contact' + ).to.equal(contact.get('expireTimer')); + }); + }); });