sarafu-vise-events/event/nats/nats_test.go

213 lines
5.3 KiB
Go
Raw Normal View History

2024-11-03 01:34:28 +01:00
package nats
import (
2024-11-03 16:13:59 +01:00
"bytes"
2024-11-03 01:34:28 +01:00
"context"
2024-11-03 17:25:43 +01:00
"fmt"
2025-01-12 15:45:34 +01:00
"math"
2024-11-03 17:25:43 +01:00
"strconv"
2024-11-03 01:34:28 +01:00
"testing"
"time"
"git.defalsify.org/vise.git/db"
"git.grassecon.net/grassrootseconomics/common/hex"
2025-01-21 16:33:29 +01:00
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
2025-01-14 08:39:52 +01:00
apimocks "git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks"
2025-01-21 16:33:29 +01:00
"git.grassecon.net/grassrootseconomics/sarafu-vise-events/config"
"git.grassecon.net/grassrootseconomics/sarafu-vise-events/internal/testutil"
2025-01-21 16:33:29 +01:00
"git.grassecon.net/grassrootseconomics/sarafu-vise-events/lookup"
2025-01-12 16:00:08 +01:00
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/application"
2025-01-13 18:47:21 +01:00
viseevent "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event"
2025-01-21 16:33:29 +01:00
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
"git.grassecon.net/grassrootseconomics/visedriver/testutil/mocks"
2025-01-21 16:33:29 +01:00
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
nats "github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
2024-11-03 01:34:28 +01:00
)
const (
2025-01-21 16:33:29 +01:00
txBlock = 42
tokenAddress = "0x765DE816845861e75A25fCA122bb6898B8B1282a"
tokenSymbol = "FOO"
tokenName = "Foo Token"
2024-11-03 17:25:43 +01:00
tokenDecimals = 6
2025-01-21 16:33:29 +01:00
txValue = 1337
tokenBalance = 362436
txTimestamp = 1730592500
txHash = "0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"
sinkAddress = "0xb42C5920014eE152F2225285219407938469BBfA"
2024-11-03 01:34:28 +01:00
)
// TODO: jetstream, would have been nice of you to provide an easier way to make a mock msg
type testMsg struct {
data []byte
}
2025-01-21 16:33:29 +01:00
func (m *testMsg) Ack() error {
2024-11-03 01:34:28 +01:00
return nil
}
2025-01-21 16:33:29 +01:00
func (m *testMsg) Nak() error {
2024-11-03 01:34:28 +01:00
return nil
}
2025-01-21 16:33:29 +01:00
func (m *testMsg) NakWithDelay(time.Duration) error {
2024-11-03 01:34:28 +01:00
return nil
}
2025-01-21 16:33:29 +01:00
func (m *testMsg) Data() []byte {
2024-11-03 01:34:28 +01:00
return m.data
}
2025-01-21 16:33:29 +01:00
func (m *testMsg) Reply() string {
2024-11-03 01:34:28 +01:00
return ""
}
2025-01-21 16:33:29 +01:00
func (m *testMsg) Subject() string {
2024-11-03 01:34:28 +01:00
return ""
}
2025-01-21 16:33:29 +01:00
func (m *testMsg) Term() error {
2024-11-03 01:34:28 +01:00
return nil
}
2025-01-21 16:33:29 +01:00
func (m *testMsg) TermWithReason(string) error {
2024-11-03 01:34:28 +01:00
return nil
}
2025-01-21 16:33:29 +01:00
func (m *testMsg) DoubleAck(ctx context.Context) error {
2024-11-03 01:34:28 +01:00
return nil
}
2025-01-21 16:33:29 +01:00
func (m *testMsg) Headers() nats.Header {
2024-11-03 01:34:28 +01:00
return nats.Header{}
}
2025-01-21 16:33:29 +01:00
func (m *testMsg) InProgress() error {
2024-11-03 01:34:28 +01:00
return nil
}
2025-01-21 16:33:29 +01:00
func (m *testMsg) Metadata() (*jetstream.MsgMetadata, error) {
2024-11-03 01:34:28 +01:00
return nil, nil
}
2024-11-03 23:25:51 +01:00
func TestHandleMsg(t *testing.T) {
err := config.LoadConfig()
if err != nil {
t.Fatal(err)
}
2024-11-03 01:34:28 +01:00
2025-01-14 08:39:52 +01:00
api := &apimocks.MockApi{}
2024-11-03 23:25:51 +01:00
api.TransactionsContent = []dataserviceapi.Last10TxResponse{
2024-11-03 16:13:59 +01:00
dataserviceapi.Last10TxResponse{
2025-01-21 16:33:29 +01:00
Sender: apimocks.AliceChecksum,
Recipient: apimocks.BobChecksum,
TransferValue: strconv.Itoa(txValue),
2024-11-03 17:25:43 +01:00
ContractAddress: tokenAddress,
2025-01-21 16:33:29 +01:00
TxHash: txHash,
DateBlock: time.Unix(txTimestamp, 0),
TokenSymbol: tokenSymbol,
TokenDecimals: strconv.Itoa(tokenDecimals),
2024-11-03 16:13:59 +01:00
},
2024-11-03 23:25:51 +01:00
}
api.VoucherDataContent = &models.VoucherDataResult{
2024-11-03 17:25:43 +01:00
TokenSymbol: tokenSymbol,
2025-01-21 16:33:29 +01:00
TokenName: tokenName,
//TokenDecimals: strconv.Itoa(tokenDecimals),
TokenDecimals: tokenDecimals,
2025-01-21 16:33:29 +01:00
SinkAddress: sinkAddress,
2024-11-03 01:34:28 +01:00
}
2024-11-03 23:25:51 +01:00
api.VouchersContent = []dataserviceapi.TokenHoldings{
dataserviceapi.TokenHoldings{
ContractAddress: tokenAddress,
2025-01-21 16:33:29 +01:00
TokenSymbol: tokenSymbol,
TokenDecimals: strconv.Itoa(tokenDecimals),
Balance: strconv.Itoa(tokenBalance),
2024-11-03 23:25:51 +01:00
},
}
lookup.Api = api
2024-11-03 01:34:28 +01:00
ctx := context.Background()
storageService := mocks.NewMemStorageService(ctx)
eu := viseevent.NewEventsUpdater(api, storageService)
userDb := storageService.Db
2024-11-03 01:34:28 +01:00
2025-01-14 08:39:52 +01:00
alice, err := hex.NormalizeHex(apimocks.AliceChecksum)
2024-11-03 01:34:28 +01:00
if err != nil {
t.Fatal(err)
}
userDb.SetSession(alice)
userDb.SetPrefix(db.DATATYPE_USERDATA)
2025-01-14 08:39:52 +01:00
err = userDb.Put(ctx, storedb.PackKey(storedb.DATA_PUBLIC_KEY_REVERSE, []byte{}), []byte(apimocks.AliceSession))
2024-11-03 01:34:28 +01:00
if err != nil {
t.Fatal(err)
}
eh := eu.ToEventsHandler()
sub := NewNatsSubscription(eh)
_ = sub.Connect(ctx, "")
2024-11-03 01:34:28 +01:00
2024-11-03 17:25:43 +01:00
data := fmt.Sprintf(`{
"block": %d,
"contractAddress": "%s",
2024-11-03 01:34:28 +01:00
"success": true,
2024-11-03 17:25:43 +01:00
"timestamp": %d,
"transactionHash": "%s",
2024-11-03 01:34:28 +01:00
"transactionType": "TOKEN_TRANSFER",
"payload": {
2024-11-03 17:25:43 +01:00
"from": "%s",
"to": "%s",
"value": "%d"
2024-11-03 01:34:28 +01:00
}
2025-01-14 08:39:52 +01:00
}`, txBlock, tokenAddress, txTimestamp, txHash, apimocks.AliceChecksum, apimocks.BobChecksum, txValue)
2024-11-03 01:34:28 +01:00
msg := &testMsg{
data: []byte(data),
}
sub.handleEvent(msg)
2024-11-03 16:13:59 +01:00
userStore := store.UserDataStore{
2024-11-03 16:13:59 +01:00
Db: userDb,
}
2025-01-14 08:39:52 +01:00
v, err := userStore.ReadEntry(ctx, apimocks.AliceSession, storedb.DATA_ACTIVE_SYM)
2024-11-03 16:13:59 +01:00
if err != nil {
t.Fatal(err)
}
2024-11-03 17:25:43 +01:00
if !bytes.Equal(v, []byte(tokenSymbol)) {
t.Fatalf("expected '%s', got %s", tokenSymbol, v)
2024-11-03 16:13:59 +01:00
}
2025-01-14 08:39:52 +01:00
v, err = userStore.ReadEntry(ctx, apimocks.AliceSession, storedb.DATA_ACTIVE_BAL)
2024-11-03 16:13:59 +01:00
if err != nil {
t.Fatal(err)
}
2025-01-12 15:45:34 +01:00
fmts := fmt.Sprintf("%%1.%df", tokenDecimals)
2025-01-21 16:33:29 +01:00
expect := fmt.Sprintf(fmts, float64(tokenBalance)/math.Pow(10, tokenDecimals))
2025-01-12 15:45:34 +01:00
if !bytes.Equal(v, []byte(expect)) {
2024-11-03 17:25:43 +01:00
t.Fatalf("expected '%d', got %s", tokenBalance, v)
2024-11-03 16:13:59 +01:00
}
2025-01-14 08:39:52 +01:00
v, err = userStore.ReadEntry(ctx, apimocks.AliceSession, storedb.DATA_TRANSACTIONS)
2024-11-03 16:13:59 +01:00
if err != nil {
t.Fatal(err)
}
2024-11-03 16:37:46 +01:00
if !bytes.Contains(v, []byte("abcdef")) {
2024-11-03 16:13:59 +01:00
t.Fatal("no transaction data")
}
2024-11-03 16:37:46 +01:00
mh, err := application.NewMenuHandlers(nil, userStore, nil, testutil.ReplaceSeparatorFunc)
2025-01-12 16:00:08 +01:00
if err != nil {
t.Fatal(err)
}
2025-01-14 08:39:52 +01:00
ctx = context.WithValue(ctx, "SessionId", apimocks.AliceSession)
2025-01-12 16:00:08 +01:00
rrs, err := mh.GetVoucherList(ctx, "", []byte{})
if err != nil {
t.Fatal(err)
}
expect = fmt.Sprintf("1:%s", tokenSymbol)
if rrs.Content != expect {
t.Fatalf("expected '%v', got '%v'", expect, rrs.Content)
}
2024-11-03 01:34:28 +01:00
}