diff --git a/garden-app/pkg/action/garden_action_test.go b/garden-app/pkg/action/garden_action_test.go index 209e6d0d..8b31b261 100644 --- a/garden-app/pkg/action/garden_action_test.go +++ b/garden-app/pkg/action/garden_action_test.go @@ -28,6 +28,11 @@ func TestGardenActionBind(t *testing.T) { &GardenAction{}, "missing required action fields", }, + { + "ErrorMissingUpdateConfig", + &GardenAction{Update: &UpdateAction{}}, + "update action must have config=true", + }, } t.Run("SuccessfulLightAction", func(t *testing.T) { diff --git a/garden-app/pkg/controller_config.go b/garden-app/pkg/controller_config.go index 9ae68864..5d81751d 100644 --- a/garden-app/pkg/controller_config.go +++ b/garden-app/pkg/controller_config.go @@ -49,11 +49,13 @@ func (c *ControllerConfig) ToMessage() ControllerConfigMessage { if c.TemperatureHumidityPin != nil { message.TemperatureHumidityEnabled = true - message.TemperatureHumidityPin = *c.LightPin + message.TemperatureHumidityPin = *c.TemperatureHumidityPin if c.TemperatureHumidityInterval != nil { //nolint:gosec message.TemperatureHumidityInterval = uint(c.TemperatureHumidityInterval.Duration.Milliseconds()) + } else { + message.TemperatureHumidityInterval = 5000 } } diff --git a/garden-app/pkg/controller_config_test.go b/garden-app/pkg/controller_config_test.go index c9698f58..a06266f9 100644 --- a/garden-app/pkg/controller_config_test.go +++ b/garden-app/pkg/controller_config_test.go @@ -3,6 +3,7 @@ package pkg import ( "errors" "testing" + "time" "github.com/calvinmclean/babyapi" "github.com/stretchr/testify/assert" @@ -26,6 +27,10 @@ func TestControllerConfigPatch(t *testing.T) { "TemperatureHumidityPin", &ControllerConfig{TemperatureHumidityPin: pointer(uint(1))}, }, + { + "TemperatureHumidityInterval", + &ControllerConfig{TemperatureHumidityInterval: &Duration{Duration: 1 * time.Second}}, + }, { "ValvePinsEmpty", &ControllerConfig{ValvePins: []uint{}}, @@ -78,3 +83,90 @@ func TestControllerConfigPatch(t *testing.T) { assert.ElementsMatch(t, []uint{5}, c.PumpPins) }) } + +func TestToMessage(t *testing.T) { + tests := []struct { + name string + config *ControllerConfig + expected ControllerConfigMessage + }{ + { + "FullConfig", + &ControllerConfig{ + ValvePins: []uint{1}, + PumpPins: []uint{1}, + LightPin: pointer(uint(1)), + TemperatureHumidityPin: pointer(uint(1)), + TemperatureHumidityInterval: &Duration{Duration: time.Second}, + }, + ControllerConfigMessage{ + NumZones: 1, + ValvePins: []uint{1}, + PumpPins: []uint{1}, + LightEnabled: true, + LightPin: uint(1), + TemperatureHumidityEnabled: true, + TemperatureHumidityPin: uint(1), + TemperatureHumidityInterval: 1000, + }, + }, + { + "DefaultTemperatureHumidityInterval", + &ControllerConfig{ + ValvePins: []uint{1}, + PumpPins: []uint{1}, + LightPin: pointer(uint(1)), + TemperatureHumidityPin: pointer(uint(1)), + }, + ControllerConfigMessage{ + NumZones: 1, + ValvePins: []uint{1}, + PumpPins: []uint{1}, + LightEnabled: true, + LightPin: uint(1), + TemperatureHumidityEnabled: true, + TemperatureHumidityPin: uint(1), + TemperatureHumidityInterval: 5000, + }, + }, + { + "NoLightPin", + &ControllerConfig{ + ValvePins: []uint{1}, + PumpPins: []uint{1}, + TemperatureHumidityPin: pointer(uint(1)), + TemperatureHumidityInterval: &Duration{Duration: time.Second}, + }, + ControllerConfigMessage{ + NumZones: 1, + ValvePins: []uint{1}, + PumpPins: []uint{1}, + LightEnabled: false, + TemperatureHumidityEnabled: true, + TemperatureHumidityPin: uint(1), + TemperatureHumidityInterval: 1000, + }, + }, + { + "NoTemperatureHumidityPin", + &ControllerConfig{ + ValvePins: []uint{1}, + PumpPins: []uint{1}, + }, + ControllerConfigMessage{ + NumZones: 1, + ValvePins: []uint{1}, + PumpPins: []uint{1}, + LightEnabled: false, + TemperatureHumidityEnabled: false, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + out := tt.config.ToMessage() + require.Equal(t, tt.expected, out) + }) + } +}