1
0
Fork 0

Initial test, added CommandHandler to main

This commit is contained in:
Michał Rudowicz 2025-01-13 23:30:34 +01:00
parent d42be83709
commit c42a651055
3 changed files with 50 additions and 4 deletions

39
command_handler_test.go Normal file
View File

@ -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()
}

View File

@ -106,6 +106,13 @@ func main() {
collect.Collect(s.Events, &wg, func() {}) 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) go CloseSatelOnCtrlC(s, &cleanShutdown)
closeDebugTools := make(chan interface{}) closeDebugTools := make(chan interface{})

View File

@ -20,16 +20,16 @@ type MockTgBotAPI struct {
updates chan tgbotapi.Update 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) { func (botMock *MockTgBotAPI) Send(c tgbotapi.Chattable) (tgbotapi.Message, error) {
botMock.messages = append(botMock.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 { 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 return botMock.updates
} }