2020-07-13 20:58:44 +00:00
|
|
|
package countdown
|
|
|
|
|
|
|
|
import (
|
2021-12-22 18:52:16 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
bh "github.com/timshannon/bolthold"
|
|
|
|
"github.com/velour/catbase/plugins/babbler"
|
|
|
|
"github.com/velour/catbase/plugins/counter"
|
|
|
|
"os"
|
2020-07-13 20:58:44 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
)
|
|
|
|
|
2021-12-22 18:52:16 +00:00
|
|
|
func init() {
|
|
|
|
output := zerolog.ConsoleWriter{Out: os.Stdout}
|
|
|
|
log.Logger = log.Output(output).With().Caller().Stack().Logger()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setup() (*bot.MockBot, *CountdownPlugin, func()) {
|
2021-12-23 10:58:09 +00:00
|
|
|
// ensure the scheduled NYE doesn't happen and cause a race
|
|
|
|
nextYear = time.Now().Add(24 * 365 * time.Hour)
|
2021-12-22 18:52:16 +00:00
|
|
|
mb := bot.NewMockBot()
|
|
|
|
cd := New(mb)
|
|
|
|
return mb, cd, func() {
|
2021-12-23 10:58:09 +00:00
|
|
|
if err := mb.TearDown(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-22 18:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 20:58:44 +00:00
|
|
|
func TestScheduleNewYears(t *testing.T) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
p := New(mb)
|
|
|
|
functionTripped := false
|
|
|
|
f := func() {
|
|
|
|
functionTripped = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is lazy, but I don't feel like fixing it
|
|
|
|
nextYear = time.Now().Add(time.Millisecond * 5)
|
|
|
|
p.scheduleNYE(f)
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
|
|
|
|
assert.True(t, functionTripped)
|
|
|
|
}
|
2021-12-22 18:52:16 +00:00
|
|
|
|
|
|
|
func TestClearBabbles(t *testing.T) {
|
|
|
|
mb, cd, td := setup()
|
|
|
|
defer td()
|
|
|
|
b := babbler.Babbler{
|
|
|
|
Name: "foo",
|
|
|
|
}
|
|
|
|
if err := mb.Store().Insert(bh.NextSequence(), &b); err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if err := mb.Store().Insert(bh.NextSequence(), &b); err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if err := mb.Store().Insert(bh.NextSequence(), &b); err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
count, err := mb.Store().Count(babbler.Babbler{}, &bh.Query{})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Greater(t, count, 0)
|
|
|
|
err = cd.clearBabblers()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
count, err = mb.Store().Count(babbler.Babbler{}, &bh.Query{})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, count, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArchiveCounters(t *testing.T) {
|
|
|
|
mb, cd, td := setup()
|
|
|
|
defer td()
|
|
|
|
i := counter.Item{
|
|
|
|
Nick: "foo",
|
|
|
|
Item: "bar",
|
|
|
|
Count: 8,
|
|
|
|
UserID: "123",
|
|
|
|
}
|
|
|
|
if err := mb.Store().Insert(bh.NextSequence(), &i); err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if err := mb.Store().Insert(bh.NextSequence(), &i); err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if err := mb.Store().Insert(bh.NextSequence(), &i); err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
count, err := mb.Store().Count(counter.Item{}, &bh.Query{})
|
|
|
|
assert.Nil(t, err)
|
2021-12-23 10:58:09 +00:00
|
|
|
assert.Equal(t, 3, count)
|
2021-12-22 18:52:16 +00:00
|
|
|
|
|
|
|
err = cd.archiveCounters()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
count, err = mb.Store().Count(counter.Item{}, &bh.Query{})
|
|
|
|
assert.Nil(t, err)
|
2021-12-23 10:58:09 +00:00
|
|
|
assert.Equal(t, 0, count)
|
2021-12-22 19:05:59 +00:00
|
|
|
|
|
|
|
count, err = mb.Store().Count(counterArchive{}, &bh.Query{})
|
|
|
|
assert.Nil(t, err)
|
2021-12-23 10:58:09 +00:00
|
|
|
assert.Equal(t, 3, count)
|
2021-12-22 18:52:16 +00:00
|
|
|
}
|