Files
gronod 512b0a11e6 Fix schedule command result parsing
- Added AddSched, ModSched, and DelSched to knownTD string set so schedule CRUD results do not emit unparsed diagnostic errors.
2026-09-24 14:01:02 +01:00

167 lines
4.3 KiB
Go

package robot_test
import (
"testing"
"time"
"git.i3omb.com/gronod/ha-n95-local-control/internal/ctl"
"git.i3omb.com/gronod/ha-n95-local-control/internal/robot"
)
func init() {
robot.ParseSchedules = robot.ScheduleParserHook
robot.ParseLifespan = robot.LifespanParserHook
}
func TestSchedulesSundayFirstAndReplace(t *testing.T) {
if got := robot.RepeatIndex(time.Sunday); got != 0 {
t.Errorf("Sunday = %d, want 0", got)
}
if got := robot.RepeatIndex(time.Wednesday); got != 3 {
t.Errorf("Wednesday = %d, want 3", got)
}
snap := robot.NewSnapshot()
in1 := ctl.Inbound{
TD: "Sched2",
Inner: []byte(`
<s n="1" o="1" t="10:00" r="0001000" f="p"><ctl td="Clean" type="auto"/></s>
`),
}
robot.Apply(&snap, in1, "")
if len(snap.Attributes.Schedules) != 1 {
t.Fatalf("len = %d, want 1", len(snap.Attributes.Schedules))
}
in2 := ctl.Inbound{
TD: "Sched2",
Inner: []byte(`
<s n="2" o="0" t="11:00" r="1111111" f="p"><ctl td="Clean" type="auto"/></s>
<s n="3" o="1" t="12:00" r="0000000" f="p"><ctl td="Clean" type="auto"/></s>
`),
}
robot.Apply(&snap, in2, "")
if len(snap.Attributes.Schedules) != 2 {
t.Fatalf("len = %d, want 2", len(snap.Attributes.Schedules))
}
if snap.Attributes.Schedules[0].Name != "2" || snap.Attributes.Schedules[1].Name != "3" {
t.Errorf("schedules not replaced entirely")
}
in3 := ctl.Inbound{
TD: "GetSched",
}
robot.Apply(&snap, in3, "")
if len(snap.Attributes.Schedules) != 0 {
t.Fatalf("len = %d, want 0", len(snap.Attributes.Schedules))
}
}
func TestEmptySchedShapes(t *testing.T) {
snap := robot.NewSnapshot()
in1 := ctl.Inbound{
TD: "Sched2",
}
robot.Apply(&snap, in1, "")
if snap.Attributes.Schedules == nil || len(snap.Attributes.Schedules) != 0 {
t.Errorf("Sched2 without inner should be []")
}
in2 := ctl.Inbound{
TD: "GetSched",
Inner: []byte{},
}
robot.Apply(&snap, in2, "")
if snap.Attributes.Schedules == nil || len(snap.Attributes.Schedules) != 0 {
t.Errorf("GetSched without inner should be []")
}
}
func TestScheduleWhitespaceIgnored(t *testing.T) {
snap := robot.NewSnapshot()
in := ctl.Inbound{
TD: "Sched2",
Inner: []byte(`<s n="1" o="1" t="10:00" r="0001000" f="p"> <ctl td="clean" type="auto"/> </s>`),
}
robot.Apply(&snap, in, "")
if len(snap.Attributes.Schedules) != 1 {
t.Fatalf("len = %d, want 1", len(snap.Attributes.Schedules))
}
s := snap.Attributes.Schedules[0]
if s.Flag != "p" || s.Action.Type != "auto" {
t.Errorf("bad parse: %+v", s)
}
}
func TestScheduleRejectsNonAuto(t *testing.T) {
snap := robot.NewSnapshot()
in := ctl.Inbound{
TD: "Sched2",
Inner: []byte(`
<s n="1" o="1" t="10:00" r="0001000" f="p"><ctl td="Clean" type="border"/></s>
<s n="2" o="1" t="11:00" r="0001000" f="p"><ctl td="Clean" type="auto"/></s>
`),
}
err := robot.Apply(&snap, in, "")
if err == nil {
t.Errorf("expected error for non-auto schedule")
}
if len(snap.Attributes.Schedules) != 1 {
t.Fatalf("len = %d, want 1", len(snap.Attributes.Schedules))
}
if snap.Attributes.Schedules[0].Name != "2" {
t.Errorf("wrong schedule kept: %s", snap.Attributes.Schedules[0].Name)
}
}
func TestLifespanUnknownUnit(t *testing.T) {
snap := robot.NewSnapshot()
in1 := ctl.Inbound{
TD: "GetLifeSpan",
Attrs: map[string]string{"type": "SideBrush", "val": "068", "total": "365"},
}
robot.Apply(&snap, in1, "")
in2 := ctl.Inbound{
TD: "GetLifeSpan",
Attrs: map[string]string{"type": "Brush", "val": "90", "total": "365"},
}
robot.Apply(&snap, in2, "")
in3 := ctl.Inbound{
TD: "GetLifeSpan",
Attrs: map[string]string{"type": "DustCaseHeap", "val": "70", "total": "365"},
}
robot.Apply(&snap, in3, "")
a := snap.Attributes
if a.SideBrush == nil || *a.SideBrush != 68 {
t.Errorf("SideBrush = %v", a.SideBrush)
}
if a.LifespanTotal.SideBrush == nil || *a.LifespanTotal.SideBrush != 365 {
t.Errorf("SideBrush total = %v", a.LifespanTotal.SideBrush)
}
if a.MainBrush == nil || *a.MainBrush != 90 {
t.Errorf("MainBrush = %v", a.MainBrush)
}
if a.Filter == nil || *a.Filter != 70 {
t.Errorf("Filter = %v", a.Filter)
}
}
func TestScheduleCommandResultsAccepted(t *testing.T) {
snap := robot.NewSnapshot()
for _, td := range []string{"AddSched", "ModSched", "DelSched"} {
in := ctl.Inbound{
TD: td,
Ret: "ok",
}
if err := robot.Apply(&snap, in, ""); err != nil {
t.Errorf("Apply failed for %s result: %v", td, err)
}
}
}