catbase/plugins/remember/remember_test.go

65 lines
1.5 KiB
Go
Raw Normal View History

package remember
import (
2021-02-19 18:23:29 +00:00
"regexp"
"strings"
"testing"
2021-02-19 18:23:29 +00:00
"github.com/velour/catbase/plugins/cli"
"github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
"github.com/velour/catbase/plugins/fact"
)
2021-02-19 18:23:29 +00:00
func makeMessage(nick, payload string, r *regexp.Regexp) bot.Request {
isCmd := strings.HasPrefix(payload, "!")
if isCmd {
payload = payload[1:]
}
2021-02-19 18:23:29 +00:00
return bot.Request{
Conn: &cli.CliPlugin{},
Kind: bot.Message,
Values: bot.ParseValues(r, payload),
Msg: msg.Message{
User: &user.User{Name: nick},
Channel: "test",
Body: payload,
Command: isCmd,
},
}
}
func makePlugin(t *testing.T) (*RememberPlugin, *fact.FactoidPlugin, *bot.MockBot) {
mb := bot.NewMockBot()
f := fact.New(mb) // for db table
p := New(mb)
assert.NotNil(t, p)
return p, f, mb
}
2021-02-19 18:23:29 +00:00
var allMsg = regexp.MustCompile(`.*`)
// Test case
func TestCornerCaseBug(t *testing.T) {
2021-02-19 18:23:29 +00:00
msgs := []bot.Request{
makeMessage("user1", "I dont want to personally touch a horse dick.", allMsg),
makeMessage("user3", "idk my bff rose?", allMsg),
}
2021-02-19 18:23:29 +00:00
rememberMsg := makeMessage("user2", "!remember user1 touch", rememberRegex)
p, _, mb := makePlugin(t)
for _, m := range msgs {
2021-02-19 18:23:29 +00:00
p.recordMsg(m)
}
2021-02-19 18:23:29 +00:00
p.rememberCmd(rememberMsg)
assert.Len(t, mb.Messages, 1)
assert.Contains(t, mb.Messages[0], "horse dick")
q, err := fact.GetSingleFact(mb.DB(), "user1 quotes")
assert.Nil(t, err)
assert.Contains(t, q.Tidbit, "horse dick")
}