bbgo_origin/pkg/interact/interact_test.go

144 lines
3.5 KiB
Go
Raw Normal View History

2022-01-12 13:45:12 +00:00
package interact
2022-01-11 18:54:13 +00:00
import (
"bytes"
2022-01-11 18:54:13 +00:00
"errors"
"io"
2022-01-11 18:54:13 +00:00
"testing"
"github.com/stretchr/testify/assert"
2022-01-13 14:15:05 +00:00
tb "gopkg.in/tucnak/telebot.v2"
2022-01-11 18:54:13 +00:00
)
func Test_parseFuncArgsAndCall_NoErrorFunction(t *testing.T) {
noErrorFunc := func(a string, b float64, c bool) error {
assert.Equal(t, "BTCUSDT", a)
assert.Equal(t, 0.123, b)
assert.Equal(t, true, c)
return nil
}
_, err := parseFuncArgsAndCall(noErrorFunc, []string{"BTCUSDT", "0.123", "true"})
2022-01-11 18:54:13 +00:00
assert.NoError(t, err)
}
func Test_parseFuncArgsAndCall_ErrorFunction(t *testing.T) {
errorFunc := func(a string, b float64) error {
return errors.New("error")
}
_, err := parseFuncArgsAndCall(errorFunc, []string{"BTCUSDT", "0.123"})
2022-01-11 18:54:13 +00:00
assert.Error(t, err)
}
func Test_parseFuncArgsAndCall_InterfaceInjection(t *testing.T) {
f := func(w io.Writer, a string, b float64) error {
_, err := w.Write([]byte("123"))
return err
}
2022-01-11 18:54:13 +00:00
2022-01-12 17:48:14 +00:00
buf := bytes.NewBuffer(nil)
_, err := parseFuncArgsAndCall(f, []string{"BTCUSDT", "0.123"}, buf)
2022-01-12 17:48:14 +00:00
assert.NoError(t, err)
assert.Equal(t, "123", buf.String())
2022-01-11 18:54:13 +00:00
}
func Test_parseCommand(t *testing.T) {
2022-01-12 09:49:34 +00:00
args := parseCommand(`closePosition "BTC USDT" 3.1415926 market`)
t.Logf("args: %+v", args)
for i, a := range args {
t.Logf("args(%d): %#v", i, a)
}
2022-01-11 18:54:13 +00:00
2022-01-12 09:49:34 +00:00
assert.Equal(t, 4, len(args))
assert.Equal(t, "closePosition", args[0])
assert.Equal(t, "BTC USDT", args[1])
assert.Equal(t, "3.1415926", args[2])
assert.Equal(t, "market", args[3])
2022-01-11 18:54:13 +00:00
}
2022-01-12 13:45:12 +00:00
type closePositionTask struct {
symbol string
percentage float64
confirmed bool
}
2022-01-12 13:45:12 +00:00
type TestInteraction struct {
closePositionTask closePositionTask
2022-01-12 13:45:12 +00:00
}
func (m *TestInteraction) Commands(interact *Interact) {
2022-01-13 18:57:39 +00:00
interact.Command("/closePosition", "", func(reply Reply) error {
// send symbol options
return nil
}).Next(func(symbol string) error {
// get symbol from user
m.closePositionTask.symbol = symbol
2022-01-12 13:45:12 +00:00
// send percentage options
return nil
}).Next(func(percentage float64) error {
// get percentage from user
m.closePositionTask.percentage = percentage
2022-01-12 13:45:12 +00:00
// send confirmation
return nil
}).Next(func(confirmed bool) error {
m.closePositionTask.confirmed = confirmed
// call position close
2022-01-12 13:45:12 +00:00
// reply result
return nil
2022-01-12 13:45:12 +00:00
})
}
func TestCustomInteraction(t *testing.T) {
2022-01-13 14:15:05 +00:00
b, err := tb.NewBot(tb.Settings{
Offline: true,
})
if !assert.NoError(t, err, "should have bot setup without error") {
return
}
globalInteraction := New()
2022-01-13 14:15:05 +00:00
telegram := &Telegram{
2022-01-13 18:36:57 +00:00
Bot: b,
2022-01-13 14:15:05 +00:00
}
2022-01-16 11:06:26 +00:00
globalInteraction.AddMessenger(telegram)
2022-01-13 14:15:05 +00:00
testInteraction := &TestInteraction{}
testInteraction.Commands(globalInteraction)
2022-01-13 14:15:05 +00:00
err = globalInteraction.init()
assert.NoError(t, err)
m := &tb.Message{
Chat: &tb.Chat{ID: 22},
Sender: &tb.User{ID: 999},
}
session := telegram.loadSession(m)
err = globalInteraction.runCommand(session, "/closePosition", []string{}, telegram.newReply(session))
assert.NoError(t, err)
assert.Equal(t, State("/closePosition_1"), session.CurrentState)
err = globalInteraction.handleResponse(session, "BTCUSDT", telegram.newReply(session))
assert.NoError(t, err)
assert.Equal(t, State("/closePosition_2"), session.CurrentState)
err = globalInteraction.handleResponse(session, "0.20", telegram.newReply(session))
assert.NoError(t, err)
assert.Equal(t, State("/closePosition_3"), session.CurrentState)
err = globalInteraction.handleResponse(session, "true", telegram.newReply(session))
assert.NoError(t, err)
assert.Equal(t, State("public"), session.CurrentState)
2022-01-12 13:45:12 +00:00
assert.Equal(t, closePositionTask{
symbol: "BTCUSDT",
percentage: 0.2,
confirmed: true,
}, testInteraction.closePositionTask)
2022-01-12 13:45:12 +00:00
}