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

159 lines
5.4 KiB
Go
Raw Normal View History

2024-02-13 22:20:15 +00:00
package main
import (
2024-02-15 19:06:06 +00:00
"io"
"log"
"os"
2024-02-13 22:20:15 +00:00
"sync"
"testing"
"github.com/probakowski/go-satel"
"github.com/stretchr/testify/assert"
)
func TestSatelEventTypeFiltering(t *testing.T) {
testEvents := make(chan satel.Event)
receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{}
go func() {
wg.Add(1)
for e := range FilterByType(testEvents, []satel.ChangeType{satel.ArmedPartition, satel.PartitionFireAlarm}) {
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true}
testEvents <- satel.Event{Type: satel.DoorOpened, Index: 2, Value: true}
testEvents <- satel.Event{Type: satel.PartitionAlarm, Index: 3, Value: true}
testEvents <- satel.Event{Type: satel.PartitionFireAlarm, Index: 4, Value: true}
testEvents <- satel.Event{Type: satel.TroublePart1, Index: 5, Value: true}
testEvents <- satel.Event{Type: satel.ZoneTamper, Index: 6, Value: true}
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 2)
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true})
assert.Contains(t, receivedEvents, satel.Event{Type: satel.PartitionFireAlarm, Index: 4, Value: true})
}
2024-02-14 20:34:16 +00:00
2024-02-18 17:44:08 +00:00
func TestSatelEventTypeFiltering_NoAllowedEventTypesMeansAllAreAllowed(t *testing.T) {
testEvents := make(chan satel.Event)
receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{}
go func() {
wg.Add(1)
for e := range FilterByType(testEvents, []satel.ChangeType{}) {
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
for index, ct := range SUPPORTED_CHANGE_TYPES {
testEvents <- satel.Event{Type: ct, Index: index, Value: true}
}
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, len(SUPPORTED_CHANGE_TYPES))
for index, ct := range SUPPORTED_CHANGE_TYPES {
assert.Contains(t, receivedEvents, satel.Event{Type: ct, Index: index, Value: true})
}
}
2024-02-14 20:34:16 +00:00
func TestSatelLastSeenFiltering(t *testing.T) {
2024-02-15 19:06:06 +00:00
f, err := os.CreateTemp("", "TestSatelLastSeenFiltering")
assert.NoError(t, err)
tempFileName := f.Name()
assert.NoError(t, f.Close())
defer os.Remove(f.Name())
2024-02-14 20:34:16 +00:00
testEvents := make(chan satel.Event)
receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{}
go func() {
wg.Add(1)
2024-02-15 19:06:06 +00:00
for e := range FilterByLastSeen(testEvents, tempFileName, log.New(io.Discard, "", log.Ltime)) {
2024-02-14 20:34:16 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 2, Value: true}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: false}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 2, Value: true}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: false}
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 3)
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true})
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 2, Value: true})
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 1, Value: false})
}
2024-02-15 19:06:06 +00:00
func TestSatelLastSeenFilteringWithPersistence(t *testing.T) {
f, err := os.CreateTemp("", "TestSatelLastSeenFilteringWithPersistence")
assert.NoError(t, err)
tempFileName := f.Name()
assert.NoError(t, f.Close())
defer os.Remove(f.Name())
testEvents := make(chan satel.Event)
receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{}
go func() {
wg.Add(1)
for e := range FilterByLastSeen(testEvents, tempFileName, log.New(io.Discard, "", log.Ltime)) {
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 2, Value: true}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: false}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 2, Value: true}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: false}
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 3)
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true})
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 2, Value: true})
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 1, Value: false})
testEvents = make(chan satel.Event)
receivedEvents = make([]satel.Event, 0)
go func() {
wg.Add(1)
for e := range FilterByLastSeen(testEvents, tempFileName, log.New(io.Discard, "", log.Ltime)) {
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
receivedEvents = make([]satel.Event, 0)
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: false}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: false}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 2, Value: true}
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 2, Value: true}
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 1)
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true})
}