package main import ( "html/template" "io" "log" "sync" "testing" "git.sr.ht/~michalr/go-satel" "github.com/stretchr/testify/assert" ) type MockTemplateSender struct { message string s SatelNameGetter } func (self *MockTemplateSender) Send(msg GenericMessage, tpl *template.Template) error { self.message = msg.Format(tpl, self.s, log.New(io.Discard, "", log.Ltime)) return nil } type MockSatelNameGetter struct { name string } func (self MockSatelNameGetter) GetName(devType satel.DeviceType, index byte) (*satel.NameEvent, error) { retval := satel.NameEvent{ DevType: devType, DevNumber: index, DevTypeFunction: 0, DevName: self.name, } return &retval, nil } var ( tplMessageTest1 = satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true} tplMessageTest2 = satel.Event{Type: satel.ZoneViolation, Index: 2, Value: true} ) func TestTelegramTemplate(t *testing.T) { testEvents := make(chan GenericMessage) wg := sync.WaitGroup{} mockSender := MockTemplateSender{s: MockSatelNameGetter{"mockPart"}} tpl, err := template.New("TestTemplate").Parse(TelegramMessageTemplate) assert.NoError(t, err) Consume(SendToTg(testEvents, &mockSender, &wg, log.New(io.Discard, "", log.Ltime), tpl)) testEvents <- GenericMessage{TgChatId{123}, []MsgContent{{tplMessageTest1}, {tplMessageTest2}}} close(testEvents) wg.Wait() // assert.Equal(t, "siemka", mockSender.message) }