status-go/services/local-notifications/core_test.go

142 lines
3.5 KiB
Go
Raw Normal View History

package localnotifications
import (
"database/sql"
"fmt"
"math/big"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/status-im/status-go/services/wallet/transfer"
"github.com/status-im/status-go/services/wallet/walletevent"
"github.com/status-im/status-go/signal"
"github.com/status-im/status-go/t/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
)
func createWalletDb(t *testing.T, db *sql.DB) (*transfer.Database, func()) {
return transfer.NewDB(db), func() {
require.NoError(t, db.Close())
}
}
func TestServiceStartStop(t *testing.T) {
db, stop := setupAppTestDb(t)
defer stop()
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
s, err := NewService(db, 1777)
require.NoError(t, err)
2021-07-12 09:44:26 +00:00
require.NoError(t, s.Start())
require.Equal(t, true, s.IsStarted())
require.NoError(t, s.Stop())
require.Equal(t, false, s.IsStarted())
}
func TestWalletSubscription(t *testing.T) {
db, stop := setupAppTestDb(t)
defer stop()
feed := &event.Feed{}
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
s, err := NewService(db, 1777)
require.NoError(t, err)
2021-07-12 09:44:26 +00:00
require.NoError(t, s.Start())
require.Equal(t, true, s.IsStarted())
require.NoError(t, s.SubscribeWallet(feed))
2020-12-28 15:09:25 +00:00
require.Equal(t, true, s.IsWatchingWallet())
s.StartWalletWatcher()
require.Equal(t, true, s.IsWatchingWallet())
s.StopWalletWatcher()
require.Equal(t, false, s.IsWatchingWallet())
require.NoError(t, s.Stop())
require.Equal(t, false, s.IsStarted())
}
func TestTransactionNotification(t *testing.T) {
db, stop := setupAppTestDb(t)
defer stop()
walletDb, stop := createWalletDb(t, db)
defer stop()
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
s, err := NewService(db, 1777)
require.NoError(t, err)
2021-07-12 09:44:26 +00:00
require.NoError(t, s.Start())
require.Equal(t, true, s.IsStarted())
var signalEvent []byte
signalHandler := signal.MobileSignalHandler(func(s []byte) {
signalEvent = s
})
signal.SetMobileSignalHandler(signalHandler)
feed := &event.Feed{}
require.NoError(t, s.SubscribeWallet(feed))
2020-12-28 15:09:25 +00:00
s.WatchingEnabled = true
s.StartWalletWatcher()
header := &transfer.DBHeader{
Number: big.NewInt(1),
Hash: common.Hash{1},
Address: common.Address{1},
}
tx := types.NewTransaction(1, common.Address{1}, nil, 10, big.NewInt(10), nil)
receipt := types.NewReceipt(nil, false, 100)
receipt.Logs = []*types.Log{}
transfers := []transfer.Transfer{
{
ID: common.Hash{1},
Type: transfer.Type("eth"),
BlockHash: header.Hash,
BlockNumber: header.Number,
Transaction: tx,
Receipt: receipt,
Address: header.Address,
},
}
nonce := int64(0)
lastBlock := &transfer.Block{
Number: big.NewInt(1),
Balance: big.NewInt(0),
Nonce: &nonce,
}
require.NoError(t, walletDb.ProcessBlocks(1777, header.Address, big.NewInt(1), lastBlock, []*transfer.DBHeader{header}))
require.NoError(t, walletDb.ProcessTransfers(1777, transfers, []*transfer.DBHeader{}))
feed.Send(walletevent.Event{
Type: transfer.EventRecentHistoryReady,
BlockNumber: big.NewInt(0),
Accounts: []common.Address{header.Address},
})
feed.Send(walletevent.Event{
Type: transfer.EventNewTransfers,
BlockNumber: header.Number,
Accounts: []common.Address{header.Address},
})
require.NoError(t, utils.Eventually(func() error {
if signalEvent == nil {
return fmt.Errorf("signal was not handled")
}
require.True(t, strings.Contains(string(signalEvent), `"type":"local-notifications"`))
require.True(t, strings.Contains(string(signalEvent), `"to":"`+header.Address.Hex()))
return nil
}, 2*time.Second, 100*time.Millisecond))
require.NoError(t, s.Stop())
}