From 268b69ac554953c6cfc07864ee1ac8fda5d20ba8 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Fri, 9 Aug 2024 22:25:24 +0700 Subject: [PATCH] chore: improve tests --- transactions/keysend_test.go | 2 + transactions/notifications_test.go | 68 ++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/transactions/keysend_test.go b/transactions/keysend_test.go index 9e7cf926..81cccf37 100644 --- a/transactions/keysend_test.go +++ b/transactions/keysend_test.go @@ -325,6 +325,8 @@ func TestSendKeysend_TLVs(t *testing.T) { assert.Equal(t, "boost", boostagram.Action) assert.Equal(t, int64(1000), boostagram.ValueMsatTotal) + assert.Equal(t, "Go podcasting!", transaction.Description) + assert.Equal(t, uint64(1000), transaction.AmountMsat) assert.Equal(t, constants.TRANSACTION_TYPE_OUTGOING, transaction.Type) assert.Equal(t, constants.TRANSACTION_STATE_SETTLED, transaction.State) diff --git a/transactions/notifications_test.go b/transactions/notifications_test.go index 8d08675e..2851bffc 100644 --- a/transactions/notifications_test.go +++ b/transactions/notifications_test.go @@ -2,11 +2,13 @@ package transactions import ( "context" + "encoding/json" "testing" "github.com/getAlby/hub/constants" "github.com/getAlby/hub/db" "github.com/getAlby/hub/events" + "github.com/getAlby/hub/lnclient" "github.com/getAlby/hub/tests" "github.com/stretchr/testify/assert" ) @@ -74,6 +76,72 @@ func TestNotifications_ReceivedUnknownPayment(t *testing.T) { assert.Equal(t, int64(1), result.RowsAffected) } +func TestNotifications_ReceivedKeysend(t *testing.T) { + ctx := context.TODO() + + defer tests.RemoveTestService() + svc, err := tests.CreateTestService() + assert.NoError(t, err) + + transactionsService := NewTransactionsService(svc.DB) + + metadata := map[string]interface{}{} + + metadata["tlv_records"] = []lnclient.TLVRecord{ + { + Type: 7629169, + Value: "7b22616374696f6e223a22626f6f7374222c2276616c75655f6d736174223a313030302c2276616c75655f6d7361745f746f74616c223a313030302c226170705f6e616d65223a22e29aa1205765624c4e2044656d6f222c226170705f76657273696f6e223a22312e30222c22666565644944223a2268747470733a2f2f66656564732e706f6463617374696e6465782e6f72672f706332302e786d6c222c22706f6463617374223a22506f6463617374696e6720322e30222c22657069736f6465223a22457069736f6465203130343a2041204e65772044756d70222c227473223a32312c226e616d65223a22e29aa1205765624c4e2044656d6f222c2273656e6465725f6e616d65223a225361746f736869204e616b616d6f746f222c226d657373616765223a22476f20706f6463617374696e6721227d", + }, + } + + transaction := &lnclient.Transaction{ + Type: "incoming", + Invoice: tests.MockInvoice, + Description: "", + DescriptionHash: "", + Preimage: tests.MockLNClientTransaction.Preimage, + PaymentHash: tests.MockLNClientTransaction.PaymentHash, + Amount: 2000, + FeesPaid: 75, + SettledAt: &tests.MockTimeUnix, + Metadata: metadata, + } + + transactionsService.ConsumeEvent(ctx, &events.Event{ + Event: "nwc_payment_received", + Properties: transaction, + }, map[string]interface{}{}) + + transactionType := constants.TRANSACTION_TYPE_INCOMING + incomingTransaction, err := transactionsService.LookupTransaction(ctx, tests.MockLNClientTransaction.PaymentHash, &transactionType, svc.LNClient, nil) + assert.NoError(t, err) + assert.Equal(t, uint64(2000), incomingTransaction.AmountMsat) + assert.Equal(t, constants.TRANSACTION_STATE_SETTLED, incomingTransaction.State) + assert.Equal(t, tests.MockLNClientTransaction.Preimage, *incomingTransaction.Preimage) + assert.Zero(t, incomingTransaction.FeeReserveMsat) + + var boostagram Boostagram + err = json.Unmarshal(incomingTransaction.Boostagram, &boostagram) + assert.NoError(t, err) + + assert.Equal(t, "⚡ WebLN Demo", boostagram.AppName) + assert.Equal(t, "⚡ WebLN Demo", boostagram.Name) + assert.Equal(t, "Podcasting 2.0", boostagram.Podcast) + assert.Equal(t, "Episode 104: A New Dump", boostagram.Episode) + assert.Equal(t, "https://feeds.podcastindex.org/pc20.xml", boostagram.FeedId) + assert.Equal(t, int64(21), boostagram.Timestamp) + assert.Equal(t, "Go podcasting!", boostagram.Message) + assert.Equal(t, "Satoshi Nakamoto", boostagram.SenderName) + assert.Equal(t, "boost", boostagram.Action) + assert.Equal(t, int64(1000), boostagram.ValueMsatTotal) + + assert.Equal(t, "Go podcasting!", incomingTransaction.Description) + + transactions := []db.Transaction{} + result := svc.DB.Find(&transactions) + assert.Equal(t, int64(1), result.RowsAffected) +} + func TestNotifications_SentKnownPayment(t *testing.T) { ctx := context.TODO()