implement some features of Venus OS
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
79
mk2driver/managed_writer_test.go
Normal file
79
mk2driver/managed_writer_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user