Files
status-go/internal/protocol/messenger_sync_chat_test.go
Igor Sirotin f3e4363808 refactor: move protocol to internal/protocol
Part of the Go project layout migration, item 27.

Pure move plus import-path rewrite across 502 files. No API or behaviour
change. `internal/` keeps the messaging application logic unimportable
from outside the module, which is what the issue asks for -- status-go is
consumed through the C-bindings in mobile/, not as a Go library.

Things that had to follow the move, beyond the Go imports:

- tools/generate-handlers/template.txt. messenger_handlers.go is
  generated, and the template hard-codes the imports it emits, so the
  generated file kept importing protocol/common and failed typecheck.
- .gitignore. The ignore rule for that generated file was pinned to the
  old path; without moving it, a 1486-line generated file starts being
  tracked.
- Makefile: the logosstorage and torrent test targets (both the archive
  packages and ./protocol itself), the archive README, migration-protocol.
- scripts/run_unit_tests.sh, which names the protocol package explicitly
  to shard its tests.
- scripts/cleanup_generated_files.sh and .golangci.yml.

scripts/migration_check.sh also needed a fix that is not specific to this
move: it validated every file the branch touched under a migration dir
against the timestamp naming rule, and a directory rename makes every
migration in it look newly added. It now excludes renames, so moving a
migration is not mistaken for adding one.

refs #7067
2026-08-21 10:11:05 +02:00

173 lines
4.3 KiB
Go

package protocol
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/suite"
"github.com/status-im/status-go/internal/protocol/common"
"github.com/status-im/status-go/internal/testutils"
messagingtypes "github.com/status-im/status-go/pkg/messaging/types"
)
const publicChatName = "status"
func TestMessengerSyncChatSuite(t *testing.T) {
suite.Run(t, new(MessengerSyncChatSuite))
}
type MessengerSyncChatSuite struct {
MessengerBaseTestSuite
alice1 *Messenger
alice2 *Messenger
}
func (s *MessengerSyncChatSuite) SetupTest() {
s.MessengerBaseTestSuite.SetupTest()
s.alice1 = s.m
s.alice2 = s.anotherMessenger()
}
func (s *MessengerSyncChatSuite) Pair() {
err := s.alice2.SetInstallationMetadata(s.alice2.installationID, &messagingtypes.InstallationMetadata{
Name: "alice2",
DeviceType: "alice2",
})
s.Require().NoError(err)
response, err := s.alice2.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)
s.Require().False(response.Chats()[0].Active)
// Wait for the message to reach its destination
response, err = WaitOnMessengerResponse(
s.alice1,
func(r *MessengerResponse) bool { return len(r.Installations()) > 0 },
"installation not received",
)
s.Require().NoError(err)
actualInstallation := response.Installations()[0]
s.Require().Equal(s.alice2.installationID, actualInstallation.ID)
s.Require().NotNil(actualInstallation.InstallationMetadata)
s.Require().Equal("alice2", actualInstallation.InstallationMetadata.Name)
s.Require().Equal("alice2", actualInstallation.InstallationMetadata.DeviceType)
_, err = s.alice1.EnableInstallation(s.alice2.installationID)
s.Require().NoError(err)
}
func (s *MessengerSyncChatSuite) TestRemovePubChat() {
chat := CreatePublicChat(publicChatName, s.alice1.getTimesource())
err := s.alice1.SaveChat(chat)
s.Require().NoError(err)
chat = CreatePublicChat(publicChatName, s.alice2.getTimesource())
err = s.alice2.SaveChat(chat)
s.Require().NoError(err)
s.Pair()
_, err = s.alice1.deactivateChat(publicChatName, 0, true, true)
s.Require().NoError(err)
var allChats []*Chat
// Wait for the message to reach its destination
err = testutils.RetryWithBackOff(func() error {
var err error
response, err := s.alice2.RetrieveAll()
if err != nil {
return err
}
allChats = append(allChats, response.Chats()...)
if len(allChats) >= 1 {
return nil
}
return errors.New("Not received all chats & contacts")
})
s.Require().NoError(err)
var statusChat *Chat
for _, c := range allChats {
if c.ID == publicChatName {
statusChat = c
}
}
s.Require().NotNil(statusChat)
s.Require().False(statusChat.Active)
}
func (s *MessengerSyncChatSuite) TestMarkChatMessagesRead() {
s.Pair()
chatID := "foobarsynctest"
_, err := s.alice1.createPublicChat(chatID, &MessengerResponse{})
s.Require().NoError(err)
_, err = s.alice2.createPublicChat(chatID, &MessengerResponse{})
s.Require().NoError(err)
otherMessenger := s.newMessenger()
_, err = otherMessenger.createPublicChat(chatID, &MessengerResponse{})
s.Require().NoError(err)
chat := otherMessenger.Chat(chatID)
message := buildTestMessage(*chat)
_, err = otherMessenger.SendChatMessage(context.Background(), message)
s.Require().NoError(err)
var receivedPubChatMessage *common.Message
err = testutils.RetryWithBackOff(func() error {
var err error
response, err := s.alice2.RetrieveAll()
if err != nil {
return err
}
messages := response.Messages()
if len(messages) > 0 {
receivedPubChatMessage = messages[0]
return nil
}
return errors.New("Not received all messages")
})
s.Require().NoError(err)
s.Require().Equal(receivedPubChatMessage.ChatId, chatID)
alice2chat := s.alice2.Chat(chatID)
s.Require().Equal(alice2chat.UnviewedMessagesCount, uint(1))
_, err = s.alice1.MarkAllRead(context.TODO(), chatID)
s.Require().NoError(err)
var receivedChat *Chat
err = testutils.RetryWithBackOff(func() error {
var err error
response, err := s.alice2.RetrieveAll()
if err != nil {
return err
}
chats := response.Chats()
if len(chats) > 0 {
receivedChat = chats[0]
return nil
}
return errors.New("Not received all messages")
})
s.Require().NoError(err)
s.Require().Equal(receivedChat.ID, chatID)
s.Require().Equal(receivedChat.UnviewedMessagesCount, uint(0))
}