|
| 1 | +import { StorageAPI } from '@dm3-org/dm3-lib-storage'; |
| 2 | +import { |
| 3 | + getMockDeliveryServiceProfile, |
| 4 | + MockDeliveryServiceProfile, |
| 5 | + MockedUserProfile, |
| 6 | + MockMessageFactory, |
| 7 | + mockUserProfile, |
| 8 | +} from '@dm3-org/dm3-lib-test-helper'; |
| 9 | +import axios from 'axios'; |
1 | 10 | import { ethers } from 'ethers';
|
2 | 11 | import { Dm3Sdk, Dm3SdkConfig } from './Dm3Sdk';
|
3 |
| -import { StorageAPI } from '@dm3-org/dm3-lib-storage'; |
| 12 | + |
| 13 | +import MockAdapter from 'axios-mock-adapter'; |
| 14 | +import { normalizeEnsName } from '@dm3-org/dm3-lib-profile'; |
| 15 | +import { ITLDResolver } from './tld/nameService/ITLDResolver'; |
4 | 16 |
|
5 | 17 | describe('Dm3Sdk', () => {
|
6 |
| - let upController: ethers.Signer; |
| 18 | + let alice: MockedUserProfile; |
| 19 | + let bob: MockedUserProfile; |
| 20 | + let karl: MockedUserProfile; |
| 21 | + |
| 22 | + //Axios mock to mock the http requests |
| 23 | + let axiosMock; |
| 24 | + |
| 25 | + let deliveryService: MockDeliveryServiceProfile; |
7 | 26 |
|
8 | 27 | beforeEach(async () => {
|
9 |
| - upController = ethers.Wallet.createRandom(); |
| 28 | + alice = await mockUserProfile( |
| 29 | + ethers.Wallet.createRandom(), |
| 30 | + 'alice.up', |
| 31 | + ['test.io'], |
| 32 | + ); |
| 33 | + bob = await mockUserProfile(ethers.Wallet.createRandom(), 'bob.up', [ |
| 34 | + 'test.io', |
| 35 | + ]); |
| 36 | + |
| 37 | + karl = await mockUserProfile(ethers.Wallet.createRandom(), 'karl.up', [ |
| 38 | + 'test.io', |
| 39 | + ]); |
| 40 | + |
| 41 | + deliveryService = await getMockDeliveryServiceProfile( |
| 42 | + ethers.Wallet.createRandom(), |
| 43 | + 'http://localhost:3000', |
| 44 | + ); |
| 45 | + axiosMock = new MockAdapter(axios); |
| 46 | + |
| 47 | + //Mock BackendConnector HttpRequests |
| 48 | + //Mock profileExistsOnDeliveryService |
| 49 | + axiosMock |
| 50 | + .onGet( |
| 51 | + `http://localhost:4060/profile/${normalizeEnsName( |
| 52 | + alice.address, |
| 53 | + )}.addr.test`, |
| 54 | + ) |
| 55 | + .reply(200); |
| 56 | + //Mock getChallenge |
| 57 | + axiosMock |
| 58 | + .onGet( |
| 59 | + `http://localhost:4060/auth/${normalizeEnsName( |
| 60 | + alice.address, |
| 61 | + )}.addr.test`, |
| 62 | + ) |
| 63 | + .reply(200, 'mock-challenge'); |
| 64 | + |
| 65 | + //Mock getToken |
| 66 | + axiosMock |
| 67 | + .onPost( |
| 68 | + `http://localhost:4060/auth/${normalizeEnsName( |
| 69 | + alice.address, |
| 70 | + )}.addr.test`, |
| 71 | + ) |
| 72 | + .reply(200); |
10 | 73 | });
|
11 | 74 |
|
12 |
| - it('test', async () => { |
13 |
| - const luksoProvider = () => ({ |
14 |
| - send: () => Promise.resolve([]), |
15 |
| - getSigner: () => Promise.resolve(upController), |
| 75 | + describe('conversations', () => { |
| 76 | + it('can add a conversation to the contact list', async () => { |
| 77 | + const mockTldResolver = { |
| 78 | + resolveTLDtoAlias: async () => |
| 79 | + `${normalizeEnsName(bob.address)}.addr.test`, |
| 80 | + resolveAliasToTLD: async () => 'bob.eth', |
| 81 | + } as unknown as ITLDResolver; |
| 82 | + |
| 83 | + const mockConfig: Dm3SdkConfig = { |
| 84 | + mainnetProvider: {} as ethers.providers.JsonRpcProvider, |
| 85 | + storageApi: { |
| 86 | + addConversation: async () => {}, |
| 87 | + } as unknown as StorageAPI, |
| 88 | + nonce: '1', |
| 89 | + defaultDeliveryService: 'test.io', |
| 90 | + addressEnsSubdomain: '.addr.test', |
| 91 | + userEnsSubdomain: '.user.test', |
| 92 | + resolverBackendUrl: 'resolver.io', |
| 93 | + backendUrl: 'http://localhost:4060', |
| 94 | + _tld: mockTldResolver, |
| 95 | + }; |
| 96 | + |
| 97 | + const dm3 = await new Dm3Sdk(mockConfig).login({ |
| 98 | + profileKeys: alice.profileKeys, |
| 99 | + profile: alice.signedUserProfile, |
| 100 | + accountAddress: alice.address, |
| 101 | + }); |
| 102 | + |
| 103 | + await dm3.conversations.addConversation('bob.eth'); |
| 104 | + expect(dm3.conversations.list.length).toBe(1); |
| 105 | + expect(dm3.conversations.list[0].contact.name).toBe('bob.eth'); |
| 106 | + }); |
| 107 | + it('can multiple conversations to the contact list', async () => { |
| 108 | + const mockTldResolver = { |
| 109 | + resolveTLDtoAlias: async (ensName: string) => { |
| 110 | + if (ensName === 'alice.eth') { |
| 111 | + return `${normalizeEnsName(alice.address)}.addr.test`; |
| 112 | + } |
| 113 | + if (ensName === 'bob.eth') { |
| 114 | + return `${normalizeEnsName(bob.address)}.addr.test`; |
| 115 | + } |
| 116 | + return `${normalizeEnsName(karl.address)}.addr.test`; |
| 117 | + }, |
| 118 | + resolveAliasToTLD: async (ensName: string) => { |
| 119 | + if ( |
| 120 | + normalizeEnsName(ensName) === |
| 121 | + normalizeEnsName(alice.address) + '.addr.test' |
| 122 | + ) { |
| 123 | + return 'alice.eth'; |
| 124 | + } |
| 125 | + if ( |
| 126 | + normalizeEnsName(ensName) === |
| 127 | + normalizeEnsName(bob.address) + '.addr.test' |
| 128 | + ) { |
| 129 | + return 'bob.eth'; |
| 130 | + } |
| 131 | + return 'karl.eth'; |
| 132 | + }, |
| 133 | + } as unknown as ITLDResolver; |
| 134 | + |
| 135 | + const mockConfig: Dm3SdkConfig = { |
| 136 | + mainnetProvider: {} as ethers.providers.JsonRpcProvider, |
| 137 | + storageApi: { |
| 138 | + addConversation: async () => {}, |
| 139 | + } as unknown as StorageAPI, |
| 140 | + nonce: '1', |
| 141 | + defaultDeliveryService: 'test.io', |
| 142 | + addressEnsSubdomain: '.addr.test', |
| 143 | + userEnsSubdomain: '.user.test', |
| 144 | + resolverBackendUrl: 'resolver.io', |
| 145 | + backendUrl: 'http://localhost:4060', |
| 146 | + _tld: mockTldResolver, |
| 147 | + }; |
| 148 | + |
| 149 | + const dm3 = await new Dm3Sdk(mockConfig).login({ |
| 150 | + profileKeys: alice.profileKeys, |
| 151 | + profile: alice.signedUserProfile, |
| 152 | + accountAddress: alice.address, |
| 153 | + }); |
| 154 | + |
| 155 | + await dm3.conversations.addConversation('bob.eth'); |
| 156 | + await dm3.conversations.addConversation('karl.eth'); |
| 157 | + |
| 158 | + expect(dm3.conversations.list.length).toBe(2); |
| 159 | + expect(dm3.conversations.list[0].contact.name).toBe('bob.eth'); |
| 160 | + expect(dm3.conversations.list[1].contact.name).toBe('karl.eth'); |
| 161 | + }); |
| 162 | + it('dont add duplicate conversations', async () => { |
| 163 | + const mockTldResolver = { |
| 164 | + resolveTLDtoAlias: async () => |
| 165 | + `${normalizeEnsName(bob.address)}.addr.test`, |
| 166 | + resolveAliasToTLD: async () => 'bob.eth', |
| 167 | + } as unknown as ITLDResolver; |
| 168 | + |
| 169 | + const mockConfig: Dm3SdkConfig = { |
| 170 | + mainnetProvider: {} as ethers.providers.JsonRpcProvider, |
| 171 | + storageApi: { |
| 172 | + addConversation: async () => {}, |
| 173 | + } as unknown as StorageAPI, |
| 174 | + nonce: '1', |
| 175 | + defaultDeliveryService: 'test.io', |
| 176 | + addressEnsSubdomain: '.addr.test', |
| 177 | + userEnsSubdomain: '.user.test', |
| 178 | + resolverBackendUrl: 'resolver.io', |
| 179 | + backendUrl: 'http://localhost:4060', |
| 180 | + _tld: mockTldResolver, |
| 181 | + }; |
| 182 | + |
| 183 | + const dm3 = await new Dm3Sdk(mockConfig).login({ |
| 184 | + profileKeys: alice.profileKeys, |
| 185 | + profile: alice.signedUserProfile, |
| 186 | + accountAddress: alice.address, |
| 187 | + }); |
| 188 | + |
| 189 | + await dm3.conversations.addConversation('bob.eth'); |
| 190 | + await dm3.conversations.addConversation('bob.eth'); |
| 191 | + expect(dm3.conversations.list.length).toBe(1); |
| 192 | + expect(dm3.conversations.list[0].contact.name).toBe('bob.eth'); |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + describe('Messages', () => { |
| 197 | + it('can send a message', async () => { |
| 198 | + const mockTldResolver = { |
| 199 | + resolveTLDtoAlias: async () => |
| 200 | + `${normalizeEnsName(bob.address)}.addr.test`, |
| 201 | + resolveAliasToTLD: async () => 'bob.eth', |
| 202 | + } as unknown as ITLDResolver; |
| 203 | + |
| 204 | + const mockConfig: Dm3SdkConfig = { |
| 205 | + mainnetProvider: {} as ethers.providers.JsonRpcProvider, |
| 206 | + storageApi: { |
| 207 | + addConversation: async () => {}, |
| 208 | + addMessage: async () => {}, |
| 209 | + } as unknown as StorageAPI, |
| 210 | + nonce: '1', |
| 211 | + defaultDeliveryService: 'test.io', |
| 212 | + addressEnsSubdomain: '.addr.test', |
| 213 | + userEnsSubdomain: '.user.test', |
| 214 | + resolverBackendUrl: 'resolver.io', |
| 215 | + backendUrl: 'http://localhost:4060', |
| 216 | + _tld: mockTldResolver, |
| 217 | + }; |
| 218 | + |
| 219 | + const dm3 = await new Dm3Sdk(mockConfig).login({ |
| 220 | + profileKeys: alice.profileKeys, |
| 221 | + profile: alice.signedUserProfile, |
| 222 | + accountAddress: alice.address, |
| 223 | + }); |
| 224 | + |
| 225 | + expect( |
| 226 | + (await dm3.conversations.addConversation('bob.eth'))?.messages |
| 227 | + .list.length, |
| 228 | + ).toBe(0); |
| 229 | + |
| 230 | + const c = await dm3.conversations.addConversation('bob.eth'); |
| 231 | + |
| 232 | + await c?.messages.sendMessage('Hi'); |
| 233 | + expect(c?.messages.list.length).toBe(1); |
| 234 | + expect(c?.messages.list[0].envelop.message.message).toBe('Hi'); |
16 | 235 | });
|
17 |
| - const mockConfig: Dm3SdkConfig = { |
18 |
| - mainnetProvider: {} as ethers.providers.JsonRpcProvider, |
19 |
| - lukso: luksoProvider as any, |
20 |
| - nonce: '1', |
21 |
| - defaultDeliveryService: 'test.io', |
22 |
| - addressEnsSubdomain: 'addr.test', |
23 |
| - userEnsSubdomain: 'user.test', |
24 |
| - resolverBackendUrl: 'resolver.io', |
25 |
| - backendUrl: 'backend.io', |
26 |
| - storageApi: {} as StorageAPI, |
27 |
| - }; |
28 |
| - |
29 |
| - // const dm3 = await new Dm3Sdk().universalProfileLogin(); |
30 |
| - // await dm3.conversations.addConversation('karl.eth'); |
31 |
| - // const c = dm3.conversations.list; |
32 |
| - // const karl = c[0]; |
33 | 236 | });
|
34 | 237 | });
|
0 commit comments