catbase/bot/mock.go

132 lines
4.9 KiB
Go
Raw Normal View History

// © 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"
2019-02-07 16:30:42 +00:00
"net/http"
"regexp"
2017-10-31 13:40:03 +00:00
"strconv"
"strings"
"github.com/jmoiron/sqlx"
2019-03-07 16:35:42 +00:00
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/mock"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
"github.com/velour/catbase/config"
)
type MockBot struct {
mock.Mock
db *sqlx.DB
Cfg *config.Config
Messages []string
Actions []string
Reactions []string
}
2019-05-27 23:21:53 +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{} }
func (mb *MockBot) WhoAmI() string { return "tester" }
func (mb *MockBot) DefaultConnector() Connector { return nil }
func (mb *MockBot) GetPassword() string { return "12345" }
2020-04-29 21:45:53 +00:00
func (mb *MockBot) SetQuiet(bool) {}
func (mb *MockBot) Send(c Connector, kind Kind, args ...any) (string, error) {
switch kind {
case Message:
mb.Messages = append(mb.Messages, args[1].(string))
return fmt.Sprintf("m-%d", len(mb.Actions)-1), nil
case Action:
mb.Actions = append(mb.Actions, args[1].(string))
return fmt.Sprintf("a-%d", len(mb.Actions)-1), nil
case Edit:
ch, m, id := args[0].(string), args[1].(string), args[2].(string)
2019-05-27 23:21:53 +00:00
return mb.edit(c, ch, m, id)
case Reaction:
ch, re, msg := args[0].(string), args[1].(string), args[2].(msg.Message)
2019-05-27 23:21:53 +00:00
return mb.react(c, ch, re, msg)
}
return "ERR", fmt.Errorf("Mesasge type unhandled")
2017-11-02 20:32:02 +00:00
}
2021-01-31 23:08:25 +00:00
func (mb *MockBot) AddPlugin(f Plugin) {}
func (mb *MockBot) Register(p Plugin, kind Kind, cb Callback) {}
2021-02-01 15:45:41 +00:00
func (mb *MockBot) RegisterTable(p Plugin, hs HandlerTable) {}
2021-01-31 23:08:25 +00:00
func (mb *MockBot) RegisterRegex(p Plugin, kind Kind, r *regexp.Regexp, h ResponseHandler) {}
func (mb *MockBot) RegisterRegexCmd(p Plugin, kind Kind, r *regexp.Regexp, h ResponseHandler) {}
func (mb *MockBot) RegisterWebName(_ http.Handler, _, _ string) {}
func (mb *MockBot) RegisterWeb(_ http.Handler, _ string) {}
2021-01-31 23:08:25 +00:00
func (mb *MockBot) GetWebNavigation() []EndPoint { return nil }
func (mb *MockBot) Receive(c Connector, kind Kind, msg msg.Message, args ...any) bool {
2019-05-27 23:21:53 +00:00
return false
}
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 }
2020-10-09 16:53:50 +00:00
func (mb *MockBot) CheckAdmin(nick string) bool { return nick == "admin" }
2019-05-27 23:21:53 +00:00
func (mb *MockBot) react(c Connector, channel, reaction string, message msg.Message) (string, error) {
mb.Reactions = append(mb.Reactions, reaction)
return "", nil
}
2017-10-31 13:40:03 +00:00
2019-05-27 23:21:53 +00:00
func (mb *MockBot) edit(c Connector, channel, newMessage, identifier string) (string, error) {
2017-10-31 13:40:03 +00:00
isMessage := identifier[0] == 'm'
if !isMessage && identifier[0] != 'a' {
err := fmt.Errorf("failed to parse identifier: %s", identifier)
2019-03-07 16:35:42 +00:00
log.Error().Err(err)
return "", err
2017-10-31 13:40:03 +00:00
}
index, err := strconv.Atoi(strings.Split(identifier, "-")[1])
if err != nil {
err := fmt.Errorf("failed to parse identifier: %s", identifier)
2019-03-07 16:35:42 +00:00
log.Error().Err(err)
return "", err
2017-10-31 13:40:03 +00:00
}
if isMessage {
if index < len(mb.Messages) {
mb.Messages[index] = newMessage
} else {
return "", fmt.Errorf("No message")
2017-10-31 13:40:03 +00:00
}
} else {
if index < len(mb.Actions) {
mb.Actions[index] = newMessage
} else {
return "", fmt.Errorf("No action")
2017-10-31 13:40:03 +00:00
}
}
return "", nil
2017-11-02 20:32:02 +00:00
}
func (mb *MockBot) GetEmojiList(bool) map[string]string { return make(map[string]string) }
func (mb *MockBot) DeleteEmojy(name string) error { return nil }
func (mb *MockBot) UploadEmojy(emojy, path string) error { return nil }
2017-10-31 13:40:03 +00:00
func (mb *MockBot) RegisterFilter(s string, f func(string) string) {}
func NewMockBot() *MockBot {
cfg := config.ReadConfig("file::memory:?mode=memory&cache=shared")
b := MockBot{
Cfg: cfg,
Messages: make([]string, 0),
Actions: make([]string, 0),
}
2019-02-07 16:30:42 +00:00
// If any plugin registered a route, we need to reset those before any new test
http.DefaultServeMux = new(http.ServeMux)
return &b
}
func (mb *MockBot) GetPluginNames() []string { return nil }
func (mb *MockBot) RefreshPluginBlacklist() error { return nil }
func (mb *MockBot) RefreshPluginWhitelist() error { return nil }
func (mb *MockBot) GetWhitelist() []string { return []string{} }
func (mb *MockBot) OnBlacklist(ch, p string) bool { return false }
func (mb *MockBot) URLFormat(title, url string) string { return title + url }
func (mb *MockBot) CheckPassword(secret, password string) bool { return true }
func (mb *MockBot) ListenAndServe() {}
func (mb *MockBot) PubToASub(subject string, payload any) {}