2019-03-22 00:12:15 +00:00
|
|
|
package tldr
|
|
|
|
|
|
|
|
import (
|
2019-03-26 21:51:28 +00:00
|
|
|
"os"
|
2019-04-12 15:53:40 +00:00
|
|
|
"strconv"
|
2019-03-22 00:12:15 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2019-04-12 15:53:40 +00:00
|
|
|
"time"
|
2019-03-22 00:12:15 +00:00
|
|
|
|
2019-03-26 21:51:28 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2019-03-22 00:12:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
|
|
|
)
|
|
|
|
|
2019-03-26 21:51:28 +00:00
|
|
|
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 {
|
2019-03-22 00:12:15 +00:00
|
|
|
isCmd := strings.HasPrefix(payload, "!")
|
|
|
|
if isCmd {
|
|
|
|
payload = payload[1:]
|
|
|
|
}
|
2024-01-05 16:48:43 +00:00
|
|
|
|
|
|
|
return bot.Request{
|
|
|
|
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,
|
|
|
|
},
|
2019-03-22 00:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 16:48:43 +00:00
|
|
|
func makeMessage(payload string) bot.Request {
|
2019-03-22 00:12:15 +00:00
|
|
|
return makeMessageBy(payload, "tester")
|
|
|
|
}
|
|
|
|
|
|
|
|
func setup(t *testing.T) (*TLDRPlugin, *bot.MockBot) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
r := New(mb)
|
|
|
|
return r, mb
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:53:40 +00:00
|
|
|
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))
|
2019-04-12 15:53:40 +00:00
|
|
|
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)
|
2019-04-12 15:53:40 +00:00
|
|
|
}
|
2024-01-09 20:13:43 +00:00
|
|
|
assert.Len(t, c.history[ch], expected, "%d != %d", len(c.history), expected)
|
2019-04-12 15:53:40 +00:00
|
|
|
}
|