2016-03-30 14:00:20 +00:00
// © 2016 the CatBase Authors under the WTFPL license. See AUTHORS for the list of authors.
package counter
import (
"fmt"
2021-02-01 02:51:28 +00:00
"regexp"
2016-03-30 14:00:20 +00:00
"strings"
"testing"
2020-01-24 21:32:24 +00:00
"github.com/velour/catbase/plugins/cli"
2016-03-30 14:00:20 +00:00
"github.com/stretchr/testify/assert"
2020-04-20 10:20:21 +00:00
2016-03-30 14:00:20 +00:00
"github.com/velour/catbase/bot"
2016-04-01 14:20:03 +00:00
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
2016-03-30 14:00:20 +00:00
)
2021-12-21 04:31:19 +00:00
func setup ( t * testing . T ) ( * bot . MockBot , * CounterPlugin , func ( ) ) {
2018-10-19 18:20:40 +00:00
mb := bot . NewMockBot ( )
c := New ( mb )
2021-12-21 04:31:19 +00:00
_ , err := MkAlias ( mb . Store ( ) , "tea" , ":tea:" )
2018-10-19 18:20:40 +00:00
assert . Nil ( t , err )
2021-12-21 04:31:19 +00:00
return mb , c , func ( ) {
if err := mb . TearDown ( ) ; err != nil {
panic ( err )
}
}
2018-10-19 18:20:40 +00:00
}
2021-02-01 02:51:28 +00:00
func makeMessage ( payload string , r * regexp . Regexp ) bot . Request {
2016-03-30 14:00:20 +00:00
isCmd := strings . HasPrefix ( payload , "!" )
if isCmd {
payload = payload [ 1 : ]
}
2021-02-01 02:51:28 +00:00
values := bot . ParseValues ( r , payload )
return bot . Request {
2021-02-14 21:57:22 +00:00
Conn : & cli . CliPlugin { } ,
2021-02-01 02:51:28 +00:00
Msg : msg . Message {
2021-02-14 21:57:22 +00:00
User : & user . User { Name : "tester" , ID : "id" } ,
2021-02-01 02:51:28 +00:00
Body : payload ,
Command : isCmd ,
} ,
Values : values ,
2016-03-30 14:00:20 +00:00
}
}
2020-04-20 10:20:21 +00:00
func TestMkAlias ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2020-04-20 10:20:21 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . mkAliasCmd ( makeMessage ( "mkalias fuck mornings" , mkAliasRegex ) )
c . incrementCmd ( makeMessage ( "fuck++" , incrementRegex ) )
2021-12-21 04:31:19 +00:00
item , err := GetUserItem ( mb . Store ( ) , "tester" , "id" , "mornings" )
2020-04-20 10:20:21 +00:00
assert . Nil ( t , err )
assert . Equal ( t , 1 , item . Count )
}
func TestRmAlias ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2020-04-20 10:20:21 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . mkAliasCmd ( makeMessage ( "mkalias fuck mornings" , mkAliasRegex ) )
c . rmAliasCmd ( makeMessage ( "rmalias fuck" , rmAliasRegex ) )
c . incrementCmd ( makeMessage ( "fuck++" , incrementRegex ) )
2021-12-21 04:31:19 +00:00
item , err := GetUserItem ( mb . Store ( ) , "tester" , "id" , "mornings" )
2020-04-20 10:20:21 +00:00
assert . Nil ( t , err )
assert . Equal ( t , 0 , item . Count )
}
2018-10-19 18:20:40 +00:00
func TestThreeSentencesExists ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2018-10-19 18:20:40 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( ":beer:++" , incrementRegex ) )
c . teaMatchCmd ( makeMessage ( ":beer:. Earl Grey. Hot." , teaRegex ) )
2021-12-21 04:31:19 +00:00
item , err := GetUserItem ( mb . Store ( ) , "tester" , "id" , ":beer:" )
2018-10-19 18:20:40 +00:00
assert . Nil ( t , err )
assert . Equal ( t , 2 , item . Count )
}
func TestThreeSentencesNotExists ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2018-10-19 18:20:40 +00:00
assert . NotNil ( t , c )
2021-12-21 04:31:19 +00:00
item , err := GetUserItem ( mb . Store ( ) , "tester" , "id" , ":beer:" )
2021-02-01 02:51:28 +00:00
c . teaMatchCmd ( makeMessage ( ":beer:. Earl Grey. Hot." , teaRegex ) )
2021-12-21 04:31:19 +00:00
item , err = GetUserItem ( mb . Store ( ) , "tester" , "id" , ":beer:" )
2018-10-19 18:20:40 +00:00
assert . Nil ( t , err )
assert . Equal ( t , 0 , item . Count )
}
2017-01-31 20:35:05 +00:00
func TestTeaEarlGreyHot ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2017-01-31 20:35:05 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . teaMatchCmd ( makeMessage ( "Tea. Earl Grey. Hot." , teaRegex ) )
c . teaMatchCmd ( makeMessage ( "Tea. Earl Grey. Hot." , teaRegex ) )
2021-12-21 04:31:19 +00:00
item , err := GetUserItem ( mb . Store ( ) , "tester" , "id" , ":tea:" )
2017-01-31 20:35:05 +00:00
assert . Nil ( t , err )
assert . Equal ( t , 2 , item . Count )
}
2018-10-07 12:47:18 +00:00
func TestTeaTwoPeriods ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2018-10-07 12:47:18 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . teaMatchCmd ( makeMessage ( "Tea. Earl Grey." , teaRegex ) )
c . teaMatchCmd ( makeMessage ( "Tea. Earl Grey." , teaRegex ) )
2021-12-21 04:31:19 +00:00
item , err := GetUserItem ( mb . Store ( ) , "tester" , "id" , ":tea:" )
2018-10-07 12:47:18 +00:00
assert . Nil ( t , err )
assert . Equal ( t , 0 , item . Count )
}
func TestTeaMultiplePeriods ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2018-10-07 12:47:18 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . teaMatchCmd ( makeMessage ( "Tea. Earl Grey. Spiked. Hot." , teaRegex ) )
c . teaMatchCmd ( makeMessage ( "Tea. Earl Grey. Spiked. Hot." , teaRegex ) )
2021-12-21 04:31:19 +00:00
item , err := GetUserItem ( mb . Store ( ) , "tester" , "id" , ":tea:" )
2018-10-07 12:47:18 +00:00
assert . Nil ( t , err )
assert . Equal ( t , 2 , item . Count )
}
2017-02-15 04:56:29 +00:00
func TestTeaGreenHot ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2017-02-15 04:56:29 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . teaMatchCmd ( makeMessage ( "Tea. Green. Hot." , teaRegex ) )
c . teaMatchCmd ( makeMessage ( "Tea. Green. Hot" , teaRegex ) )
c . teaMatchCmd ( makeMessage ( "Tea. Green. Iced." , teaRegex ) )
2021-12-21 04:31:19 +00:00
item , err := GetUserItem ( mb . Store ( ) , "tester" , "id" , ":tea:" )
2017-02-15 04:56:29 +00:00
assert . Nil ( t , err )
assert . Equal ( t , 3 , item . Count )
}
func TestTeaUnrelated ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2017-02-15 04:56:29 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . teaMatchCmd ( makeMessage ( "Tea." , teaRegex ) )
c . teaMatchCmd ( makeMessage ( "Tea. It's great." , teaRegex ) )
2021-12-21 04:31:19 +00:00
item , err := GetUserItem ( mb . Store ( ) , "tester" , "id" , ":tea:" )
2017-02-20 19:57:12 +00:00
assert . Nil ( t , err )
assert . Equal ( t , 0 , item . Count )
}
func TestTeaSkieselQuote ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2017-02-20 19:57:12 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . teaMatchCmd ( makeMessage ( "blah, this is a whole page of explanation where \"we did local search and used a tabu list\" would have sufficed" , teaRegex ) )
2021-12-21 04:31:19 +00:00
item , err := GetUserItem ( mb . Store ( ) , "tester" , "id" , ":tea:" )
2017-02-15 04:56:29 +00:00
assert . Nil ( t , err )
assert . Equal ( t , 0 , item . Count )
}
2017-07-24 11:37:33 +00:00
func TestTeaUnicodeJapanese ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2017-07-24 11:37:33 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . teaMatchCmd ( makeMessage ( "Tea. おちや. Hot." , teaRegex ) )
2021-12-21 04:31:19 +00:00
item , err := GetUserItem ( mb . Store ( ) , "tester" , "id" , ":tea:" )
2017-07-24 11:37:33 +00:00
assert . Nil ( t , err )
assert . Equal ( t , 1 , item . Count )
}
2017-02-15 04:56:29 +00:00
2017-01-24 02:13:21 +00:00
func TestResetMe ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2017-01-24 02:13:21 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( "test++" , incrementRegex ) )
c . resetCmd ( makeMessage ( "!reset me" , resetRegex ) )
2021-12-21 04:31:19 +00:00
items , err := GetItems ( mb . Store ( ) , "tester" , "id" )
2017-01-24 02:13:21 +00:00
assert . Nil ( t , err )
assert . Len ( t , items , 0 )
}
2016-03-30 14:00:20 +00:00
func TestCounterOne ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2016-03-30 14:00:20 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( "test++" , incrementRegex ) )
2016-03-30 14:00:20 +00:00
assert . Len ( t , mb . Messages , 1 )
assert . Equal ( t , mb . Messages [ 0 ] , "tester has 1 test." )
}
2018-09-28 02:47:24 +00:00
func TestCounterOneWithSpace ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2018-09-28 02:47:24 +00:00
assert . NotNil ( t , c )
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( ":test: ++" , incrementRegex ) )
2018-09-28 02:47:24 +00:00
assert . Len ( t , mb . Messages , 1 )
assert . Equal ( t , mb . Messages [ 0 ] , "tester has 1 :test:." )
}
2016-03-30 14:00:20 +00:00
func TestCounterFour ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2016-03-30 14:00:20 +00:00
assert . NotNil ( t , c )
for i := 0 ; i < 4 ; i ++ {
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( "test++" , incrementRegex ) )
2016-03-30 14:00:20 +00:00
}
assert . Len ( t , mb . Messages , 4 )
assert . Equal ( t , mb . Messages [ 3 ] , "tester has 4 test." )
}
func TestCounterDecrement ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2016-03-30 14:00:20 +00:00
assert . NotNil ( t , c )
for i := 0 ; i < 4 ; i ++ {
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( "test++" , incrementRegex ) )
2016-03-30 14:00:20 +00:00
assert . Equal ( t , mb . Messages [ i ] , fmt . Sprintf ( "tester has %d test." , i + 1 ) )
}
2021-02-01 02:51:28 +00:00
c . decrementCmd ( makeMessage ( "test--" , decrementRegex ) )
2016-03-30 14:00:20 +00:00
assert . Len ( t , mb . Messages , 5 )
assert . Equal ( t , mb . Messages [ 4 ] , "tester has 3 test." )
}
func TestFriendCounterDecrement ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2016-03-30 14:00:20 +00:00
assert . NotNil ( t , c )
for i := 0 ; i < 4 ; i ++ {
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( "other.test++" , incrementRegex ) )
2016-03-30 14:00:20 +00:00
assert . Equal ( t , mb . Messages [ i ] , fmt . Sprintf ( "other has %d test." , i + 1 ) )
}
2021-02-01 02:51:28 +00:00
c . decrementCmd ( makeMessage ( "other.test--" , decrementRegex ) )
2016-03-30 14:00:20 +00:00
assert . Len ( t , mb . Messages , 5 )
assert . Equal ( t , mb . Messages [ 4 ] , "other has 3 test." )
}
func TestDecrementZero ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2016-03-30 14:00:20 +00:00
assert . NotNil ( t , c )
for i := 0 ; i < 4 ; i ++ {
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( "test++" , incrementRegex ) )
2016-03-30 14:00:20 +00:00
assert . Equal ( t , mb . Messages [ i ] , fmt . Sprintf ( "tester has %d test." , i + 1 ) )
}
j := 4
for i := 4 ; i > 0 ; i -- {
2021-02-01 02:51:28 +00:00
c . decrementCmd ( makeMessage ( "test--" , decrementRegex ) )
2016-03-30 14:00:20 +00:00
assert . Equal ( t , mb . Messages [ j ] , fmt . Sprintf ( "tester has %d test." , i - 1 ) )
j ++
}
assert . Len ( t , mb . Messages , 8 )
assert . Equal ( t , mb . Messages [ 7 ] , "tester has 0 test." )
}
func TestClear ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2016-03-30 14:00:20 +00:00
assert . NotNil ( t , c )
for i := 0 ; i < 4 ; i ++ {
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( "test++" , incrementRegex ) )
2016-03-30 14:00:20 +00:00
assert . Equal ( t , mb . Messages [ i ] , fmt . Sprintf ( "tester has %d test." , i + 1 ) )
}
2021-02-01 02:51:28 +00:00
res := c . clearCmd ( makeMessage ( "!clear test" , clearRegex ) )
2016-03-30 14:00:20 +00:00
assert . True ( t , res )
assert . Len ( t , mb . Actions , 1 )
assert . Equal ( t , mb . Actions [ 0 ] , "chops a few test out of his brain" )
}
func TestCount ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2016-03-30 14:00:20 +00:00
assert . NotNil ( t , c )
for i := 0 ; i < 4 ; i ++ {
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( "test++" , incrementRegex ) )
2016-03-30 14:00:20 +00:00
assert . Equal ( t , mb . Messages [ i ] , fmt . Sprintf ( "tester has %d test." , i + 1 ) )
}
2021-02-01 02:51:28 +00:00
res := c . countCmd ( makeMessage ( "!count test" , countRegex ) )
2016-03-30 14:00:20 +00:00
assert . True ( t , res )
2021-02-14 21:57:22 +00:00
if assert . Len ( t , mb . Messages , 5 ) {
assert . Equal ( t , "tester has 4 test." , mb . Messages [ 4 ] )
}
2016-03-30 14:00:20 +00:00
}
func TestInspectMe ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2016-03-30 14:00:20 +00:00
assert . NotNil ( t , c )
for i := 0 ; i < 4 ; i ++ {
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( "test++" , incrementRegex ) )
2016-03-30 14:00:20 +00:00
assert . Equal ( t , mb . Messages [ i ] , fmt . Sprintf ( "tester has %d test." , i + 1 ) )
}
for i := 0 ; i < 2 ; i ++ {
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( "fucks++" , incrementRegex ) )
2016-03-30 14:00:20 +00:00
assert . Equal ( t , mb . Messages [ i + 4 ] , fmt . Sprintf ( "tester has %d fucks." , i + 1 ) )
}
for i := 0 ; i < 20 ; i ++ {
2021-02-01 02:51:28 +00:00
c . incrementCmd ( makeMessage ( "cheese++" , incrementRegex ) )
2016-03-30 14:00:20 +00:00
assert . Equal ( t , mb . Messages [ i + 6 ] , fmt . Sprintf ( "tester has %d cheese." , i + 1 ) )
}
2021-02-01 02:51:28 +00:00
res := c . inspectCmd ( makeMessage ( "!inspect me" , inspectRegex ) )
2016-03-30 14:00:20 +00:00
assert . True ( t , res )
assert . Len ( t , mb . Messages , 27 )
assert . Equal ( t , mb . Messages [ 26 ] , "tester has the following counters: test: 4, fucks: 2, cheese: 20." )
}
func TestHelp ( t * testing . T ) {
2021-12-21 04:31:19 +00:00
mb , c , td := setup ( t )
defer td ( )
2016-03-30 14:00:20 +00:00
assert . NotNil ( t , c )
2019-05-27 23:21:53 +00:00
c . help ( & cli . CliPlugin { } , bot . Help , msg . Message { Channel : "channel" } , [ ] string { } )
2020-04-20 10:20:21 +00:00
assert . Greater ( t , len ( mb . Messages ) , 1 )
2016-03-30 14:00:20 +00:00
}