github.com/0xsequence/ethkit@v1.25.0/cmd/ethkit/balance_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "math" 7 "strconv" 8 "strings" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func execBalanceCmd(args string) (string, error) { 15 cmd := NewBalanceCmd() 16 actual := new(bytes.Buffer) 17 cmd.SetOut(actual) 18 cmd.SetErr(actual) 19 cmd.SetArgs(strings.Split(args, " ")) 20 if err := cmd.Execute(); err != nil { 21 return "", err 22 } 23 24 return actual.String(), nil 25 } 26 27 func Test_BalanceCmd(t *testing.T) { 28 res, err := execBalanceCmd("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia") 29 assert.Nil(t, err) 30 assert.NotNil(t, res) 31 } 32 33 func Test_BalanceCmd_ValidWei(t *testing.T) { 34 res, err := execBalanceCmd("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia") 35 assert.Nil(t, err) 36 assert.Equal(t, res, fmt.Sprintln(strconv.Itoa(500_000_000_000_000_000), "wei")) 37 } 38 39 func Test_BalanceCmd_ValidEther(t *testing.T) { 40 res, err := execBalanceCmd("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia --ether") 41 assert.Nil(t, err) 42 assert.Equal(t, res, fmt.Sprintln(strconv.FormatFloat(0.5, 'f', -1, 64), "ether")) 43 } 44 45 func Test_BalanceCmd_InvalidAddress(t *testing.T) { 46 res, err := execBalanceCmd("0x1 --rpc-url https://nodes.sequence.app/sepolia") 47 assert.NotNil(t, err) 48 assert.Empty(t, res) 49 assert.Contains(t, err.Error(), "please provide a valid account address") 50 } 51 52 func Test_BalanceCmd_InvalidRPC(t *testing.T) { 53 res, err := execBalanceCmd("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url nodes.sequence.app/sepolia") 54 assert.NotNil(t, err) 55 assert.Empty(t, res) 56 assert.Contains(t, err.Error(), "please provide a valid rpc url") 57 } 58 59 func Test_BalanceCmd_NotExistingBlockHeigh(t *testing.T) { 60 res, err := execBalanceCmd("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia --block " + fmt.Sprint(math.MaxInt64)) 61 assert.NotNil(t, err) 62 assert.Empty(t, res) 63 assert.Contains(t, err.Error(), "jsonrpc error -32000: header not found") 64 } 65 66 func Test_BalanceCmd_NotAValidStringBlockHeigh(t *testing.T) { 67 res, err := execBalanceCmd("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia --block something") 68 assert.NotNil(t, err) 69 assert.Empty(t, res) 70 assert.Contains(t, err.Error(), "invalid block height") 71 } 72 73 func Test_BalanceCmd_NotAValidNumberBlockHeigh(t *testing.T) { 74 res, err := execBalanceCmd("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia --block -100") 75 assert.NotNil(t, err) 76 assert.Empty(t, res) 77 }