You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

package m9zApi
import (
"testing"
"time"
"tgk-touch/internal/library/m9z"
)
func TestApplySolarTaskTime(t *testing.T) {
sunrise := time.Date(2026, 8, 21, 5, 32, 0, 0, time.Local)
sunset := time.Date(2026, 8, 21, 18, 41, 0, 0, time.Local)
sunTimes := &m9z.SunTimes{Sunrise: sunrise, Sunset: sunset}
tests := []struct {
name string
apply func(*m9z.ControlTask, *m9z.SunTimes)
want time.Time
}{
{name: "start task uses sunset", apply: applyStartTaskSolarTime, want: sunset},
{name: "stop task uses sunrise", apply: applyStopTaskSolarTime, want: sunrise},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
task := &m9z.ControlTask{TimeType: 1}
tt.apply(task, sunTimes)
if !task.ExecTime.Equal(tt.want) {
t.Fatalf("ExecTime = %v, want %v", task.ExecTime, tt.want)
}
})
}
}
func TestApplySolarTaskTimeKeepsScheduledTime(t *testing.T) {
scheduled := time.Date(2026, 8, 21, 20, 0, 0, 0, time.Local)
task := &m9z.ControlTask{TimeType: 0, ExecTime: scheduled}
sunTimes := &m9z.SunTimes{
Sunrise: scheduled.Add(-14 * time.Hour),
Sunset: scheduled.Add(-time.Hour),
}
applyStartTaskSolarTime(task, sunTimes)
applyStopTaskSolarTime(task, sunTimes)
if !task.ExecTime.Equal(scheduled) {
t.Fatalf("ExecTime = %v, want scheduled time %v", task.ExecTime, scheduled)
}
}