2019-02-15 18:22:54 +00:00
|
|
|
|
package remember
|
2016-04-08 16:18:34 +00:00
|
|
|
|
|
|
|
|
|
import (
|
2021-02-19 18:23:29 +00:00
|
|
|
|
"regexp"
|
2016-04-08 16:18:34 +00:00
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2021-02-19 18:23:29 +00:00
|
|
|
|
"github.com/velour/catbase/plugins/cli"
|
|
|
|
|
|
2016-04-08 16:18:34 +00:00
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
|
"github.com/velour/catbase/bot/user"
|
2019-02-15 18:22:54 +00:00
|
|
|
|
"github.com/velour/catbase/plugins/fact"
|
2016-04-08 16:18:34 +00:00
|
|
|
|
)
|
|
|
|
|
|
2021-02-19 18:23:29 +00:00
|
|
|
|
func makeMessage(nick, payload string, r *regexp.Regexp) bot.Request {
|
2016-04-08 16:18:34 +00:00
|
|
|
|
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,
|
|
|
|
|
},
|
2016-04-08 16:18:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-15 18:22:54 +00:00
|
|
|
|
func makePlugin(t *testing.T) (*RememberPlugin, *fact.FactoidPlugin, *bot.MockBot) {
|
2016-04-08 16:18:34 +00:00
|
|
|
|
mb := bot.NewMockBot()
|
2021-07-21 18:52:45 +00:00
|
|
|
|
f := fact.New(mb) // for db table
|
2019-02-15 18:22:54 +00:00
|
|
|
|
p := New(mb)
|
2016-04-08 16:18:34 +00:00
|
|
|
|
assert.NotNil(t, p)
|
|
|
|
|
return p, f, mb
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 18:23:29 +00:00
|
|
|
|
var allMsg = regexp.MustCompile(`.*`)
|
|
|
|
|
|
2016-04-08 16:18:34 +00:00
|
|
|
|
// Test case
|
|
|
|
|
func TestCornerCaseBug(t *testing.T) {
|
2021-02-19 18:23:29 +00:00
|
|
|
|
msgs := []bot.Request{
|
|
|
|
|
makeMessage("user1", "I don’t want to personally touch a horse dick.", allMsg),
|
|
|
|
|
makeMessage("user3", "idk my bff rose?", allMsg),
|
2016-04-08 16:18:34 +00:00
|
|
|
|
}
|
2021-02-19 18:23:29 +00:00
|
|
|
|
rememberMsg := makeMessage("user2", "!remember user1 touch", rememberRegex)
|
2016-04-08 16:18:34 +00:00
|
|
|
|
|
|
|
|
|
p, _, mb := makePlugin(t)
|
|
|
|
|
|
|
|
|
|
for _, m := range msgs {
|
2021-02-19 18:23:29 +00:00
|
|
|
|
p.recordMsg(m)
|
2016-04-08 16:18:34 +00:00
|
|
|
|
}
|
2021-02-19 18:23:29 +00:00
|
|
|
|
p.rememberCmd(rememberMsg)
|
2016-04-08 16:18:34 +00:00
|
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
|
assert.Contains(t, mb.Messages[0], "horse dick")
|
2021-12-21 19:08:20 +00:00
|
|
|
|
q, err := fact.GetSingleFact(mb.Store(), "user1 quotes")
|
|
|
|
|
if assert.Nil(t, err) {
|
|
|
|
|
assert.Contains(t, q.Tidbit, "horse dick")
|
|
|
|
|
}
|
2016-04-08 16:18:34 +00:00
|
|
|
|
}
|