From c79142201ef1172a5995f46cb76dab335196e52e Mon Sep 17 00:00:00 2001 From: Christiaan Date: Fri, 9 Feb 2024 22:34:42 +0100 Subject: [PATCH] Write tests Signed-off-by: Christiaan de Die le Clercq --- pkg/services/telegram.go | 41 ++++++++++++++------------ pkg/services/telegram_test.go | 54 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 18 deletions(-) create mode 100644 pkg/services/telegram_test.go diff --git a/pkg/services/telegram.go b/pkg/services/telegram.go index 60aaf5d0..c7624afe 100644 --- a/pkg/services/telegram.go +++ b/pkg/services/telegram.go @@ -19,45 +19,50 @@ type telegramService struct { opts TelegramOptions } -func (s telegramService) Send(notification Notification, dest Destination) error { - bot, err := tgbotapi.NewBotAPI(s.opts.Token) - if err != nil { - return err - } +func buildTelegramMessageOptions(notification Notification, dest Destination) (*tgbotapi.MessageConfig, error) { + msg := tgbotapi.MessageConfig{} if strings.HasPrefix(dest.Recipient, "-") { chatChannel := strings.Split(dest.Recipient, "|") chatID, err := strconv.ParseInt(chatChannel[0], 10, 64) if err != nil { - return err + return nil, err } // Init message with ParseMode is 'Markdown' - msg := tgbotapi.NewMessage(chatID, notification.Message) + msg = tgbotapi.NewMessage(chatID, notification.Message) msg.ParseMode = "Markdown" if len(chatChannel) > 1 { threadID, err := strconv.Atoi(chatChannel[1]) if err != nil { - return err + return nil, err } msg.MessageThreadID = threadID } - - _, err = bot.Send(msg) - if err != nil { - return err - } } else { // Init message with ParseMode is 'Markdown' - msg := tgbotapi.NewMessageToChannel("@"+dest.Recipient, notification.Message) + msg = tgbotapi.NewMessageToChannel("@"+dest.Recipient, notification.Message) msg.ParseMode = "Markdown" + } + return &msg, nil +} - _, err := bot.Send(msg) - if err != nil { - return err - } +func (s telegramService) Send(notification Notification, dest Destination) error { + bot, err := tgbotapi.NewBotAPI(s.opts.Token) + if err != nil { + return err + } + + msg, err := buildTelegramMessageOptions(notification, dest) + if err != nil { + return err + } + + _, err = bot.Send(msg) + if err != nil { + return err } return nil diff --git a/pkg/services/telegram_test.go b/pkg/services/telegram_test.go new file mode 100644 index 00000000..8e081c30 --- /dev/null +++ b/pkg/services/telegram_test.go @@ -0,0 +1,54 @@ +package services + +import ( + "reflect" + "testing" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" +) + +func TestBuildTelegramMessageOptions(t *testing.T) { + tests := []struct { + name string + notification Notification + dest Destination + want *tgbotapi.MessageConfig + wantErr bool + }{ + { + name: "Message to chat", + notification: Notification{Message: "Test message"}, + dest: Destination{Recipient: "-123456"}, + want: &tgbotapi.MessageConfig{Text: "Test message", BaseChat: tgbotapi.BaseChat{ChatConfig: tgbotapi.ChatConfig{ChatID: -123456}, MessageThreadID: 0}, ParseMode: "Markdown"}, + wantErr: false, + }, + { + name: "Message to chat with thread", + notification: Notification{Message: "Test message"}, + dest: Destination{Recipient: "-123456|1"}, + want: &tgbotapi.MessageConfig{Text: "Test message", BaseChat: tgbotapi.BaseChat{ChatConfig: tgbotapi.ChatConfig{ChatID: -123456}, MessageThreadID: 1}, ParseMode: "Markdown"}, + wantErr: false, + }, + { + name: "Message to channel", + notification: Notification{Message: "Test message"}, + dest: Destination{Recipient: "123456"}, + want: &tgbotapi.MessageConfig{Text: "Test message", BaseChat: tgbotapi.BaseChat{ChatConfig: tgbotapi.ChatConfig{ChannelUsername: "@123456"}}, ParseMode: "Markdown"}, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := buildTelegramMessageOptions(tt.notification, tt.dest) + if (err != nil) != tt.wantErr { + t.Errorf("buildTelegramMessageOptions() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("buildTelegramMessageOptions() = %v, want %v", got, tt.want) + } + println(got.Text) + }) + } +}