github.com/status-im/status-go@v1.1.0/protocol/messenger_builder_test.go (about)

     1  package protocol
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  
     6  	"github.com/google/uuid"
     7  	"go.uber.org/zap"
     8  
     9  	"github.com/status-im/status-go/account/generator"
    10  	"github.com/status-im/status-go/appdatabase"
    11  	"github.com/status-im/status-go/common/dbsetup"
    12  	"github.com/status-im/status-go/eth-node/crypto"
    13  	"github.com/status-im/status-go/eth-node/types"
    14  	"github.com/status-im/status-go/multiaccounts"
    15  	"github.com/status-im/status-go/multiaccounts/settings"
    16  	"github.com/status-im/status-go/protocol/protobuf"
    17  	"github.com/status-im/status-go/protocol/tt"
    18  	v1protocol "github.com/status-im/status-go/protocol/v1"
    19  	"github.com/status-im/status-go/t/helpers"
    20  	"github.com/status-im/status-go/walletdatabase"
    21  )
    22  
    23  type testMessengerConfig struct {
    24  	name       string
    25  	privateKey *ecdsa.PrivateKey
    26  	logger     *zap.Logger
    27  
    28  	unhandledMessagesTracker *unhandledMessagesTracker
    29  	messagesOrderController  *MessagesOrderController
    30  
    31  	extraOptions []Option
    32  }
    33  
    34  func (tmc *testMessengerConfig) complete() error {
    35  	if len(tmc.name) == 0 {
    36  		tmc.name = uuid.NewString()
    37  	}
    38  
    39  	if tmc.privateKey == nil {
    40  		privateKey, err := crypto.GenerateKey()
    41  		if err != nil {
    42  			return err
    43  		}
    44  		tmc.privateKey = privateKey
    45  	}
    46  
    47  	if tmc.logger == nil {
    48  		logger := tt.MustCreateTestLogger()
    49  		tmc.logger = logger.Named(tmc.name)
    50  	}
    51  
    52  	return nil
    53  }
    54  
    55  func newTestMessenger(waku types.Waku, config testMessengerConfig) (*Messenger, error) {
    56  	err := config.complete()
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	acc := generator.NewAccount(config.privateKey, nil)
    62  	iai := acc.ToIdentifiedAccountInfo("")
    63  
    64  	madb, err := multiaccounts.InitializeDB(dbsetup.InMemoryPath)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	walletDb, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	appDb, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	options := []Option{
    78  		WithCustomLogger(config.logger),
    79  		WithDatabase(appDb),
    80  		WithWalletDatabase(walletDb),
    81  		WithMultiAccounts(madb),
    82  		WithAccount(iai.ToMultiAccount()),
    83  		WithDatasync(),
    84  		WithToplevelDatabaseMigrations(),
    85  		WithBrowserDatabase(nil),
    86  		WithCuratedCommunitiesUpdateLoop(false),
    87  	}
    88  	options = append(options, config.extraOptions...)
    89  
    90  	m, err := NewMessenger(
    91  		config.name,
    92  		config.privateKey,
    93  		&testNode{shh: waku},
    94  		uuid.New().String(),
    95  		nil,
    96  		"testVersion",
    97  		options...,
    98  	)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	if config.unhandledMessagesTracker != nil {
   104  		m.unhandledMessagesTracker = config.unhandledMessagesTracker.addMessage
   105  	}
   106  
   107  	if config.messagesOrderController != nil {
   108  		m.retrievedMessagesIteratorFactory = config.messagesOrderController.newMessagesIterator
   109  	}
   110  
   111  	err = m.InitInstallations()
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	err = m.InitFilters()
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	return m, nil
   122  }
   123  
   124  type unhandedMessage struct {
   125  	*v1protocol.StatusMessage
   126  	err error
   127  }
   128  
   129  type unhandledMessagesTracker struct {
   130  	messages map[protobuf.ApplicationMetadataMessage_Type][]*unhandedMessage
   131  }
   132  
   133  func (u *unhandledMessagesTracker) addMessage(msg *v1protocol.StatusMessage, err error) {
   134  	msgType := msg.ApplicationLayer.Type
   135  
   136  	if _, exists := u.messages[msgType]; !exists {
   137  		u.messages[msgType] = []*unhandedMessage{}
   138  	}
   139  
   140  	newMessage := &unhandedMessage{
   141  		StatusMessage: msg,
   142  		err:           err,
   143  	}
   144  	u.messages[msgType] = append(u.messages[msgType], newMessage)
   145  }
   146  
   147  func newTestSettings() settings.Settings {
   148  	return settings.Settings{
   149  		DisplayName:               DefaultProfileDisplayName,
   150  		ProfilePicturesShowTo:     1,
   151  		ProfilePicturesVisibility: 1,
   152  		URLUnfurlingMode:          settings.URLUnfurlingAlwaysAsk,
   153  	}
   154  }