-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CON-96] feat: add conversation caching and some tests #22
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
356b8f7
feat: add conversation caching and some tests
ttypic 7544fc6
feat: add delete method
ttypic 94ef340
feat: add release and delete methods
ttypic 7d20d57
feat: add message reactions SDK
ttypic f217bca
feat: add presence
ttypic 8b05e81
feat: provide auth header
ttypic 499a6ce
fix: server token details
ttypic b5ffd64
fix: mock reactions calls
ttypic d685187
feat: update demo
ttypic c166a9f
Merge branch 'add-tests-and-caching' into delete-message-sdk
ttypic 4b41890
Merge pull request #25 from ably-labs/delete-message-sdk
ttypic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Types } from 'ably/promises'; | ||
|
||
const MOCK_CLIENT_ID = 'MOCK_CLIENT_ID'; | ||
|
||
const mockPromisify = <T>(expectedReturnValue): Promise<T> => new Promise((resolve) => resolve(expectedReturnValue)); | ||
const methodReturningVoidPromise = () => mockPromisify<void>((() => {})()); | ||
|
||
function createMockPresence() { | ||
return { | ||
get: () => mockPromisify<Types.PresenceMessage[]>([]), | ||
update: () => mockPromisify<void>(undefined), | ||
enter: methodReturningVoidPromise, | ||
leave: methodReturningVoidPromise, | ||
subscriptions: { | ||
once: (_: unknown, fn: Function) => { | ||
fn(); | ||
}, | ||
}, | ||
subscribe: () => {}, | ||
unsubscribe: () => {}, | ||
}; | ||
} | ||
|
||
function createMockEmitter() { | ||
return { | ||
any: [], | ||
events: {}, | ||
anyOnce: [], | ||
eventsOnce: {}, | ||
}; | ||
} | ||
|
||
function createMockChannel() { | ||
return { | ||
presence: createMockPresence(), | ||
subscribe: () => {}, | ||
unsubscribe: () => {}, | ||
on: () => {}, | ||
off: () => {}, | ||
publish: () => {}, | ||
subscriptions: createMockEmitter(), | ||
}; | ||
} | ||
|
||
class MockRealtime { | ||
public channels: { | ||
get: () => ReturnType<typeof createMockChannel>; | ||
}; | ||
public auth: { | ||
clientId: string; | ||
}; | ||
public connection: { | ||
id?: string; | ||
state: string; | ||
}; | ||
|
||
public time() {} | ||
|
||
constructor() { | ||
this.channels = { | ||
get: (() => { | ||
const mockChannel = createMockChannel(); | ||
return () => mockChannel; | ||
})(), | ||
}; | ||
this.auth = { | ||
clientId: MOCK_CLIENT_ID, | ||
}; | ||
this.connection = { | ||
id: '1', | ||
state: 'connected', | ||
}; | ||
|
||
this['options'] = {}; | ||
} | ||
} | ||
|
||
export { MockRealtime as Realtime }; |
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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { beforeEach, describe, vi, it, expect } from 'vitest'; | ||
import { Realtime, Types } from 'ably/promises'; | ||
import { ChatApi } from './ChatApi.js'; | ||
import { Conversation } from './Conversation.js'; | ||
|
||
interface TestContext { | ||
realtime: Realtime; | ||
chatApi: ChatApi; | ||
emulateBackendPublish: Types.messageCallback<Partial<Types.Message>>; | ||
} | ||
|
||
vi.mock('ably/promises'); | ||
|
||
describe('Messages', () => { | ||
beforeEach<TestContext>((context) => { | ||
context.realtime = new Realtime({ clientId: 'clientId', key: 'key' }); | ||
context.chatApi = new ChatApi('clientId'); | ||
|
||
const channel = context.realtime.channels.get('conversationId'); | ||
vi.spyOn(channel, 'subscribe').mockImplementation( | ||
// @ts-ignore | ||
async (name: string, listener: Types.messageCallback<Types.Message>) => { | ||
context.emulateBackendPublish = listener; | ||
}, | ||
); | ||
}); | ||
|
||
describe('sending message', () => { | ||
it<TestContext>('should return message if chat backend request come before realtime', async (context) => { | ||
const { chatApi, realtime } = context; | ||
vi.spyOn(chatApi, 'sendMessage').mockResolvedValue({ id: 'messageId' }); | ||
|
||
const conversation = new Conversation('conversationId', realtime, chatApi); | ||
const messagePromise = conversation.messages.send('text'); | ||
|
||
context.emulateBackendPublish({ | ||
clientId: 'clientId', | ||
data: { | ||
id: 'messageId', | ||
content: 'text', | ||
client_id: 'clientId', | ||
}, | ||
}); | ||
|
||
const message = await messagePromise; | ||
|
||
expect(message).toContain({ | ||
id: 'messageId', | ||
content: 'text', | ||
client_id: 'clientId', | ||
}); | ||
}); | ||
|
||
it<TestContext>('should return message if chat backend request come after realtime', async (context) => { | ||
const { chatApi, realtime } = context; | ||
|
||
vi.spyOn(chatApi, 'sendMessage').mockImplementation(async (conversationId, text) => { | ||
context.emulateBackendPublish({ | ||
clientId: 'clientId', | ||
data: { | ||
id: 'messageId', | ||
content: text, | ||
client_id: 'clientId', | ||
}, | ||
}); | ||
return { id: 'messageId' }; | ||
}); | ||
|
||
const conversation = new Conversation('conversationId', realtime, chatApi); | ||
const message = await conversation.messages.send('text'); | ||
|
||
expect(message).toContain({ | ||
id: 'messageId', | ||
content: 'text', | ||
client_id: 'clientId', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('editing message', () => { | ||
it<TestContext>('should return message if chat backend request come before realtime', async (context) => { | ||
const { chatApi, realtime } = context; | ||
vi.spyOn(chatApi, 'editMessage').mockResolvedValue({ id: 'messageId' }); | ||
|
||
const conversation = new Conversation('conversationId', realtime, chatApi); | ||
const messagePromise = conversation.messages.edit('messageId', 'new_text'); | ||
|
||
context.emulateBackendPublish({ | ||
clientId: 'clientId', | ||
data: { | ||
id: 'messageId', | ||
content: 'new_text', | ||
client_id: 'clientId', | ||
}, | ||
}); | ||
|
||
const message = await messagePromise; | ||
|
||
expect(message).toContain({ | ||
id: 'messageId', | ||
content: 'new_text', | ||
client_id: 'clientId', | ||
}); | ||
}); | ||
|
||
it<TestContext>('should return message if chat backend request come after realtime', async (context) => { | ||
const { chatApi, realtime } = context; | ||
|
||
vi.spyOn(chatApi, 'editMessage').mockImplementation(async (conversationId, messageId, text) => { | ||
context.emulateBackendPublish({ | ||
clientId: 'clientId', | ||
data: { | ||
id: messageId, | ||
content: text, | ||
client_id: 'clientId', | ||
}, | ||
}); | ||
return { id: 'messageId' }; | ||
}); | ||
|
||
const conversation = new Conversation('conversationId', realtime, chatApi); | ||
const message = await conversation.messages.edit('messageId', 'new_text'); | ||
|
||
expect(message).toContain({ | ||
id: 'messageId', | ||
content: 'new_text', | ||
client_id: 'clientId', | ||
}); | ||
}); | ||
}); | ||
}); |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also have a
release
method to remove the conversation