babbler: refactor

This commit is contained in:
Chris Sexton 2021-02-01 10:45:41 -05:00 committed by Chris Sexton
parent b7576bd855
commit a6847a996f
7 changed files with 277 additions and 271 deletions

View File

@ -230,6 +230,17 @@ func (b *bot) RegisterFilter(name string, f func(string) string) {
b.filters[name] = f b.filters[name] = f
} }
// RegisterTable registers multiple regex handlers at a time
func (b *bot) RegisterTable(p Plugin, handlers HandlerTable) {
for _, h := range handlers {
if h.IsCmd {
b.RegisterRegexCmd(p, h.Kind, h.Regex, h.Handler)
} else {
b.RegisterRegex(p, h.Kind, h.Regex, h.Handler)
}
}
}
// RegisterRegex does what register does, but with a matcher // RegisterRegex does what register does, but with a matcher
func (b *bot) RegisterRegex(p Plugin, kind Kind, r *regexp.Regexp, resp ResponseHandler) { func (b *bot) RegisterRegex(p Plugin, kind Kind, r *regexp.Regexp, resp ResponseHandler) {
t := reflect.TypeOf(p).String() t := reflect.TypeOf(p).String()

View File

@ -57,15 +57,15 @@ func (b *bot) runCallback(conn Connector, plugin Plugin, evt Kind, message msg.M
t := reflect.TypeOf(plugin).String() t := reflect.TypeOf(plugin).String()
for r, cbs := range b.callbacks[t][evt] { for r, cbs := range b.callbacks[t][evt] {
if r.MatchString(message.Body) { if r.MatchString(message.Body) {
req := Request{
Conn: conn,
Kind: evt,
Msg: message,
Values: ParseValues(r, message.Body),
Args: args,
}
for _, cb := range cbs { for _, cb := range cbs {
resp := Request{ if cb(req) {
Conn: conn,
Kind: evt,
Msg: message,
Values: ParseValues(r, message.Body),
Args: args,
}
if cb(resp) {
return true return true
} }
} }

View File

@ -61,6 +61,14 @@ type Callback func(Connector, Kind, msg.Message, ...interface{}) bool
type ResponseHandler func(Request) bool type ResponseHandler func(Request) bool
type CallbackMap map[string]map[Kind]map[*regexp.Regexp][]ResponseHandler type CallbackMap map[string]map[Kind]map[*regexp.Regexp][]ResponseHandler
type HandlerSpec struct {
Kind Kind
IsCmd bool
Regex *regexp.Regexp
Handler ResponseHandler
}
type HandlerTable []HandlerSpec
type RegexValues map[string]string type RegexValues map[string]string
// b interface serves to allow mocking of the actual bot // b interface serves to allow mocking of the actual bot
@ -90,6 +98,10 @@ type Bot interface {
// The Kind arg should be one of bot.Message/Reply/Action/etc // The Kind arg should be one of bot.Message/Reply/Action/etc
Receive(Connector, Kind, msg.Message, ...interface{}) bool Receive(Connector, Kind, msg.Message, ...interface{}) bool
// Register a set of plugin callbacks
// Kind will be matched to the event for the callback
RegisterTable(Plugin, HandlerTable)
// Register a plugin callback // Register a plugin callback
// Kind will be matched to the event for the callback // Kind will be matched to the event for the callback
RegisterRegex(Plugin, Kind, *regexp.Regexp, ResponseHandler) RegisterRegex(Plugin, Kind, *regexp.Regexp, ResponseHandler)

View File

@ -54,6 +54,7 @@ func (mb *MockBot) Send(c Connector, kind Kind, args ...interface{}) (string, er
} }
func (mb *MockBot) AddPlugin(f Plugin) {} func (mb *MockBot) AddPlugin(f Plugin) {}
func (mb *MockBot) Register(p Plugin, kind Kind, cb Callback) {} func (mb *MockBot) Register(p Plugin, kind Kind, cb Callback) {}
func (mb *MockBot) RegisterTable(p Plugin, hs HandlerTable) {}
func (mb *MockBot) RegisterRegex(p Plugin, kind Kind, r *regexp.Regexp, h ResponseHandler) {} 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) RegisterRegexCmd(p Plugin, kind Kind, r *regexp.Regexp, h ResponseHandler) {}
func (mb *MockBot) RegisterWeb(_, _ string) {} func (mb *MockBot) RegisterWeb(_, _ string) {}

View File

@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand"
"regexp"
"strings" "strings"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -26,6 +27,8 @@ type BabblerPlugin struct {
Bot bot.Bot Bot bot.Bot
db *sqlx.DB db *sqlx.DB
WithGoRoutines bool WithGoRoutines bool
handlers bot.HandlerTable
} }
type Babbler struct { type Babbler struct {
@ -95,57 +98,87 @@ func New(b bot.Bot) *BabblerPlugin {
plugin.createNewWord("") plugin.createNewWord("")
b.Register(plugin, bot.Message, plugin.message) plugin.register()
b.Register(plugin, bot.Help, plugin.help)
return plugin return plugin
} }
func (p *BabblerPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *BabblerPlugin) register() {
lowercase := strings.ToLower(message.Body) p.handlers = bot.HandlerTable{
tokens := strings.Fields(lowercase) bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
numTokens := len(tokens) Regex: regexp.MustCompile(`(?i)^(?P<who>\S+) says-bridge (?P<start>.+)\|(?P<end>.+)$`),
Handler: func(r bot.Request) bool {
who := r.Values["who"]
start := strings.Fields(strings.ToLower(r.Values["start"]))
end := strings.Fields(strings.ToLower(r.Values["end"]))
return p.sayIt(r, p.getBabbleWithBookends(who, start, end))
}},
bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
Regex: regexp.MustCompile(`(?i)^(?P<who>\S+) says-tail (?P<what>.*)$`),
Handler: func(r bot.Request) bool {
who := r.Values["who"]
what := strings.Fields(strings.ToLower(r.Values["what"]))
return p.sayIt(r, p.getBabbleWithSuffix(who, what))
}},
bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
Regex: regexp.MustCompile(`(?i)^(?P<who>\S+) says-middle-out (?P<what>.*)$`),
Handler: func(r bot.Request) bool {
who := r.Values["who"]
what := strings.ToLower(r.Values["what"])
tokens := strings.Fields(what)
saidSomething := false
saidWhat := ""
saidSomething := false saidWhatStart := p.getBabbleWithSuffix(who, tokens)
saidWhat := "" saidSomethingStart := saidWhatStart != ""
neverSaidLooksLike := fmt.Sprintf("%s never said", who)
if numTokens > 2 && tokens[1] == "says-bridge" && strings.Contains(lowercase, "|") { if !saidSomethingStart || strings.HasPrefix(saidWhatStart, neverSaidLooksLike) {
split := strings.Split(lowercase, "|") saidSomething = saidSomethingStart
start := strings.Fields(split[0]) saidWhat = saidWhatStart
end := strings.Fields(split[1]) } else {
saidWhat, saidSomething = p.getBabbleWithBookends(start, end) saidWhatEnd := p.getBabble(who, tokens)
} else if numTokens >= 2 && tokens[1] == "says" { saidSomethingEnd := saidWhatEnd != ""
saidWhat, saidSomething = p.getBabble(tokens) saidSomething = saidSomethingStart && saidSomethingEnd
} else if numTokens > 2 && tokens[1] == "says-tail" { if saidSomething {
saidWhat, saidSomething = p.getBabbleWithSuffix(tokens) saidWhat = saidWhatStart + strings.TrimPrefix(saidWhatEnd, what)
} else if numTokens >= 2 && tokens[1] == "says-middle-out" { }
saidWhatStart, saidSomethingStart := p.getBabbleWithSuffix(tokens) }
neverSaidLooksLike := fmt.Sprintf("%s never said '%s'", tokens[0], strings.Join(tokens[2:], " ")) return p.sayIt(r, saidWhat)
if !saidSomethingStart || saidWhatStart == neverSaidLooksLike { }},
saidSomething = saidSomethingStart bot.HandlerSpec{Kind: bot.Message, IsCmd: false, Regex: regexp.MustCompile(`(?i)^(?P<who>\S+) (says (?P<what>.*)?|says)$`),
saidWhat = saidWhatStart Handler: func(r bot.Request) bool {
} else { who := r.Values["who"]
saidWhatEnd, saidSomethingEnd := p.getBabble(tokens) what := strings.Fields(strings.ToLower(r.Values["what"]))
saidSomething = saidSomethingStart && saidSomethingEnd return p.sayIt(r, p.getBabble(who, what))
if saidSomething { }},
saidWhat = saidWhatStart + " " + strings.Join(strings.Fields(saidWhatEnd)[len(tokens)-2:], " ") bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
} Regex: regexp.MustCompile(`(?i)^initialize babbler for (?P<who>\S+)$`),
} Handler: func(r bot.Request) bool {
} else if len(tokens) == 4 && strings.Index(lowercase, "initialize babbler for ") == 0 { who := r.Values["who"]
saidWhat, saidSomething = p.initializeBabbler(tokens) return p.sayIt(r, p.initializeBabbler(who))
} else if strings.Index(lowercase, "batch learn for ") == 0 { }},
saidWhat, saidSomething = p.batchLearn(tokens) bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
} else if len(tokens) == 5 && strings.Index(lowercase, "merge babbler") == 0 { Regex: regexp.MustCompile(`(?i)^merge babbler (?P<from>\S+) into (?P<to>\S+)$`),
saidWhat, saidSomething = p.merge(tokens) Handler: func(r bot.Request) bool {
} else { from, to := r.Values["from"], r.Values["to"]
//this should always return "", false return p.sayIt(r, p.merge(from, to))
saidWhat, saidSomething = p.addToBabbler(message.User.Name, lowercase) }},
bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
Regex: regexp.MustCompile(`.*`),
Handler: func(r bot.Request) bool {
p.addToBabbler(r.Msg.User.Name, strings.ToLower(r.Msg.Body))
return false
}},
} }
p.Bot.RegisterTable(p, p.handlers)
p.Bot.Register(p, bot.Help, p.help)
}
if saidSomething { func (p *BabblerPlugin) sayIt(r bot.Request, what string) bool {
p.Bot.Send(c, bot.Message, message.Channel, saidWhat) if what != "" {
p.Bot.Send(r.Conn, bot.Message, r.Msg.Channel, what)
} }
return saidSomething return what != ""
} }
func (p *BabblerPlugin) help(c bot.Connector, kind bot.Kind, msg msg.Message, args ...interface{}) bool { func (p *BabblerPlugin) help(c bot.Connector, kind bot.Kind, msg msg.Message, args ...interface{}) bool {

View File

@ -3,27 +3,34 @@
package babbler package babbler
import ( import (
"github.com/velour/catbase/plugins/cli" "regexp"
"strings" "strings"
"testing" "testing"
"github.com/velour/catbase/plugins/cli"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg" "github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user" "github.com/velour/catbase/bot/user"
) )
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) { func makeMessage(payload string, r *regexp.Regexp) bot.Request {
c := &cli.CliPlugin{} c := &cli.CliPlugin{}
isCmd := strings.HasPrefix(payload, "!") isCmd := strings.HasPrefix(payload, "!")
if isCmd { if isCmd {
payload = payload[1:] payload = payload[1:]
} }
return c, bot.Message, msg.Message{ return bot.Request{
User: &user.User{Name: "tester"}, Conn: c,
Channel: "test", Kind: bot.Message,
Body: payload, Values: bot.ParseValues(r, payload),
Command: isCmd, Msg: msg.Message{
User: &user.User{Name: "tester"},
Channel: "test",
Body: payload,
Command: isCmd,
},
} }
} }
@ -39,11 +46,23 @@ func newBabblerPlugin(mb *bot.MockBot) *BabblerPlugin {
return bp return bp
} }
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
}
func TestBabblerNoBabbler(t *testing.T) { func TestBabblerNoBabbler(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
bp.message(makeMessage("!seabass2 says")) testMessage(bp, "!seabass2 says")
res := assert.Len(t, mb.Messages, 0) res := assert.Len(t, mb.Messages, 0)
assert.True(t, res) assert.True(t, res)
// assert.Contains(t, mb.Messages[0], "seabass2 babbler not found") // assert.Contains(t, mb.Messages[0], "seabass2 babbler not found")
@ -53,229 +72,176 @@ func TestBabblerNothingSaid(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
res := bp.message(makeMessage("initialize babbler for seabass")) res := testMessage(bp, "initialize babbler for seabass")
assert.True(t, res) assert.True(t, res)
res = bp.message(makeMessage("!seabass says")) res = testMessage(bp, "seabass says")
assert.True(t, res) assert.True(t, res)
assert.Len(t, mb.Messages, 2) if assert.Len(t, mb.Messages, 2) {
assert.Contains(t, mb.Messages[0], "okay.") assert.Contains(t, mb.Messages[0], "okay.")
assert.Contains(t, mb.Messages[1], "seabass hasn't said anything yet.") assert.Contains(t, mb.Messages[1], "seabass hasn't said anything yet.")
}
} }
func TestBabbler(t *testing.T) { func TestBabbler(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
c, k, seabass := makeMessage("This is a message") testMessage(bp, "!initialize babbler for tester")
seabass.User = &user.User{Name: "seabass"} testMessage(bp, "This is a message")
res := bp.message(c, k, seabass) testMessage(bp, "This is another message")
seabass.Body = "This is another message" testMessage(bp, "This is a long message")
res = bp.message(c, k, seabass) res := testMessage(bp, "!tester says")
seabass.Body = "This is a long message"
res = bp.message(c, k, seabass)
res = bp.message(makeMessage("!seabass says"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res) assert.True(t, res)
assert.Contains(t, mb.Messages[0], "this is") if assert.Len(t, mb.Messages, 1) {
assert.Contains(t, mb.Messages[0], "message") assert.Contains(t, mb.Messages[0], "this is")
assert.Contains(t, mb.Messages[0], "message")
}
} }
func TestBabblerSeed(t *testing.T) { func TestBabblerSeed(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
c, k, seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"} testMessage(bp, "This is a message")
res := bp.message(c, k, seabass) testMessage(bp, "This is another message")
seabass.Body = "This is another message" testMessage(bp, "This is a long message")
res = bp.message(c, k, seabass) res := testMessage(bp, "tester says long")
seabass.Body = "This is a long message"
res = bp.message(c, k, seabass)
res = bp.message(makeMessage("!seabass says long"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res) assert.True(t, res)
assert.Contains(t, mb.Messages[0], "long message") if assert.Len(t, mb.Messages, 1) {
assert.Contains(t, mb.Messages[0], "long message")
}
} }
func TestBabblerMultiSeed(t *testing.T) { func TestBabblerMultiSeed(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
c, k, seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"}
res := bp.message(c, k, seabass)
seabass.Body = "This is another message"
res = bp.message(c, k, seabass)
seabass.Body = "This is a long message"
res = bp.message(c, k, seabass)
res = bp.message(makeMessage("!seabass says This is a long"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "this is a long message")
}
func TestBabblerMultiSeed2(t *testing.T) { testMessage(bp, "This is a message")
mb := bot.NewMockBot() testMessage(bp, "This is another message")
bp := newBabblerPlugin(mb) testMessage(bp, "This is a long message")
assert.NotNil(t, bp) res := testMessage(bp, "tester says is another")
c, k, seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"}
res := bp.message(c, k, seabass)
seabass.Body = "This is another message"
res = bp.message(c, k, seabass)
seabass.Body = "This is a long message"
res = bp.message(c, k, seabass)
res = bp.message(makeMessage("!seabass says is a long"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res) assert.True(t, res)
assert.Contains(t, mb.Messages[0], "is a long message") if assert.Len(t, mb.Messages, 1) {
assert.Contains(t, mb.Messages[0], "is another")
}
} }
func TestBabblerBadSeed(t *testing.T) { func TestBabblerBadSeed(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
c, k, seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"} testMessage(bp, "This is a message")
bp.message(c, k, seabass) testMessage(bp, "This is another message")
seabass.Body = "This is another message" testMessage(bp, "This is a long message")
bp.message(c, k, seabass) res := testMessage(bp, "tester says this is bad")
seabass.Body = "This is a long message" assert.True(t, res)
bp.message(c, k, seabass) if assert.Len(t, mb.Messages, 1) {
bp.message(makeMessage("!seabass says noooo this is bad")) assert.Contains(t, mb.Messages[0], "tester never said 'this is bad'")
assert.Len(t, mb.Messages, 1) }
assert.Contains(t, mb.Messages[0], "seabass never said 'noooo this is bad'")
} }
func TestBabblerBadSeed2(t *testing.T) { func TestBabblerBadSeed2(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
c, k, seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"} testMessage(bp, "This is a message")
bp.message(c, k, seabass) testMessage(bp, "This is another message")
seabass.Body = "This is another message" testMessage(bp, "This is a long message")
bp.message(c, k, seabass) res := testMessage(bp, "tester says This is a really")
seabass.Body = "This is a long message" assert.True(t, res)
bp.message(c, k, seabass) if assert.Len(t, mb.Messages, 1) {
bp.message(makeMessage("!seabass says This is a really")) assert.Contains(t, mb.Messages[0], "tester never said 'this is a really'")
assert.Len(t, mb.Messages, 1) }
assert.Contains(t, mb.Messages[0], "seabass never said 'this is a really'")
} }
func TestBabblerSuffixSeed(t *testing.T) { func TestBabblerSuffixSeed(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
c, k, seabass := makeMessage("This is message one")
seabass.User = &user.User{Name: "seabass"} testMessage(bp, "This is message one")
res := bp.message(c, k, seabass) testMessage(bp, "It's easier to test with unique messages")
seabass.Body = "It's easier to test with unique messages" testMessage(bp, "tester says-tail message one")
res = bp.message(c, k, seabass) res := testMessage(bp, "tester says-tail with unique")
seabass.Body = "hi there"
res = bp.message(c, k, seabass)
res = bp.message(makeMessage("!seabass says-tail message one"))
res = bp.message(makeMessage("!seabass says-tail with unique"))
assert.Len(t, mb.Messages, 2)
assert.True(t, res) assert.True(t, res)
assert.Contains(t, mb.Messages[0], "this is message one") if assert.Len(t, mb.Messages, 2) {
assert.Contains(t, mb.Messages[1], "it's easier to test with unique") assert.Contains(t, mb.Messages[0], "this is message one")
assert.Contains(t, mb.Messages[1], "it's easier to test with unique")
}
} }
func TestBabblerBadSuffixSeed(t *testing.T) { func TestBabblerBadSuffixSeed(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
c, k, seabass := makeMessage("This is message one")
seabass.User = &user.User{Name: "seabass"} testMessage(bp, "This is message one")
res := bp.message(c, k, seabass) testMessage(bp, "It's easier to test with unique messages")
seabass.Body = "It's easier to test with unique messages" res := testMessage(bp, "tester says-tail anything true")
res = bp.message(c, k, seabass)
seabass.Body = "hi there"
res = bp.message(c, k, seabass)
res = bp.message(makeMessage("!seabass says-tail anything true"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res) assert.True(t, res)
assert.Contains(t, mb.Messages[0], "seabass never said 'anything true'") if assert.Len(t, mb.Messages, 1) {
assert.Contains(t, mb.Messages[0], "tester never said 'anything true'")
}
} }
func TestBabblerBookendSeed(t *testing.T) { func TestBabblerBookendSeed(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
c, k, seabass := makeMessage("It's easier to test with unique messages")
seabass.User = &user.User{Name: "seabass"}
res := bp.message(c, k, seabass)
res = bp.message(makeMessage("!seabass says-bridge It's easier | unique messages"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages")
}
func TestBabblerBookendSeedShort(t *testing.T) { testMessage(bp, "This is message one")
mb := bot.NewMockBot() testMessage(bp, "It's easier to test with unique messages")
bp := newBabblerPlugin(mb) res := testMessage(bp, "tester says-bridge it's easier | unique messages")
assert.NotNil(t, bp)
c, k, seabass := makeMessage("It's easier to test with unique messages")
seabass.User = &user.User{Name: "seabass"}
res := bp.message(c, k, seabass)
res = bp.message(makeMessage("!seabass says-bridge It's easier to test with | unique messages"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res) assert.True(t, res)
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages") if assert.Len(t, mb.Messages, 1) {
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages")
}
} }
func TestBabblerBadBookendSeed(t *testing.T) { func TestBabblerBadBookendSeed(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
c, k, seabass := makeMessage("It's easier to test with unique messages")
seabass.User = &user.User{Name: "seabass"} testMessage(bp, "This is message one")
res := bp.message(c, k, seabass) testMessage(bp, "It's easier to test with unique messages")
res = bp.message(makeMessage("!seabass says-bridge It's easier | not unique messages")) res := testMessage(bp, "tester says-bridge says-bridge It's easier | not unique messages")
assert.Len(t, mb.Messages, 1)
assert.True(t, res) assert.True(t, res)
assert.Contains(t, mb.Messages[0], "seabass never said 'it's easier ... not unique messages'") if assert.Len(t, mb.Messages, 1) {
assert.Contains(t, mb.Messages[0], "tester never said 'it's easier ... not unique messages'")
}
} }
func TestBabblerMiddleOutSeed(t *testing.T) { func TestBabblerMiddleOutSeed(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
c, k, seabass := makeMessage("It's easier to test with unique messages")
seabass.User = &user.User{Name: "seabass"} testMessage(bp, "This is message one")
res := bp.message(c, k, seabass) testMessage(bp, "It's easier to test with unique messages")
res = bp.message(makeMessage("!seabass says-middle-out test with")) res := testMessage(bp, "tester says-middle-out test with")
assert.Len(t, mb.Messages, 1)
assert.True(t, res) assert.True(t, res)
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages") if assert.Len(t, mb.Messages, 1) {
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages")
}
} }
func TestBabblerBadMiddleOutSeed(t *testing.T) { func TestBabblerBadMiddleOutSeed(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
c, k, seabass := makeMessage("It's easier to test with unique messages")
seabass.User = &user.User{Name: "seabass"}
res := bp.message(c, k, seabass)
res = bp.message(makeMessage("!seabass says-middle-out anything true"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Equal(t, mb.Messages[0], "seabass never said 'anything true'")
}
func TestBabblerBatch(t *testing.T) { testMessage(bp, "This is message one")
mb := bot.NewMockBot() testMessage(bp, "It's easier to test with unique messages")
bp := newBabblerPlugin(mb) res := testMessage(bp, "tester says-middle-out anything true")
assert.NotNil(t, bp)
c, k, seabass := makeMessage("batch learn for seabass This is a message! This is another message. This is not a long message? This is not a message! This is not another message. This is a long message?")
res := bp.message(c, k, seabass)
assert.Len(t, mb.Messages, 1)
res = bp.message(makeMessage("!seabass says"))
assert.Len(t, mb.Messages, 2)
assert.True(t, res) assert.True(t, res)
assert.Contains(t, mb.Messages[1], "this is") if assert.Len(t, mb.Messages, 1) {
assert.Contains(t, mb.Messages[1], "message") assert.Contains(t, mb.Messages[0], "tester never said 'anything true'")
}
} }
func TestBabblerMerge(t *testing.T) { func TestBabblerMerge(t *testing.T) {
@ -283,28 +249,23 @@ func TestBabblerMerge(t *testing.T) {
bp := newBabblerPlugin(mb) bp := newBabblerPlugin(mb)
assert.NotNil(t, bp) assert.NotNil(t, bp)
c, k, seabass := makeMessage("<seabass> This is a message") testMessage(bp, "<tester> This is a message")
seabass.User = &user.User{Name: "seabass"}
res := bp.message(c, k, seabass)
assert.Len(t, mb.Messages, 0) assert.Len(t, mb.Messages, 0)
seabass.Body = "<seabass> This is another message" testMessage(bp, "<tester> This is another message")
res = bp.message(c, k, seabass) testMessage(bp, "<tester> This is a long message")
res := testMessage(bp, "merge babbler tester into tester2")
seabass.Body = "<seabass> This is a long message"
res = bp.message(c, k, seabass)
res = bp.message(makeMessage("!merge babbler seabass into seabass2"))
assert.True(t, res) assert.True(t, res)
assert.Len(t, mb.Messages, 1) if assert.Len(t, mb.Messages, 1) {
assert.Contains(t, mb.Messages[0], "mooooiggged") assert.Contains(t, mb.Messages[0], "mooooiggged")
}
res = bp.message(makeMessage("!seabass2 says")) res = testMessage(bp, "!tester2 says")
assert.True(t, res) assert.True(t, res)
assert.Len(t, mb.Messages, 2) if assert.Len(t, mb.Messages, 2) {
assert.Contains(t, mb.Messages[1], "<tester2> this is")
assert.Contains(t, mb.Messages[1], "<seabass2> this is") assert.Contains(t, mb.Messages[1], "message")
assert.Contains(t, mb.Messages[1], "message") }
} }
func TestHelp(t *testing.T) { func TestHelp(t *testing.T) {

View File

@ -5,117 +5,112 @@ import (
"strings" "strings"
) )
func (p *BabblerPlugin) initializeBabbler(tokens []string) (string, bool) { func (p *BabblerPlugin) initializeBabbler(who string) string {
who := tokens[3]
_, err := p.getOrCreateBabbler(who) _, err := p.getOrCreateBabbler(who)
if err != nil { if err != nil {
return "babbler initialization failed.", true return "babbler initialization failed."
} }
return "okay.", true return "okay."
} }
func (p *BabblerPlugin) addToBabbler(babblerName, whatWasSaid string) (string, bool) { func (p *BabblerPlugin) addToBabbler(babblerName, whatWasSaid string) {
babblerId, err := p.getOrCreateBabbler(babblerName) babblerID, err := p.getOrCreateBabbler(babblerName)
if err == nil { if err == nil {
if p.WithGoRoutines { if p.WithGoRoutines {
go p.addToMarkovChain(babblerId, whatWasSaid) go p.addToMarkovChain(babblerID, whatWasSaid)
} else { } else {
p.addToMarkovChain(babblerId, whatWasSaid) p.addToMarkovChain(babblerID, whatWasSaid)
} }
} }
return "", false
} }
func (p *BabblerPlugin) getBabble(tokens []string) (string, bool) { func (p *BabblerPlugin) getBabble(who string, tokens []string) string {
who := tokens[0]
_, err := p.getBabbler(who) _, err := p.getBabbler(who)
if err != nil { if err != nil {
if err == NO_BABBLER { if err == NO_BABBLER {
// return fmt.Sprintf("%s babbler not found.", who), true // return fmt.Sprintf("%s babbler not found.", who), true
return "", false return ""
} }
} else { } else {
var saying string var saying string
if len(tokens) == 2 { if len(tokens) == 0 {
saying, err = p.babble(who) saying, err = p.babble(who)
} else { } else {
saying, err = p.babbleSeed(who, tokens[2:]) saying, err = p.babbleSeed(who, tokens)
} }
if err != nil { if err != nil {
if err == SAID_NOTHING { if err == SAID_NOTHING {
return fmt.Sprintf("%s hasn't said anything yet.", who), true return fmt.Sprintf("%s hasn't said anything yet.", who)
} else if err == NEVER_SAID { } else if err == NEVER_SAID {
return fmt.Sprintf("%s never said '%s'", who, strings.Join(tokens[2:], " ")), true return fmt.Sprintf("%s never said '%s'", who, strings.Join(tokens, " "))
} }
} else if saying != "" { } else if saying != "" {
return saying, true return saying
} }
} }
return "", false return ""
} }
func (p *BabblerPlugin) getBabbleWithSuffix(tokens []string) (string, bool) { func (p *BabblerPlugin) getBabbleWithSuffix(who string, tokens []string) string {
who := tokens[0]
_, err := p.getBabbler(who) _, err := p.getBabbler(who)
if err != nil { if err != nil {
if err == NO_BABBLER { if err == NO_BABBLER {
// return fmt.Sprintf("%s babbler not found.", who), true // return fmt.Sprintf("%s babbler not found.", who), true
return "", false return ""
} }
} else { } else {
saying, err := p.babbleSeedSuffix(who, tokens[2:]) saying, err := p.babbleSeedSuffix(who, tokens)
if err != nil { if err != nil {
if err == SAID_NOTHING { if err == SAID_NOTHING {
return fmt.Sprintf("%s hasn't said anything yet.", who), true return fmt.Sprintf("%s hasn't said anything yet.", who)
} else if err == NEVER_SAID { } else if err == NEVER_SAID {
return fmt.Sprintf("%s never said '%s'", who, strings.Join(tokens[2:], " ")), true return fmt.Sprintf("%s never said '%s'", who, strings.Join(tokens, " "))
} }
} else if saying != "" { } else if saying != "" {
return saying, true return saying
} }
} }
return "", false return ""
} }
func (p *BabblerPlugin) getBabbleWithBookends(start, end []string) (string, bool) { func (p *BabblerPlugin) getBabbleWithBookends(who string, start, end []string) string {
who := start[0]
_, err := p.getBabbler(who) _, err := p.getBabbler(who)
if err != nil { if err != nil {
if err == NO_BABBLER { if err == NO_BABBLER {
// return fmt.Sprintf("%s babbler not found.", who), true // return fmt.Sprintf("%s babbler not found.", who), true
return "", false return ""
} }
} else { } else {
saying, err := p.babbleSeedBookends(who, start[2:], end) saying, err := p.babbleSeedBookends(who, start, end)
if err != nil { if err != nil {
if err == SAID_NOTHING { if err == SAID_NOTHING {
return fmt.Sprintf("%s hasn't said anything yet.", who), true return fmt.Sprintf("%s hasn't said anything yet.", who)
} else if err == NEVER_SAID { } else if err == NEVER_SAID {
seeds := append(start[2:], "...") seeds := append(start[1:], "...")
seeds = append(seeds, end...) seeds = append(seeds, end...)
return fmt.Sprintf("%s never said '%s'", who, strings.Join(seeds, " ")), true return fmt.Sprintf("%s never said '%s'", who, strings.Join(seeds, " "))
} }
} else if saying != "" { } else if saying != "" {
return saying, true return saying
} }
} }
return "", false return ""
} }
func (p *BabblerPlugin) batchLearn(tokens []string) (string, bool) { func (p *BabblerPlugin) batchLearn(tokens []string) string {
who := tokens[3] who := tokens[3]
babblerId, err := p.getOrCreateBabbler(who) babblerId, err := p.getOrCreateBabbler(who)
if err != nil { if err != nil {
return "batch learn failed.", true return "batch learn failed."
} }
body := strings.Join(tokens[4:], " ") body := strings.Join(tokens[4:], " ")
@ -133,37 +128,30 @@ func (p *BabblerPlugin) batchLearn(tokens []string) (string, bool) {
} }
} }
} }
return "phew that was tiring.", true return "phew that was tiring."
} }
func (p *BabblerPlugin) merge(tokens []string) (string, bool) { func (p *BabblerPlugin) merge(who, into string) string {
if tokens[3] != "into" {
return "try using 'merge babbler [x] into [y]'", true
}
who := tokens[2]
into := tokens[4]
if who == into { if who == into {
return "that's annoying. stop it.", true return "that's annoying. stop it."
} }
whoBabbler, err := p.getBabbler(who) whoBabbler, err := p.getBabbler(who)
if err != nil { if err != nil {
if err == NO_BABBLER { if err == NO_BABBLER {
return fmt.Sprintf("%s babbler not found.", who), true return fmt.Sprintf("%s babbler not found.", who)
} }
return "merge failed.", true return "merge failed."
} }
intoBabbler, err := p.getOrCreateBabbler(into) intoBabbler, err := p.getOrCreateBabbler(into)
if err != nil { if err != nil {
return "merge failed.", true return "merge failed."
} }
err = p.mergeBabblers(intoBabbler, whoBabbler, into, who) err = p.mergeBabblers(intoBabbler, whoBabbler, into, who)
if err != nil { if err != nil {
return "merge failed.", true return "merge failed."
} }
return "mooooiggged", true return "mooooiggged"
} }