Skip to content

Commit

Permalink
Merge pull request stakwork#2043 from stakwork/feat/build-v2keysendda…
Browse files Browse the repository at this point in the history
…ta-test

feat: Test TestBuildV2KeysendBodyData
  • Loading branch information
elraphty authored Dec 2, 2024
2 parents 9f1041c + ddc07f3 commit 324ec48
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
8 changes: 8 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
)

func GetPaginationParams(r *http.Request) (int, int, string, string, string) {
Expand Down Expand Up @@ -65,7 +66,14 @@ func BuildKeysendBodyData(amount uint, receiver_pubkey string, route_hint string
}

func BuildV2KeysendBodyData(amount uint, receiver_pubkey string, route_hint string, memo string) string {
// convert amount to msat
amountMsat := amount * 1000

// trim the memo
memo = strings.TrimSpace(memo)
// trim the route hint
route_hint = strings.TrimSpace(route_hint)

var bodyData string
if route_hint != "" {
bodyData = fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "%s", "data": "%s", "wait": true}`, amountMsat, receiver_pubkey, route_hint, memo)
Expand Down
101 changes: 101 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package utils

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBuildV2KeysendBodyData(t *testing.T) {
tests := []struct {
name string
amount uint
receiverPubkey string
routeHint string
memo string
expected string
expectError bool
}{
{
name: "Standard Input with Route Hint",
amount: 100,
receiverPubkey: "abcdef123456",
routeHint: "hint123",
memo: "Test transaction",
expected: `{"amt_msat": 100000, "dest": "abcdef123456", "route_hint": "hint123", "data": "Test transaction", "wait": true}`,
},
{
name: "Standard Input without Route Hint",
amount: 100,
receiverPubkey: "abcdef123456",
routeHint: "",
memo: "Test transaction",
expected: `{"amt_msat": 100000, "dest": "abcdef123456", "route_hint": "", "data": "Test transaction", "wait": true}`,
},
{
name: "Minimum Amount",
amount: 0,
receiverPubkey: "abcdef123456",
routeHint: "hint123",
memo: "Test transaction",
expected: `{"amt_msat": 0, "dest": "abcdef123456", "route_hint": "hint123", "data": "Test transaction", "wait": true}`,
},
{
name: "Empty Strings for All String Parameters",
amount: 100,
receiverPubkey: "",
routeHint: "",
memo: "",
expected: `{"amt_msat": 100000, "dest": "", "route_hint": "", "data": "", "wait": true}`,
},
{
name: "Large Amount",
amount: 4294967295,
receiverPubkey: "abcdef123456",
routeHint: "hint123",
memo: "Test transaction",
expected: `{"amt_msat": 4294967295000, "dest": "abcdef123456", "route_hint": "hint123", "data": "Test transaction", "wait": true}`,
},
{
name: "Long Strings",
amount: 100,
receiverPubkey: strings.Repeat("a", 1000),
routeHint: strings.Repeat("b", 1000),
memo: strings.Repeat("c", 1000),
expected: fmt.Sprintf(`{"amt_msat": 100000, "dest": "%s", "route_hint": "%s", "data": "%s", "wait": true}`, strings.Repeat("a", 1000), strings.Repeat("b", 1000), strings.Repeat("c", 1000)),
},
{
name: "Special Characters in Strings",
amount: 100,
receiverPubkey: "abc!@#123",
routeHint: "hint$%^",
memo: "Test &*() transaction",
expected: `{"amt_msat": 100000, "dest": "abc!@#123", "route_hint": "hint$%^", "data": "Test &*() transaction", "wait": true}`,
},
{
name: "Whitespace in Strings",
amount: 100,
receiverPubkey: "abcdef123456",
routeHint: "hint123 ",
memo: " Test transaction ",
expected: `{"amt_msat": 100000, "dest": "abcdef123456", "route_hint": "hint123", "data": "Test transaction", "wait": true}`,
},
{
name: "Non-ASCII Characters in Strings",
amount: 100,
receiverPubkey: "abcñ123",
routeHint: "hintü123",
memo: "Test transaction with emoji 😊",
expected: `{"amt_msat": 100000, "dest": "abcñ123", "route_hint": "hintü123", "data": "Test transaction with emoji 😊", "wait": true}`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := BuildV2KeysendBodyData(tt.amount, tt.receiverPubkey, tt.routeHint, tt.memo)
assert.JSONEq(t, tt.expected, result)
})
}
}

0 comments on commit 324ec48

Please sign in to comment.