2016-05-09 20:45:02 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
package babbler
|
|
|
|
|
|
|
|
import (
|
2021-12-21 19:08:20 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"os"
|
2021-02-01 15:45:41 +00:00
|
|
|
"regexp"
|
2016-05-09 20:45:02 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-01 15:45:41 +00:00
|
|
|
"github.com/velour/catbase/plugins/cli"
|
|
|
|
|
2016-05-09 20:45:02 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
|
|
|
)
|
|
|
|
|
2021-12-21 19:08:20 +00:00
|
|
|
func init() {
|
|
|
|
output := zerolog.ConsoleWriter{Out: os.Stdout}
|
|
|
|
log.Logger = log.Output(output).With().Caller().Stack().Logger()
|
|
|
|
}
|
|
|
|
|
2021-02-01 15:45:41 +00:00
|
|
|
func makeMessage(payload string, r *regexp.Regexp) bot.Request {
|
2019-05-27 23:21:53 +00:00
|
|
|
c := &cli.CliPlugin{}
|
2016-05-09 20:45:02 +00:00
|
|
|
isCmd := strings.HasPrefix(payload, "!")
|
|
|
|
if isCmd {
|
|
|
|
payload = payload[1:]
|
|
|
|
}
|
2021-12-21 19:08:20 +00:00
|
|
|
log.Debug().Msgf("isCmd: %v, payload: %v", isCmd, payload)
|
2021-02-01 15:45:41 +00:00
|
|
|
return bot.Request{
|
|
|
|
Conn: c,
|
|
|
|
Kind: bot.Message,
|
|
|
|
Values: bot.ParseValues(r, payload),
|
|
|
|
Msg: msg.Message{
|
|
|
|
User: &user.User{Name: "tester"},
|
|
|
|
Channel: "test",
|
|
|
|
Body: payload,
|
|
|
|
Command: isCmd,
|
|
|
|
},
|
2016-05-09 20:45:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 19:08:20 +00:00
|
|
|
func newBabblerPlugin(mb *bot.MockBot) (*BabblerPlugin, func()) {
|
2019-01-19 14:43:33 +00:00
|
|
|
bp := New(mb)
|
|
|
|
bp.WithGoRoutines = false
|
2021-12-21 19:08:20 +00:00
|
|
|
return bp, func() {
|
|
|
|
mb.TearDown()
|
|
|
|
}
|
2019-01-19 14:43:33 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 15:45:41 +00:00
|
|
|
func testMessage(p *BabblerPlugin, msg string) bool {
|
|
|
|
for _, h := range p.handlers {
|
|
|
|
if h.Regex.MatchString(msg) {
|
|
|
|
req := makeMessage(msg, h.Regex)
|
|
|
|
if h.Handler(req) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-10-19 23:34:16 +00:00
|
|
|
func TestBabblerNoBabbler(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2021-02-01 15:45:41 +00:00
|
|
|
testMessage(bp, "!seabass2 says")
|
2021-12-21 19:08:20 +00:00
|
|
|
res := assert.Len(t, mb.Messages, 1)
|
2016-10-19 23:34:16 +00:00
|
|
|
assert.True(t, res)
|
2021-12-21 19:08:20 +00:00
|
|
|
assert.Contains(t, mb.Messages[0], "seabass2 babbler not found")
|
2016-10-19 23:34:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBabblerNothingSaid(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2021-02-01 15:45:41 +00:00
|
|
|
res := testMessage(bp, "initialize babbler for seabass")
|
2017-05-10 12:41:41 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
res = testMessage(bp, "seabass says")
|
2017-05-10 12:41:41 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
if assert.Len(t, mb.Messages, 2) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "okay.")
|
|
|
|
assert.Contains(t, mb.Messages[1], "seabass hasn't said anything yet.")
|
|
|
|
}
|
2016-10-19 23:34:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 20:45:02 +00:00
|
|
|
func TestBabbler(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, _ := newBabblerPlugin(mb)
|
|
|
|
//defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2021-02-01 15:45:41 +00:00
|
|
|
testMessage(bp, "!initialize babbler for tester")
|
|
|
|
testMessage(bp, "This is a message")
|
|
|
|
testMessage(bp, "This is another message")
|
|
|
|
testMessage(bp, "This is a long message")
|
|
|
|
res := testMessage(bp, "!tester says")
|
2021-12-21 19:08:20 +00:00
|
|
|
log.Debug().Msgf("messages: %+v", mb.Messages)
|
2016-05-09 20:45:02 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "this is")
|
|
|
|
assert.Contains(t, mb.Messages[0], "message")
|
|
|
|
}
|
2016-05-09 20:45:02 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 23:34:16 +00:00
|
|
|
func TestBabblerSeed(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2021-02-01 15:45:41 +00:00
|
|
|
|
|
|
|
testMessage(bp, "This is a message")
|
|
|
|
testMessage(bp, "This is another message")
|
|
|
|
testMessage(bp, "This is a long message")
|
|
|
|
res := testMessage(bp, "tester says long")
|
2016-10-19 23:34:16 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "long message")
|
|
|
|
}
|
2016-10-19 23:34:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 19:35:18 +00:00
|
|
|
func TestBabblerMultiSeed(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2017-04-07 19:35:18 +00:00
|
|
|
|
2021-02-01 15:45:41 +00:00
|
|
|
testMessage(bp, "This is a message")
|
|
|
|
testMessage(bp, "This is another message")
|
|
|
|
testMessage(bp, "This is a long message")
|
|
|
|
res := testMessage(bp, "tester says is another")
|
2017-04-07 19:35:18 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "is another")
|
|
|
|
}
|
2017-04-07 19:35:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 23:34:16 +00:00
|
|
|
func TestBabblerBadSeed(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2021-02-01 15:45:41 +00:00
|
|
|
|
|
|
|
testMessage(bp, "This is a message")
|
|
|
|
testMessage(bp, "This is another message")
|
|
|
|
testMessage(bp, "This is a long message")
|
|
|
|
res := testMessage(bp, "tester says this is bad")
|
|
|
|
assert.True(t, res)
|
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "tester never said 'this is bad'")
|
|
|
|
}
|
2017-04-07 19:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBabblerBadSeed2(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2021-02-01 15:45:41 +00:00
|
|
|
|
|
|
|
testMessage(bp, "This is a message")
|
|
|
|
testMessage(bp, "This is another message")
|
|
|
|
testMessage(bp, "This is a long message")
|
|
|
|
res := testMessage(bp, "tester says This is a really")
|
|
|
|
assert.True(t, res)
|
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "tester never said 'this is a really'")
|
|
|
|
}
|
2016-10-19 23:34:16 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 23:53:19 +00:00
|
|
|
func TestBabblerSuffixSeed(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2021-02-01 15:45:41 +00:00
|
|
|
|
|
|
|
testMessage(bp, "This is message one")
|
|
|
|
testMessage(bp, "It's easier to test with unique messages")
|
|
|
|
testMessage(bp, "tester says-tail message one")
|
|
|
|
res := testMessage(bp, "tester says-tail with unique")
|
2017-06-05 23:53:19 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
if assert.Len(t, mb.Messages, 2) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "this is message one")
|
|
|
|
assert.Contains(t, mb.Messages[1], "it's easier to test with unique")
|
|
|
|
}
|
2017-06-05 23:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBabblerBadSuffixSeed(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2021-02-01 15:45:41 +00:00
|
|
|
|
|
|
|
testMessage(bp, "This is message one")
|
|
|
|
testMessage(bp, "It's easier to test with unique messages")
|
|
|
|
res := testMessage(bp, "tester says-tail anything true")
|
2017-06-05 23:53:19 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "tester never said 'anything true'")
|
|
|
|
}
|
2017-06-05 23:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 00:03:07 +00:00
|
|
|
func TestBabblerBookendSeed(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2017-06-07 00:03:07 +00:00
|
|
|
|
2021-02-01 15:45:41 +00:00
|
|
|
testMessage(bp, "This is message one")
|
|
|
|
testMessage(bp, "It's easier to test with unique messages")
|
|
|
|
res := testMessage(bp, "tester says-bridge it's easier | unique messages")
|
2017-06-07 00:03:07 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages")
|
|
|
|
}
|
2017-06-07 00:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBabblerBadBookendSeed(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2021-02-01 15:45:41 +00:00
|
|
|
|
|
|
|
testMessage(bp, "This is message one")
|
|
|
|
testMessage(bp, "It's easier to test with unique messages")
|
|
|
|
res := testMessage(bp, "tester says-bridge says-bridge It's easier | not unique messages")
|
2017-06-07 00:03:07 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "tester never said 'it's easier ... not unique messages'")
|
|
|
|
}
|
2017-06-07 00:03:07 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 00:02:42 +00:00
|
|
|
func TestBabblerMiddleOutSeed(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2021-02-01 15:45:41 +00:00
|
|
|
|
|
|
|
testMessage(bp, "This is message one")
|
|
|
|
testMessage(bp, "It's easier to test with unique messages")
|
|
|
|
res := testMessage(bp, "tester says-middle-out test with")
|
2017-06-08 00:02:42 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages")
|
|
|
|
}
|
2017-06-08 00:02:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-16 19:02:28 +00:00
|
|
|
func TestBabblerBadMiddleOutSeed(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2017-06-16 19:02:28 +00:00
|
|
|
|
2021-02-01 15:45:41 +00:00
|
|
|
testMessage(bp, "This is message one")
|
|
|
|
testMessage(bp, "It's easier to test with unique messages")
|
|
|
|
res := testMessage(bp, "tester says-middle-out anything true")
|
2016-05-11 17:07:16 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "tester never said 'anything true'")
|
|
|
|
}
|
2016-05-11 17:07:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 15:06:22 +00:00
|
|
|
func TestBabblerMerge(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2016-05-26 15:06:22 +00:00
|
|
|
|
2021-02-01 15:45:41 +00:00
|
|
|
testMessage(bp, "<tester> This is a message")
|
2016-05-26 15:06:22 +00:00
|
|
|
assert.Len(t, mb.Messages, 0)
|
|
|
|
|
2021-02-01 15:45:41 +00:00
|
|
|
testMessage(bp, "<tester> This is another message")
|
|
|
|
testMessage(bp, "<tester> This is a long message")
|
|
|
|
res := testMessage(bp, "merge babbler tester into tester2")
|
2016-05-26 15:06:22 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "mooooiggged")
|
|
|
|
}
|
2016-05-26 15:06:22 +00:00
|
|
|
|
2021-02-01 15:45:41 +00:00
|
|
|
res = testMessage(bp, "!tester2 says")
|
2016-05-26 15:06:22 +00:00
|
|
|
assert.True(t, res)
|
2021-02-01 15:45:41 +00:00
|
|
|
if assert.Len(t, mb.Messages, 2) {
|
|
|
|
assert.Contains(t, mb.Messages[1], "<tester2> this is")
|
|
|
|
assert.Contains(t, mb.Messages[1], "message")
|
|
|
|
}
|
2016-05-26 15:06:22 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 20:45:02 +00:00
|
|
|
func TestHelp(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
2021-12-21 19:08:20 +00:00
|
|
|
bp, td := newBabblerPlugin(mb)
|
|
|
|
defer td()
|
2019-01-19 14:43:33 +00:00
|
|
|
assert.NotNil(t, bp)
|
2019-05-27 23:21:53 +00:00
|
|
|
c := &cli.CliPlugin{}
|
|
|
|
bp.help(c, bot.Help, msg.Message{Channel: "channel"}, []string{})
|
2016-05-09 20:45:02 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
}
|