1
0
Fork 0
hswro-alarm-bot/templates_test.go

56 lines
1.4 KiB
Go
Raw Normal View History

2024-02-19 18:52:40 +00:00
package main
import (
"html/template"
"io"
"log"
"sync"
"testing"
2024-03-04 20:11:34 +00:00
"git.sr.ht/~michalr/go-satel"
2024-02-19 18:52:40 +00:00
"github.com/stretchr/testify/assert"
)
type MockTemplateSender struct {
message string
2024-03-04 20:11:34 +00:00
s SatelNameGetter
2024-02-19 18:52:40 +00:00
}
func (self *MockTemplateSender) Send(msg GenericMessage, tpl *template.Template) error {
2024-03-04 20:11:34 +00:00
self.message = msg.Format(tpl, self.s, log.New(io.Discard, "", log.Ltime))
2024-02-19 18:52:40 +00:00
return nil
}
2024-03-04 20:11:34 +00:00
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
}
2024-02-19 18:52:40 +00:00
var (
tplMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
tplMessageTest2 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: true}
2024-02-19 18:52:40 +00:00
)
func TestTelegramTemplate(t *testing.T) {
testEvents := make(chan GenericMessage)
wg := sync.WaitGroup{}
2024-03-04 20:11:34 +00:00
mockSender := MockTemplateSender{s: MockSatelNameGetter{"mockPart"}}
2024-02-19 18:52:40 +00:00
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{[]satel.BasicEventElement{tplMessageTest1, tplMessageTest2}}
2024-02-19 18:52:40 +00:00
close(testEvents)
wg.Wait()
// assert.Equal(t, "siemka", mockSender.message)
}