Skip to content

Commit

Permalink
add more conversation test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNi245 committed Nov 19, 2024
1 parent dc84d86 commit a147f28
Showing 1 changed file with 99 additions and 4 deletions.
103 changes: 99 additions & 4 deletions packages/js-sdk/src/Dm3Sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -40,7 +44,7 @@ describe('Dm3Sdk', () => {
)}.addr.test`,
)
.reply(200);

//Mock getChallenge
axiosMock
.onGet(
`http://localhost:4060/auth/${normalizeEnsName(
Expand All @@ -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`,
Expand Down Expand Up @@ -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');
});
Expand Down

0 comments on commit a147f28

Please sign in to comment.