-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1204 from dm3-org/add-conversation-test
Add basic Messages and Conversation API
- Loading branch information
Showing
19 changed files
with
763 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,237 @@ | ||
import { StorageAPI } from '@dm3-org/dm3-lib-storage'; | ||
import { | ||
getMockDeliveryServiceProfile, | ||
MockDeliveryServiceProfile, | ||
MockedUserProfile, | ||
MockMessageFactory, | ||
mockUserProfile, | ||
} from '@dm3-org/dm3-lib-test-helper'; | ||
import axios from 'axios'; | ||
import { ethers } from 'ethers'; | ||
import { Dm3Sdk, Dm3SdkConfig } from './Dm3Sdk'; | ||
import { StorageAPI } from '@dm3-org/dm3-lib-storage'; | ||
|
||
import MockAdapter from 'axios-mock-adapter'; | ||
import { normalizeEnsName } from '@dm3-org/dm3-lib-profile'; | ||
import { ITLDResolver } from './tld/nameService/ITLDResolver'; | ||
|
||
describe('Dm3Sdk', () => { | ||
let upController: ethers.Signer; | ||
let alice: MockedUserProfile; | ||
let bob: MockedUserProfile; | ||
let karl: MockedUserProfile; | ||
|
||
//Axios mock to mock the http requests | ||
let axiosMock; | ||
|
||
let deliveryService: MockDeliveryServiceProfile; | ||
|
||
beforeEach(async () => { | ||
upController = ethers.Wallet.createRandom(); | ||
alice = await mockUserProfile( | ||
ethers.Wallet.createRandom(), | ||
'alice.up', | ||
['test.io'], | ||
); | ||
bob = await mockUserProfile(ethers.Wallet.createRandom(), 'bob.up', [ | ||
'test.io', | ||
]); | ||
|
||
karl = await mockUserProfile(ethers.Wallet.createRandom(), 'karl.up', [ | ||
'test.io', | ||
]); | ||
|
||
deliveryService = await getMockDeliveryServiceProfile( | ||
ethers.Wallet.createRandom(), | ||
'http://localhost:3000', | ||
); | ||
axiosMock = new MockAdapter(axios); | ||
|
||
//Mock BackendConnector HttpRequests | ||
//Mock profileExistsOnDeliveryService | ||
axiosMock | ||
.onGet( | ||
`http://localhost:4060/profile/${normalizeEnsName( | ||
alice.address, | ||
)}.addr.test`, | ||
) | ||
.reply(200); | ||
//Mock getChallenge | ||
axiosMock | ||
.onGet( | ||
`http://localhost:4060/auth/${normalizeEnsName( | ||
alice.address, | ||
)}.addr.test`, | ||
) | ||
.reply(200, 'mock-challenge'); | ||
|
||
//Mock getToken | ||
axiosMock | ||
.onPost( | ||
`http://localhost:4060/auth/${normalizeEnsName( | ||
alice.address, | ||
)}.addr.test`, | ||
) | ||
.reply(200); | ||
}); | ||
|
||
it('test', async () => { | ||
const luksoProvider = () => ({ | ||
send: () => Promise.resolve([]), | ||
getSigner: () => Promise.resolve(upController), | ||
describe('conversations', () => { | ||
it('can add a conversation to the contact list', 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'); | ||
expect(dm3.conversations.list.length).toBe(1); | ||
expect(dm3.conversations.list[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'); | ||
|
||
expect(dm3.conversations.list.length).toBe(2); | ||
expect(dm3.conversations.list[0].contact.name).toBe('bob.eth'); | ||
expect(dm3.conversations.list[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'); | ||
expect(dm3.conversations.list.length).toBe(1); | ||
expect(dm3.conversations.list[0].contact.name).toBe('bob.eth'); | ||
}); | ||
}); | ||
|
||
describe('Messages', () => { | ||
it('can send a message', 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 () => {}, | ||
addMessage: 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, | ||
}); | ||
|
||
expect( | ||
(await dm3.conversations.addConversation('bob.eth'))?.messages | ||
.list.length, | ||
).toBe(0); | ||
|
||
const c = await dm3.conversations.addConversation('bob.eth'); | ||
|
||
await c?.messages.sendMessage('Hi'); | ||
expect(c?.messages.list.length).toBe(1); | ||
expect(c?.messages.list[0].envelop.message.message).toBe('Hi'); | ||
}); | ||
const mockConfig: Dm3SdkConfig = { | ||
mainnetProvider: {} as ethers.providers.JsonRpcProvider, | ||
lukso: luksoProvider as any, | ||
nonce: '1', | ||
defaultDeliveryService: 'test.io', | ||
addressEnsSubdomain: 'addr.test', | ||
userEnsSubdomain: 'user.test', | ||
resolverBackendUrl: 'resolver.io', | ||
backendUrl: 'backend.io', | ||
storageApi: {} as StorageAPI, | ||
}; | ||
|
||
// const dm3 = await new Dm3Sdk().universalProfileLogin(); | ||
// await dm3.conversations.addConversation('karl.eth'); | ||
// const c = dm3.conversations.list; | ||
// const karl = c[0]; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.