github.com/0xsequence/ethkit@v1.25.0/cmd/ethkit/block_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 "testing" 8 9 "github.com/0xsequence/ethkit/go-ethereum/common/math" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func execBlockCmd(args string) (string, error) { 14 cmd := NewBlockCmd() 15 actual := new(bytes.Buffer) 16 cmd.SetOut(actual) 17 cmd.SetErr(actual) 18 cmd.SetArgs(strings.Split(args, " ")) 19 if err := cmd.Execute(); err != nil { 20 return "", err 21 } 22 23 return actual.String(), nil 24 } 25 26 func Test_BlockCmd(t *testing.T) { 27 res, err := execBlockCmd("18855325 --rpc-url https://nodes.sequence.app/mainnet") 28 assert.Nil(t, err) 29 assert.NotNil(t, res) 30 } 31 32 func Test_BlockCmd_InvalidRpcUrl(t *testing.T) { 33 res, err := execBlockCmd("18855325 --rpc-url nodes.sequence.app/mainnet") 34 assert.Contains(t, err.Error(), "please provide a valid rpc url") 35 assert.Empty(t, res) 36 } 37 38 // Note: this test will eventually fail 39 func Test_BlockCmd_NotFound(t *testing.T) { 40 res, err := execBlockCmd(fmt.Sprint(math.MaxInt64) + " --rpc-url https://nodes.sequence.app/mainnet") 41 assert.Contains(t, err.Error(), "not found") 42 assert.Empty(t, res) 43 } 44 45 func Test_BlockCmd_InvalidBlockHeight(t *testing.T) { 46 res, err := execBlockCmd("invalid --rpc-url https://nodes.sequence.app/mainnet") 47 assert.Contains(t, err.Error(), "invalid block height") 48 assert.Empty(t, res) 49 } 50 51 func Test_BlockCmd_HeaderValidJSON(t *testing.T) { 52 res, err := execBlockCmd("18855325 --rpc-url https://nodes.sequence.app/mainnet --json") 53 assert.Nil(t, err) 54 h := Header{} 55 var p Printable 56 _ = p.FromStruct(h) 57 for k := range p { 58 assert.Contains(t, res, k) 59 } 60 } 61 62 func Test_BlockCmd_BlockValidJSON(t *testing.T) { 63 res, err := execBlockCmd("18855325 --rpc-url https://nodes.sequence.app/mainnet --full --json") 64 assert.Nil(t, err) 65 h := Block{} 66 var p Printable 67 _ = p.FromStruct(h) 68 for k := range p { 69 assert.Contains(t, res, k) 70 } 71 } 72 73 func Test_BlockCmd_BlockValidFieldHash(t *testing.T) { 74 // validating also that -f is case-insensitive 75 res, err := execBlockCmd("18855325 --rpc-url https://nodes.sequence.app/mainnet --full -f HASh") 76 assert.Nil(t, err) 77 assert.Equal(t, res, "0x97e5c24dc2fd74f6e56773a0ad1cf29fe403130ca6ec1dd10ff8828d72b0a352\n") 78 } 79 80 func Test_BlockCmd_BlockInvalidField(t *testing.T) { 81 res, err := execBlockCmd("18855325 --rpc-url https://nodes.sequence.app/mainnet --full -f invalid") 82 assert.Nil(t, err) 83 assert.Equal(t, res, "<nil>\n") 84 }