1
0
Fork 0

Preparation for command tests

This commit is contained in:
Michał Rudowicz 2025-01-13 20:37:50 +01:00
parent e385a72841
commit d42be83709
4 changed files with 33 additions and 9 deletions

View File

@ -13,7 +13,7 @@ func getCurrentStatus(dataStore *DataStore, logger *log.Logger, config AppConfig
return msg.Format(tpl, s, logger) return msg.Format(tpl, s, logger)
} }
func HandleTelegramCommands(bot tgbotapi.BotAPI, logger *log.Logger, dataStore *DataStore, config AppConfig, tpl *template.Template, s SatelNameGetter) { func HandleTelegramCommands(bot TelegramBot, logger *log.Logger, dataStore *DataStore, config AppConfig, tpl *template.Template, s SatelNameGetter) {
u := tgbotapi.NewUpdate(0) u := tgbotapi.NewUpdate(0)
u.Timeout = 60 u.Timeout = 60
@ -42,4 +42,6 @@ func HandleTelegramCommands(bot tgbotapi.BotAPI, logger *log.Logger, dataStore *
logger.Print(err) logger.Print(err)
} }
} }
logger.Println("Stopping command handler")
} }

View File

@ -61,7 +61,7 @@ func main() {
s := makeSatel(config.SatelAddr, config.PoolInterval.GetDuration()) s := makeSatel(config.SatelAddr, config.PoolInterval.GetDuration())
logger.Printf("Connected to Satel: %s", config.SatelAddr) logger.Printf("Connected to Satel: %s", config.SatelAddr)
var bot TelegramBotSender = nil var bot TelegramBot = nil
if len(config.TelegramApiKey) != 0 { if len(config.TelegramApiKey) != 0 {
b, err := tgbotapi.NewBotAPI(config.TelegramApiKey) b, err := tgbotapi.NewBotAPI(config.TelegramApiKey)
if err != nil { if err != nil {
@ -70,7 +70,7 @@ func main() {
logger.Print("Created Telegram Bot API client") logger.Print("Created Telegram Bot API client")
bot = b bot = b
} else { } else {
bot = EmptySender{} bot = EmptyBot{}
} }
tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), config.ChatIds} tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), config.ChatIds}
@ -114,6 +114,7 @@ func main() {
} }
logger.Print("Closing...") logger.Print("Closing...")
bot.StopReceivingUpdates()
close(closeDebugTools) close(closeDebugTools)
close(tgEvents) close(tgEvents)
wg.Wait() wg.Wait()

View File

@ -7,18 +7,28 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
type TelegramBotSender interface { type TelegramBot interface {
Send(c tgbotapi.Chattable) (tgbotapi.Message, error) Send(c tgbotapi.Chattable) (tgbotapi.Message, error)
GetUpdatesChan(u tgbotapi.UpdateConfig) tgbotapi.UpdatesChannel
StopReceivingUpdates()
} }
type EmptySender struct{} type EmptyBot struct{}
func (self EmptySender) Send(_ tgbotapi.Chattable) (tgbotapi.Message, error) { func (_ EmptyBot) Send(_ tgbotapi.Chattable) (tgbotapi.Message, error) {
return tgbotapi.Message{}, nil return tgbotapi.Message{}, nil
} }
func (_ EmptyBot) GetUpdatesChan(u tgbotapi.UpdateConfig) tgbotapi.UpdatesChannel {
ch := make(chan tgbotapi.Update)
close(ch)
return ch
}
func (_ EmptyBot) StopReceivingUpdates() {}
type TgSender struct { type TgSender struct {
bot TelegramBotSender bot TelegramBot
s SatelNameGetter s SatelNameGetter
logger *log.Logger logger *log.Logger
chatIds []int64 chatIds []int64

View File

@ -17,13 +17,24 @@ func makeGenericMessage(changeType satel.ChangeType, index int, val bool) Generi
type MockTgBotAPI struct { type MockTgBotAPI struct {
messages []tgbotapi.Chattable messages []tgbotapi.Chattable
updates chan tgbotapi.Update
} }
func (self *MockTgBotAPI) Send(c tgbotapi.Chattable) (tgbotapi.Message, error) { func (botMock *MockTgBotAPI) Send(c tgbotapi.Chattable) (tgbotapi.Message, error) {
self.messages = append(self.messages, c) botMock.messages = append(botMock.messages, c)
return tgbotapi.Message{}, nil return tgbotapi.Message{}, nil
} }
func (botMock *MockTgBotAPI) GetUpdatesChan(u tgbotapi.UpdateConfig) tgbotapi.UpdatesChannel {
if botMock.updates != nil {
panic("Called mock GetUpdatesChan more than once")
}
botMock.updates = make(chan tgbotapi.Update)
return botMock.updates
}
func (botMock *MockTgBotAPI) StopReceivingUpdates() { close(botMock.updates) }
type MockSatelNameGetter struct { type MockSatelNameGetter struct {
name string name string
} }