Browse Source

enhance(rtc): add testcases, fix filter-remote-data-by-local-unpushed-ops

rcmerci 2 years ago
parent
commit
23bd9c1d76

+ 12 - 7
src/main/frontend/db/rtc/core.cljs

@@ -293,8 +293,8 @@
       (page-handler/delete! page-name nil {:redirect-to-home? false :persist-op? false}))))
 
 
-(defn- filter-remote-data-by-local-unpushed-ops
-  "when remote-data request client to move/update blocks,
+(defn filter-remote-data-by-local-unpushed-ops
+  "when remote-data request client to move/update/remove/... blocks,
   these updates maybe not needed, because this client just updated some of these blocks,
   so we need to filter these just-updated blocks out, according to the unpushed-local-ops in indexeddb"
   [affected-blocks-map local-unpushed-ops]
@@ -303,17 +303,22 @@
      (case (first local-op)
        "move"
        (let [block-uuids (:block-uuids (second local-op))
-             remote-ops (vals (select-keys affected-blocks-map block-uuids))
-             block-uuids-to-del-in-result
-             (keep (fn [op] (when (= :move (:op op)) (:self op))) remote-ops)]
-         (apply dissoc affected-blocks-map block-uuids-to-del-in-result))
+             remote-ops (vals (select-keys affected-blocks-map block-uuids))]
+         (reduce
+          (fn [r remote-op]
+            (case (:op remote-op)
+              :remove (dissoc r (:block-uuid remote-op))
+              :move (dissoc r (:self remote-op))
+              ;; default
+              r))
+          affected-blocks-map remote-ops))
 
        "update"
        (let [block-uuid (:block-uuid (second local-op))
              local-updated-attr-set (set (keys (:updated-attrs (second local-op))))]
          (if-let [remote-op (get affected-blocks-map block-uuid)]
            (assoc affected-blocks-map block-uuid
-                  (if (= :update-attrs (:op remote-op))
+                  (if (#{:update-attrs :move} (:op remote-op))
                     (apply dissoc remote-op local-updated-attr-set)
                     remote-op))
            affected-blocks-map))

+ 4 - 1
src/test/frontend/db/rtc_test.cljs → src/test/frontend/db/rtc/rtc_effects_test.cljs

@@ -1,4 +1,7 @@
-(ns frontend.db.rtc-test
+(ns frontend.db.rtc.rtc-effects-test
+  "This ns include tests abouts rtc-part with other components.
+  These tests need to start the rtc-loop.
+  Other simple fn tests are located at `frontend.db.rtc.rtc-fns-test`"
   (:require ["/frontend/idbkv" :as idb-keyval]
             [cljs.core.async :as async :refer [<! go timeout]]
             [clojure.test :as t :refer [deftest is use-fixtures]]

+ 56 - 0
src/test/frontend/db/rtc/rtc_fns_test.cljs

@@ -0,0 +1,56 @@
+(ns frontend.db.rtc.rtc-fns-test
+  (:require [clojure.test :as t :refer [deftest is testing]]
+            [frontend.db.rtc.core :as rtc-core]))
+
+
+(deftest filter-remote-data-by-local-unpushed-ops-test
+  (testing "case1"
+    (let [[uuid1 uuid2] (repeatedly (comp str random-uuid))
+          affected-blocks-map
+          {uuid1
+           {:op :move
+            :self uuid1
+            :parents [uuid2]
+            :left uuid2
+            :content "content-str"}}
+          unpushed-ops
+          [["update" {:block-uuid uuid1
+                      :updated-attrs {:content nil}}]]
+          r (rtc-core/filter-remote-data-by-local-unpushed-ops affected-blocks-map unpushed-ops)]
+      (is (= {uuid1
+              {:op :move
+               :self uuid1
+               :parents [uuid2]
+               :left uuid2}}
+             r))))
+  (testing "case2"
+    (let [[uuid1 uuid2] (repeatedly (comp str random-uuid))
+          affected-blocks-map
+          {uuid1
+           {:op :update-attrs
+            :self uuid1
+            :parents [uuid2]
+            :left uuid2
+            :content "content-str"
+            :created-at 123}}
+          unpushed-ops
+          [["update" {:block-uuid uuid1
+                      :updated-attrs {:content nil}}]]
+          r (rtc-core/filter-remote-data-by-local-unpushed-ops affected-blocks-map unpushed-ops)]
+      (is (= {uuid1
+              {:op :update-attrs
+               :self uuid1
+               :parents [uuid2]
+               :left uuid2
+               :created-at 123}}
+             r))))
+  (testing "case3"
+    (let [[uuid1] (repeatedly (comp str random-uuid))
+          affected-blocks-map
+          {uuid1
+           {:op :remove
+            :block-uuid uuid1}}
+          unpushed-ops
+          [["move" {:block-uuids [uuid1]}]]
+          r (rtc-core/filter-remote-data-by-local-unpushed-ops affected-blocks-map unpushed-ops)]
+      (is (empty? r)))))