Skip to content

Commit

Permalink
Don't treat tasks scheduled in future as missed
Browse files Browse the repository at this point in the history
  • Loading branch information
strika committed Nov 2, 2022
1 parent 74b8137 commit 123d18e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
22 changes: 14 additions & 8 deletions src/commands/schedule_tasks.janet
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@
(string "every year on " (remove-year formatted-date)) true
(string "on " formatted-date) true))

(defn- missed-on-day [plan task]
(defn- missed-on-day [plan task date]
(find (fn [day] (and (scheduled-for? task (day :date))
(not (day/has-task? day task))))
(plan/all-days plan)))
(plan/all-days-before plan date)))

# Public
(defn missed? [plan task]
(def day (missed-on-day plan task))
(defn missed?
```
Checks if the task was missed in the plan up to the passed date.
Returns true or false.
```
[plan task date]
(def day (missed-on-day plan task date))
(and day (not (plan/has-task-after? plan task (day :date)))))

(defn- mark-tasks-as-missed [plan tasks]
(defn- mark-tasks-as-missed [plan tasks date]
(map (fn [task]
(let [day (missed-on-day plan task)]
(let [day (missed-on-day plan task date)]
(task/mark-as-missed task (day :date))))
tasks))

Expand All @@ -52,8 +58,8 @@
(let [tasks (filter (fn [task] (scheduled-for? task (day :date))) scheduled-tasks)]
(day/add-tasks day tasks)))
(loop [day :in future-days]
(let [tasks (filter (fn [task] (missed? plan task)) scheduled-tasks)
missed-tasks (mark-tasks-as-missed plan tasks)]
(let [tasks (filter (fn [task] (missed? plan task date)) scheduled-tasks)
missed-tasks (mark-tasks-as-missed plan tasks date)]
(day/add-tasks day missed-tasks)))

plan)
Expand Down
22 changes: 19 additions & 3 deletions test/commands/schedule_tasks_test.janet
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,27 @@
(def plan (plan/build-plan
:days @[(day/build-day (d/date 2022 8 2))
(day/build-day (d/date 2022 8 1))]))
(is (missed? plan scheduled-task)))
(is (missed? plan scheduled-task (d/date 2022 8 3))))

(deftest missed-returns-true-when-day-does-not-exist
(def plan (plan/build-plan
:days @[(day/build-day (d/date 2022 8 2))
(day/build-day (d/date 2022 7 15))]))
(is (missed? plan scheduled-task)))
(is (missed? plan scheduled-task (d/date 2022 8 3))))

(deftest missed-returns-false-when-task-was-scheduled-for-another-day
(def plan (plan/build-plan
:days @[(day/build-day (d/date 2022 8 2)
@[]
@[(task/build-task "Weekly meeting" true)])
(day/build-day (d/date 2022 8 1))]))
(is (not (missed? plan scheduled-task))))
(is (not (missed? plan scheduled-task (d/date 2022 8 3)))))

(deftest missed-returns-false-when-task-will-be-scheduled-in-future
(def plan (plan/build-plan
:days @[(day/build-day (d/date 2022 8 2))
(day/build-day (d/date 2022 7 31))]))
(is (not (missed? plan scheduled-task (d/date 2022 7 31)))))

## —————————————————————————————————————————————————————————————————————————————————————————————————
## Test schedule-tasks
Expand Down Expand Up @@ -161,6 +167,16 @@
(is (= false (task :done)))
(is (= "every Monday" (task :schedule)))))))

(deftest does-not-schedule-tasks-today-that-are-not-scheduled-for-future-day-that-is-not-in-plan
(def plan (plan/build-plan
:days @[(day/build-day (d/date 2022 1 19))
(day/build-day (d/date 2022 1 18))
(day/build-day (d/date 2022 1 16))]))
(schedule-tasks plan scheduled-tasks (d/date 2022 1 16))
(is (= 1 (length (((plan :days) 0) :tasks))))
(is (empty? (((plan :days) 1) :tasks)))
(is (empty? (((plan :days) 2) :tasks))))

## —————————————————————————————————————————————————————————————————————————————————————————————————
## Test build-command

Expand Down

0 comments on commit 123d18e

Please sign in to comment.