From e86959106d37815c901bdce019f035870391c9d9 Mon Sep 17 00:00:00 2001 From: lash Date: Sun, 12 Jan 2025 15:00:08 +0000 Subject: [PATCH] Rehabilitate voucherlist generation --- event/custodial_test.go | 1 - event/nats/nats_test.go | 16 +++++++++++- event/token.go | 11 --------- event/token_test.go | 49 ++++++++++++++++++++----------------- internal/testutil/format.go | 5 ++++ 5 files changed, 46 insertions(+), 36 deletions(-) create mode 100644 internal/testutil/format.go diff --git a/event/custodial_test.go b/event/custodial_test.go index 761e0a0..8d92b6a 100644 --- a/event/custodial_test.go +++ b/event/custodial_test.go @@ -60,5 +60,4 @@ func TestCustodialRegistration(t *testing.T) { if err != nil { t.Fatal(err) } - } diff --git a/event/nats/nats_test.go b/event/nats/nats_test.go index f202e7c..9ed4d93 100644 --- a/event/nats/nats_test.go +++ b/event/nats/nats_test.go @@ -19,9 +19,9 @@ import ( storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db" "git.grassecon.net/grassrootseconomics/sarafu-api/models" "git.grassecon.net/grassrootseconomics/sarafu-vise-events/lookup" -// "git.grassecon.net/grassrootseconomics/sarafu-vise-events/event" "git.grassecon.net/grassrootseconomics/common/hex" "git.grassecon.net/grassrootseconomics/sarafu-vise-events/internal/testutil" + "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/application" ) const ( @@ -198,6 +198,20 @@ func TestHandleMsg(t *testing.T) { t.Fatal("no transaction data") } + + mh, err := application.NewMenuHandlers(nil, userStore, nil, nil, testutil.ReplaceSeparatorFunc) + if err != nil { + t.Fatal(err) + } + ctx = context.WithValue(ctx, "SessionId", testutil.AliceSession) + 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) + } // userDb.SetPrefix(event.DATATYPE_USERSUB) // userDb.SetSession(testutil.AliceSession) // k := append([]byte("vouchers"), []byte("sym")...) diff --git a/event/token.go b/event/token.go index f7b4e80..6ee8da2 100644 --- a/event/token.go +++ b/event/token.go @@ -75,36 +75,26 @@ func updateTokenList(ctx context.Context, userStore *store.UserDataStore, identi // TODO: make sure subprefixdb is thread safe when using gdbm // TODO: why is address session here unless explicitly set - //userStore.Db.SetPrefix(DATATYPE_USERSUB) pfxDb := toPrefixDb(userStore, identity.SessionId) - //k := append([]byte("vouchers"), []byte("sym")...) - //err = userStore.Db.Put(ctx, k, []byte(metadata.Symbols)) typ := storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS) err = pfxDb.Put(ctx, typ, []byte(metadata.Symbols)) if err != nil { return err } - - //k = append([]byte("vouchers"), []byte("bal")...) - //err = userStore.Db.Put(ctx, k, []byte(metadata.Balances)) typ = storedb.ToBytes(storedb.DATA_VOUCHER_BALANCES) err = pfxDb.Put(ctx, typ, []byte(metadata.Balances)) if err != nil { return err } - //k = append([]byte("vouchers"), []byte("deci")...) - //err = userStore.Db.Put(ctx, k, []byte(metadata.Decimals)) typ = storedb.ToBytes(storedb.DATA_VOUCHER_DECIMALS) err = pfxDb.Put(ctx, typ, []byte(metadata.Decimals)) if err != nil { return err } - //k = append([]byte("vouchers"), []byte("addr")...) - //err = userStore.Db.Put(ctx, k, []byte(metadata.Addresses)) typ = storedb.ToBytes(storedb.DATA_VOUCHER_ADDRESSES) err = pfxDb.Put(ctx, typ, []byte(metadata.Addresses)) if err != nil { @@ -116,7 +106,6 @@ func updateTokenList(ctx context.Context, userStore *store.UserDataStore, identi // set default token to given symbol. func updateDefaultToken(ctx context.Context, userStore *store.UserDataStore, identity lookup.Identity, activeSym string) error { - //pfxDb := store.StoreToPrefixDb(userStore, []byte("vouchers")) pfxDb := toPrefixDb(userStore, identity.SessionId) // TODO: the activeSym input should instead be newline separated list? tokenData, err := store.GetVoucherData(ctx, pfxDb, activeSym) diff --git a/event/token_test.go b/event/token_test.go index b0f2995..8b1563c 100644 --- a/event/token_test.go +++ b/event/token_test.go @@ -33,8 +33,10 @@ const ( txTimestamp = 1730592500 txHash = "0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789" sinkAddress = "0xb42C5920014eE152F2225285219407938469BBfA" + bogusSym = "/-21380u" ) + func TestTokenTransfer(t *testing.T) { err := config.LoadConfig() if err != nil { @@ -131,22 +133,19 @@ func TestTokenTransfer(t *testing.T) { t.Fatal("no transaction data") } - mh, err := application.NewMenuHandlers(nil, userStore, nil, nil, nil) + mh, err := application.NewMenuHandlers(nil, userStore, nil, nil, testutil.ReplaceSeparatorFunc) if err != nil { t.Fatal(err) } - _ = mh - -// userDb.SetPrefix(DATATYPE_USERSUB) -// userDb.SetSession(testutil.AliceSession) -// k := append([]byte("vouchers"), []byte("sym")...) -// v, err = userDb.Get(ctx, k) -// if err != nil { -// t.Fatal(err) -// } -// if !bytes.Contains(v, []byte(fmt.Sprintf("1:%s", tokenSymbol))) { -// t.Fatalf("expected '1:%s', got %s", tokenSymbol, v) -// } + ctx = context.WithValue(ctx, "SessionId", testutil.AliceSession) + 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) + } } func TestTokenMint(t *testing.T) { @@ -243,14 +242,18 @@ func TestTokenMint(t *testing.T) { t.Fatal("no transaction data") } -// userDb.SetPrefix(DATATYPE_USERSUB) -// userDb.SetSession(testutil.AliceSession) -// k := append([]byte("vouchers"), []byte("sym")...) -// v, err = userDb.Get(ctx, k) -// if err != nil { -// t.Fatal(err) -// } -// if !bytes.Contains(v, []byte(fmt.Sprintf("1:%s", tokenSymbol))) { -// t.Fatalf("expected '1:%s', got %s", tokenSymbol, v) -// } + + mh, err := application.NewMenuHandlers(nil, userStore, nil, nil, testutil.ReplaceSeparatorFunc) + if err != nil { + t.Fatal(err) + } + ctx = context.WithValue(ctx, "SessionId", testutil.AliceSession) + 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) + } } diff --git a/internal/testutil/format.go b/internal/testutil/format.go new file mode 100644 index 0000000..6d285bb --- /dev/null +++ b/internal/testutil/format.go @@ -0,0 +1,5 @@ +package testutil + +func ReplaceSeparatorFunc(s string) string { + return s +}