All checks were successful
continuous-integration/drone/push Build is passing
98 lines
2.4 KiB
Go
98 lines
2.4 KiB
Go
package mk2driver
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type writerStub struct {
|
|
settingWrites int
|
|
ramWrites int
|
|
panelWrites int
|
|
standbyWrites int
|
|
}
|
|
|
|
func (w *writerStub) WriteRAMVar(id uint16, value int16) error {
|
|
w.ramWrites++
|
|
return nil
|
|
}
|
|
|
|
func (w *writerStub) WriteSetting(id uint16, value int16) error {
|
|
w.settingWrites++
|
|
return nil
|
|
}
|
|
|
|
func (w *writerStub) SetPanelState(switchState PanelSwitchState, currentLimitA *float64) error {
|
|
w.panelWrites++
|
|
return nil
|
|
}
|
|
|
|
func (w *writerStub) SetStandby(enabled bool) error {
|
|
w.standbyWrites++
|
|
return nil
|
|
}
|
|
|
|
func TestManagedWriterReadOnlyProfile(t *testing.T) {
|
|
base := &writerStub{}
|
|
managed := NewManagedWriter(base, WriterPolicy{
|
|
Profile: WriterProfileReadOnly,
|
|
})
|
|
|
|
err := managed.WriteSettingWithSource(CommandSourceMQTT, 1, 1)
|
|
assert.Error(t, err)
|
|
assert.Equal(t, 0, base.settingWrites)
|
|
history := managed.History(10)
|
|
if assert.Len(t, history, 1) {
|
|
assert.False(t, history[0].Allowed)
|
|
assert.Equal(t, CommandSourceMQTT, history[0].Source)
|
|
}
|
|
}
|
|
|
|
func TestManagedWriterCurrentLimitGuard(t *testing.T) {
|
|
base := &writerStub{}
|
|
max := 16.0
|
|
managed := NewManagedWriter(base, WriterPolicy{
|
|
Profile: WriterProfileNormal,
|
|
MaxCurrentLimitA: &max,
|
|
})
|
|
|
|
limit := 20.0
|
|
err := managed.SetPanelStateWithSource(CommandSourceUI, PanelSwitchOn, &limit)
|
|
assert.Error(t, err)
|
|
assert.Equal(t, 0, base.panelWrites)
|
|
}
|
|
|
|
func TestManagedWriterModeRateLimit(t *testing.T) {
|
|
base := &writerStub{}
|
|
managed := NewManagedWriter(base, WriterPolicy{
|
|
Profile: WriterProfileNormal,
|
|
ModeChangeMinInterval: 10 * time.Second,
|
|
})
|
|
|
|
err := managed.SetPanelStateWithSource(CommandSourceAutomation, PanelSwitchOn, nil)
|
|
assert.NoError(t, err)
|
|
err = managed.SetPanelStateWithSource(CommandSourceAutomation, PanelSwitchOff, nil)
|
|
assert.Error(t, err)
|
|
assert.Equal(t, 1, base.panelWrites)
|
|
}
|
|
|
|
func TestManagedWriterPanelModeAllowlist(t *testing.T) {
|
|
base := &writerStub{}
|
|
managed := NewManagedWriter(base, WriterPolicy{
|
|
Profile: WriterProfileNormal,
|
|
AllowedPanelStates: map[PanelSwitchState]struct{}{
|
|
PanelSwitchOff: {},
|
|
},
|
|
})
|
|
|
|
err := managed.SetPanelStateWithSource(CommandSourceMQTT, PanelSwitchOn, nil)
|
|
assert.Error(t, err)
|
|
assert.Equal(t, 0, base.panelWrites)
|
|
|
|
err = managed.SetPanelStateWithSource(CommandSourceMQTT, PanelSwitchOff, nil)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, base.panelWrites)
|
|
}
|