2016-05-09 17:09:17 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
package reminder
|
|
|
|
|
|
|
|
import (
|
2017-05-01 15:54:44 +00:00
|
|
|
"fmt"
|
2019-05-27 23:21:53 +00:00
|
|
|
"github.com/velour/catbase/plugins/cli"
|
2016-05-09 17:09:17 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
|
|
|
)
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
|
2019-02-05 20:02:15 +00:00
|
|
|
return makeMessageBy(payload, "tester")
|
2016-05-09 17:09:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func makeMessageBy(payload, by string) (bot.Connector, bot.Kind, msg.Message) {
|
2018-02-05 21:04:40 +00:00
|
|
|
isCmd := strings.HasPrefix(payload, "!")
|
|
|
|
if isCmd {
|
|
|
|
payload = payload[1:]
|
|
|
|
}
|
2019-05-27 23:21:53 +00:00
|
|
|
return &cli.CliPlugin{}, bot.Message, msg.Message{
|
2018-02-05 21:04:40 +00:00
|
|
|
User: &user.User{Name: by},
|
|
|
|
Channel: "test",
|
|
|
|
Body: payload,
|
|
|
|
Command: isCmd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 04:31:19 +00:00
|
|
|
func setup(t *testing.T) (*ReminderPlugin, *bot.MockBot, func()) {
|
2018-10-26 15:01:01 +00:00
|
|
|
mb := bot.NewMockBot()
|
2019-01-21 17:36:02 +00:00
|
|
|
r := New(mb)
|
2021-12-21 04:31:19 +00:00
|
|
|
return r, mb, func() {
|
|
|
|
mb.TearDown()
|
|
|
|
}
|
2019-01-21 17:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMeReminder(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessage("!remind me in 1s don't fail this test"))
|
2018-10-26 15:01:01 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
assert.Len(t, mb.Messages, 2)
|
|
|
|
assert.True(t, res)
|
|
|
|
assert.Contains(t, mb.Messages[0], "Okay. I'll remind you.")
|
2018-10-26 17:38:12 +00:00
|
|
|
assert.Contains(t, mb.Messages[1], "Hey tester, you wanted to be reminded: don't fail this test")
|
2018-10-26 15:01:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 17:09:17 +00:00
|
|
|
func TestReminder(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessage("!remind testuser in 1s don't fail this test"))
|
2016-05-09 17:09:17 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
assert.Len(t, mb.Messages, 2)
|
|
|
|
assert.True(t, res)
|
|
|
|
assert.Contains(t, mb.Messages[0], "Sure tester, I'll remind testuser.")
|
|
|
|
assert.Contains(t, mb.Messages[1], "Hey testuser, tester wanted you to be reminded: don't fail this test")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReminderReorder(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2021-12-21 19:08:20 +00:00
|
|
|
min, max := 1, 2
|
|
|
|
res := c.message(makeMessage(fmt.Sprintf("!remind testuser in %ds don't fail this test 1", min)))
|
2016-05-09 17:09:17 +00:00
|
|
|
assert.True(t, res)
|
2021-12-21 19:08:20 +00:00
|
|
|
res = c.message(makeMessage(fmt.Sprintf("!remind testuser in %ds don't fail this test 2", max)))
|
2016-05-09 17:09:17 +00:00
|
|
|
assert.True(t, res)
|
2021-12-21 19:08:20 +00:00
|
|
|
|
|
|
|
time.Sleep(time.Duration(max+1) * time.Second)
|
2016-05-09 17:09:17 +00:00
|
|
|
assert.Len(t, mb.Messages, 4)
|
|
|
|
assert.Contains(t, mb.Messages[0], "Sure tester, I'll remind testuser.")
|
|
|
|
assert.Contains(t, mb.Messages[1], "Sure tester, I'll remind testuser.")
|
|
|
|
assert.Contains(t, mb.Messages[2], "Hey testuser, tester wanted you to be reminded: don't fail this test 1")
|
|
|
|
assert.Contains(t, mb.Messages[3], "Hey testuser, tester wanted you to be reminded: don't fail this test 2")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReminderParse(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessage("!remind testuser in unparseable don't fail this test"))
|
2016-05-09 17:09:17 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
assert.True(t, res)
|
2019-02-20 19:17:45 +00:00
|
|
|
assert.Contains(t, mb.Messages[0], "Easy cowboy, not sure I can parse that duration. Try something like '1.5h' or '2h45m'.")
|
2016-05-09 17:09:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 16:47:18 +00:00
|
|
|
func TestEmptyList(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessage("!list reminders"))
|
2017-04-27 16:47:18 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
assert.True(t, res)
|
|
|
|
assert.Contains(t, mb.Messages[0], "no pending reminders")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestList(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessage("!remind testuser in 5m don't fail this test 1"))
|
2017-04-27 16:47:18 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessage("!remind testuser in 5m don't fail this test 2"))
|
2017-04-27 16:47:18 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessage("!list reminders"))
|
2017-04-27 16:47:18 +00:00
|
|
|
assert.True(t, res)
|
|
|
|
assert.Len(t, mb.Messages, 3)
|
|
|
|
assert.Contains(t, mb.Messages[2], "1) tester -> testuser :: don't fail this test 1 @ ")
|
|
|
|
assert.Contains(t, mb.Messages[2], "2) tester -> testuser :: don't fail this test 2 @ ")
|
|
|
|
}
|
|
|
|
|
2018-02-05 21:04:40 +00:00
|
|
|
func TestListBy(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessageBy("!remind testuser in 5m don't fail this test 1", "testuser"))
|
2018-02-05 21:04:40 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2"))
|
2018-02-05 21:04:40 +00:00
|
|
|
assert.True(t, res)
|
2021-12-21 19:08:20 +00:00
|
|
|
res = c.message(makeMessage("!list reminders from testuser"))
|
2018-02-05 21:04:40 +00:00
|
|
|
assert.True(t, res)
|
|
|
|
assert.Len(t, mb.Messages, 3)
|
|
|
|
assert.Contains(t, mb.Messages[2], "don't fail this test 1 @ ")
|
|
|
|
assert.NotContains(t, mb.Messages[2], "don't fail this test 2 @ ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListTo(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser"))
|
2018-02-05 21:04:40 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2"))
|
2018-02-05 21:04:40 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessage("!list reminders to testuser"))
|
2018-02-05 21:04:40 +00:00
|
|
|
assert.True(t, res)
|
|
|
|
assert.Len(t, mb.Messages, 3)
|
|
|
|
assert.NotContains(t, mb.Messages[2], "don't fail this test 1 @ ")
|
|
|
|
assert.Contains(t, mb.Messages[2], "don't fail this test 2 @ ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToEmptyList(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser"))
|
2018-02-05 21:04:40 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2"))
|
2018-02-05 21:04:40 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessage("!list reminders to test"))
|
2018-02-05 21:04:40 +00:00
|
|
|
assert.True(t, res)
|
|
|
|
assert.Len(t, mb.Messages, 3)
|
|
|
|
assert.Contains(t, mb.Messages[2], "no pending reminders")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromEmptyList(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser"))
|
2018-02-05 21:04:40 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2"))
|
2018-02-05 21:04:40 +00:00
|
|
|
assert.True(t, res)
|
2021-12-21 04:31:19 +00:00
|
|
|
res = c.message(makeMessage("!list reminders From test"))
|
2018-02-05 21:04:40 +00:00
|
|
|
assert.True(t, res)
|
|
|
|
assert.Len(t, mb.Messages, 3)
|
|
|
|
assert.Contains(t, mb.Messages[2], "no pending reminders")
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:54:44 +00:00
|
|
|
func TestBatchMax(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-01-20 20:21:26 +00:00
|
|
|
c.config.Set("Reminder.MaxBatchAdd", "10")
|
2017-05-01 15:54:44 +00:00
|
|
|
assert.NotNil(t, c)
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessage("!remind testuser every 1h for 24h yikes"))
|
2017-05-01 15:54:44 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessage("!list reminders"))
|
2017-05-01 15:54:44 +00:00
|
|
|
assert.True(t, res)
|
|
|
|
time.Sleep(6 * time.Second)
|
|
|
|
assert.Len(t, mb.Messages, 2)
|
|
|
|
assert.Contains(t, mb.Messages[0], "Easy cowboy, that's a lot of reminders. I'll add some of them.")
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
assert.Contains(t, mb.Messages[1], fmt.Sprintf("%d) tester -> testuser :: yikes", i+1))
|
|
|
|
}
|
|
|
|
}
|
2017-04-27 16:47:18 +00:00
|
|
|
|
2017-05-09 14:12:24 +00:00
|
|
|
func TestCancel(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2017-05-09 14:12:24 +00:00
|
|
|
assert.NotNil(t, c)
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessage("!remind testuser in 1m don't fail this test"))
|
2017-05-09 14:12:24 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessage("!cancel reminder 1"))
|
2017-05-09 14:12:24 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessage("!list reminders"))
|
2017-05-09 14:12:24 +00:00
|
|
|
assert.True(t, res)
|
|
|
|
assert.Len(t, mb.Messages, 3)
|
|
|
|
assert.Contains(t, mb.Messages[0], "Sure tester, I'll remind testuser.")
|
2017-05-11 16:40:59 +00:00
|
|
|
assert.Contains(t, mb.Messages[1], "successfully canceled reminder: 1")
|
2017-05-09 14:12:24 +00:00
|
|
|
assert.Contains(t, mb.Messages[2], "no pending reminders")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCancelMiss(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2017-05-09 14:12:24 +00:00
|
|
|
assert.NotNil(t, c)
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessage("!cancel reminder 1"))
|
2017-05-09 14:12:24 +00:00
|
|
|
assert.True(t, res)
|
|
|
|
assert.Len(t, mb.Messages, 1)
|
2017-05-11 16:40:59 +00:00
|
|
|
assert.Contains(t, mb.Messages[0], "failed to find and cancel reminder: 1")
|
2017-05-09 14:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 21:18:16 +00:00
|
|
|
func TestLimitList(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-01-27 21:18:16 +00:00
|
|
|
c.config.Set("Reminder.MaxBatchAdd", "10")
|
|
|
|
c.config.Set("Reminder.MaxList", "25")
|
|
|
|
assert.NotNil(t, c)
|
|
|
|
|
|
|
|
//Someone can redo this with a single batch add, but I can't locally due to an old version of sqllite (maybe).
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessage("!remind testuser every 1h for 10h don't fail this test"))
|
2019-01-27 21:18:16 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessage("!remind testuser every 1h for 10h don't fail this test"))
|
2019-01-27 21:18:16 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessage("!remind testuser every 1h for 10h don't fail this test"))
|
2019-01-27 21:18:16 +00:00
|
|
|
assert.True(t, res)
|
2019-02-05 20:02:15 +00:00
|
|
|
res = c.message(makeMessage("!list reminders"))
|
2019-01-27 21:18:16 +00:00
|
|
|
assert.True(t, res)
|
|
|
|
assert.Len(t, mb.Messages, 4)
|
|
|
|
assert.Contains(t, mb.Messages[0], "Sure tester, I'll remind testuser.")
|
|
|
|
assert.Contains(t, mb.Messages[1], "Sure tester, I'll remind testuser.")
|
|
|
|
assert.Contains(t, mb.Messages[2], "Sure tester, I'll remind testuser.")
|
|
|
|
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
assert.Contains(t, mb.Messages[3], fmt.Sprintf("%d) tester -> testuser :: don't fail this test", i+1))
|
|
|
|
}
|
2019-02-05 16:36:18 +00:00
|
|
|
assert.Contains(t, mb.Messages[3], "more...")
|
2019-01-27 21:18:16 +00:00
|
|
|
|
|
|
|
assert.NotContains(t, mb.Messages[3], "26) tester -> testuser")
|
|
|
|
}
|
|
|
|
|
2016-05-09 17:09:17 +00:00
|
|
|
func TestHelp(t *testing.T) {
|
2021-12-21 04:31:19 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
2016-05-09 17:09:17 +00:00
|
|
|
assert.NotNil(t, c)
|
2021-12-21 04:31:19 +00:00
|
|
|
c.help(&cli.CliPlugin{}, bot.Help, msg.Message{Channel: "Channel"}, []string{})
|
2016-05-09 17:09:17 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
}
|