catbase/plugins/tldr/tldr_test.go

70 lines
1.4 KiB
Go
Raw Normal View History

package tldr
import (
2019-05-27 23:21:53 +00:00
"github.com/velour/catbase/plugins/cli"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
)
func init() {
log.Logger = log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
2024-01-09 20:13:43 +00:00
var ch = "test"
2024-01-05 16:48:43 +00:00
func makeMessageBy(payload, by string) bot.Request {
isCmd := strings.HasPrefix(payload, "!")
if isCmd {
payload = payload[1:]
}
2024-01-05 16:48:43 +00:00
return bot.Request{
Conn: &cli.CliPlugin{},
Kind: bot.Message,
Msg: msg.Message{
User: &user.User{Name: by},
2024-01-09 20:13:43 +00:00
Channel: ch,
2024-01-05 16:48:43 +00:00
Body: payload,
Command: isCmd,
},
}
}
2024-01-05 16:48:43 +00:00
func makeMessage(payload string) bot.Request {
return makeMessageBy(payload, "tester")
}
func setup(t *testing.T) (*TLDRPlugin, *bot.MockBot) {
mb := bot.NewMockBot()
r := New(mb)
return r, mb
}
func TestAddHistoryLimitsDays(t *testing.T) {
c, _ := setup(t)
hrs := 24
expected := 24
2024-01-05 16:44:05 +00:00
c.b.Config().Set("TLDR.HistorySize", "100")
c.b.Config().Set("TLDR.KeepHours", strconv.Itoa(hrs))
t0 := time.Now().Add(-time.Duration(hrs*2) * time.Hour)
for i := 0; i < 48; i++ {
hist := history{
body: "test",
user: "tester",
timestamp: t0.Add(time.Duration(i) * time.Hour),
}
2024-01-09 20:13:43 +00:00
c.addHistory(ch, hist)
}
2024-01-09 20:13:43 +00:00
assert.Len(t, c.history[ch], expected, "%d != %d", len(c.history), expected)
}