Compare commits

..

No commits in common. "4f9b50f47a9731211f2ca5064dcadac697f3d4aa" and "fff478a9f66deb502c2757c01585b3f44ab28ea6" have entirely different histories.

2 changed files with 99 additions and 2 deletions

View File

@ -26,6 +26,7 @@ type Impossible struct {
title string title string
content []string content []string
updated time.Time updated time.Time
testing bool
} }
func New(b bot.Bot) *Impossible { func New(b bot.Bot) *Impossible {
@ -35,6 +36,23 @@ func New(b bot.Bot) *Impossible {
title: "", title: "",
content: []string{}, content: []string{},
updated: getTodaysMidnight().Add(time.Hour * -24), updated: getTodaysMidnight().Add(time.Hour * -24),
testing: false,
}
b.Register(i, bot.Help, i.help)
i.register()
return i
}
func newTesting(b bot.Bot) *Impossible {
i := &Impossible{
b: b,
c: b.Config(),
title: "",
content: []string{},
updated: getTodaysMidnight().Add(time.Hour * -24),
testing: true,
} }
b.Register(i, bot.Help, i.help) b.Register(i, bot.Help, i.help)
@ -57,6 +75,10 @@ func (p *Impossible) tryRefresh(r bot.Request) (sent bool) {
for !p.refreshImpossible() { for !p.refreshImpossible() {
} }
if p.testing {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, p.title)
sent = true
}
} }
return sent return sent
} }
@ -151,6 +173,6 @@ func (p *Impossible) refreshImpossible() bool {
} }
func getTodaysMidnight() time.Time { func getTodaysMidnight() time.Time {
y, m, d := time.Now().Date() now := time.Now()
return time.Date(y, m, d, 0, 0, 0, 0, time.Local) return time.Date(now.Year(), now.Month(), now.Day(), 24, 0, 0, 0, now.Location())
} }

View File

@ -0,0 +1,75 @@
package impossible
import (
"fmt"
"regexp"
"strings"
"testing"
"github.com/velour/catbase/plugins/cli"
"github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
"github.com/velour/catbase/plugins/counter"
)
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{},
Kind: bot.Message,
Values: values,
Msg: msg.Message{
User: &user.User{Name: "tester"},
Channel: "test",
Body: payload,
Command: isCmd,
},
}
}
func makePlugin(t *testing.T) (*Impossible, *bot.MockBot) {
mb := bot.NewMockBot()
counter.New(mb)
p := newTesting(mb)
assert.NotNil(t, p)
return p, mb
}
func testMessage(p *Impossible, body string) {
for _, h := range p.handlers {
if h.Regex.MatchString(body) && h.Handler(makeMessage(body, h.Regex)) {
return
}
}
}
func TestNothing(t *testing.T) {
p, mb := makePlugin(t)
testMessage(p, "hi")
testMessage(p, "nothing")
assert.Len(t, mb.Messages, 1)
}
func TestHint(t *testing.T) {
p, mb := makePlugin(t)
testMessage(p, "hi")
testMessage(p, "hint")
assert.Len(t, mb.Messages, 2)
}
func TestCorrect(t *testing.T) {
p, mb := makePlugin(t)
testMessage(p, "hi")
testMessage(p, mb.Messages[0])
congrats := fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", mb.Messages[0])
assert.Contains(t, mb.Messages[1], congrats)
}