Procházet zdrojové kódy

Add repeat task get-next-time tests

Tienson Qin před 8 měsíci
rodič
revize
3605a82483

+ 7 - 6
src/main/frontend/worker/commands.cljs

@@ -111,10 +111,12 @@
 (defn- repeat-until-future-timestamp
   [datetime recur-unit frequency period-f keep-week?]
   (let [now (t/now)
-        v (if (t/after? datetime now)
-            1
-            (period-f (t/interval datetime now)))
-        delta (->> (Math/ceil (/ (if (zero? v) 1 v) frequency))
+        v (max
+           1
+           (if (t/after? datetime now)
+             1
+             (period-f (t/interval datetime now))))
+        delta (->> (Math/ceil (/ v frequency))
                    (* frequency)
                    recur-unit)
         result* (t/plus datetime delta)
@@ -133,7 +135,6 @@
 (defn- get-next-time
   [current-value unit frequency]
   (let [current-date-time (tc/to-date-time current-value)
-        default-timezone-time (t/to-default-time-zone current-date-time)
         [recur-unit period-f] (case (:db/ident unit)
                                 :logseq.property.repeat/recur-unit.minute [t/minutes t/in-minutes]
                                 :logseq.property.repeat/recur-unit.hour [t/hours t/in-hours]
@@ -144,7 +145,7 @@
                                 nil)]
     (when recur-unit
       (let [week? (= (:db/ident unit) :logseq.property.repeat/recur-unit.week)
-            next-time (repeat-until-future-timestamp default-timezone-time recur-unit frequency period-f week?)]
+            next-time (repeat-until-future-timestamp current-date-time recur-unit frequency period-f week?)]
         (tc/to-long next-time)))))
 
 (defn- compute-reschedule-property-tx

+ 122 - 0
src/test/frontend/worker/commands_test.cljs

@@ -0,0 +1,122 @@
+(ns frontend.worker.commands-test
+  (:require [cljs-time.coerce :as tc]
+            [cljs-time.core :as t]
+            [cljs.test :refer [deftest is are testing]]
+            [frontend.worker.commands :as commands]))
+
+(def get-next-time #'commands/get-next-time)
+(def minute-unit {:db/ident :logseq.property.repeat/recur-unit.minute})
+(def hour-unit {:db/ident :logseq.property.repeat/recur-unit.hour})
+(def day-unit {:db/ident :logseq.property.repeat/recur-unit.day})
+(def week-unit {:db/ident :logseq.property.repeat/recur-unit.week})
+(def month-unit {:db/ident :logseq.property.repeat/recur-unit.month})
+(def year-unit {:db/ident :logseq.property.repeat/recur-unit.year})
+
+(deftest ^:focus get-next-time-test
+  (let [now (t/now)
+        one-minute-ago (t/minus now (t/minutes 1))
+        one-hour-ago (t/minus now (t/hours 1))
+        one-day-ago (t/minus now (t/days 1))
+        one-week-ago (t/minus now (t/weeks 1))
+        one-month-ago (t/minus now (t/months 1))
+        one-year-ago (t/minus now (t/years 1))
+        in-minutes (fn [next-time] (/ (- next-time now) (* 1000 60)))
+        in-hours (fn [next-time] (/ (- next-time now) (* 1000 60 60)))
+        in-days (fn [next-time] (/ (- next-time now) (* 1000 60 60 24)))
+        in-weeks (fn [next-time] (/ (- next-time now) (* 1000 60 60 24 7)))
+        in-months (fn [next-time] (t/in-months (t/interval now (tc/from-long next-time))))
+        in-years (fn [next-time] (t/in-years (t/interval now (tc/from-long next-time))))]
+    (testing "basic test for get-next-time"
+      ;; minute
+      (let [next-time (get-next-time now minute-unit 1)]
+        (is (= 1 (in-minutes next-time))))
+      (let [next-time (get-next-time one-minute-ago minute-unit 1)]
+        (is (= 1 (in-minutes next-time))))
+      (let [next-time (get-next-time one-minute-ago minute-unit 3)]
+        (is (= 2 (in-minutes next-time))))
+      (let [next-time (get-next-time one-minute-ago minute-unit 5)]
+        (is (= 4 (in-minutes next-time))))
+
+      ;; hour
+      (let [next-time (get-next-time now hour-unit 1)]
+        (is (= 1 (in-hours next-time))))
+      (let [next-time (get-next-time one-hour-ago hour-unit 1)]
+        (is (= 1 (in-hours next-time))))
+      (let [next-time (get-next-time one-hour-ago hour-unit 3)]
+        (is (= 2 (in-hours next-time))))
+      (let [next-time (get-next-time one-hour-ago hour-unit 5)]
+        (is (= 4 (in-hours next-time))))
+
+      ;; day
+      (let [next-time (get-next-time now day-unit 1)]
+        (is (= 1 (in-days next-time))))
+      (let [next-time (get-next-time one-day-ago day-unit 1)]
+        (is (= 1 (in-days next-time))))
+      (let [next-time (get-next-time one-day-ago day-unit 3)]
+        (is (= 2 (in-days next-time))))
+      (let [next-time (get-next-time one-day-ago day-unit 5)]
+        (is (= 4 (in-days next-time))))
+
+      ;; week
+      (let [next-time (get-next-time now week-unit 1)]
+        (is (= 1 (in-weeks next-time))))
+      (let [next-time (get-next-time one-week-ago week-unit 1)]
+        (is (= 1 (in-weeks next-time))))
+      (let [next-time (get-next-time one-week-ago week-unit 3)]
+        (is (= 2 (in-weeks next-time))))
+      (let [next-time (get-next-time one-week-ago week-unit 5)]
+        (is (= 4 (in-weeks next-time))))
+
+      ;; month
+      (let [next-time (get-next-time now month-unit 1)]
+        (is (= 1 (in-months next-time))))
+      (let [next-time (get-next-time one-month-ago month-unit 1)]
+        (is (= 1 (in-months next-time))))
+      (let [next-time (get-next-time one-month-ago month-unit 3)]
+        (is (= 2 (in-months next-time))))
+      (let [next-time (get-next-time one-month-ago month-unit 5)]
+        (is (= 4 (in-months next-time))))
+
+      ;; year
+      (let [next-time (get-next-time now year-unit 1)]
+        (is (= 1 (in-years next-time))))
+      (let [next-time (get-next-time one-year-ago year-unit 1)]
+        (is (= 1 (in-years next-time))))
+      (let [next-time (get-next-time one-year-ago year-unit 3)]
+        (is (= 2 (in-years next-time))))
+      (let [next-time (get-next-time one-year-ago year-unit 5)]
+        (is (= 4 (in-years next-time)))))
+
+    (testing "perserves week day"
+      (let [next-time (get-next-time now week-unit 1)]
+        (is (= (t/day-of-week (tc/from-long next-time)) (t/day-of-week now))))
+      (let [next-time (get-next-time one-week-ago week-unit 1)]
+        (is (= (t/day-of-week (tc/from-long next-time)) (t/day-of-week now)))))
+
+    (testing "schedule on future time should move it to the next one"
+      (let [next-time (get-next-time (t/plus now (t/minutes 10)) minute-unit 1)]
+        (is (= 11 (in-minutes next-time))))
+      (let [next-time (get-next-time (t/plus now (t/hours 10)) hour-unit 1)]
+        (is (= 11 (in-hours next-time))))
+      (let [next-time (get-next-time (t/plus now (t/days 10)) day-unit 1)]
+        (is (= 11 (in-days next-time))))
+      (let [next-time (get-next-time (t/plus now (t/weeks 10)) week-unit 1)]
+        (is (= 11 (in-weeks next-time))))
+      (let [next-time (get-next-time (t/plus now (t/months 10)) month-unit 1)]
+        (is (= 11 (in-months next-time))))
+      (let [next-time (get-next-time (t/plus now (t/years 10)) year-unit 1)]
+        (is (= 11 (in-years next-time)))))
+
+    (testing "schedule on past time should move it to future"
+      (let [next-time (get-next-time (t/minus now (t/minutes 10)) minute-unit 1)]
+        (is (= 1 (in-minutes next-time))))
+      (let [next-time (get-next-time (t/minus now (t/hours 10)) hour-unit 1)]
+        (is (= 1 (in-hours next-time))))
+      (let [next-time (get-next-time (t/minus now (t/days 10)) day-unit 1)]
+        (is (= 1 (in-days next-time))))
+      (let [next-time (get-next-time (t/minus now (t/weeks 10)) week-unit 1)]
+        (is (= 1 (in-weeks next-time))))
+      (let [next-time (get-next-time (t/minus now (t/months 10)) month-unit 1)]
+        (is (= 1 (in-months next-time))))
+      (let [next-time (get-next-time (t/minus now (t/years 10)) year-unit 1)]
+        (is (= 1 (in-years next-time)))))))