184 lines
3.4 KiB
Go
184 lines
3.4 KiB
Go
package clients
|
|
|
|
import (
|
|
"btclock/broker"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMempoolClient_HandleMempoolBlocksMessage(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input map[string]any
|
|
expectedError bool
|
|
expectedFee float64
|
|
}{
|
|
{
|
|
name: "valid mempool blocks message",
|
|
input: map[string]any{
|
|
"mempool-blocks": []any{
|
|
map[string]any{
|
|
"blockSize": 1000.0,
|
|
"blockVSize": 2000.0,
|
|
"nTx": 100.0,
|
|
"totalFees": 500.0,
|
|
"medianFee": 5.5,
|
|
"feeRange": []any{1.0, 2.0, 3.0},
|
|
},
|
|
},
|
|
},
|
|
expectedError: false,
|
|
expectedFee: 5.5,
|
|
},
|
|
{
|
|
name: "invalid mempool blocks format",
|
|
input: map[string]any{
|
|
"mempool-blocks": "invalid",
|
|
},
|
|
expectedError: true,
|
|
},
|
|
{
|
|
name: "empty mempool blocks",
|
|
input: map[string]any{
|
|
"mempool-blocks": []any{},
|
|
},
|
|
expectedError: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
broker := broker.NewEventBroker()
|
|
client := &MempoolClient{
|
|
broker: broker,
|
|
}
|
|
|
|
err := client.handleMempoolBlocksMessage(tt.input)
|
|
if tt.expectedError {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMempoolClient_HandleBlocksMessage(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input map[string]any
|
|
expectedError bool
|
|
expectedHeight int
|
|
}{
|
|
{
|
|
name: "valid block message",
|
|
input: map[string]any{
|
|
"block": map[string]any{
|
|
"height": 800000.0,
|
|
},
|
|
},
|
|
expectedError: false,
|
|
expectedHeight: 800000,
|
|
},
|
|
{
|
|
name: "invalid block format",
|
|
input: map[string]any{
|
|
"block": "invalid",
|
|
},
|
|
expectedError: true,
|
|
},
|
|
{
|
|
name: "missing height",
|
|
input: map[string]any{
|
|
"block": map[string]any{},
|
|
},
|
|
expectedError: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
broker := broker.NewEventBroker()
|
|
client := &MempoolClient{
|
|
broker: broker,
|
|
}
|
|
|
|
err := client.handleBlocksMessage(tt.input)
|
|
if tt.expectedError {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMempoolClient_HandleBlocksArrayMessage(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input map[string]any
|
|
expectedError bool
|
|
expectedHeight int
|
|
}{
|
|
{
|
|
name: "valid blocks array message",
|
|
input: map[string]any{
|
|
"blocks": []any{
|
|
map[string]any{
|
|
"height": 799999.0,
|
|
},
|
|
map[string]any{
|
|
"height": 800000.0,
|
|
},
|
|
},
|
|
},
|
|
expectedError: false,
|
|
expectedHeight: 800000,
|
|
},
|
|
{
|
|
name: "invalid blocks format",
|
|
input: map[string]any{
|
|
"blocks": "invalid",
|
|
},
|
|
expectedError: true,
|
|
},
|
|
{
|
|
name: "empty blocks array",
|
|
input: map[string]any{
|
|
"blocks": []any{},
|
|
},
|
|
expectedError: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
broker := broker.NewEventBroker()
|
|
client := &MempoolClient{
|
|
broker: broker,
|
|
}
|
|
|
|
err := client.handleBlocksArrayMessage(tt.input)
|
|
if tt.expectedError {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMempoolClient_Init(t *testing.T) {
|
|
broker := broker.NewEventBroker()
|
|
client := NewMempoolClient()
|
|
|
|
err := client.Init(broker)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, broker, client.broker)
|
|
}
|
|
|
|
func TestMempoolClient_ID(t *testing.T) {
|
|
client := NewMempoolClient()
|
|
assert.Equal(t, "mempool", client.ID())
|
|
}
|