Skip to content

Commit

Permalink
test reproducing issue #3869
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Aug 10, 2023
1 parent dbb4369 commit 7bdb5e2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
79 changes: 79 additions & 0 deletions protocol/communities_messenger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2666,6 +2666,85 @@ func (s *MessengerCommunitiesSuite) TestSyncCommunity_Leave() {
s.Equal(aCom, aoCom)
}

func (s *MessengerCommunitiesSuite) sendTestMessage(sender *Messenger, chatId string, messageText string) string {
message := &common.Message{}
message.ContentType = protobuf.ChatMessage_TEXT_PLAIN
message.ChatId = chatId
message.Text = messageText

response, err := sender.SendChatMessage(context.Background(), message)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Messages(), 1)

return response.Messages()[0].ID
}

func (s *MessengerCommunitiesSuite) TestSyncCommunity_Messaging() {

admin1 := s.admin
admin2 := s.createOtherDevice(admin1)
user1 := s.alice

// Pair Admin devices
PairDevices(&s.Suite, admin1, admin2)
PairDevices(&s.Suite, admin2, admin1)

// Create community
community, chat := createCommunity(&s.Suite, admin1)
joinRequest := &requests.RequestToJoinCommunity{CommunityID: community.ID()}

// Make sure Admin-2 receives the created community
response, err := WaitOnMessengerResponse(admin2, func(r *MessengerResponse) bool {
return len(r.Communities()) == 1
}, "community not received")

s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1)
s.Require().Equal(response.Communities()[0].ID(), community.ID())

// User 1 joins community
advertiseCommunityTo(&s.Suite, community, admin1, user1)
joinCommunity(&s.Suite, community, admin1, user1, joinRequest)

// Common func for sending messages
var sentMessages = map[string]struct{}{}

// User 1: send a message to community
messageId := s.sendTestMessage(user1, chat.ID, "A")
sentMessages[messageId] = struct{}{}

// Admin-2: Go online
// NOTE: We should be receiving both things here:
// - new community member (User 2)
// - message "A"
// Unfortunately, we get message "A" first and therefor never get message "A"
// https://github.com/status-im/status-go/issues/3869
response, err = WaitOnMessengerResponse(admin2, func(r *MessengerResponse) bool {
return len(r.Communities()) == 1 && len(r.Communities()[0].Members()) == 2 // && len(r.Messages()) == 2
}, "member not received")

// FIXME: Message "A" is lost here
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Messages(), 1)
s.Require().Equal(protobuf.ChatMessage_COMMUNITY, response.Messages()[0].ContentType)

// User 1: send another message to community
messageId = s.sendTestMessage(user1, chat.ID, "B")
sentMessages[messageId] = struct{}{}

// This time Admin-2 receives the message
response, _ = WaitOnMessengerResponse(user1, func(r *MessengerResponse) bool {
return len(r.Messages()) == 1
}, "message not received")

s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Equal(sentMessages, response.messageIds())
}

func (s *MessengerCommunitiesSuite) TestSetMutePropertyOnChatsByCategory() {
// Create a community
createCommunityReq := &requests.CreateCommunity{
Expand Down
8 changes: 8 additions & 0 deletions protocol/messenger_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,14 @@ func (r *MessengerResponse) Messages() []*common.Message {
return ms
}

func (r *MessengerResponse) messageIds() map[string]struct{} {
var ids = map[string]struct{}{}
for _, message := range r.Messages() {
ids[message.ID] = struct{}{}
}
return ids
}

func (r *MessengerResponse) AddDiscordMessageAuthor(author *protobuf.DiscordMessageAuthor) {
if r.discordMessageAuthors == nil {
r.discordMessageAuthors = make(map[string]*protobuf.DiscordMessageAuthor)
Expand Down

0 comments on commit 7bdb5e2

Please sign in to comment.