2024-07-18 22:40:36 +08:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-telegram/bot/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_applyMiddlewares(t *testing.T) {
|
|
|
|
h := func(ctx context.Context, bot *Bot, update *models.Update) {}
|
|
|
|
|
|
|
|
middleware1 := func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(ctx context.Context, bot *Bot, update *models.Update) {
|
|
|
|
next(ctx, bot, update)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
middleware2 := func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(ctx context.Context, bot *Bot, update *models.Update) {
|
|
|
|
next(ctx, bot, update)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapped := applyMiddlewares(h, middleware1, middleware2)
|
|
|
|
if wrapped == nil {
|
|
|
|
t.Fatal("Expected wrapped handler to be non-nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessUpdate(t *testing.T) {
|
|
|
|
var called bool
|
|
|
|
h := func(ctx context.Context, bot *Bot, update *models.Update) {
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
|
|
|
|
bot := &Bot{
|
|
|
|
defaultHandlerFunc: h,
|
|
|
|
middlewares: []Middleware{},
|
2024-10-17 16:17:15 +08:00
|
|
|
notAsyncHandlers: true,
|
2024-07-18 22:40:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
upd := &models.Update{Message: &models.Message{Text: "test"}}
|
|
|
|
|
|
|
|
bot.ProcessUpdate(ctx, upd)
|
|
|
|
if !called {
|
|
|
|
t.Fatal("Expected default handler to be called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessUpdate_WithMiddlewares(t *testing.T) {
|
|
|
|
var called bool
|
|
|
|
h := func(ctx context.Context, bot *Bot, update *models.Update) {
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
|
|
|
|
middleware := func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(ctx context.Context, bot *Bot, update *models.Update) {
|
|
|
|
next(ctx, bot, update)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bot := &Bot{
|
|
|
|
defaultHandlerFunc: h,
|
|
|
|
middlewares: []Middleware{middleware},
|
2024-10-17 16:17:15 +08:00
|
|
|
notAsyncHandlers: true,
|
2024-07-18 22:40:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
upd := &models.Update{Message: &models.Message{Text: "test"}}
|
|
|
|
|
|
|
|
bot.ProcessUpdate(ctx, upd)
|
|
|
|
if !called {
|
|
|
|
t.Fatal("Expected default handler to be called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-10 23:13:16 +08:00
|
|
|
func TestProcessUpdate_WithMatchTypeFunc(t *testing.T) {
|
|
|
|
var called string
|
|
|
|
h1 := func(ctx context.Context, bot *Bot, update *models.Update) {
|
|
|
|
called = "h1"
|
|
|
|
}
|
|
|
|
h2 := func(ctx context.Context, bot *Bot, update *models.Update) {
|
|
|
|
called = "h2"
|
|
|
|
}
|
|
|
|
m := func(update *models.Update) bool {
|
|
|
|
return update.CallbackQuery.GameShortName == "game"
|
|
|
|
}
|
|
|
|
|
|
|
|
bot := &Bot{
|
|
|
|
defaultHandlerFunc: h1,
|
2024-10-17 16:17:15 +08:00
|
|
|
notAsyncHandlers: true,
|
2024-09-10 23:13:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bot.RegisterHandlerMatchFunc(m, h2)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
upd := &models.Update{ID: 42, CallbackQuery: &models.CallbackQuery{ID: "test", GameShortName: "game"}}
|
|
|
|
|
|
|
|
bot.ProcessUpdate(ctx, upd)
|
|
|
|
if called != "h2" {
|
|
|
|
t.Fatalf("Expected h2 handler to be called but %s handler was called", called)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 22:40:36 +08:00
|
|
|
func Test_findHandler(t *testing.T) {
|
|
|
|
var called bool
|
|
|
|
h := func(ctx context.Context, bot *Bot, update *models.Update) {
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
|
|
|
|
bot := &Bot{
|
|
|
|
defaultHandlerFunc: h,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a handler
|
2024-09-16 16:38:22 +08:00
|
|
|
bot.handlers = append(bot.handlers, handler{
|
|
|
|
id: "test",
|
2024-07-18 22:40:36 +08:00
|
|
|
handlerType: HandlerTypeMessageText,
|
|
|
|
matchType: MatchTypeExact,
|
|
|
|
pattern: "test",
|
|
|
|
handler: h,
|
2024-09-16 16:38:22 +08:00
|
|
|
})
|
2024-07-18 22:40:36 +08:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
upd := &models.Update{Message: &models.Message{Text: "test"}}
|
|
|
|
|
2024-09-10 23:13:16 +08:00
|
|
|
handler := bot.findHandler(upd)
|
2024-07-18 22:40:36 +08:00
|
|
|
handler(ctx, bot, upd)
|
|
|
|
|
|
|
|
if !called {
|
|
|
|
t.Fatal("Expected registered handler to be called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-23 16:04:26 +08:00
|
|
|
func Test_findPhotoCaptionHandler(t *testing.T) {
|
|
|
|
var called bool
|
|
|
|
h := func(ctx context.Context, bot *Bot, update *models.Update) {
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
|
|
|
|
bot := &Bot{
|
|
|
|
defaultHandlerFunc: h,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a handler
|
|
|
|
bot.handlers = append(bot.handlers, handler{
|
|
|
|
id: "test",
|
|
|
|
handlerType: HandlerTypePhotoCaption,
|
|
|
|
matchType: MatchTypeExact,
|
|
|
|
pattern: "test",
|
|
|
|
handler: h,
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
upd := &models.Update{Message: &models.Message{Caption: "test"}}
|
|
|
|
|
|
|
|
handler := bot.findHandler(upd)
|
|
|
|
handler(ctx, bot, upd)
|
|
|
|
|
|
|
|
if !called {
|
|
|
|
t.Fatal("Expected registered handler to be called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 22:40:36 +08:00
|
|
|
func Test_findHandler_Default(t *testing.T) {
|
|
|
|
var called bool
|
|
|
|
h := func(ctx context.Context, bot *Bot, update *models.Update) {
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
|
|
|
|
bot := &Bot{
|
|
|
|
defaultHandlerFunc: h,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
upd := &models.Update{Message: &models.Message{Text: "test"}}
|
|
|
|
|
2024-09-10 23:13:16 +08:00
|
|
|
handler := bot.findHandler(upd)
|
2024-07-18 22:40:36 +08:00
|
|
|
handler(ctx, bot, upd)
|
|
|
|
|
|
|
|
if !called {
|
|
|
|
t.Fatal("Expected default handler to be called")
|
|
|
|
}
|
|
|
|
}
|