2018-08-30 20:05:03 +00:00
|
|
|
// © 2018 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
package picker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-10-08 22:15:07 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"github.com/velour/catbase/plugins/cli"
|
|
|
|
|
2018-08-30 20:05:03 +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-05-27 23:21:53 +00:00
|
|
|
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
|
2018-08-30 20:05:03 +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-08-30 20:05:03 +00:00
|
|
|
User: &user.User{Name: "tester"},
|
|
|
|
Channel: "test",
|
|
|
|
Body: payload,
|
|
|
|
Command: isCmd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 02:32:10 +00:00
|
|
|
func TestPick2(t *testing.T) {
|
2018-08-30 20:05:03 +00:00
|
|
|
mb := bot.NewMockBot()
|
|
|
|
c := New(mb)
|
|
|
|
assert.NotNil(t, c)
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessage("!pick 2 { a, b,c}"))
|
2018-08-30 20:05:03 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
2019-10-08 22:15:07 +00:00
|
|
|
assert.Contains(t, mb.Messages[0], "hot picks")
|
2018-08-30 20:05:03 +00:00
|
|
|
if !res {
|
|
|
|
t.Fatalf("expected a successful choice, got %q", mb.Messages[0])
|
|
|
|
}
|
|
|
|
}
|
2018-08-31 02:32:10 +00:00
|
|
|
|
|
|
|
func TestPickDefault(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
c := New(mb)
|
|
|
|
assert.NotNil(t, c)
|
2019-02-05 20:02:15 +00:00
|
|
|
_ = c.message(makeMessage("!pick { a}"))
|
2018-08-31 02:32:10 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
assert.Equal(t, `I've chosen "a" for you.`, mb.Messages[0])
|
|
|
|
}
|
2019-10-08 22:15:07 +00:00
|
|
|
|
|
|
|
func TestPickDefaultWithSeprator(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
c := New(mb)
|
|
|
|
assert.NotNil(t, c)
|
|
|
|
_ = c.message(makeMessage("!pick { a, b, c}"))
|
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
assert.Contains(t, mb.Messages[0], "I've chosen")
|
|
|
|
assert.NotContains(t, mb.Messages[0], "hot picks")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPickDelimiter(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
c := New(mb)
|
|
|
|
_ = c.message(makeMessage("!pick; {a; b}"))
|
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
assert.Contains(t, mb.Messages[0], "I've chosen")
|
|
|
|
assert.NotContains(t, mb.Messages[0], "hot picks")
|
|
|
|
log.Debug().Str("resp", mb.Messages[0]).Msg("choose")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPickDelimiterMulti(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
c := New(mb)
|
|
|
|
_ = c.message(makeMessage("!pick; 2 {a; b}"))
|
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
assert.Contains(t, mb.Messages[0], "hot picks")
|
|
|
|
log.Debug().Str("resp", mb.Messages[0]).Msg("choose")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPickDelimiterString(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
c := New(mb)
|
|
|
|
_ = c.message(makeMessage("!pick123 {a 123 b}"))
|
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
assert.Contains(t, mb.Messages[0], "I've chosen")
|
|
|
|
assert.NotContains(t, mb.Messages[0], "hot picks")
|
|
|
|
log.Debug().Str("resp", mb.Messages[0]).Msg("choose")
|
|
|
|
}
|
2019-11-22 16:52:20 +00:00
|
|
|
|
|
|
|
func TestKnownBrokenPick(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
c := New(mb)
|
|
|
|
_ = c.message(makeMessage("!pick⌘ { bagel/egg/smoked turkey/butte/cheese ⌘ fuck all that, just have a bagel and cream cheese }"))
|
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
assert.Contains(t, mb.Messages[0], "I've chosen")
|
|
|
|
assert.NotContains(t, mb.Messages[0], "hot picks")
|
|
|
|
log.Debug().Str("resp", mb.Messages[0]).Msg("choose")
|
|
|
|
}
|