Fixed babbler tests by not running go routines during testing. (go fmt on nerdepedia because of course this should all happen in one commit)

This commit is contained in:
skkiesel 2019-01-19 09:43:33 -05:00
parent 265fe441b4
commit 91a334b32b
4 changed files with 138 additions and 126 deletions

View File

@ -23,9 +23,10 @@ var (
)
type BabblerPlugin struct {
Bot bot.Bot
db *sqlx.DB
config *config.Config
Bot bot.Bot
db *sqlx.DB
config *config.Config
WithGoRoutines bool
}
type Babbler struct {
@ -90,9 +91,10 @@ func New(bot bot.Bot) *BabblerPlugin {
}
plugin := &BabblerPlugin{
Bot: bot,
db: bot.DB(),
config: bot.Config(),
Bot: bot,
db: bot.DB(),
config: bot.Config(),
WithGoRoutines: true,
}
plugin.createNewWord("")

View File

@ -25,12 +25,18 @@ func makeMessage(payload string) msg.Message {
}
}
func newBabblerPlugin(mb *bot.MockBot) *BabblerPlugin {
bp := New(mb)
bp.WithGoRoutines = false
return bp
}
func TestBabblerNoBabbler(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
c.Message(makeMessage("!seabass2 says"))
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
bp.Message(makeMessage("!seabass2 says"))
res := assert.Len(t, mb.Messages, 0)
assert.True(t, res)
// assert.Contains(t, mb.Messages[0], "seabass2 babbler not found")
@ -38,12 +44,12 @@ func TestBabblerNoBabbler(t *testing.T) {
func TestBabblerNothingSaid(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
res := c.Message(makeMessage("initialize babbler for seabass"))
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
res := bp.Message(makeMessage("initialize babbler for seabass"))
assert.True(t, res)
res = c.Message(makeMessage("!seabass says"))
res = bp.Message(makeMessage("!seabass says"))
assert.True(t, res)
assert.Len(t, mb.Messages, 2)
assert.Contains(t, mb.Messages[0], "okay.")
@ -52,17 +58,17 @@ func TestBabblerNothingSaid(t *testing.T) {
func TestBabbler(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
res := bp.Message(seabass)
seabass.Body = "This is another message"
res = c.Message(seabass)
res = bp.Message(seabass)
seabass.Body = "This is a long message"
res = c.Message(seabass)
res = c.Message(makeMessage("!seabass says"))
res = bp.Message(seabass)
res = bp.Message(makeMessage("!seabass says"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "this is")
@ -71,17 +77,17 @@ func TestBabbler(t *testing.T) {
func TestBabblerSeed(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
res := bp.Message(seabass)
seabass.Body = "This is another message"
res = c.Message(seabass)
res = bp.Message(seabass)
seabass.Body = "This is a long message"
res = c.Message(seabass)
res = c.Message(makeMessage("!seabass says long"))
res = bp.Message(seabass)
res = bp.Message(makeMessage("!seabass says long"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "long message")
@ -89,17 +95,17 @@ func TestBabblerSeed(t *testing.T) {
func TestBabblerMultiSeed(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
res := bp.Message(seabass)
seabass.Body = "This is another message"
res = c.Message(seabass)
res = bp.Message(seabass)
seabass.Body = "This is a long message"
res = c.Message(seabass)
res = c.Message(makeMessage("!seabass says This is a long"))
res = bp.Message(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")
@ -107,17 +113,17 @@ func TestBabblerMultiSeed(t *testing.T) {
func TestBabblerMultiSeed2(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
res := bp.Message(seabass)
seabass.Body = "This is another message"
res = c.Message(seabass)
res = bp.Message(seabass)
seabass.Body = "This is a long message"
res = c.Message(seabass)
res = c.Message(makeMessage("!seabass says is a long"))
res = bp.Message(seabass)
res = bp.Message(makeMessage("!seabass says is a long"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "is a long message")
@ -125,52 +131,52 @@ func TestBabblerMultiSeed2(t *testing.T) {
func TestBabblerBadSeed(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"}
c.Message(seabass)
bp.Message(seabass)
seabass.Body = "This is another message"
c.Message(seabass)
bp.Message(seabass)
seabass.Body = "This is a long message"
c.Message(seabass)
c.Message(makeMessage("!seabass says noooo this is bad"))
bp.Message(seabass)
bp.Message(makeMessage("!seabass says noooo 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) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"}
c.Message(seabass)
bp.Message(seabass)
seabass.Body = "This is another message"
c.Message(seabass)
bp.Message(seabass)
seabass.Body = "This is a long message"
c.Message(seabass)
c.Message(makeMessage("!seabass says This is a really"))
bp.Message(seabass)
bp.Message(makeMessage("!seabass says 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) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("This is message one")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
res := bp.Message(seabass)
seabass.Body = "It's easier to test with unique messages"
res = c.Message(seabass)
res = bp.Message(seabass)
seabass.Body = "hi there"
res = c.Message(seabass)
res = c.Message(makeMessage("!seabass says-tail message one"))
res = c.Message(makeMessage("!seabass says-tail with unique"))
res = bp.Message(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.Contains(t, mb.Messages[0], "this is message one")
@ -179,17 +185,17 @@ func TestBabblerSuffixSeed(t *testing.T) {
func TestBabblerBadSuffixSeed(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("This is message one")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
res := bp.Message(seabass)
seabass.Body = "It's easier to test with unique messages"
res = c.Message(seabass)
res = bp.Message(seabass)
seabass.Body = "hi there"
res = c.Message(seabass)
res = c.Message(makeMessage("!seabass says-tail anything true"))
res = bp.Message(seabass)
res = bp.Message(makeMessage("!seabass says-tail anything true"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "seabass never said 'anything true'")
@ -197,13 +203,13 @@ func TestBabblerBadSuffixSeed(t *testing.T) {
func TestBabblerBookendSeed(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("It's easier to test with unique messages")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
res = c.Message(makeMessage("!seabass says-bridge It's easier | unique messages"))
res := bp.Message(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")
@ -211,13 +217,13 @@ func TestBabblerBookendSeed(t *testing.T) {
func TestBabblerBookendSeedShort(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("It's easier to test with unique messages")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
res = c.Message(makeMessage("!seabass says-bridge It's easier to test with | unique messages"))
res := bp.Message(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.Contains(t, mb.Messages[0], "it's easier to test with unique messages")
@ -225,13 +231,13 @@ func TestBabblerBookendSeedShort(t *testing.T) {
func TestBabblerBadBookendSeed(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("It's easier to test with unique messages")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
res = c.Message(makeMessage("!seabass says-bridge It's easier | not unique messages"))
res := bp.Message(seabass)
res = bp.Message(makeMessage("!seabass says-bridge It's easier | not unique messages"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "seabass never said 'it's easier ... not unique messages'")
@ -239,13 +245,13 @@ func TestBabblerBadBookendSeed(t *testing.T) {
func TestBabblerMiddleOutSeed(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("It's easier to test with unique messages")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
res = c.Message(makeMessage("!seabass says-middle-out test with"))
res := bp.Message(seabass)
res = bp.Message(makeMessage("!seabass says-middle-out test with"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages")
@ -253,13 +259,13 @@ func TestBabblerMiddleOutSeed(t *testing.T) {
func TestBabblerBadMiddleOutSeed(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("It's easier to test with unique messages")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
res = c.Message(makeMessage("!seabass says-middle-out anything true"))
res := bp.Message(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'")
@ -267,13 +273,13 @@ func TestBabblerBadMiddleOutSeed(t *testing.T) {
func TestBabblerBatch(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
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 := c.Message(seabass)
res := bp.Message(seabass)
assert.Len(t, mb.Messages, 1)
res = c.Message(makeMessage("!seabass says"))
res = bp.Message(makeMessage("!seabass says"))
assert.Len(t, mb.Messages, 2)
assert.True(t, res)
assert.Contains(t, mb.Messages[1], "this is")
@ -282,27 +288,27 @@ func TestBabblerBatch(t *testing.T) {
func TestBabblerMerge(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
bp := newBabblerPlugin(mb)
bp.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, bp)
seabass := makeMessage("<seabass> This is a message")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
res := bp.Message(seabass)
assert.Len(t, mb.Messages, 0)
seabass.Body = "<seabass> This is another message"
res = c.Message(seabass)
res = bp.Message(seabass)
seabass.Body = "<seabass> This is a long message"
res = c.Message(seabass)
res = bp.Message(seabass)
res = c.Message(makeMessage("!merge babbler seabass into seabass2"))
res = bp.Message(makeMessage("!merge babbler seabass into seabass2"))
assert.True(t, res)
assert.Len(t, mb.Messages, 1)
assert.Contains(t, mb.Messages[0], "mooooiggged")
res = c.Message(makeMessage("!seabass2 says"))
res = bp.Message(makeMessage("!seabass2 says"))
assert.True(t, res)
assert.Len(t, mb.Messages, 2)
@ -312,29 +318,29 @@ func TestBabblerMerge(t *testing.T) {
func TestHelp(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
c.Help("channel", []string{})
bp := newBabblerPlugin(mb)
assert.NotNil(t, bp)
bp.Help("channel", []string{})
assert.Len(t, mb.Messages, 1)
}
func TestBotMessage(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
assert.False(t, c.BotMessage(makeMessage("test")))
bp := newBabblerPlugin(mb)
assert.NotNil(t, bp)
assert.False(t, bp.BotMessage(makeMessage("test")))
}
func TestEvent(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
assert.False(t, c.Event("dummy", makeMessage("test")))
bp := newBabblerPlugin(mb)
assert.NotNil(t, bp)
assert.False(t, bp.Event("dummy", makeMessage("test")))
}
func TestRegisterWeb(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
assert.Nil(t, c.RegisterWeb())
bp := newBabblerPlugin(mb)
assert.NotNil(t, bp)
assert.Nil(t, bp.RegisterWeb())
}

View File

@ -17,7 +17,11 @@ func (p *BabblerPlugin) initializeBabbler(tokens []string) (string, bool) {
func (p *BabblerPlugin) addToBabbler(babblerName, whatWasSaid string) (string, bool) {
babblerId, err := p.getOrCreateBabbler(babblerName)
if err == nil {
go p.addToMarkovChain(babblerId, whatWasSaid)
if p.WithGoRoutines {
go p.addToMarkovChain(babblerId, whatWasSaid)
} else {
p.addToMarkovChain(babblerId, whatWasSaid)
}
}
return "", false
}

View File

@ -3,11 +3,11 @@
package nerdepedia
import (
"bufio"
"fmt"
"strings"
"html"
"net/http"
"bufio"
"strings"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
@ -16,7 +16,7 @@ import (
const (
descriptionPrefix = "<meta name=\"description\" content=\""
linkPrefix = "<link rel=\"canonical\" href=\""
linkPrefix = "<link rel=\"canonical\" href=\""
closingTagSuffix = "\" />"
)