From ffe9e26d764b04cc51241cd87775d47a90ba4933 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Mon, 9 Jan 2023 10:35:46 +1100 Subject: [PATCH] test: finish contact test but still missing the .set function --- .../libsession_wrapper_contacts_test.ts | 125 ++++++++++++++++-- 1 file changed, 117 insertions(+), 8 deletions(-) diff --git a/ts/test/session/unit/libsession_wrapper/libsession_wrapper_contacts_test.ts b/ts/test/session/unit/libsession_wrapper/libsession_wrapper_contacts_test.ts index 8b79cc02c..d4eff8842 100644 --- a/ts/test/session/unit/libsession_wrapper/libsession_wrapper_contacts_test.ts +++ b/ts/test/session/unit/libsession_wrapper/libsession_wrapper_contacts_test.ts @@ -1,19 +1,19 @@ import { expect } from 'chai'; -import { from_hex, from_string, to_hex } from 'libsodium-wrappers-sumo'; +import { from_hex, from_string } from 'libsodium-wrappers-sumo'; import * as SessionUtilWrapper from 'session_util_wrapper'; // tslint:disable: chai-vague-errors no-unused-expression no-http-string no-octal-literal whitespace describe('libsession_wrapper_contacts ', () => { - it('contacts', () => { - // Note: To run this test, you need to compile the libsession wrapper for node (and not for electron). - // To do this, you can cd to the node_module/libsession_wrapper folder and do - // yarn configure && yarn build - // once that is done, you can rename this file and remove the _skip suffix so that test is run. + // Note: To run this test, you need to compile the libsession wrapper for node (and not for electron). + // To do this, you can cd to the node_module/libsession_wrapper folder and do + // yarn configure && yarn build + // once that is done, you can rename this file and remove the _skip suffix so that test is run. - // We have to disable it by filename as nodejs tries to load the module during the import step above, and fails as it is not compiled for nodejs but for electron. + // We have to disable it by filename as nodejs tries to load the module during the import step above, and fails as it is not compiled for nodejs but for electron. + it('[config][contacts]', () => { const edSecretKey = from_hex( '0123456789abcdef0123456789abcdef000000000000000000000000000000004cb76fdc6d32278e3f83dbf608360ecc6b65727934b85d2fb86862ff98c46ab7' ); @@ -139,7 +139,7 @@ describe('libsession_wrapper_contacts ', () => { contacts2.setNickname(third_id, 'Nickname 3'); contacts2.setApproved(third_id, true); contacts2.setBlocked(third_id, true); - contacts2.setProfilePicture(third_id, 'http://example.com/huge.bmp', from_string('qwerty')); + contacts2.setProfilePic(third_id, 'http://example.com/huge.bmp', from_string('qwerty')); expect(contacts.needsPush()).to.be.true; expect(contacts2.needsPush()).to.be.true; @@ -180,4 +180,113 @@ describe('libsession_wrapper_contacts ', () => { expect(session_ids2).to.be.deep.eq([another_id, third_id]); expect(nicknames2).to.be.deep.eq(['(N/A)', 'Nickname 3']); }); + + it('[config][contacts][c]', () => { + const edSecretKey = from_hex( + '0123456789abcdef0123456789abcdef000000000000000000000000000000004cb76fdc6d32278e3f83dbf608360ecc6b65727934b85d2fb86862ff98c46ab7' + ); + + // Initialize a brand new, empty config because we have no dump data to deal with. + const contacts = new SessionUtilWrapper.ContactsConfigWrapper(edSecretKey, null); + + const real_id = '050000000000000000000000000000000000000000000000000000000000000000'; + + expect(contacts.get(real_id)).to.be.null; + const c = contacts.getOrCreate(real_id); + expect(c.id).to.be.eq(real_id); + expect(c.name).to.be.null; + expect(c.nickname).to.be.null; + expect(c.approved).to.be.false; + expect(c.approvedMe).to.be.false; + expect(c.blocked).to.be.false; + expect(c.profilePicture?.key).to.be.undefined; + expect(c.profilePicture?.url).to.be.undefined; + + c.name = 'Joe'; + c.nickname = 'Joey'; + c.approved = true; + c.approvedMe = true; + // contacts.set(c); // FIXME use .set when ready + contacts.setName(real_id, c.name); + contacts.setNickname(real_id, c.nickname); + contacts.setApproved(real_id, c.approved); + contacts.setApprovedMe(real_id, c.approvedMe); + + const c2 = contacts.getOrCreate(real_id); + expect(c2.name).to.be.eq('Joe'); + expect(c2.nickname).to.be.eq('Joey'); + expect(c2.approved).to.be.true; + expect(c2.approvedMe).to.be.true; + expect(c2.blocked).to.be.false; + expect(c2.profilePicture?.key).to.be.undefined; + expect(c2.profilePicture?.url).to.be.undefined; + + expect(contacts.needsDump()).to.be.true; + expect(contacts.needsPush()).to.be.true; + + let push1 = contacts.push(); + expect(push1.seqno).to.be.equal(1); + + const contacts2 = new SessionUtilWrapper.ContactsConfigWrapper(edSecretKey, null); + + let accepted = contacts2.merge([push1.data]); + expect(accepted).to.be.equal(1); + + contacts.confirmPushed(push1.seqno); + + let c3 = contacts2.getOrCreate(real_id); + expect(c3.name).to.be.eq('Joe'); + expect(c3.nickname).to.be.eq('Joey'); + expect(c3.approved).to.be.true; + expect(c3.approvedMe).to.be.true; + expect(c3.blocked).to.be.false; + expect(c3.profilePicture?.key).to.be.undefined; + expect(c3.profilePicture?.url).to.be.undefined; + + const another_id = '051111111111111111111111111111111111111111111111111111111111111111'; + c3 = contacts.getOrCreate(another_id); + expect(c3.name).to.be.null; + expect(c3.nickname).to.be.null; + expect(c3.approved).to.be.false; + expect(c3.approvedMe).to.be.false; + expect(c3.blocked).to.be.false; + + expect(c3.profilePicture?.key).to.be.undefined; + expect(c3.profilePicture?.url).to.be.undefined; + + contacts2.set(c3); + + let push2 = contacts2.push(); + accepted = contacts.merge([push2.data]); + expect(accepted).to.be.equal(1); + + const allContacts2 = contacts.getAll(); + const session_ids2 = allContacts2.map(m => m.id); + const nicknames2 = allContacts2.map(m => m.nickname || '(N/A)'); + + expect(session_ids2.length).to.be.eq(2); + expect(nicknames2.length).to.be.eq(2); + expect(session_ids2).to.be.deep.eq([real_id, another_id]); + expect(nicknames2).to.be.deep.eq(['Joey', '(N/A)']); + + // Changing things while iterating: + // no need to support this in JS for now. + + const allContacts3 = contacts.getAll(); + let deletions = 0; + let non_deletions = 0; + allContacts3.forEach(c => { + if (c.id !== real_id) { + contacts.erase(c.id); + deletions++; + } else { + non_deletions++; + } + }); + + expect(deletions).to.be.eq(1); + expect(non_deletions).to.be.eq(1); + expect(contacts.get(real_id)).to.exist; + expect(contacts.get(another_id)).to.be.null; + }); });