mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-05 21:15:06 -06:00
138 lines
3.2 KiB
Go
138 lines
3.2 KiB
Go
package gomeassistant
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/Xevion/go-ha/internal"
|
|
"github.com/Xevion/go-ha/types"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type MockState struct {
|
|
EqualsReturn bool
|
|
EqualsError bool
|
|
GetReturn EntityState
|
|
GetError bool
|
|
}
|
|
|
|
func (s MockState) AfterSunrise(_ ...types.DurationString) bool {
|
|
return true
|
|
}
|
|
func (s MockState) BeforeSunrise(_ ...types.DurationString) bool {
|
|
return true
|
|
}
|
|
func (s MockState) AfterSunset(_ ...types.DurationString) bool {
|
|
return true
|
|
}
|
|
func (s MockState) BeforeSunset(_ ...types.DurationString) bool {
|
|
return true
|
|
}
|
|
func (s MockState) Get(eid string) (EntityState, error) {
|
|
if s.GetError {
|
|
return EntityState{}, errors.New("some error")
|
|
}
|
|
return s.GetReturn, nil
|
|
}
|
|
func (s MockState) ListEntities() ([]EntityState, error) {
|
|
return []EntityState{}, nil
|
|
}
|
|
func (s MockState) Equals(eid, state string) (bool, error) {
|
|
if s.EqualsError {
|
|
return false, errors.New("some error")
|
|
}
|
|
return s.EqualsReturn, nil
|
|
}
|
|
|
|
var runOnError = internal.EnabledDisabledInfo{
|
|
Entity: "eid",
|
|
State: "state",
|
|
RunOnError: true,
|
|
}
|
|
|
|
var dontRunOnError = internal.EnabledDisabledInfo{
|
|
Entity: "eid",
|
|
State: "state",
|
|
RunOnError: false,
|
|
}
|
|
|
|
func list(infos ...internal.EnabledDisabledInfo) []internal.EnabledDisabledInfo {
|
|
ret := []internal.EnabledDisabledInfo{}
|
|
ret = append(ret, infos...)
|
|
return ret
|
|
}
|
|
|
|
func TestEnabledEntity_StateEqual_Passes(t *testing.T) {
|
|
state := MockState{
|
|
EqualsReturn: true,
|
|
}
|
|
c := CheckEnabledEntity(state, list(runOnError))
|
|
assert.False(t, c.fail, "should pass")
|
|
}
|
|
|
|
func TestEnabledEntity_StateNotEqual_Fails(t *testing.T) {
|
|
state := MockState{
|
|
EqualsReturn: false,
|
|
}
|
|
c := CheckEnabledEntity(state, list(runOnError))
|
|
assert.True(t, c.fail, "should fail")
|
|
}
|
|
|
|
func TestEnabledEntity_NetworkError_DontRun_Fails(t *testing.T) {
|
|
state := MockState{
|
|
EqualsError: true,
|
|
}
|
|
c := CheckEnabledEntity(state, list(dontRunOnError))
|
|
assert.True(t, c.fail, "should fail")
|
|
}
|
|
|
|
func TestEnabledEntity_NetworkError_StillRun_Passes(t *testing.T) {
|
|
state := MockState{
|
|
EqualsError: true,
|
|
}
|
|
c := CheckEnabledEntity(state, list(runOnError))
|
|
assert.False(t, c.fail, "should fail")
|
|
}
|
|
|
|
func TestDisabledEntity_StateEqual_Fails(t *testing.T) {
|
|
state := MockState{
|
|
EqualsReturn: true,
|
|
}
|
|
c := CheckDisabledEntity(state, list(runOnError))
|
|
assert.True(t, c.fail, "should pass")
|
|
}
|
|
|
|
func TestDisabledEntity_StateNotEqual_Passes(t *testing.T) {
|
|
state := MockState{
|
|
EqualsReturn: false,
|
|
}
|
|
c := CheckDisabledEntity(state, list(runOnError))
|
|
assert.False(t, c.fail, "should fail")
|
|
}
|
|
|
|
func TestDisabledEntity_NetworkError_DontRun_Fails(t *testing.T) {
|
|
state := MockState{
|
|
EqualsError: true,
|
|
}
|
|
c := CheckDisabledEntity(state, list(dontRunOnError))
|
|
assert.True(t, c.fail, "should fail")
|
|
}
|
|
|
|
func TestDisabledEntity_NetworkError_StillRun_Passes(t *testing.T) {
|
|
state := MockState{
|
|
EqualsError: true,
|
|
}
|
|
c := CheckDisabledEntity(state, list(runOnError))
|
|
assert.False(t, c.fail, "should fail")
|
|
}
|
|
|
|
func TestStatesMatch(t *testing.T) {
|
|
c := CheckStatesMatch("hey", "hey")
|
|
assert.False(t, c.fail, "should pass")
|
|
}
|
|
|
|
func TestStatesDontMatch(t *testing.T) {
|
|
c := CheckStatesMatch("hey", "bye")
|
|
assert.True(t, c.fail, "should fail")
|
|
}
|