catbase/plugins/goals/goals_test.go

122 lines
2.0 KiB
Go

package goals
import (
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
"github.com/velour/catbase/plugins/cli"
"regexp"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/plugins/counter"
)
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)
}
}
}
func TestRemainingSomeToGo(t *testing.T) {
mb := bot.NewMockBot()
i := &counter.Item{
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()
i := &counter.Item{
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)
}
func TestRegister(t *testing.T) {
_, gp, td := setup(t)
defer td()
sendMessage(gp, "register goal :tea: 5")
}