2016-03-30 14:00:20 +00:00
|
|
|
// © 2016 the CatBase Authors under the WTFPL license. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
2017-10-31 13:40:03 +00:00
|
|
|
"fmt"
|
2016-03-30 14:00:20 +00:00
|
|
|
"log"
|
2017-10-31 13:40:03 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-03-30 14:00:20 +00:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/stretchr/testify/mock"
|
2016-04-01 14:20:03 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
2016-03-30 14:00:20 +00:00
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MockBot struct {
|
|
|
|
mock.Mock
|
|
|
|
db *sqlx.DB
|
|
|
|
|
2019-01-20 20:21:26 +00:00
|
|
|
Cfg *config.Config
|
2016-05-11 01:15:52 +00:00
|
|
|
|
2019-01-20 17:33:19 +00:00
|
|
|
Messages []string
|
|
|
|
Actions []string
|
|
|
|
Reactions []string
|
2016-03-30 14:00:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 15:54:13 +00:00
|
|
|
func (mb *MockBot) Config() *config.Config { return mb.Cfg }
|
|
|
|
func (mb *MockBot) DB() *sqlx.DB { return mb.Cfg.DB }
|
|
|
|
func (mb *MockBot) Who(string) []user.User { return []user.User{} }
|
2019-02-05 18:33:18 +00:00
|
|
|
func (mb *MockBot) Send(kind Kind, args ...interface{}) (string, error) {
|
2019-02-05 16:36:18 +00:00
|
|
|
switch kind {
|
|
|
|
case Message:
|
|
|
|
mb.Messages = append(mb.Messages, args[1].(string))
|
2019-02-05 18:33:18 +00:00
|
|
|
return fmt.Sprintf("m-%d", len(mb.Actions)-1), nil
|
2019-02-05 16:36:18 +00:00
|
|
|
case Action:
|
|
|
|
mb.Actions = append(mb.Actions, args[1].(string))
|
2019-02-05 18:33:18 +00:00
|
|
|
return fmt.Sprintf("a-%d", len(mb.Actions)-1), nil
|
2019-02-05 16:36:18 +00:00
|
|
|
case Edit:
|
|
|
|
ch, m, id := args[0].(string), args[1].(string), args[2].(string)
|
|
|
|
return mb.edit(ch, m, id)
|
|
|
|
case Reaction:
|
|
|
|
ch, re, msg := args[0].(string), args[1].(string), args[2].(msg.Message)
|
|
|
|
return mb.react(ch, re, msg)
|
|
|
|
}
|
2019-02-05 18:33:18 +00:00
|
|
|
return "ERR", fmt.Errorf("Mesasge type unhandled")
|
2017-11-02 20:32:02 +00:00
|
|
|
}
|
2019-02-05 17:25:31 +00:00
|
|
|
func (mb *MockBot) AddPlugin(name string, f Plugin) {}
|
|
|
|
func (mb *MockBot) Register(name string, kind Kind, cb Callback) {}
|
|
|
|
func (mb *MockBot) Receive(kind Kind, msg msg.Message, args ...interface{}) {}
|
|
|
|
func (mb *MockBot) Filter(msg msg.Message, s string) string { return s }
|
|
|
|
func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil }
|
|
|
|
func (mb *MockBot) CheckAdmin(nick string) bool { return false }
|
2017-07-25 10:15:44 +00:00
|
|
|
|
2019-02-05 18:33:18 +00:00
|
|
|
func (mb *MockBot) react(channel, reaction string, message msg.Message) (string, error) {
|
2019-01-20 17:33:19 +00:00
|
|
|
mb.Reactions = append(mb.Reactions, reaction)
|
2019-02-05 18:33:18 +00:00
|
|
|
return "", nil
|
2019-01-20 17:33:19 +00:00
|
|
|
}
|
2017-10-31 13:40:03 +00:00
|
|
|
|
2019-02-05 18:33:18 +00:00
|
|
|
func (mb *MockBot) edit(channel, newMessage, identifier string) (string, error) {
|
2017-10-31 13:40:03 +00:00
|
|
|
isMessage := identifier[0] == 'm'
|
|
|
|
if !isMessage && identifier[0] != 'a' {
|
2019-02-05 16:36:18 +00:00
|
|
|
err := fmt.Errorf("failed to parse identifier: %s", identifier)
|
|
|
|
log.Println(err)
|
2019-02-05 18:33:18 +00:00
|
|
|
return "", err
|
2017-10-31 13:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
index, err := strconv.Atoi(strings.Split(identifier, "-")[1])
|
|
|
|
if err != nil {
|
2019-02-05 16:36:18 +00:00
|
|
|
err := fmt.Errorf("failed to parse identifier: %s", identifier)
|
|
|
|
log.Println(err)
|
2019-02-05 18:33:18 +00:00
|
|
|
return "", err
|
2017-10-31 13:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if isMessage {
|
|
|
|
if index < len(mb.Messages) {
|
|
|
|
mb.Messages[index] = newMessage
|
|
|
|
} else {
|
2019-02-05 18:33:18 +00:00
|
|
|
return "", fmt.Errorf("No message")
|
2017-10-31 13:40:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if index < len(mb.Actions) {
|
|
|
|
mb.Actions[index] = newMessage
|
|
|
|
} else {
|
2019-02-05 18:33:18 +00:00
|
|
|
return "", fmt.Errorf("No action")
|
2017-10-31 13:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-05 18:33:18 +00:00
|
|
|
return "", nil
|
2017-11-02 20:32:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 13:40:03 +00:00
|
|
|
func (mb *MockBot) GetEmojiList() map[string]string { return make(map[string]string) }
|
|
|
|
func (mb *MockBot) RegisterFilter(s string, f func(string) string) {}
|
2017-07-25 10:15:44 +00:00
|
|
|
|
2016-03-30 14:00:20 +00:00
|
|
|
func NewMockBot() *MockBot {
|
2019-01-21 17:36:02 +00:00
|
|
|
cfg := config.ReadConfig("file::memory:?mode=memory&cache=shared")
|
2016-03-30 14:00:20 +00:00
|
|
|
b := MockBot{
|
2019-01-20 20:21:26 +00:00
|
|
|
Cfg: cfg,
|
2016-03-30 14:00:20 +00:00
|
|
|
Messages: make([]string, 0),
|
|
|
|
Actions: make([]string, 0),
|
|
|
|
}
|
|
|
|
return &b
|
|
|
|
}
|