root moves fixed future schedules to shceduled on automove

master
Bel LaPointe 2022-03-24 14:43:45 -06:00
parent d7dab75f48
commit 55e174e3b1
3 changed files with 101 additions and 7 deletions

View File

@ -22,18 +22,19 @@ func (root Root) Equals(other Root) bool {
func (root *Root) AutoMove() {
root.MoveScheduledToTodo()
//root.MoveTodoToScheduled()
root.MoveTodoToScheduled()
}
func (root *Root) MoveTodoToScheduled() {
for i := len(root.Todo) - 1; i >= 0; i-- {
if root.Todo[i].Schedule != "" && !root.Todo[i].Triggered() {
root.Scheduled = append(root.Scheduled, root.Todo[i])
for j := i; j < len(root.Todo)-1; j++ {
root.Todo[j] = root.Todo[j+1]
}
root.Todo = root.Todo[:len(root.Todo)-1]
if !root.Todo[i].Schedule.isFixedFuture() {
continue
}
root.Scheduled = append(root.Scheduled, root.Todo[i])
for j := i; j < len(root.Todo)-1; j++ {
root.Todo[j] = root.Todo[j+1]
}
root.Todo = root.Todo[:len(root.Todo)-1]
}
}

View File

@ -25,6 +25,42 @@ func (schedule Schedule) Next(t time.Time) (time.Time, error) {
return scheduler.next(t)
}
func (schedule Schedule) isFixedFuture() bool {
if !schedule.isFixed() {
return false
}
return schedule.isFuture()
}
func (schedule Schedule) isFixed() bool {
if schedule.empty() {
return false
}
scheduler := schedulerFactory(string(schedule))
switch scheduler.(type) {
case scheduleEZDate, scheduleDue:
default:
return false
}
return true
}
func (schedule Schedule) isFuture() bool {
if schedule.empty() {
return false
}
scheduler := schedulerFactory(string(schedule))
t, err := scheduler.next(time.Now())
if err != nil {
return false
}
return time.Now().Before(t)
}
func (schedule Schedule) empty() bool {
return string(schedule) == ""
}
type scheduler interface {
next(time.Time) (time.Time, error)
}

View File

@ -130,3 +130,60 @@ func TestSchedulerFactory(t *testing.T) {
}
})
}
func TestScheduleIsFixedFuture(t *testing.T) {
cases := map[string]struct {
input string
want bool
}{
"empty": {
input: "",
want: false,
},
"cron": {
input: "* * * * *",
want: false,
},
"duration": {
input: "1m",
want: false,
},
"due past": {
input: "123",
want: false,
},
"due future": {
input: "9648154541",
want: true,
},
"ez date short past": {
input: "2000-01-02",
want: false,
},
"ez date short future": {
input: "2100-01-02",
want: true,
},
"ez date long past": {
input: "2000-01-02T01",
want: false,
},
"ez date long future": {
input: "2100-01-02T02",
want: true,
},
}
for name, d := range cases {
c := d
t.Run(name, func(t *testing.T) {
schedule := Schedule(c.input)
got := schedule.isFixedFuture()
if got != c.want {
gotFuture := schedule.isFuture()
gotFixed := schedule.isFixed()
t.Errorf("want %v, got %v = %v && %v", c.want, got, gotFuture, gotFixed)
}
})
}
}