2023-07-12 09:46:56 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2023-08-10 13:57:52 +00:00
|
|
|
"context"
|
2023-07-12 09:46:56 +00:00
|
|
|
"errors"
|
|
|
|
|
2023-08-10 13:57:52 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2023-07-12 09:46:56 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
"github.com/status-im/status-go/protocol/tt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WaitOnMessengerResponse Wait until the condition is true or the timeout is reached.
|
|
|
|
func WaitOnMessengerResponse(m *Messenger, condition func(*MessengerResponse) bool, errorMessage string) (*MessengerResponse, error) {
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
err := tt.RetryWithBackOff(func() error {
|
|
|
|
var err error
|
|
|
|
r, err := m.RetrieveAll()
|
2023-08-10 15:12:23 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-07-12 09:46:56 +00:00
|
|
|
if err := response.Merge(r); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && !condition(response) {
|
|
|
|
err = errors.New(errorMessage)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
2023-08-04 10:41:24 +00:00
|
|
|
return response, err
|
2023-07-12 09:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FindFirstByContentType(messages []*common.Message, contentType protobuf.ChatMessage_ContentType) *common.Message {
|
|
|
|
for _, message := range messages {
|
|
|
|
if message.ContentType == contentType {
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-10 13:57:52 +00:00
|
|
|
|
|
|
|
func PairDevices(s *suite.Suite, device1, device2 *Messenger) {
|
|
|
|
// Send pairing data
|
|
|
|
response, err := device1.SendPairInstallation(context.Background(), nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Len(response.Chats(), 1)
|
|
|
|
s.False(response.Chats()[0].Active)
|
|
|
|
|
|
|
|
i, ok := device1.allInstallations.Load(device1.installationID)
|
|
|
|
s.Require().True(ok)
|
|
|
|
|
|
|
|
// Wait for the message to reach its destination
|
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
device2,
|
|
|
|
func(r *MessengerResponse) bool {
|
|
|
|
for _, installation := range r.Installations {
|
|
|
|
if installation.ID == device1.installationID {
|
|
|
|
return installation.InstallationMetadata != nil &&
|
|
|
|
i.InstallationMetadata.Name == installation.InstallationMetadata.Name &&
|
|
|
|
i.InstallationMetadata.DeviceType == installation.InstallationMetadata.DeviceType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
|
|
|
|
},
|
|
|
|
"installation not received",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
|
|
|
|
// Ensure installation is enabled
|
|
|
|
err = device2.EnableInstallation(device1.installationID)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|