Repository navigation
chore: migrate loadNextMessages and loadSurroundingMessages methods to rooms.history rest #7793
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
065b8d2
chore: migrate loadNextMessages DDP method to GET rooms.history
Rohit3523 aeb4ca1
test: assert the Newer Loader row in one expectation
Rohit3523 9fd627b
fix: return rooms.history _updatedAt as a Date so thread rows keep up…
Rohit3523 ade7e2d
fix: convert a string _updatedAt to a Date in normalizeMessage
Rohit3523 1b3f8ed
test: cover an ISO string _updatedAt through updateMessages with the …
Rohit3523 910d865
fix: convert _updatedAt to a Date in getNextMessages instead of norma…
Rohit3523 2d9458e
refactor: make loadNextMessages an async function
Rohit3523 d699dd1
Merge branch 'develop' into chore/migrate-load-next-messages-rest
Rohit3523 e31f8d6
chore: format the rooms.history type and import auxStore by alias in …
Rohit3523 ae5b15e
refactor: use getTime() for the rooms.history cursor instead of tsToMs
Rohit3523 cb82efc
Merge branch 'develop' into chore/migrate-load-next-messages-rest
Rohit3523 2d3c67c
chore: remove duplicate ejson import and pass runSlashCommand params …
Rohit3523 b0ffcee
revert: restore runSlashCommand positional params
Rohit3523 f33ff41
chore: migrate loadSurroundingMessages DDP method to GET rooms.history
Rohit3523 b1bfb04
fix: keep the loadNextMessages DDP argument unchanged and offset only…
Rohit3523 f87815e
refactor: type the loadSurroundingMessages dep as IMessage[] and drop…
Rohit3523 31203fc
refactor: share one rooms.history helper between the wrappers and rej…
Rohit3523 50770da
fix: request thread replies from rooms.history so the loaders store t…
Rohit3523 86feebe
refactor: name the newest loaded timestamp behind the rooms.history n…
Rohit3523 460c2f2
test: pin the jump cancel when the server Chunk fails to load and use…
Rohit3523 2abbd9c
Merge branch 'develop' into chore/migrate-load-next-messages-rest
Rohit3523 28012ee
fix: keep milliseconds on the Older Loader timestamp so Load more can…
Rohit3523 3424df6
Merge branch 'develop' into chore/migrate-load-next-messages-rest
diegolmello 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 hidden or 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 hidden or 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,120 @@ | ||
| import { loadNextMessages } from '../loadNextMessages'; | ||
| import { getNextMessages } from '../../services/restApi'; | ||
| import { getMessageById } from '../../database/services/Message'; | ||
| import updateMessages from '../updateMessages'; | ||
| import log from '../helpers/log'; | ||
| import { MessageTypeLoad } from '../../constants/messageTypeLoad'; | ||
|
|
||
| jest.mock('../../services/restApi', () => ({ | ||
| getNextMessages: jest.fn() | ||
| })); | ||
|
|
||
| jest.mock('../../database/services/Message', () => ({ | ||
| getMessageById: jest.fn() | ||
| })); | ||
|
|
||
| jest.mock('../updateMessages', () => jest.fn()); | ||
|
|
||
| jest.mock('../helpers/log', () => ({ | ||
| __esModule: true, | ||
| default: jest.fn() | ||
| })); | ||
|
|
||
| const mockedGetNextMessages = getNextMessages as jest.MockedFunction<typeof getNextMessages>; | ||
| const mockedGetMessageById = getMessageById as jest.MockedFunction<typeof getMessageById>; | ||
| const mockedUpdateMessages = updateMessages as jest.MockedFunction<typeof updateMessages>; | ||
| const mockedLog = log as jest.MockedFunction<typeof log>; | ||
|
|
||
| const RID = 'ROOM_ID'; | ||
| const TS = new Date(Date.UTC(2024, 0, 1, 12, 0, 0)); | ||
| const COUNT = 50; | ||
| const LOADER_ITEM = { id: 'tapped-loader' } as any; | ||
|
|
||
| const buildMessages = (length: number) => | ||
| Array.from( | ||
| { length }, | ||
| (_, index) => ({ _id: `msg-${index + 1}`, rid: RID, ts: new Date(TS.getTime() + (index + 1) * 1000) }) as any | ||
| ); | ||
|
|
||
| const serve = (messages: any[], hasMore: boolean) => mockedGetNextMessages.mockResolvedValue({ messages, hasMore }); | ||
|
|
||
| describe('loadNextMessages', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockedGetMessageById.mockResolvedValue(null as never); | ||
| mockedUpdateMessages.mockResolvedValue(0 as never); | ||
| }); | ||
|
|
||
| it('asks for the messages newer than the loader', async () => { | ||
| serve([], false); | ||
|
|
||
| await loadNextMessages({ rid: RID, ts: TS, loaderItem: LOADER_ITEM }); | ||
|
|
||
| expect(mockedGetNextMessages).toHaveBeenCalledWith({ rid: RID, after: TS, count: COUNT }); | ||
| }); | ||
|
|
||
| it('removes the Newer Loader when the server returns nothing newer', async () => { | ||
| serve([], false); | ||
|
|
||
| await expect(loadNextMessages({ rid: RID, ts: TS, loaderItem: LOADER_ITEM })).resolves.toBeUndefined(); | ||
|
|
||
| expect(mockedUpdateMessages).toHaveBeenCalledTimes(1); | ||
| expect(mockedUpdateMessages).toHaveBeenCalledWith({ rid: RID, update: [], remove: [{ _id: 'tapped-loader' }] }); | ||
| }); | ||
|
|
||
| it('delegates a full response without adding a Newer Loader when the server reports nothing newer', async () => { | ||
| const messages = buildMessages(COUNT); | ||
| serve(messages, false); | ||
|
|
||
| await loadNextMessages({ rid: RID, ts: TS, loaderItem: LOADER_ITEM }); | ||
|
|
||
| expect(mockedGetMessageById).not.toHaveBeenCalled(); | ||
| expect(mockedUpdateMessages).toHaveBeenCalledTimes(1); | ||
| expect(mockedUpdateMessages).toHaveBeenCalledWith({ rid: RID, update: messages, loaderItem: LOADER_ITEM }); | ||
| }); | ||
|
|
||
| it('adds no Newer Loader when more exist but the newest message is already local', async () => { | ||
| const messages = buildMessages(COUNT); | ||
| serve(messages, true); | ||
| mockedGetMessageById.mockResolvedValue({ id: 'msg-50' } as never); | ||
|
|
||
| await loadNextMessages({ rid: RID, ts: TS, loaderItem: LOADER_ITEM }); | ||
|
|
||
| expect(mockedUpdateMessages).toHaveBeenCalledWith({ rid: RID, update: messages, loaderItem: LOADER_ITEM }); | ||
| }); | ||
|
|
||
| it('appends a Newer Loader one millisecond after the newest message when more exist and it is not local', async () => { | ||
| const messages = buildMessages(COUNT); | ||
| serve(messages, true); | ||
|
|
||
| await loadNextMessages({ rid: RID, ts: TS, loaderItem: LOADER_ITEM }); | ||
|
|
||
| expect(mockedGetMessageById).toHaveBeenCalledWith('msg-50'); | ||
| expect(mockedUpdateMessages.mock.calls[0][0].update).toEqual([ | ||
| ...messages, | ||
| { _id: 'load-more-msg-50', rid: RID, t: MessageTypeLoad.NEXT_CHUNK, ts: new Date(messages[COUNT - 1].ts.getTime() + 1) } | ||
| ]); | ||
| }); | ||
|
|
||
| it('stores a newest-first response oldest-first', async () => { | ||
| const newestFirst = buildMessages(COUNT) | ||
| .reverse() | ||
| .map(message => ({ ...message, ts: message.ts.toISOString() })); | ||
| serve(newestFirst, false); | ||
|
|
||
| await loadNextMessages({ rid: RID, ts: TS, loaderItem: LOADER_ITEM }); | ||
|
|
||
| const update = mockedUpdateMessages.mock.calls[0][0].update as any[]; | ||
| expect(update.map(m => m._id)).toEqual(buildMessages(COUNT).map(m => m._id)); | ||
| }); | ||
|
|
||
| it('logs and rejects with the same error when loading fails', async () => { | ||
| const error = new Error('boom'); | ||
| mockedGetNextMessages.mockRejectedValue(error); | ||
|
|
||
| await expect(loadNextMessages({ rid: RID, ts: TS, loaderItem: LOADER_ITEM })).rejects.toBe(error); | ||
|
|
||
| expect(mockedLog).toHaveBeenCalledWith(error); | ||
| expect(mockedUpdateMessages).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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.
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.
Minor: these new tests go into a file outside
__tests__, but this PR already movedloadNextMessages.test.tsintoapp/lib/methods/__tests__/. Could this file move too?