2020-07-13 15:34:53 +00:00
|
|
|
package goals
|
|
|
|
|
|
|
|
import (
|
2021-12-21 04:31:19 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
|
|
|
"github.com/velour/catbase/plugins/cli"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-07-13 15:34:53 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/plugins/counter"
|
|
|
|
)
|
|
|
|
|
2021-12-21 04:31:19 +00:00
|
|
|
func setup(t *testing.T) (*bot.MockBot, *GoalsPlugin, func()) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
c := New(mb)
|
|
|
|
return mb, c, func() {
|
|
|
|
if err := mb.TearDown(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeMessage(payload string, r *regexp.Regexp) bot.Request {
|
|
|
|
isCmd := strings.HasPrefix(payload, "!")
|
|
|
|
if isCmd {
|
|
|
|
payload = payload[1:]
|
|
|
|
}
|
|
|
|
values := bot.ParseValues(r, payload)
|
|
|
|
return bot.Request{
|
|
|
|
Conn: &cli.CliPlugin{},
|
|
|
|
Msg: msg.Message{
|
|
|
|
User: &user.User{Name: "tester", ID: "id"},
|
|
|
|
Body: payload,
|
|
|
|
Command: isCmd,
|
|
|
|
},
|
|
|
|
Values: values,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendMessage(gp *GoalsPlugin, message string) {
|
|
|
|
for _, h := range gp.handlers {
|
|
|
|
r := makeMessage(message, h.Regex)
|
|
|
|
if h.Regex.MatchString(message) {
|
|
|
|
h.Handler(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 15:34:53 +00:00
|
|
|
func TestRemainingSomeToGo(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
|
2021-12-21 04:31:19 +00:00
|
|
|
i := &counter.Item{
|
2020-07-13 15:34:53 +00:00
|
|
|
ID: 0,
|
|
|
|
Nick: "",
|
|
|
|
Item: "",
|
|
|
|
Count: 190,
|
|
|
|
}
|
|
|
|
|
|
|
|
g := goal{
|
|
|
|
ID: 0,
|
|
|
|
Kind: "",
|
|
|
|
Who: "",
|
|
|
|
What: "",
|
|
|
|
Amount: 366,
|
|
|
|
gp: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
now = func() time.Time {
|
|
|
|
return time.Date(2020, 07, 13, 11, 23, 00, 00, time.UTC)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := New(mb)
|
|
|
|
|
|
|
|
expected := 5
|
|
|
|
actual := p.calculateRemaining(i, &g)
|
|
|
|
|
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemainingAheadOfCurve(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
|
2021-12-21 04:31:19 +00:00
|
|
|
i := &counter.Item{
|
2020-07-13 15:34:53 +00:00
|
|
|
ID: 0,
|
|
|
|
Nick: "",
|
|
|
|
Item: "",
|
|
|
|
Count: 200,
|
|
|
|
}
|
|
|
|
|
|
|
|
g := goal{
|
|
|
|
ID: 0,
|
|
|
|
Kind: "",
|
|
|
|
Who: "",
|
|
|
|
What: "",
|
|
|
|
Amount: 366,
|
|
|
|
gp: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
now = func() time.Time {
|
|
|
|
return time.Date(2020, 07, 13, 11, 23, 00, 00, time.UTC)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := New(mb)
|
|
|
|
|
|
|
|
expected := -5
|
|
|
|
actual := p.calculateRemaining(i, &g)
|
|
|
|
|
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
2021-12-21 04:31:19 +00:00
|
|
|
|
|
|
|
func TestRegister(t *testing.T) {
|
|
|
|
_, gp, td := setup(t)
|
|
|
|
defer td()
|
|
|
|
|
|
|
|
sendMessage(gp, "register goal :tea: 5")
|
|
|
|
}
|