-
Notifications
You must be signed in to change notification settings - Fork 13k
test(federation): validate empty room state #37824
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
11 commits
Select commit
Hold shift + click to select a range
a39db7c
test: validate empty room state
sampaiodiego 61f0761
fix: update room uids and usernames
sampaiodiego 74ca426
test: add retry
sampaiodiego 75913f4
make TS happy
sampaiodiego 61bd649
test: create a new room per describe
sampaiodiego 7377796
chore: standardize DM subscription name generation
sampaiodiego 51eb8c6
test: add error handling to getSubscriptionByRoomId
sampaiodiego 6056128
test: add more checks to rooms.get response
sampaiodiego 2059988
test: add retry on async operations
sampaiodiego b522a2d
test: update getSubscriptionByRoomId to accept request parameter
ggazzo 3692b7a
fix empty room name
sampaiodiego 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
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,33 @@ | ||
| import type { AtLeast, IUser } from '@rocket.chat/core-typings'; | ||
|
|
||
| const getFname = (members: AtLeast<IUser, 'name' | 'username'>[]): string | undefined => { | ||
| if (members.length === 0) { | ||
| return; | ||
| } | ||
| return members.map(({ name, username }) => name || username).join(', '); | ||
| }; | ||
| const getName = (members: AtLeast<IUser, 'name' | 'username'>[]): string | undefined => { | ||
| if (members.length === 0) { | ||
| return; | ||
| } | ||
| return members.map(({ username }) => username).join(', '); | ||
| }; | ||
|
|
||
| type NameMap = { [userId: string]: { fname: string; name: string } }; | ||
|
|
||
| export function getNameForDMs(members: AtLeast<IUser, '_id' | 'name' | 'username'>[]): NameMap { | ||
| const nameMap: NameMap = {}; | ||
|
|
||
| const sortedMembers = members.sort((u1, u2) => (u1.name! || u1.username!).localeCompare(u2.name! || u2.username!)); | ||
|
|
||
| for (const member of sortedMembers) { | ||
| const otherMembers = sortedMembers.filter((m) => m._id !== member._id); | ||
|
|
||
| nameMap[member._id] = { | ||
| fname: getFname(otherMembers) || member.name || member.username || '', | ||
| name: getName(otherMembers) || member.username || '', | ||
| }; | ||
| } | ||
|
|
||
| return nameMap; | ||
| } | ||
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
128 changes: 128 additions & 0 deletions
128
apps/meteor/tests/unit/server/services/room/getNameForDMs.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,128 @@ | ||
| import { expect } from 'chai'; | ||
|
|
||
| import { getNameForDMs } from '../../../../../server/services/room/getNameForDMs'; | ||
|
|
||
| describe('getNameForDMs', () => { | ||
| it('should return empty object when members array is empty', () => { | ||
| const result = getNameForDMs([]); | ||
| expect(result).to.deep.equal({}); | ||
| }); | ||
|
|
||
| it('should return own name for single member', () => { | ||
| const members = [{ _id: 'user1', name: 'John Doe', username: 'john' }]; | ||
|
|
||
| const result = getNameForDMs(members); | ||
|
|
||
| expect(result).to.deep.equal({ | ||
| user1: { | ||
| fname: 'John Doe', | ||
| name: 'john', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should return name map for two members using name field', () => { | ||
| const members = [ | ||
| { _id: 'user1', name: 'John Doe', username: 'john' }, | ||
| { _id: 'user2', name: 'Jane Smith', username: 'jane' }, | ||
| ]; | ||
|
|
||
| const result = getNameForDMs(members); | ||
|
|
||
| expect(result).to.deep.equal({ | ||
| user1: { | ||
| fname: 'Jane Smith', | ||
| name: 'jane', | ||
| }, | ||
| user2: { | ||
| fname: 'John Doe', | ||
| name: 'john', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should fallback to username when name is not available', () => { | ||
| const members = [ | ||
| { _id: 'user1', name: '', username: 'john' }, | ||
| { _id: 'user2', name: '', username: 'jane' }, | ||
| ]; | ||
|
|
||
| const result = getNameForDMs(members); | ||
|
|
||
| expect(result).to.deep.equal({ | ||
| user1: { | ||
| fname: 'jane', | ||
| name: 'jane', | ||
| }, | ||
| user2: { | ||
| fname: 'john', | ||
| name: 'john', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should handle multiple members and sort them alphabetically', () => { | ||
| const members = [ | ||
| { _id: 'user3', name: 'Charlie Brown', username: 'charlie' }, | ||
| { _id: 'user1', name: 'Alice Wonder', username: 'alice' }, | ||
| { _id: 'user2', name: 'Bob Builder', username: 'bob' }, | ||
| ]; | ||
|
|
||
| const result = getNameForDMs(members); | ||
|
|
||
| expect(result).to.deep.equal({ | ||
| user1: { | ||
| fname: 'Bob Builder, Charlie Brown', | ||
| name: 'bob, charlie', | ||
| }, | ||
| user2: { | ||
| fname: 'Alice Wonder, Charlie Brown', | ||
| name: 'alice, charlie', | ||
| }, | ||
| user3: { | ||
| fname: 'Alice Wonder, Bob Builder', | ||
| name: 'alice, bob', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should handle mix of members with and without names', () => { | ||
| const members = [ | ||
| { _id: 'user1', name: 'Alice Wonder', username: 'alice' }, | ||
| { _id: 'user2', name: '', username: 'bob' }, | ||
| ]; | ||
|
|
||
| const result = getNameForDMs(members); | ||
|
|
||
| expect(result).to.deep.equal({ | ||
| user1: { | ||
| fname: 'bob', | ||
| name: 'bob', | ||
| }, | ||
| user2: { | ||
| fname: 'Alice Wonder', | ||
| name: 'alice', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should sort by username when names are empty', () => { | ||
| const members = [ | ||
| { _id: 'user1', name: '', username: 'zebra' }, | ||
| { _id: 'user2', name: '', username: 'alpha' }, | ||
| ]; | ||
|
|
||
| const result = getNameForDMs(members); | ||
|
|
||
| expect(result).to.deep.equal({ | ||
| user1: { | ||
| fname: 'alpha', | ||
| name: 'alpha', | ||
| }, | ||
| user2: { | ||
| fname: 'zebra', | ||
| name: 'zebra', | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
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.
Mutates input array and uses unsafe non-null assertions.
Two issues on this line:
members.sort()mutates the input array in place. If callers don't expect mutation, this could cause subtle bugs. Consider using[...members].sort()to sort a copy.The non-null assertions
u1.name!andu1.username!are unsafe—if bothnameandusernameareundefined(which is possible perIUsertype), the expression evaluates toundefinedandlocaleComparethrows.🔎 Suggested fix:
🤖 Prompt for AI Agents