From e99ad95a44f8ea007ea13da2e969bcf5dda8d05a Mon Sep 17 00:00:00 2001 From: Slyghtning Date: Mon, 22 Apr 2024 12:47:48 +0200 Subject: [PATCH] tmp unit test --- staticaddr/address/manager_test.go | 157 +++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 staticaddr/address/manager_test.go diff --git a/staticaddr/address/manager_test.go b/staticaddr/address/manager_test.go new file mode 100644 index 000000000..51cee2cd8 --- /dev/null +++ b/staticaddr/address/manager_test.go @@ -0,0 +1,157 @@ +package address + +import ( + "context" + "encoding/hex" + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcutil" + "github.com/lightninglabs/loop/loopdb" + "github.com/lightninglabs/loop/swapserverrpc" + "github.com/lightninglabs/loop/test" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + "google.golang.org/grpc" + "testing" +) + +var ( + defaultAddresssId = mustDecodeID("17cecc61ab4aafebdc0542dabdae0d0cb8907ec1c9c8ae387bc5a3309ca8b600") + + defaultPubkeyBytes, _ = hex.DecodeString("021c97a90a411ff2b10dc2a8e32de2f29d2fa49d41bfbb52bd416e460db0747d0d") + + defaultPubkey, _ = btcec.ParsePubKey(defaultPubkeyBytes) + + defaultValue = btcutil.Amount(100) + + defaultExpiry = uint32(100) +) + +type mockStaticAddressClient struct { + mock.Mock +} + +type mockSwapServerClient struct { + mock.Mock +} + +type mockSwapClient struct { + mock.Mock + Server *mockSwapServerClient +} + +func (m *mockStaticAddressClient) ServerNewAddress(ctx context.Context, + in *swapserverrpc.ServerNewAddressRequest, opts ...grpc.CallOption) ( + *swapserverrpc.ServerNewAddressResponse, error) { + + args := m.Called(ctx, in, opts) + return args.Get(0).(*swapserverrpc.ServerNewAddressResponse), + args.Error(1) +} + +func (m *mockSwapServerClient) FetchL402(ctx context.Context, + in *swapserverrpc.FetchL402Request, + opts ...grpc.CallOption) (*swapserverrpc.FetchL402Response, error) { + + args := m.Called(ctx, in, opts) + + return args.Get(0).(*swapserverrpc.FetchL402Response), + args.Error(1) +} + +func TestManager(t *testing.T) { + ctxb, cancel := context.WithCancel(context.Background()) + defer cancel() + + testContext := newManagerTestContext(t) + + // Start the manager. + go func() { + err := testContext.manager.Run(ctxb) + require.NoError(t, err) + }() + + // Create a new reservation. + _, err := testContext.manager.NewAddress(ctxb) + require.NoError(t, err) +} + +// ManagerTestContext is a helper struct that contains all the necessary +// components to test the reservation manager. +type ManagerTestContext struct { + manager *Manager + context test.Context + mockLnd *test.LndMockServices + reservationNotificationChan chan *swapserverrpc.ServerReservationNotification + mockStaticAddressClient *mockStaticAddressClient +} + +// newManagerTestContext creates a new test context for the reservation manager. +func newManagerTestContext(t *testing.T) *ManagerTestContext { + mockLnd := test.NewMockLnd() + lndContext := test.NewContext(t, mockLnd) + + dbFixture := loopdb.NewTestDB(t) + + store := NewSqlStore(dbFixture.BaseDB) + + mockStaticAddressClient := new(mockStaticAddressClient) + mockSwapClient := new(mockSwapClient) + + sendChan := make(chan *swapserverrpc.ServerReservationNotification) + + mockStaticAddressClient.On( + "ReservationNotificationStream", mock.Anything, mock.Anything, + mock.Anything, + ).Return( + &dummyReservationNotificationServer{ + SendChan: sendChan, + }, nil, + ) + + mockStaticAddressClient.On( + "OpenReservation", mock.Anything, mock.Anything, mock.Anything, + ).Return( + &swapserverrpc.ServerOpenReservationResponse{}, nil, + ) + + cfg := &ManagerConfig{ + SwapClient: mockSwapClient, + Store: store, + WalletKit: mockLnd.WalletKit, + ChainParams: mockLnd.ChainParams, + AddressClient: mockStaticAddressClient, + } + + manager := NewManager(cfg) + + return &ManagerTestContext{ + manager: manager, + context: lndContext, + mockLnd: mockLnd, + mockStaticAddressClient: mockStaticAddressClient, + reservationNotificationChan: sendChan, + } +} + +type dummyReservationNotificationServer struct { + grpc.ClientStream + + // SendChan is the channel that is used to send notifications. + SendChan chan *swapserverrpc.ServerReservationNotification +} + +func (d *dummyReservationNotificationServer) Recv() ( + *swapserverrpc.ServerReservationNotification, error) { + + return <-d.SendChan, nil +} + +func mustDecodeID(id string) []byte { + bytes, err := hex.DecodeString(id) + if err != nil { + panic(err) + } + var decoded []byte + copy(decoded[:], bytes) + return decoded +}