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 (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
)
func makeMessage ( payload string ) msg . Message {
isCmd := strings . HasPrefix ( payload , "!" )
if isCmd {
payload = payload [ 1 : ]
}
return msg . Message {
User : & user . User { Name : "tester" } ,
Channel : "test" ,
Body : payload ,
Command : isCmd ,
}
}
2019-01-19 14:43:33 +00:00
func newBabblerPlugin ( mb * bot . MockBot ) * BabblerPlugin {
bp := New ( mb )
bp . WithGoRoutines = false
2019-01-21 17:36:02 +00:00
mb . DB ( ) . MustExec ( `
delete from babblers ;
delete from babblerWords ;
delete from babblerNodes ;
delete from babblerArcs ;
` )
2019-01-19 14:43:33 +00:00
return bp
}
2016-10-19 23:34:16 +00:00
func TestBabblerNoBabbler ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
bp . Message ( makeMessage ( "!seabass2 says" ) )
2017-06-16 19:02:28 +00:00
res := assert . Len ( t , mb . Messages , 0 )
2016-10-19 23:34:16 +00:00
assert . True ( t , res )
2017-06-16 19:02:28 +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 ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
res := bp . Message ( makeMessage ( "initialize babbler for seabass" ) )
2017-05-10 12:41:41 +00:00
assert . True ( t , res )
2019-01-19 14:43:33 +00:00
res = bp . Message ( makeMessage ( "!seabass says" ) )
2017-05-10 12:41:41 +00:00
assert . True ( t , res )
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 ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2016-05-09 20:45:02 +00:00
seabass := makeMessage ( "This is a message" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
2016-05-09 20:45:02 +00:00
seabass . Body = "This is another message"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
2016-05-09 20:45:02 +00:00
seabass . Body = "This is a long message"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
res = bp . Message ( makeMessage ( "!seabass says" ) )
2016-05-09 20:45:02 +00:00
assert . Len ( t , mb . Messages , 1 )
assert . True ( t , res )
assert . Contains ( t , mb . Messages [ 0 ] , "this is" )
assert . Contains ( t , mb . Messages [ 0 ] , "message" )
}
2016-10-19 23:34:16 +00:00
func TestBabblerSeed ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2016-10-19 23:34:16 +00:00
seabass := makeMessage ( "This is a message" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
2016-10-19 23:34:16 +00:00
seabass . Body = "This is another message"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
2016-10-19 23:34:16 +00:00
seabass . Body = "This is a long message"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
res = bp . Message ( makeMessage ( "!seabass says long" ) )
2016-10-19 23:34:16 +00:00
assert . Len ( t , mb . Messages , 1 )
assert . True ( t , res )
assert . Contains ( t , mb . Messages [ 0 ] , "long message" )
}
2017-04-07 19:35:18 +00:00
func TestBabblerMultiSeed ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2017-04-07 19:35:18 +00:00
seabass := makeMessage ( "This is a message" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
2017-04-07 19:35:18 +00:00
seabass . Body = "This is another message"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
2017-04-07 19:35:18 +00:00
seabass . Body = "This is a long message"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
res = bp . Message ( makeMessage ( "!seabass says This is a long" ) )
2017-04-07 19:35:18 +00:00
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 ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2017-04-07 19:35:18 +00:00
seabass := makeMessage ( "This is a message" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
2017-04-07 19:35:18 +00:00
seabass . Body = "This is another message"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
2017-04-07 19:35:18 +00:00
seabass . Body = "This is a long message"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
res = bp . Message ( makeMessage ( "!seabass says is a long" ) )
2017-04-07 19:35:18 +00:00
assert . Len ( t , mb . Messages , 1 )
assert . True ( t , res )
assert . Contains ( t , mb . Messages [ 0 ] , "is a long message" )
}
2016-10-19 23:34:16 +00:00
func TestBabblerBadSeed ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2016-10-19 23:34:16 +00:00
seabass := makeMessage ( "This is a message" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
bp . Message ( seabass )
2016-10-19 23:34:16 +00:00
seabass . Body = "This is another message"
2019-01-19 14:43:33 +00:00
bp . Message ( seabass )
2016-10-19 23:34:16 +00:00
seabass . Body = "This is a long message"
2019-01-19 14:43:33 +00:00
bp . Message ( seabass )
bp . Message ( makeMessage ( "!seabass says noooo this is bad" ) )
2017-05-10 14:11:49 +00:00
assert . Len ( t , mb . Messages , 1 )
assert . Contains ( t , mb . Messages [ 0 ] , "seabass never said 'noooo this is bad'" )
2017-04-07 19:35:18 +00:00
}
func TestBabblerBadSeed2 ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2017-04-07 19:35:18 +00:00
seabass := makeMessage ( "This is a message" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
bp . Message ( seabass )
2017-04-07 19:35:18 +00:00
seabass . Body = "This is another message"
2019-01-19 14:43:33 +00:00
bp . Message ( seabass )
2017-04-07 19:35:18 +00:00
seabass . Body = "This is a long message"
2019-01-19 14:43:33 +00:00
bp . Message ( seabass )
bp . Message ( makeMessage ( "!seabass says This is a really" ) )
2017-05-10 14:11:49 +00:00
assert . Len ( t , mb . Messages , 1 )
assert . Contains ( t , mb . Messages [ 0 ] , "seabass 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 ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2017-06-05 23:53:19 +00:00
seabass := makeMessage ( "This is message one" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
2017-06-05 23:53:19 +00:00
seabass . Body = "It's easier to test with unique messages"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
2017-06-05 23:53:19 +00:00
seabass . Body = "hi there"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
res = bp . Message ( makeMessage ( "!seabass says-tail message one" ) )
res = bp . Message ( makeMessage ( "!seabass says-tail with unique" ) )
2017-06-05 23:53:19 +00:00
assert . Len ( t , mb . Messages , 2 )
assert . True ( t , res )
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 ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2017-06-05 23:53:19 +00:00
seabass := makeMessage ( "This is message one" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
2017-06-05 23:53:19 +00:00
seabass . Body = "It's easier to test with unique messages"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
2017-06-05 23:53:19 +00:00
seabass . Body = "hi there"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
res = bp . Message ( makeMessage ( "!seabass says-tail anything true" ) )
2017-06-05 23:53:19 +00:00
assert . Len ( t , mb . Messages , 1 )
assert . True ( t , res )
assert . Contains ( t , mb . Messages [ 0 ] , "seabass never said 'anything true'" )
}
2017-06-07 00:03:07 +00:00
func TestBabblerBookendSeed ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2017-06-07 00:03:07 +00:00
seabass := makeMessage ( "It's easier to test with unique messages" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
res = bp . Message ( makeMessage ( "!seabass says-bridge It's easier | unique messages" ) )
2017-06-07 00:03:07 +00:00
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 ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2017-06-07 00:03:07 +00:00
seabass := makeMessage ( "It's easier to test with unique messages" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
res = bp . Message ( makeMessage ( "!seabass says-bridge It's easier to test with | unique messages" ) )
2017-06-07 00:03:07 +00:00
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 TestBabblerBadBookendSeed ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2017-06-07 00:03:07 +00:00
seabass := makeMessage ( "It's easier to test with unique messages" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
res = bp . Message ( makeMessage ( "!seabass says-bridge It's easier | not unique messages" ) )
2017-06-07 00:03:07 +00:00
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'" )
}
2017-06-08 00:02:42 +00:00
func TestBabblerMiddleOutSeed ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2017-06-08 00:02:42 +00:00
seabass := makeMessage ( "It's easier to test with unique messages" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
res = bp . Message ( makeMessage ( "!seabass says-middle-out test with" ) )
2017-06-08 00:02:42 +00:00
assert . Len ( t , mb . Messages , 1 )
assert . True ( t , res )
assert . Contains ( t , mb . Messages [ 0 ] , "it's easier to test with unique messages" )
}
2017-06-16 19:02:28 +00:00
func TestBabblerBadMiddleOutSeed ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2017-06-16 19:02:28 +00:00
seabass := makeMessage ( "It's easier to test with unique messages" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
res = bp . Message ( makeMessage ( "!seabass says-middle-out anything true" ) )
2017-06-16 19:02:28 +00:00
assert . Len ( t , mb . Messages , 1 )
assert . True ( t , res )
2017-06-16 19:19:11 +00:00
assert . Equal ( t , mb . Messages [ 0 ] , "seabass never said 'anything true'" )
2017-06-16 19:02:28 +00:00
}
2016-05-11 17:07:16 +00:00
func TestBabblerBatch ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2016-05-11 17:07:16 +00:00
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?" )
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
2016-05-11 17:07:16 +00:00
assert . Len ( t , mb . Messages , 1 )
2019-01-19 14:43:33 +00:00
res = bp . Message ( makeMessage ( "!seabass says" ) )
2016-05-11 17:07:16 +00:00
assert . Len ( t , mb . Messages , 2 )
assert . True ( t , res )
assert . Contains ( t , mb . Messages [ 1 ] , "this is" )
assert . Contains ( t , mb . Messages [ 1 ] , "message" )
}
2016-05-26 15:06:22 +00:00
func TestBabblerMerge ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
2016-05-26 15:06:22 +00:00
seabass := makeMessage ( "<seabass> This is a message" )
seabass . User = & user . User { Name : "seabass" }
2019-01-19 14:43:33 +00:00
res := bp . Message ( seabass )
2016-05-26 15:06:22 +00:00
assert . Len ( t , mb . Messages , 0 )
seabass . Body = "<seabass> This is another message"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
2016-05-26 15:06:22 +00:00
seabass . Body = "<seabass> This is a long message"
2019-01-19 14:43:33 +00:00
res = bp . Message ( seabass )
2016-05-26 15:06:22 +00:00
2019-01-19 14:43:33 +00:00
res = bp . Message ( makeMessage ( "!merge babbler seabass into seabass2" ) )
2016-05-26 15:06:22 +00:00
assert . True ( t , res )
assert . Len ( t , mb . Messages , 1 )
assert . Contains ( t , mb . Messages [ 0 ] , "mooooiggged" )
2019-01-19 14:43:33 +00:00
res = bp . Message ( makeMessage ( "!seabass2 says" ) )
2016-05-26 15:06:22 +00:00
assert . True ( t , res )
assert . Len ( t , mb . Messages , 2 )
assert . Contains ( t , mb . Messages [ 1 ] , "<seabass2> this is" )
assert . Contains ( t , mb . Messages [ 1 ] , "message" )
}
2016-05-09 20:45:02 +00:00
func TestHelp ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
bp . Help ( "channel" , [ ] string { } )
2016-05-09 20:45:02 +00:00
assert . Len ( t , mb . Messages , 1 )
}
func TestBotMessage ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
assert . False ( t , bp . BotMessage ( makeMessage ( "test" ) ) )
2016-05-09 20:45:02 +00:00
}
func TestEvent ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
assert . False ( t , bp . Event ( "dummy" , makeMessage ( "test" ) ) )
2016-05-09 20:45:02 +00:00
}
func TestRegisterWeb ( t * testing . T ) {
mb := bot . NewMockBot ( )
2019-01-19 14:43:33 +00:00
bp := newBabblerPlugin ( mb )
assert . NotNil ( t , bp )
assert . Nil ( t , bp . RegisterWeb ( ) )
2016-05-09 20:45:02 +00:00
}