alby-hub/transactions/self_payment_detection_test.go
Roland 84e56a23db
feat: bark backend (#2374)
* feat: bark backend

* fix: use real preimage

* fix: balance shows as 0 on startup after hours offline period

* chore: add bark-specific warnings to setup security page

* chore: update setup security page

* chore: use notifications instead of polling

* feat: bark sub-wallet support

* fix: tests

* feat: add env variables for mainnet support

* chore: add custom node commands for debugging, log next notification

* fix: notification handling

* fix: fees for outgoing payments, pending payment handling, decrease required tx confirmations

* chore: bump bark version

* chore: use better bark image

* fix: back button logic and imported mnemonic state in onboarding flow

* chore: update bark copy on security page

* chore: improve copy for first transaction checklist item

* fix: backup copy based on backend

* fix: remove unused/unnecessary code
2026-06-04 11:57:29 +07:00

99 lines
3.6 KiB
Go

package transactions
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/db"
"github.com/getAlby/hub/tests"
)
func TestSendPaymentSync_SelfPaymentDetection_WithIncomingTransaction(t *testing.T) {
svc, err := tests.CreateTestService(t)
require.NoError(t, err)
defer svc.Remove()
svc.LNClient.(*tests.MockLn).Pubkey = "03cbd788f5b22bd56e2714bff756372d2293504c064e03250ed16a4dd80ad70e2c"
mockPreimage := "123preimage"
svc.DB.Create(&db.Transaction{
State: constants.TRANSACTION_STATE_PENDING,
Type: constants.TRANSACTION_TYPE_INCOMING,
PaymentRequest: tests.MockInvoice,
PaymentHash: tests.MockPaymentHash,
Preimage: &mockPreimage,
AmountMsat: 123000,
})
transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
transaction, err := transactionsService.SendPaymentSync(tests.MockInvoice, nil, nil, svc.LNClient, nil, nil)
assert.NoError(t, err)
assert.NotNil(t, transaction)
assert.True(t, transaction.SelfPayment)
assert.Equal(t, constants.TRANSACTION_STATE_SETTLED, transaction.State)
assert.Equal(t, uint64(123000), transaction.AmountMsat)
assert.Equal(t, mockPreimage, *transaction.Preimage)
assert.Zero(t, transaction.FeeMsat)
}
func TestSendPaymentSync_SelfPaymentDetection_WithoutIncomingTransaction(t *testing.T) {
svc, err := tests.CreateTestService(t)
require.NoError(t, err)
defer svc.Remove()
svc.LNClient.(*tests.MockLn).Pubkey = "03cbd788f5b22bd56e2714bff756372d2293504c064e03250ed16a4dd80ad70e2c"
mockPreimage := "123preimage"
svc.DB.Create(&db.Transaction{
State: constants.TRANSACTION_STATE_FAILED, // otherwise we won't be able to create a new payment with the same hash
Type: constants.TRANSACTION_TYPE_OUTGOING,
PaymentRequest: tests.MockInvoice,
PaymentHash: tests.MockPaymentHash,
Preimage: &mockPreimage,
AmountMsat: 123000,
})
transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
transaction, err := transactionsService.SendPaymentSync(tests.MockInvoice, nil, nil, svc.LNClient, nil, nil)
assert.NoError(t, err)
assert.NotNil(t, transaction)
assert.False(t, transaction.SelfPayment)
assert.Equal(t, constants.TRANSACTION_STATE_SETTLED, transaction.State)
assert.Equal(t, uint64(123000), transaction.AmountMsat)
}
// Self-payment detection is based solely on having an incoming transaction for
// the same invoice, not on the invoice payee matching our node pubkey. A
// mismatching pubkey must not prevent detection: some backends (e.g. Bark)
// don't own the node behind the invoice, and the payee is unavailable for
// private BOLT12 payments.
func TestSendPaymentSync_SelfPaymentDetection_DifferentPubkey(t *testing.T) {
svc, err := tests.CreateTestService(t)
require.NoError(t, err)
defer svc.Remove()
svc.LNClient.(*tests.MockLn).Pubkey = "different_pubkey_123"
mockPreimage := "123preimage"
svc.DB.Create(&db.Transaction{
State: constants.TRANSACTION_STATE_PENDING,
Type: constants.TRANSACTION_TYPE_INCOMING,
PaymentRequest: tests.MockInvoice,
PaymentHash: tests.MockPaymentHash,
Preimage: &mockPreimage,
AmountMsat: 123000,
})
transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
transaction, err := transactionsService.SendPaymentSync(tests.MockInvoice, nil, nil, svc.LNClient, nil, nil)
assert.NoError(t, err)
assert.NotNil(t, transaction)
assert.True(t, transaction.SelfPayment)
assert.Equal(t, constants.TRANSACTION_STATE_SETTLED, transaction.State)
}