Skip to content

Commit e3913d6

Browse files
committed
looprpc: add test for ListSwaps pagination and filtering
1 parent b8cbe31 commit e3913d6

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

loopd/swapclient_server_test.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ import (
44
"context"
55
"os"
66
"testing"
7+
"time"
78

89
"github.com/btcsuite/btcd/btcutil"
910
"github.com/btcsuite/btcd/chaincfg"
1011
"github.com/btcsuite/btclog/v2"
1112
"github.com/lightninglabs/lndclient"
1213
"github.com/lightninglabs/loop"
1314
"github.com/lightninglabs/loop/labels"
15+
"github.com/lightninglabs/loop/loopdb"
1416
"github.com/lightninglabs/loop/looprpc"
17+
"github.com/lightninglabs/loop/swap"
1518
mock_lnd "github.com/lightninglabs/loop/test"
19+
"github.com/lightningnetwork/lnd/lntypes"
1620
"github.com/lightningnetwork/lnd/lnwire"
1721
"github.com/lightningnetwork/lnd/routing/route"
1822
"github.com/stretchr/testify/require"
@@ -595,3 +599,170 @@ func TestHasBandwidth(t *testing.T) {
595599
})
596600
}
597601
}
602+
603+
func TestListSwapsFilterAndPagination(t *testing.T) {
604+
swapIn1 := loop.SwapInfo{
605+
SwapStateData: loopdb.SwapStateData{
606+
State: loopdb.StateInitiated,
607+
Cost: loopdb.SwapCost{},
608+
},
609+
LastUpdate: time.Now(),
610+
SwapHash: lntypes.Hash{1},
611+
SwapType: swap.Type(swap.TypeIn),
612+
HtlcAddressP2WSH: testnetAddr,
613+
HtlcAddressP2TR: testnetAddr,
614+
}
615+
616+
swapOut1 := loop.SwapInfo{
617+
SwapStateData: loopdb.SwapStateData{
618+
State: loopdb.StateInitiated,
619+
Cost: loopdb.SwapCost{},
620+
},
621+
LastUpdate: time.Now(),
622+
SwapHash: lntypes.Hash{2},
623+
SwapType: swap.Type(swap.TypeOut),
624+
HtlcAddressP2WSH: testnetAddr,
625+
HtlcAddressP2TR: testnetAddr,
626+
}
627+
628+
swapOut2 := loop.SwapInfo{
629+
SwapStateData: loopdb.SwapStateData{
630+
State: loopdb.StateInitiated,
631+
Cost: loopdb.SwapCost{},
632+
},
633+
LastUpdate: time.Now(),
634+
SwapHash: lntypes.Hash{3},
635+
SwapType: swap.Type(swap.TypeOut),
636+
HtlcAddressP2WSH: testnetAddr,
637+
HtlcAddressP2TR: testnetAddr,
638+
}
639+
// Create test cases with different filter/pagination parameters
640+
tests := []struct {
641+
name string
642+
// Define the mock swaps that will be stored in the mock client
643+
mockSwaps []loop.SwapInfo
644+
// Request parameters
645+
req *looprpc.ListSwapsRequest
646+
// Expected response fields
647+
expectedReturnedSwapCount int
648+
expectedFirstIdx uint32
649+
expectedLastIdx uint32
650+
expectedFilteredTotalCount uint32
651+
}{
652+
{
653+
name: "fetch all swaps no pagination",
654+
mockSwaps: []loop.SwapInfo{swapIn1},
655+
req: &looprpc.ListSwapsRequest{},
656+
expectedReturnedSwapCount: 1,
657+
expectedFirstIdx: 0,
658+
expectedLastIdx: 1,
659+
expectedFilteredTotalCount: 1,
660+
},
661+
{
662+
name: "fetch swaps-ins no pagination",
663+
mockSwaps: []loop.SwapInfo{swapIn1, swapOut1, swapOut2},
664+
req: &looprpc.ListSwapsRequest{
665+
ListSwapFilter: &looprpc.ListSwapsFilter{
666+
SwapType: looprpc.ListSwapsFilter_LOOP_IN},
667+
},
668+
expectedReturnedSwapCount: 1,
669+
expectedFirstIdx: 0,
670+
expectedLastIdx: 1,
671+
expectedFilteredTotalCount: 1,
672+
},
673+
{
674+
name: "fetch swaps-outs no pagination",
675+
mockSwaps: []loop.SwapInfo{swapIn1, swapOut1, swapOut2},
676+
req: &looprpc.ListSwapsRequest{ListSwapFilter: &looprpc.ListSwapsFilter{SwapType: looprpc.ListSwapsFilter_LOOP_OUT}},
677+
expectedReturnedSwapCount: 2,
678+
expectedFirstIdx: 0,
679+
expectedLastIdx: 2,
680+
expectedFilteredTotalCount: 2,
681+
},
682+
{
683+
name: "fetch swaps-outs increment start_index",
684+
mockSwaps: []loop.SwapInfo{swapIn1, swapOut1, swapOut2},
685+
req: &looprpc.ListSwapsRequest{
686+
ListSwapFilter: &looprpc.ListSwapsFilter{
687+
SwapType: looprpc.ListSwapsFilter_LOOP_OUT},
688+
IndexOffset: 1,
689+
},
690+
expectedReturnedSwapCount: 1,
691+
expectedFirstIdx: 1,
692+
expectedLastIdx: 2,
693+
expectedFilteredTotalCount: 2,
694+
},
695+
{
696+
name: "fetch all swaps set swap limit",
697+
mockSwaps: []loop.SwapInfo{swapIn1, swapOut1, swapOut2},
698+
req: &looprpc.ListSwapsRequest{
699+
MaxSwaps: 2,
700+
},
701+
expectedReturnedSwapCount: 2,
702+
expectedFirstIdx: 0,
703+
expectedLastIdx: 2,
704+
expectedFilteredTotalCount: 3,
705+
},
706+
{
707+
name: "fetch all swaps set swap limit set start index",
708+
mockSwaps: []loop.SwapInfo{swapIn1, swapOut1, swapOut2},
709+
req: &looprpc.ListSwapsRequest{
710+
711+
IndexOffset: 1,
712+
MaxSwaps: 1,
713+
},
714+
expectedReturnedSwapCount: 1,
715+
expectedFirstIdx: 1,
716+
expectedLastIdx: 2,
717+
expectedFilteredTotalCount: 3,
718+
},
719+
{
720+
name: "fetch all swaps set start index out of bounds",
721+
mockSwaps: []loop.SwapInfo{swapIn1, swapOut1, swapOut2},
722+
req: &looprpc.ListSwapsRequest{
723+
IndexOffset: 5,
724+
},
725+
expectedReturnedSwapCount: 0,
726+
expectedFirstIdx: 5,
727+
expectedLastIdx: 3,
728+
expectedFilteredTotalCount: 3,
729+
},
730+
{
731+
name: "fetch all swaps set limit to 0",
732+
mockSwaps: []loop.SwapInfo{swapIn1, swapOut1, swapOut2},
733+
req: &looprpc.ListSwapsRequest{
734+
MaxSwaps: 0,
735+
},
736+
expectedReturnedSwapCount: 3,
737+
expectedFirstIdx: 0,
738+
expectedLastIdx: 3,
739+
expectedFilteredTotalCount: 3,
740+
},
741+
}
742+
743+
for _, test := range tests {
744+
t.Run(test.name, func(t *testing.T) {
745+
t.Parallel()
746+
747+
// Create the swap client server with our mock client
748+
server := &swapClientServer{
749+
swaps: make(map[lntypes.Hash]loop.SwapInfo),
750+
}
751+
752+
// Populate the server's swap cache with our mock swaps
753+
for _, swap := range test.mockSwaps {
754+
server.swaps[swap.SwapHash] = swap
755+
}
756+
757+
// Call the ListSwaps method
758+
resp, err := server.ListSwaps(context.Background(), test.req)
759+
require.NoError(t, err)
760+
761+
// Verify the response matches our expectations
762+
require.Equal(t, test.expectedReturnedSwapCount, len(resp.Swaps))
763+
require.Equal(t, test.expectedFirstIdx, resp.FirstIndexOffset)
764+
require.Equal(t, test.expectedLastIdx, resp.LastIndexOffset)
765+
require.Equal(t, test.expectedFilteredTotalCount, resp.TotalFilteredSwaps)
766+
})
767+
}
768+
}

0 commit comments

Comments
 (0)