Jelajahi Sumber

fix: unit tests

Tienson Qin 1 tahun lalu
induk
melakukan
08f4fc2a63

+ 10 - 10
src/main/frontend/worker/db/fix.cljs

@@ -9,7 +9,7 @@
             [frontend.worker.util :as worker-util]))
             [frontend.worker.util :as worker-util]))
 
 
 (defn- fix-parent-broken-chain
 (defn- fix-parent-broken-chain
-  [db parent-id tx-report]
+  [db parent-id tx-report from-fix-test?]
   (let [parent (d/entity db parent-id)
   (let [parent (d/entity db parent-id)
         parent-id (:db/id parent)
         parent-id (:db/id parent)
         blocks (:block/_parent parent)]
         blocks (:block/_parent parent)]
@@ -20,7 +20,7 @@
             broken-chain? (or (not= (count sorted) (count blocks))
             broken-chain? (or (not= (count sorted) (count blocks))
                               ;; :block/left points to other blocks that're not current parent or its children
                               ;; :block/left points to other blocks that're not current parent or its children
                               (not (every? (fn [b] (contains? valid-left-ids (:db/id (:block/left b)))) blocks)))]
                               (not (every? (fn [b] (contains? valid-left-ids (:db/id (:block/left b)))) blocks)))]
-        (when (and (exists? js/process) broken-chain?)
+        (when (and (not from-fix-test?) (exists? js/process) broken-chain?)
           (throw (ex-info "outliner broken chain" {:tx-meta (:tx-meta tx-report)
           (throw (ex-info "outliner broken chain" {:tx-meta (:tx-meta tx-report)
                                                    :tx-data (:tx-data tx-report)
                                                    :tx-data (:tx-data tx-report)
                                                    :db-before (:db-before tx-report)})))
                                                    :db-before (:db-before tx-report)})))
@@ -99,9 +99,9 @@
              (apply concat))))))))
              (apply concat))))))))
 
 
 (defn- fix-broken-chain
 (defn- fix-broken-chain
-  [db parent-left->es tx-report]
+  [db parent-left->es tx-report from-fix-test?]
   (let [parents (distinct (map first (keys parent-left->es)))]
   (let [parents (distinct (map first (keys parent-left->es)))]
-    (mapcat #(fix-parent-broken-chain db % tx-report) parents)))
+    (mapcat #(fix-parent-broken-chain db % tx-report from-fix-test?) parents)))
 
 
 (defn- build-parent-left->es
 (defn- build-parent-left->es
   [db page-id]
   [db page-id]
@@ -146,10 +146,10 @@
     (filter #(> (count (second %)) 1) parent-left->es)))
     (filter #(> (count (second %)) 1) parent-left->es)))
 
 
 (defn- loop-fix-conflicts
 (defn- loop-fix-conflicts
-  [conn page-id transact-opts *fix-tx-data]
+  [conn page-id transact-opts *fix-tx-data from-fix-test?]
   (let [db @conn
   (let [db @conn
         conflicts (get-conflicts db page-id)
         conflicts (get-conflicts db page-id)
-        _ (when (and (exists? js/process) (seq conflicts))
+        _ (when (and (not from-fix-test?) (exists? js/process) (seq conflicts))
             (throw (ex-info "outliner core conflicts" {:conflicts conflicts})))
             (throw (ex-info "outliner core conflicts" {:conflicts conflicts})))
         fix-conflicts-tx (when (seq conflicts)
         fix-conflicts-tx (when (seq conflicts)
                            (fix-parent-left-conflicts db conflicts page-id))]
                            (fix-parent-left-conflicts db conflicts page-id))]
@@ -159,11 +159,11 @@
       (let [tx-data (:tx-data (ldb/transact! conn fix-conflicts-tx transact-opts))]
       (let [tx-data (:tx-data (ldb/transact! conn fix-conflicts-tx transact-opts))]
         (swap! *fix-tx-data (fn [old-data] (concat old-data tx-data))))
         (swap! *fix-tx-data (fn [old-data] (concat old-data tx-data))))
       (when (seq (get-conflicts @conn page-id))
       (when (seq (get-conflicts @conn page-id))
-        (loop-fix-conflicts conn page-id transact-opts *fix-tx-data)))))
+        (loop-fix-conflicts conn page-id transact-opts *fix-tx-data from-fix-test?)))))
 
 
 (defn fix-page-if-broken!
 (defn fix-page-if-broken!
   "Fix the page if it has either parent-left conflicts or broken chains."
   "Fix the page if it has either parent-left conflicts or broken chains."
-  [conn page-id {:keys [fix-parent-left? fix-broken-chain? replace-tx? tx-report]
+  [conn page-id {:keys [fix-parent-left? fix-broken-chain? replace-tx? tx-report from-fix-test?]
                  :or {fix-parent-left? true
                  :or {fix-parent-left? true
                       fix-broken-chain? true
                       fix-broken-chain? true
                       replace-tx? false}
                       replace-tx? false}
@@ -175,11 +175,11 @@
       (let [transact-opts (if replace-tx? {:replace? true} {})
       (let [transact-opts (if replace-tx? {:replace? true} {})
             *fix-tx-data (atom [])]
             *fix-tx-data (atom [])]
         (when fix-parent-left?
         (when fix-parent-left?
-          (loop-fix-conflicts conn page-id transact-opts *fix-tx-data))
+          (loop-fix-conflicts conn page-id transact-opts *fix-tx-data from-fix-test?))
         (when fix-broken-chain?
         (when fix-broken-chain?
           (let [db' @conn
           (let [db' @conn
                 parent-left->es' (build-parent-left->es db page-id)
                 parent-left->es' (build-parent-left->es db page-id)
-                fix-broken-chain-tx (fix-broken-chain db' parent-left->es' tx-report)]
+                fix-broken-chain-tx (fix-broken-chain db' parent-left->es' tx-report from-fix-test?)]
             (when (seq fix-broken-chain-tx)
             (when (seq fix-broken-chain-tx)
               (let [tx-data (:tx-data (ldb/transact! conn fix-broken-chain-tx transact-opts))]
               (let [tx-data (:tx-data (ldb/transact! conn fix-broken-chain-tx transact-opts))]
                 (swap! *fix-tx-data (fn [old-data] (concat old-data tx-data)))))))
                 (swap! *fix-tx-data (fn [old-data] (concat old-data tx-data)))))))

+ 10 - 57
src/main/frontend/worker/undo_redo.cljs

@@ -11,8 +11,7 @@
             [logseq.outliner.core :as outliner-core]
             [logseq.outliner.core :as outliner-core]
             [logseq.outliner.transaction :as outliner-tx]
             [logseq.outliner.transaction :as outliner-tx]
             [malli.core :as m]
             [malli.core :as m]
-            [malli.util :as mu]
-            [frontend.util :as util]))
+            [malli.util :as mu]))
 
 
 (sr/defkeyword :gen-undo-ops?
 (sr/defkeyword :gen-undo-ops?
   "tx-meta option, generate undo ops from tx-data when true (default true)")
   "tx-meta option, generate undo ops from tx-data when true (default true)")
@@ -240,35 +239,6 @@ when undo this op, this original entity-map will be transacted back into db")
        (:block/left entity)))
        (:block/left entity)))
 
 
 
 
-(defn- node-test-check
-  [origin-db db other-info]
-  (let [seen (volatile! #{})
-        datoms (d/datoms db :eavt)]
-    (doseq [e (map :e datoms)]
-      (when-not (contains? @seen e)
-        (when-let [ent (d/entity db e)]
-          (when (and (:block/uuid ent)
-                     (:block/parent ent)
-                     (:block/left ent))
-            (let [rightmost-ent (loop [ent ent]
-                                  (if-let [right (:block/_left ent)]
-                                    (recur right)
-                                    ent))]
-              (loop [ent rightmost-ent]
-                (let [left-ent (:block/left ent)
-                      parent-ent (:block/parent ent)]
-                  (when (and left-ent parent-ent
-                             (not= left-ent parent-ent)
-                             (not= (:block/parent left-ent) parent-ent))
-                    (throw (ex-info "debug" {:origin-db origin-db
-                                             :db db
-                                             :illegal-entity (:db/id ent)
-                                             :other other-info})))
-
-                  (when (and left-ent (not= left-ent parent-ent))
-                    (vswap! seen conj (:db/id ent))
-                    (recur left-ent)))))))))))
-
 (defmulti ^:private reverse-apply-op (fn [op _conn _repo] (first op)))
 (defmulti ^:private reverse-apply-op (fn [op _conn _repo] (first op)))
 (defmethod reverse-apply-op ::remove-block
 (defmethod reverse-apply-op ::remove-block
   [op conn repo]
   [op conn repo]
@@ -277,7 +247,6 @@ when undo this op, this original entity-map will be transacted back into db")
     (when-not block-entity ;; this block shouldn't exist now
     (when-not block-entity ;; this block shouldn't exist now
       (when-let [left-entity (d/entity @conn [:block/uuid (:block/left block-entity-map)])]
       (when-let [left-entity (d/entity @conn [:block/uuid (:block/left block-entity-map)])]
         (let [sibling? (not= (:block/left block-entity-map) (:block/parent block-entity-map))
         (let [sibling? (not= (:block/left block-entity-map) (:block/parent block-entity-map))
-              origin-db (when util/node-test? @conn)
               r (outliner-tx/transact!
               r (outliner-tx/transact!
                  {:gen-undo-ops? false
                  {:gen-undo-ops? false
                   :outliner-op :insert-blocks
                   :outliner-op :insert-blocks
@@ -299,11 +268,6 @@ when undo this op, this original entity-map will be transacted back into db")
                                                                               (d/pull-many @conn [:db/id])
                                                                               (d/pull-many @conn [:db/id])
                                                                               (keep :db/id))))]
                                                                               (keep :db/id))))]
                                                left-entity {:sibling? sibling? :keep-uuid? true}))]
                                                left-entity {:sibling? sibling? :keep-uuid? true}))]
-          (when util/node-test?
-            (node-test-check origin-db @conn {:type :insert
-                                              :insert-blocks [block-uuid]
-                                              :target-block (:db/id left-entity)
-                                              :sibling? sibling?}))
           (when (d/entity @conn [:block/uuid block-uuid])
           (when (d/entity @conn [:block/uuid block-uuid])
             [:push-undo-redo r]))))))
             [:push-undo-redo r]))))))
 
 
