package countdown import ( "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" "testing" "time" "github.com/stretchr/testify/assert" "github.com/velour/catbase/bot" ) func init() { output := zerolog.ConsoleWriter{Out: os.Stdout} log.Logger = log.Output(output).With().Caller().Stack().Logger() } func setup() (*bot.MockBot, *CountdownPlugin, func()) { // ensure the scheduled NYE doesn't happen and cause a race nextYear = time.Now().Add(24 * 365 * time.Hour) mb := bot.NewMockBot() cd := New(mb) return mb, cd, func() { if err := mb.TearDown(); err != nil { panic(err) } } } 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) } 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) assert.Equal(t, 3, count) err = cd.archiveCounters() assert.Nil(t, err) count, err = mb.Store().Count(counter.Item{}, &bh.Query{}) assert.Nil(t, err) assert.Equal(t, 0, count) count, err = mb.Store().Count(counterArchive{}, &bh.Query{}) assert.Nil(t, err) assert.Equal(t, 3, count) }