73 lines
2.5 KiB
Go
73 lines
2.5 KiB
Go
package ha_test
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.i3omb.com/gronod/ha-n95-local-control/internal/config"
|
|
"git.i3omb.com/gronod/ha-n95-local-control/internal/ha"
|
|
)
|
|
|
|
func TestSendCommandExtensions(t *testing.T) {
|
|
cfg := config.Config{RawCommands: false}
|
|
hook := ha.SendCommandHook(cfg)
|
|
|
|
tests := []struct {
|
|
payload string
|
|
want string
|
|
err string
|
|
}{
|
|
{`{"command":"clean","clean_type":"border"}`, "clean", ""},
|
|
{`{"command":"move","action":"SpinLeft"}`, "move", ""},
|
|
{`{"command":"cancel_return"}`, "cancel_return", ""},
|
|
{`{"command":"set_time"}`, "set_time", ""},
|
|
{`{"command":"get_status"}`, "get_status", ""},
|
|
{`{"command":"get_lifespan"}`, "get_lifespan", ""},
|
|
{`{"command":"add_sched","name":"123","on":"1","time":"21:30","repeat":"0111000","clean_type":"auto"}`, "add_sched", ""},
|
|
{`{"command":"resume"}`, "", "rejected:resume"},
|
|
{`{"command":"move","action":"backward"}`, "", "rejected:backward"},
|
|
{`{"command":"add_sched","name":"123","on":"1","time":"21:30","repeat":"999","clean_type":"auto"}`, "", "rejected:repeat"},
|
|
{`{"command":"add_sched","name":"123","on":"1","time":"2130","repeat":"0111000","clean_type":"auto"}`, "", "rejected:time"},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
cmd, err := hook(context.Background(), "SER1", []byte(tc.payload))
|
|
if tc.err != "" {
|
|
if err == nil || !strings.Contains(err.Error(), tc.err) {
|
|
t.Errorf("payload %s: want err %s, got %v", tc.payload, tc.err, err)
|
|
}
|
|
} else {
|
|
if err != nil {
|
|
t.Errorf("payload %s: unexpected err: %v", tc.payload, err)
|
|
} else if cmd.Name != tc.want {
|
|
t.Errorf("payload %s: want name %s, got %s", tc.payload, tc.want, cmd.Name)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRawCommandsDefaultOff(t *testing.T) {
|
|
cfgOff := config.Config{RawCommands: false}
|
|
hookOff := ha.SendCommandHook(cfgOff)
|
|
_, err := hookOff(context.Background(), "SER1", []byte(`{"command":"raw","xml":"<ctl id='1'/>"}`))
|
|
if err == nil || err.Error() != "raw-disabled" {
|
|
t.Errorf("expected raw-disabled, got %v", err)
|
|
}
|
|
|
|
cfgOn := config.Config{RawCommands: true}
|
|
hookOn := ha.SendCommandHook(cfgOn)
|
|
cmd, err := hookOn(context.Background(), "SER1", []byte(`{"command":"raw","xml":"<ctl id='1'/>"}`))
|
|
if err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
if cmd.Name != "raw" {
|
|
t.Errorf("expected raw command, got %s", cmd.Name)
|
|
}
|
|
|
|
_, err = hookOn(context.Background(), "SER1", []byte(`{"command":"raw","xml":"<stream/>"}`))
|
|
if err == nil || !strings.Contains(err.Error(), "rejected:") {
|
|
t.Errorf("expected stream root to be rejected, got %v", err)
|
|
}
|
|
}
|