@@ -328,20 +292,15 @@ when undo this op, this original entity-map will be transacted back into db")
                                    (keep #(d/entity @conn [:block/uuid %]))
                                    (keep #(d/entity @conn [:block/uuid %]))
                                    not-empty)]
                                    not-empty)]
       (when-not (other-children-exist? block-entities)
       (when-not (other-children-exist? block-entities)
-        (let [origin-db (when util/node-test? @conn)
-              _
-              (outliner-tx/transact!
-               {:gen-undo-ops? false
-                :outliner-op :delete-blocks
-                :transact-opts {:repo repo
-                                :conn conn}}
-               (outliner-core/delete-blocks! repo conn
-                                             (common-config/get-date-formatter (worker-state/get-config repo))
-                                             block-entities
-                                             {}))]
-          (when util/node-test?
-            (node-test-check origin-db @conn {:type :delete
-                                              :delete-blocks (map :db/id block-entities)}))))
+        (outliner-tx/transact!
+         {:gen-undo-ops? false
+          :outliner-op :delete-blocks
+          :transact-opts {:repo repo
+                          :conn conn}}
+         (outliner-core/delete-blocks! repo conn
+                                       (common-config/get-date-formatter (worker-state/get-config repo))
+                                       block-entities
+                                       {})))
 
 
       (when (every? nil? (map #(d/entity @conn [:block/uuid %]) block-uuids))
       (when (every? nil? (map #(d/entity @conn [:block/uuid %]) block-uuids))
         [:push-undo-redo {}]))))
         [:push-undo-redo {}]))))
