Skip to content

Commit

Permalink
tmp unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
hieblmi committed Apr 22, 2024
1 parent ebff44f commit e99ad95
Showing 1 changed file with 157 additions and 0 deletions.
157 changes: 157 additions & 0 deletions staticaddr/address/manager_test.go
Original file line number Diff line number Diff line change
@@ -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,

Check failure on line 118 in staticaddr/address/manager_test.go

View workflow job for this annotation

GitHub Actions / build and lint code

cannot use mockSwapClient (variable of type *mockSwapClient) as *loop.Client value in struct literal (typecheck)

Check failure on line 118 in staticaddr/address/manager_test.go

View workflow job for this annotation

GitHub Actions / run unit tests

cannot use mockSwapClient (variable of type *mockSwapClient) as *loop.Client value in struct literal
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
}

0 comments on commit e99ad95

Please sign in to comment.