catbase/bot/mock.go

145 lines
5.2 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"
2021-12-21 04:31:19 +00:00
bh "github.com/timshannon/bolthold"
"io/ioutil"
2019-02-07 16:30:42 +00:00
"net/http"
2021-12-21 04:31:19 +00:00
"os"
"regexp"
2017-10-31 13:40:03 +00:00
"strconv"
"strings"
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
2021-12-21 04:31:19 +00:00
store *bh.Store
Cfg *config.Config
Messages []string
Actions []string
Reactions []string
2021-12-21 04:31:19 +00:00
storeFile *os.File
}
2019-05-27 23:21:53 +00:00
func (mb *MockBot) Config() *config.Config { return mb.Cfg }
2021-12-21 04:31:19 +00:00
func (mb *MockBot) Store() *bh.Store { return mb.Cfg.Store() }
2019-05-27 23:21:53 +00:00
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) {}
2019-05-27 23:21:53 +00:00
func (mb *MockBot) Send(c Connector, kind Kind, args ...interface{}) (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 }
2019-05-27 23:21:53 +00:00
func (mb *MockBot) Receive(c Connector, kind Kind, msg msg.Message, args ...interface{}) bool {
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
}
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) {}
func NewMockBot() *MockBot {
2021-12-21 04:31:19 +00:00
storeFile, err := ioutil.TempFile(os.TempDir(), "prefix-")
if err != nil {
panic(err)
}
2021-12-21 19:08:20 +00:00
log.Debug().Msgf("MockBot temp store: %s", storeFile.Name())
2021-12-21 04:31:19 +00:00
cfg := config.ReadConfig("file::memory:?mode=memory&cache=shared", storeFile.Name())
b := MockBot{
2021-12-21 04:31:19 +00:00
Cfg: cfg,
Messages: make([]string, 0),
Actions: make([]string, 0),
storeFile: storeFile,
store: cfg.Store(),
}
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
}
2021-11-16 01:34:09 +00:00
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 interface{}) {}
2021-12-21 04:31:19 +00:00
func (mb *MockBot) TearDown() error {
mb.store.Close()
return os.Remove(mb.storeFile.Name())
}