@@ -352,18 +311,12 @@ when undo this op, this original entity-map will be transacted back into db")
     (when-let [block-entity (d/entity @conn [:block/uuid block-uuid])]
     (when-let [block-entity (d/entity @conn [:block/uuid block-uuid])]
       (when-let [left-entity (d/entity @conn [:block/uuid block-origin-left])]
       (when-let [left-entity (d/entity @conn [:block/uuid block-origin-left])]
         (let [sibling? (not= block-origin-left block-origin-parent)
         (let [sibling? (not= block-origin-left block-origin-parent)
-              origin-db (when util/node-test? @conn)
               r (outliner-tx/transact!
               r (outliner-tx/transact!
                     {:gen-undo-ops? false
                     {:gen-undo-ops? false
                      :outliner-op :move-blocks
                      :outliner-op :move-blocks
                      :transact-opts {:repo repo
                      :transact-opts {:repo repo
                                      :conn conn}}
                                      :conn conn}}
                     (outliner-core/move-blocks! repo conn [block-entity] left-entity sibling?))]
                     (outliner-core/move-blocks! repo conn [block-entity] left-entity sibling?))]
-          (when util/node-test?
-            (node-test-check origin-db @conn {:type :move
-                                              :move-blocks [(:db/id block-entity)]
-                                              :target-block (:db/id left-entity)
-                                              :sibling? sibling?}))
           (when r [:push-undo-redo r]))))))
           (when r [:push-undo-redo r]))))))
 
 
 (defmethod reverse-apply-op ::update-block
 (defmethod reverse-apply-op ::update-block

+ 11 - 7
src/test/frontend/db/fix_test.cljs

@@ -18,11 +18,15 @@
     :block/parent [:block/uuid "1"]
     :block/parent [:block/uuid "1"]
     :block/left [:block/uuid "1"]}])
     :block/left [:block/uuid "1"]}])
 
 
