Skip to content

Commit

Permalink
add Messages to conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNi245 committed Nov 21, 2024
1 parent 14438b9 commit 6076931
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 165 deletions.
288 changes: 171 additions & 117 deletions packages/js-sdk/src/Dm3Sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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';
Expand All @@ -19,6 +22,8 @@ describe('Dm3Sdk', () => {
//Axios mock to mock the http requests
let axiosMock;

let deliveryService: MockDeliveryServiceProfile;

beforeEach(async () => {
alice = await mockUserProfile(
ethers.Wallet.createRandom(),
Expand All @@ -33,6 +38,11 @@ describe('Dm3Sdk', () => {
'test.io',
]);

deliveryService = await getMockDeliveryServiceProfile(
ethers.Wallet.createRandom(),
'http://localhost:3000',
);

axiosMock = new MockAdapter(axios);

//Mock BackendConnector HttpRequests
Expand Down Expand Up @@ -63,126 +73,170 @@ describe('Dm3Sdk', () => {
.reply(200);
});

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,
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');
const c = dm3.conversations.list;
expect(c.length).toBe(1);
expect(c[0].contact.name).toBe('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');
});
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,
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;
dm3;
console.log(c);
expect(c.length).toBe(2);
expect(c[0].contact.name).toBe('bob.eth');
expect(c[1].contact.name).toBe('karl.eth');
});

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,
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');
});
});

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');
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,
});

const msgFactory = MockMessageFactory(alice, bob, deliveryService);

const msg1 = await msgFactory.createMessage('Hi');

const c = await dm3.conversations.addConversation('bob.eth');
expect(c?.messages.list().length).toBe(0);
await c?.messages.addMessage('bob.eth', msg1);

expect(c?.messages.list().length).toBe(1);
expect(c?.messages.list()[0].envelop.message.message).toBe('Hi');
});
});
});
7 changes: 4 additions & 3 deletions packages/js-sdk/src/conversation/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ethers } from 'ethers';
import { ITLDResolver } from '../tld/nameService/ITLDResolver';
import { hydrateContract as hydrateContact } from './hydrate/hydrateContact';
import { Contact, Conversation, getEmptyContact } from './types';
import { Messages } from '../message/Messages';

export class Conversations {
private readonly provider: ethers.providers.JsonRpcProvider;
Expand Down Expand Up @@ -88,7 +89,7 @@ export class Conversations {

const newConversation: Conversation = {
//TODO change that once Message class has been implemented
messages: undefined as any,
messages: new Messages(this.storageApi, this),
contact: newContact,
};
//Set the new contact to the list
Expand All @@ -102,7 +103,7 @@ export class Conversations {
);

const hydratedConversation: Conversation = {
messages: undefined as any,
messages: new Messages(this.storageApi, this),
contact: hydratedContact,
};
//find existing contact and replace it with the hydrated one
Expand Down Expand Up @@ -131,7 +132,7 @@ export class Conversations {
this.addressEnsSubdomain,
);
const hydratedConversation: Conversation = {
messages: undefined as any,
messages: new Messages(this.storageApi, this),
contact: hydratedContact,
};
this.list.push(hydratedConversation);
Expand Down
Loading

0 comments on commit 6076931

Please sign in to comment.