From a147f288f34d45f9c81a129758e7ffe4bb9ad4c3 Mon Sep 17 00:00:00 2001 From: AlexNi245 Date: Tue, 19 Nov 2024 15:27:23 +0700 Subject: [PATCH] add more conversation test --- packages/js-sdk/src/Dm3Sdk.test.ts | 103 +++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 4 deletions(-) diff --git a/packages/js-sdk/src/Dm3Sdk.test.ts b/packages/js-sdk/src/Dm3Sdk.test.ts index 432dfc418..f8ebd0ca3 100644 --- a/packages/js-sdk/src/Dm3Sdk.test.ts +++ b/packages/js-sdk/src/Dm3Sdk.test.ts @@ -14,6 +14,7 @@ import { ITLDResolver } from './tld/nameService/ITLDResolver'; describe('Dm3Sdk', () => { let alice: MockedUserProfile; let bob: MockedUserProfile; + let karl: MockedUserProfile; //Axios mock to mock the http requests let axiosMock; @@ -27,10 +28,13 @@ describe('Dm3Sdk', () => { bob = await mockUserProfile(ethers.Wallet.createRandom(), 'bob.up', [ 'test.io', ]); - }); - it('can add a conversaton to the contact list', async () => { + karl = await mockUserProfile(ethers.Wallet.createRandom(), 'karl.up', [ + 'test.io', + ]); + axiosMock = new MockAdapter(axios); + //Mock BackendConnector HttpRequests //Mock profileExistsOnDeliveryService axiosMock @@ -40,7 +44,7 @@ describe('Dm3Sdk', () => { )}.addr.test`, ) .reply(200); - + //Mock getChallenge axiosMock .onGet( `http://localhost:4060/auth/${normalizeEnsName( @@ -49,14 +53,17 @@ describe('Dm3Sdk', () => { ) .reply(200, 'mock-challenge'); + //Mock getToken axiosMock .onPost( `http://localhost:4060/auth/${normalizeEnsName( alice.address, )}.addr.test`, ) - .reply(200, 'mock-challenge'); + .reply(200); + }); + it('can add a conversation to the contact list', async () => { const mockTldResolver = { resolveTLDtoAlias: async () => `${normalizeEnsName(bob.address)}.addr.test`, @@ -85,7 +92,95 @@ describe('Dm3Sdk', () => { await dm3.conversations.addConversation('bob.eth'); const c = dm3.conversations.list; + expect(c.length).toBe(1); + expect(c[0].contact.name).toBe('bob.eth'); + }); + it('can multiple conversations to the contact list', async () => { + const mockTldResolver = { + resolveTLDtoAlias: async (ensName: string) => { + if (ensName === 'alice.eth') { + return `${normalizeEnsName(alice.address)}.addr.test`; + } + if (ensName === 'bob.eth') { + return `${normalizeEnsName(bob.address)}.addr.test`; + } + return `${normalizeEnsName(karl.address)}.addr.test`; + }, + resolveAliasToTLD: async (ensName: string) => { + if ( + normalizeEnsName(ensName) === + normalizeEnsName(alice.address) + '.addr.test' + ) { + return 'alice.eth'; + } + if ( + normalizeEnsName(ensName) === + normalizeEnsName(bob.address) + '.addr.test' + ) { + return 'bob.eth'; + } + return 'karl.eth'; + }, + } as unknown as ITLDResolver; + + const mockConfig: Dm3SdkConfig = { + mainnetProvider: {} as ethers.providers.JsonRpcProvider, + storageApi: { + addConversation: async () => {}, + } as unknown as StorageAPI, + nonce: '1', + defaultDeliveryService: 'test.io', + addressEnsSubdomain: '.addr.test', + userEnsSubdomain: '.user.test', + resolverBackendUrl: 'resolver.io', + backendUrl: 'http://localhost:4060', + _tld: mockTldResolver, + }; + + const dm3 = await new Dm3Sdk(mockConfig).login({ + profileKeys: alice.profileKeys, + profile: alice.signedUserProfile, + accountAddress: alice.address, + }); + + await dm3.conversations.addConversation('bob.eth'); + await dm3.conversations.addConversation('karl.eth'); + const c = dm3.conversations.list; console.log(c); + expect(c.length).toBe(2); + expect(c[0].contact.name).toBe('bob.eth'); + expect(c[1].contact.name).toBe('karl.eth'); + }); + it('dont add duplicate conversations', async () => { + const mockTldResolver = { + resolveTLDtoAlias: async () => + `${normalizeEnsName(bob.address)}.addr.test`, + resolveAliasToTLD: async () => 'bob.eth', + } as unknown as ITLDResolver; + + const mockConfig: Dm3SdkConfig = { + mainnetProvider: {} as ethers.providers.JsonRpcProvider, + storageApi: { + addConversation: async () => {}, + } as unknown as StorageAPI, + nonce: '1', + defaultDeliveryService: 'test.io', + addressEnsSubdomain: '.addr.test', + userEnsSubdomain: '.user.test', + resolverBackendUrl: 'resolver.io', + backendUrl: 'http://localhost:4060', + _tld: mockTldResolver, + }; + + const dm3 = await new Dm3Sdk(mockConfig).login({ + profileKeys: alice.profileKeys, + profile: alice.signedUserProfile, + accountAddress: alice.address, + }); + + await dm3.conversations.addConversation('bob.eth'); + await dm3.conversations.addConversation('bob.eth'); + const c = dm3.conversations.list; expect(c.length).toBe(1); expect(c[0].contact.name).toBe('bob.eth'); });