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

219 lines
6.8 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"
2024-03-04 20:11:34 +00:00
"git.sr.ht/~michalr/go-satel"
2024-02-13 22:20:15 +00:00
"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 FilterByTypeOrIndex(testEvents, []satel.ChangeType{satel.ArmedPartition, satel.PartitionFireAlarm}, []int{}) {
2024-02-13 22:20:15 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.DoorOpened, 2, true)
testEvents <- makeTestSatelEvent(satel.PartitionAlarm, 3, true)
testEvents <- makeTestSatelEvent(satel.PartitionFireAlarm, 4, true)
testEvents <- makeTestSatelEvent(satel.TroublePart1, 5, true)
testEvents <- makeTestSatelEvent(satel.ZoneTamper, 6, true)
2024-02-13 22:20:15 +00:00
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 2)
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.PartitionFireAlarm, 4, true))
2024-02-13 22:20:15 +00:00
}
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 FilterByTypeOrIndex(testEvents, []satel.ChangeType{}, []int{}) {
2024-02-18 17:44:08 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
for index, ct := range SUPPORTED_CHANGE_TYPES {
testEvents <- makeTestSatelEvent(ct, index, true)
2024-02-18 17:44:08 +00:00
}
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, len(SUPPORTED_CHANGE_TYPES))
for index, ct := range SUPPORTED_CHANGE_TYPES {
assert.Contains(t, receivedEvents, makeTestSatelEvent(ct, index, true))
2024-02-18 17:44:08 +00:00
}
}
2024-02-18 18:00:49 +00:00
func TestSatelIndexFiltering(t *testing.T) {
testEvents := make(chan satel.Event)
receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{}
go func() {
wg.Add(1)
for e := range FilterByTypeOrIndex(testEvents, []satel.ChangeType{}, []int{1, 3}) {
2024-02-18 18:00:49 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.DoorOpened, 2, true)
testEvents <- makeTestSatelEvent(satel.PartitionAlarm, 3, true)
testEvents <- makeTestSatelEvent(satel.PartitionFireAlarm, 4, true)
testEvents <- makeTestSatelEvent(satel.TroublePart1, 5, true)
testEvents <- makeTestSatelEvent(satel.ZoneTamper, 6, true)
2024-02-18 18:00:49 +00:00
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 2)
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.PartitionAlarm, 3, true))
2024-02-18 18:00:49 +00:00
}
func TestSatelIndexFiltering_NoAllowedEventTypesMeansAllAreAllowed(t *testing.T) {
testEvents := make(chan satel.Event)
receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{}
myReasonableMaxIndex := 100 // I wanted to use math.MaxInt at first, but it's kind of a waste of time here
go func() {
wg.Add(1)
for e := range FilterByTypeOrIndex(testEvents, []satel.ChangeType{}, []int{}) {
2024-02-18 18:00:49 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
for i := 0; i < myReasonableMaxIndex; i++ {
testEvents <- makeTestSatelEvent(satel.ArmedPartition, i, true)
2024-02-18 18:00:49 +00:00
}
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, myReasonableMaxIndex)
for i := 0; i < myReasonableMaxIndex; i++ {
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, i, true))
2024-02-18 18:00:49 +00:00
}
}
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{}
fakeLog := log.New(io.Discard, "", log.Ltime)
ds := MakeDataStore(fakeLog, tempFileName)
2024-02-14 20:34:16 +00:00
go func() {
wg.Add(1)
for e := range FilterByLastSeen(testEvents, &ds, fakeLog) {
2024-02-14 20:34:16 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
2024-02-14 20:34:16 +00:00
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 3)
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 2, true))
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, false))
2024-02-14 20:34:16 +00:00
}
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{}
fakeLog := log.New(io.Discard, "", log.Ltime)
ds := MakeDataStore(fakeLog, tempFileName)
2024-02-15 19:06:06 +00:00
go func() {
wg.Add(1)
for e := range FilterByLastSeen(testEvents, &ds, fakeLog) {
2024-02-15 19:06:06 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
2024-02-15 19:06:06 +00:00
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 3)
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 2, true))
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, false))
2024-02-15 19:06:06 +00:00
testEvents = make(chan satel.Event)
receivedEvents = make([]satel.Event, 0)
ds = MakeDataStore(fakeLog, tempFileName)
2024-02-15 19:06:06 +00:00
go func() {
wg.Add(1)
for e := range FilterByLastSeen(testEvents, &ds, fakeLog) {
2024-02-15 19:06:06 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
receivedEvents = make([]satel.Event, 0)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
2024-02-15 19:06:06 +00:00
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 1)
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
2024-02-15 19:06:06 +00:00
}