From c42a651055988b6dd47672c806c566989b7ad0bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Mon, 13 Jan 2025 23:30:34 +0100 Subject: [PATCH] Initial test, added CommandHandler to main --- command_handler_test.go | 39 +++++++++++++++++++++++++++++++++++++++ main.go | 7 +++++++ test_utils.go | 8 ++++---- 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 command_handler_test.go diff --git a/command_handler_test.go b/command_handler_test.go new file mode 100644 index 0000000..53b0f7a --- /dev/null +++ b/command_handler_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "html/template" + "io" + "log" + "os" + "sync" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCommandHandler_DoesNothing(t *testing.T) { + f, err := os.CreateTemp("", "TestSatelLastSeenFiltering") + assert.NoError(t, err) + tempFileName := f.Name() + assert.NoError(t, f.Close()) + defer os.Remove(f.Name()) + + fakeLog := log.New(io.Discard, "", log.Ltime) + ds := MakeDataStore(fakeLog, tempFileName) + wg := sync.WaitGroup{} + + mockBot := MakeMockTgBotAPI() + mockSatel := MockSatelNameGetter{"test"} + tpl := template.Must(template.New("IRCMessage").Parse(IRCMessageTemplate)) + config := AppConfig{} + config.AllowedIndexes = []int{} + config.AllowedTypes = []SatelChangeType{} + + wg.Add(1) + go func() { + HandleTelegramCommands(mockBot, fakeLog, &ds, config, tpl, &mockSatel) + wg.Done() + }() + mockBot.StopReceivingUpdates() + wg.Wait() +} diff --git a/main.go b/main.go index 0ac8d7f..c5e0a34 100644 --- a/main.go +++ b/main.go @@ -106,6 +106,13 @@ func main() { collect.Collect(s.Events, &wg, func() {}) + wg.Add(1) + go func() { + HandleTelegramCommands(bot, log.New(os.Stderr, "CommandHandler", log.Lmicroseconds), + &dataStore, config, tgTpl, s) + wg.Done() + }() + go CloseSatelOnCtrlC(s, &cleanShutdown) closeDebugTools := make(chan interface{}) diff --git a/test_utils.go b/test_utils.go index 9cfb89b..77d7b86 100644 --- a/test_utils.go +++ b/test_utils.go @@ -20,16 +20,16 @@ type MockTgBotAPI struct { updates chan tgbotapi.Update } +func MakeMockTgBotAPI() *MockTgBotAPI { + return &MockTgBotAPI{messages: []tgbotapi.Chattable{}, updates: make(chan tgbotapi.Update)} +} + func (botMock *MockTgBotAPI) Send(c tgbotapi.Chattable) (tgbotapi.Message, error) { botMock.messages = append(botMock.messages, c) 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 }