-
Notifications
You must be signed in to change notification settings - Fork 13.9k
fix: normalize the id used to key cached store records #42110
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
Open
Naab2k3
wants to merge
2
commits into
RocketChat:develop
Choose a base branch
from
Naab2k3:fix/normalize-cached-store-ids
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+192
−15
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Fixes rooms showing up more than once in the sidebar, caused by the cached stores keeping a separate entry for every merge of a record whose `_id` does not arrive as a string. |
121 changes: 121 additions & 0 deletions
121
apps/meteor/client/lib/cachedStores/DocumentMapStore.spec.ts
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,121 @@ | ||
| import { createDocumentMapStore, toRecordId } from './DocumentMapStore'; | ||
|
|
||
| const hexId = '6a7fc7b8376b837b4b8aa6c3'; | ||
| const binaryId = Uint8Array.from(hexId.match(/../g) ?? [], (byte) => parseInt(byte, 16)); | ||
|
|
||
| interface ITestRecord { | ||
| _id: string; | ||
| rid: string; | ||
| } | ||
|
|
||
| const fromBinary = { _id: binaryId as unknown as string, rid: hexId }; | ||
| const fromString = { _id: hexId, rid: hexId }; | ||
| const objectId = { toHexString: () => hexId }; | ||
|
|
||
| describe('cachedStores/DocumentMapStore', () => { | ||
| describe('toRecordId', () => { | ||
| it('should keep a string id as it is', () => { | ||
| expect(toRecordId(hexId)).toBe(hexId); | ||
| }); | ||
|
|
||
| it('should read the hexadecimal id out of a binary id', () => { | ||
| expect(toRecordId(binaryId)).toBe(hexId); | ||
| }); | ||
|
|
||
| it('should read the hexadecimal id out of an ObjectId', () => { | ||
| expect(toRecordId(objectId)).toBe(hexId); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createDocumentMapStore', () => { | ||
| it('should store a document carrying a binary id under its hexadecimal id', () => { | ||
| const store = createDocumentMapStore<ITestRecord>(); | ||
|
|
||
| store.getState().store(fromBinary); | ||
|
|
||
| expect([...store.getState().records.keys()]).toEqual([hexId]); | ||
| expect(store.getState().has(hexId)).toBe(true); | ||
| expect(store.getState().has(fromBinary._id)).toBe(true); | ||
| expect(store.getState().get(hexId)?.rid).toBe(hexId); | ||
| }); | ||
|
|
||
| it('should not store the same document twice when its id arrives in different representations', () => { | ||
| const store = createDocumentMapStore<ITestRecord>(); | ||
|
|
||
| store.getState().store(fromBinary); | ||
| store.getState().store(fromString); | ||
|
|
||
| expect(store.getState().records.size).toBe(1); | ||
| }); | ||
|
|
||
| it('should not duplicate a document when many records are stored at once', () => { | ||
| const store = createDocumentMapStore<ITestRecord>(); | ||
|
|
||
| store.getState().storeMany([fromBinary, fromString]); | ||
|
|
||
| expect(store.getState().records.size).toBe(1); | ||
| }); | ||
|
|
||
| it('should collapse the records of a cache written with mixed representations', () => { | ||
| const store = createDocumentMapStore<ITestRecord>(); | ||
|
|
||
| store.getState().replaceAll([fromString, fromBinary, fromBinary, fromString]); | ||
|
|
||
| expect(store.getState().records.size).toBe(1); | ||
| }); | ||
|
|
||
| it('should delete a document by any representation of its id', () => { | ||
| const store = createDocumentMapStore<ITestRecord>(); | ||
|
|
||
| store.getState().store(fromBinary); | ||
| store.getState().delete(fromBinary._id); | ||
|
|
||
| expect(store.getState().records.size).toBe(0); | ||
| }); | ||
|
|
||
| it('should merge a document carrying an ObjectId with the same document carrying a string id', () => { | ||
| const store = createDocumentMapStore<ITestRecord>(); | ||
|
|
||
| store.getState().store({ _id: objectId as unknown as string, rid: hexId }); | ||
| store.getState().store(fromString); | ||
|
|
||
| expect(store.getState().records.size).toBe(1); | ||
| expect([...store.getState().records.keys()]).toEqual([hexId]); | ||
| }); | ||
|
|
||
| it('should keep an updated document keyed by its hexadecimal id', () => { | ||
| const store = createDocumentMapStore<ITestRecord>(); | ||
|
|
||
| store.getState().store(fromBinary); | ||
| store.getState().update( | ||
| (record) => record.rid === hexId, | ||
| (record) => ({ ...record }), | ||
| ); | ||
|
|
||
| expect(store.getState().records.size).toBe(1); | ||
| expect([...store.getState().records.keys()]).toEqual([hexId]); | ||
| }); | ||
|
|
||
| it('should keep an asynchronously updated document keyed by its hexadecimal id', async () => { | ||
| const store = createDocumentMapStore<ITestRecord>(); | ||
|
|
||
| store.getState().store(fromBinary); | ||
| await store.getState().updateAsync( | ||
| (record) => record.rid === hexId, | ||
| async (record) => ({ ...record }), | ||
| ); | ||
|
|
||
| expect(store.getState().records.size).toBe(1); | ||
| expect([...store.getState().records.keys()]).toEqual([hexId]); | ||
| }); | ||
|
|
||
| it('should remove a document that was stored under a binary id', () => { | ||
| const store = createDocumentMapStore<ITestRecord>(); | ||
|
|
||
| store.getState().store(fromBinary); | ||
| store.getState().remove((record) => record.rid === hexId); | ||
|
|
||
| expect(store.getState().records.size).toBe(0); | ||
| }); | ||
| }); | ||
| }); | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.