2022-01-12 13:45:12 +00:00
|
|
|
package interact
|
2022-01-11 18:54:13 +00:00
|
|
|
|
|
|
|
import (
|
2022-01-12 17:38:24 +00:00
|
|
|
"bytes"
|
2022-01-11 18:54:13 +00:00
|
|
|
"errors"
|
2022-01-12 17:38:24 +00:00
|
|
|
"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"})
|
|
|
|
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"})
|
|
|
|
assert.Error(t, err)
|
2022-01-12 17:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
2022-01-12 16:59:58 +00:00
|
|
|
type closePositionTask struct {
|
2022-01-12 17:38:24 +00:00
|
|
|
symbol string
|
2022-01-12 16:59:58 +00:00
|
|
|
percentage float64
|
2022-01-12 17:38:24 +00:00
|
|
|
confirmed bool
|
2022-01-12 16:59:58 +00:00
|
|
|
}
|
2022-01-12 13:45:12 +00:00
|
|
|
|
|
|
|
type TestInteraction struct {
|
2022-01-12 16:59:58 +00:00
|
|
|
closePositionTask closePositionTask
|
2022-01-12 13:45:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 16:59:58 +00:00
|
|
|
func (m *TestInteraction) Commands(interact *Interact) {
|
2022-01-13 14:15:05 +00:00
|
|
|
interact.Command("/closePosition", func(reply Reply) error {
|
2022-01-12 16:59:58 +00:00
|
|
|
// 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
|
|
|
|
2022-01-12 16:59:58 +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
|
|
|
|
2022-01-12 16:59:58 +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
|
|
|
|
2022-01-12 16:59:58 +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
|
|
|
|
}
|
|
|
|
|
2022-01-12 16:59:58 +00:00
|
|
|
globalInteraction := New()
|
2022-01-13 14:15:05 +00:00
|
|
|
|
|
|
|
telegram := &Telegram{
|
|
|
|
Bot: b,
|
|
|
|
Interact: globalInteraction,
|
|
|
|
}
|
|
|
|
globalInteraction.SetMessenger(telegram)
|
|
|
|
|
2022-01-12 16:59:58 +00:00
|
|
|
testInteraction := &TestInteraction{}
|
|
|
|
testInteraction.Commands(globalInteraction)
|
|
|
|
|
2022-01-13 14:15:05 +00:00
|
|
|
err = globalInteraction.init()
|
2022-01-12 16:59:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-01-13 14:15:05 +00:00
|
|
|
err = globalInteraction.runCommand("/closePosition", []string{}, telegram.newReply())
|
2022-01-12 16:59:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-01-13 14:15:05 +00:00
|
|
|
assert.Equal(t, State("/closePosition_1"), globalInteraction.currentState)
|
2022-01-12 16:59:58 +00:00
|
|
|
|
2022-01-13 14:15:05 +00:00
|
|
|
err = globalInteraction.handleResponse("BTCUSDT", telegram.newReply())
|
2022-01-12 16:59:58 +00:00
|
|
|
assert.NoError(t, err)
|
2022-01-13 14:15:05 +00:00
|
|
|
assert.Equal(t, State("/closePosition_2"), globalInteraction.currentState)
|
2022-01-12 16:59:58 +00:00
|
|
|
|
2022-01-13 14:15:05 +00:00
|
|
|
err = globalInteraction.handleResponse("0.20", telegram.newReply())
|
2022-01-12 16:59:58 +00:00
|
|
|
assert.NoError(t, err)
|
2022-01-13 14:15:05 +00:00
|
|
|
assert.Equal(t, State("/closePosition_3"), globalInteraction.currentState)
|
2022-01-12 16:59:58 +00:00
|
|
|
|
2022-01-13 14:15:05 +00:00
|
|
|
err = globalInteraction.handleResponse("true", telegram.newReply())
|
2022-01-12 16:59:58 +00:00
|
|
|
assert.NoError(t, err)
|
2022-01-13 14:15:05 +00:00
|
|
|
assert.Equal(t, State("public"), globalInteraction.currentState)
|
2022-01-12 13:45:12 +00:00
|
|
|
|
2022-01-12 16:59:58 +00:00
|
|
|
assert.Equal(t, closePositionTask{
|
|
|
|
symbol: "BTCUSDT",
|
|
|
|
percentage: 0.2,
|
|
|
|
confirmed: true,
|
|
|
|
}, testInteraction.closePositionTask)
|
2022-01-12 13:45:12 +00:00
|
|
|
}
|