+(defn fix-page-if-broken!
+  [conn page-id opts]
+  (db-fix/fix-page-if-broken! conn page-id (merge opts {:from-fix-test? true})))
+
 (deftest test-conflicts
 (deftest test-conflicts
   (let [conn (core-test/get-current-conn)
   (let [conn (core-test/get-current-conn)
         _ (d/transact! conn init-conflicts)
         _ (d/transact! conn init-conflicts)
         page-id (:db/id (d/entity @conn 1))
         page-id (:db/id (d/entity @conn 1))
-        _ (db-fix/fix-page-if-broken! conn page-id {})]
+        _ (fix-page-if-broken! conn page-id {})]
     (is (= 2 (:db/id (:block/left (d/entity @conn 3)))))))
     (is (= 2 (:db/id (:block/left (d/entity @conn 3)))))))
 
 
 (deftest test-conflicts-with-right
 (deftest test-conflicts-with-right
@@ -34,7 +38,7 @@
                        :block/left [:block/uuid "2"]}])
                        :block/left [:block/uuid "2"]}])
         _ (d/transact! conn data)
         _ (d/transact! conn data)
         page-id (:db/id (d/entity @conn 1))
         page-id (:db/id (d/entity @conn 1))
-        _ (db-fix/fix-page-if-broken! conn page-id {})]
+        _ (fix-page-if-broken! conn page-id {})]
     (is (= 3 (:db/id (:block/left (d/entity @conn 4)))))))
     (is (= 3 (:db/id (:block/left (d/entity @conn 4)))))))
 
 
 (def init-broken-chain
 (def init-broken-chain
@@ -58,7 +62,7 @@
         data init-broken-chain
         data init-broken-chain
         _ (d/transact! conn data)
         _ (d/transact! conn data)
         page-id (:db/id (d/entity @conn 1))
         page-id (:db/id (d/entity @conn 1))
-        _ (db-fix/fix-page-if-broken! conn page-id {})]
+        _ (fix-page-if-broken! conn page-id {})]
     (is
     (is
      (=
      (=
       (set [{:db/id 2, :block/left 1}
       (set [{:db/id 2, :block/left 1}
@@ -84,7 +88,7 @@
                :block/left [:block/uuid "2"]}]
                :block/left [:block/uuid "2"]}]
         _ (d/transact! conn data)
         _ (d/transact! conn data)
         page-id (:db/id (d/entity @conn 1))
         page-id (:db/id (d/entity @conn 1))
-        _ (db-fix/fix-page-if-broken! conn page-id {})]
+        _ (fix-page-if-broken! conn page-id {})]
     (is
     (is
      (=
      (=
       (set [{:db/id 3, :block/left 1}
       (set [{:db/id 3, :block/left 1}
@@ -112,7 +116,7 @@
                :block/left [:block/uuid "3"]}]
                :block/left [:block/uuid "3"]}]
         _ (d/transact! conn data)
         _ (d/transact! conn data)
         page-id (:db/id (d/entity @conn 1))
         page-id (:db/id (d/entity @conn 1))
-        _ (db-fix/fix-page-if-broken! conn page-id {})]
+        _ (fix-page-if-broken! conn page-id {})]
     (is
     (is
      (=
      (=
       (set [{:db/id 2, :block/left 1}
       (set [{:db/id 2, :block/left 1}
@@ -150,7 +154,7 @@
                :block/left [:block/uuid "2"]}]
                :block/left [:block/uuid "2"]}]
         _ (d/transact! conn data)
         _ (d/transact! conn data)
         page-id (:db/id (d/entity @conn 1))
         page-id (:db/id (d/entity @conn 1))
-        _ (db-fix/fix-page-if-broken! conn page-id {})]
+        _ (fix-page-if-broken! conn page-id {})]
     (is
     (is
      (=
      (=
       #{{:db/id 3, :block/left 1}
       #{{:db/id 3, :block/left 1}
@@ -190,7 +194,7 @@
                :block/left [:block/uuid "5"]}]
                :block/left [:block/uuid "5"]}]
         _ (d/transact! conn data)
         _ (d/transact! conn data)
         page-id (:db/id (d/entity @conn 1))
         page-id (:db/id (d/entity @conn 1))
-        _ (db-fix/fix-page-if-broken! conn page-id {})]
+        _ (fix-page-if-broken! conn page-id {})]
     (is
     (is
      (=
      (=
       #{{:db/id 2, :block/left 1}
       #{{:db/id 2, :block/left 1}

+ 1 - 3
src/test/frontend/worker/undo_redo_test.cljs

@@ -15,9 +15,7 @@
             [frontend.worker.state :as worker-state]
             [frontend.worker.state :as worker-state]
             ["fs" :as fs-node]
             ["fs" :as fs-node]
             [logseq.db.sqlite.util :as sqlite-util]
             [logseq.db.sqlite.util :as sqlite-util]
-            [datascript.transit :as dt]
-            [logseq.outliner.transaction :as outliner-tx]
-            [logseq.outliner.core :as outliner-core]))
+            [datascript.transit :as dt]))
 
 
 (def ^:private page-uuid (random-uuid))
 (def ^:private page-uuid (random-uuid))
 (def ^:private init-data (test-helper/initial-test-page-and-blocks {:page-uuid page-uuid}))
 (def ^:private init-data (test-helper/initial-test-page-and-blocks {:page-uuid page-uuid}))