瀏覽代碼

test(undo): node-test check block-pos

rcmerci 1 年之前
父節點
當前提交
47bd0bf706
共有 2 個文件被更改,包括 66 次插入64 次删除
  1. 49 34
      src/main/frontend/worker/undo_redo.cljs
  2. 17 30
      src/test/frontend/worker/undo_redo_test.cljs

+ 49 - 34
src/main/frontend/worker/undo_redo.cljs

@@ -239,6 +239,36 @@ when undo this op, this original entity-map will be transacted back into db")
        (:block/parent 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)))
 (defmethod reverse-apply-op ::remove-block
   [op conn repo]
@@ -247,6 +277,7 @@ 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-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))
+              origin-db (when util/node-test? @conn)
               r (outliner-tx/transact!
                  {:gen-undo-ops? false
                   :outliner-op :insert-blocks
@@ -268,6 +299,11 @@ when undo this op, this original entity-map will be transacted back into db")
                                                                               (d/pull-many @conn [:db/id])
                                                                               (keep :db/id))))]
                                                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])
             [:push-undo-redo r]))))))
 
@@ -284,6 +320,7 @@ when undo this op, this original entity-map will be transacted back into db")
           block-uuid-set)))
       block-entities))))
 
+
 (defmethod reverse-apply-op ::insert-blocks
   [op conn repo]
   (let [[_ {:keys [block-uuids]}] op]
@@ -292,7 +329,7 @@ when undo this op, this original entity-map will be transacted back into db")
                                    not-empty)]
       (when-not (other-children-exist? block-entities)
         (let [origin-db (when util/node-test? @conn)
-              {:keys [tx-data]}
+              _
               (outliner-tx/transact!
                {:gen-undo-ops? false
                 :outliner-op :delete-blocks
@@ -303,19 +340,8 @@ when undo this op, this original entity-map will be transacted back into db")
                                              block-entities
                                              {}))]
           (when util/node-test?
-            (doseq [e (distinct (keep :e tx-data))]
-              (when-let [ent (d/entity @conn e)]
-                (when-not (:block/name ent)
-                  (let [self (:db/id ent)
-                        left (:db/id (:block/left ent))
-                        parent (:db/id (:block/parent ent))]
-                    (when (or (nil? left)
-                              (nil? parent)
-                              (= left self)
-                              (= parent self))
-                      (throw (ex-info "debug" {:origin-db origin-db
-                                               :illegal-entity (:db/id ent)
-                                               :blocks-to-delete (map :db/id block-entities)}))))))))))
+            (node-test-check origin-db @conn {:type :delete
+                                              :delete-blocks (map :db/id block-entities)}))))
 
       (when (every? nil? (map #(d/entity @conn [:block/uuid %]) block-uuids))
         [:push-undo-redo {}]))))
@@ -328,27 +354,16 @@ when undo this op, this original entity-map will be transacted back into db")
         (let [sibling? (not= block-origin-left block-origin-parent)
               origin-db (when util/node-test? @conn)
               r (outliner-tx/transact!
-                 {:gen-undo-ops? false
-                  :outliner-op :move-blocks
-                  :transact-opts {:repo repo
-                                  :conn conn}}
-                 (outliner-core/move-blocks! repo conn [block-entity] left-entity sibling?))]
+                    {:gen-undo-ops? false
+                     :outliner-op :move-blocks
+                     :transact-opts {:repo repo
+                                     :conn conn}}
+                    (outliner-core/move-blocks! repo conn [block-entity] left-entity sibling?))]
           (when util/node-test?
-            (doseq [e (distinct (keep :e (:tx-data r)))]
-              (when-let [ent (d/entity @conn e)]
-                (when-not (:block/name ent)
-                  (let [self (:db/id ent)
-                        left (:db/id (:block/left ent))
-                        parent (:db/id (:block/parent ent))]
-                    (when (or (nil? left)
-                              (nil? parent)
-                              (= left self)
-                              (= parent self))
-                      (throw (ex-info "debug" {:origin-db origin-db
-                                               :illegal-entity (:db/id ent)
-                                               :blocks-to-move [(:db/id block-entity)]
-                                               :target-block (:db/id left-entity)
-                                               :sibling? sibling?}))))))))
+            (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]))))))
 
 (defmethod reverse-apply-op ::update-block

+ 17 - 30
src/test/frontend/worker/undo_redo_test.cljs

@@ -280,34 +280,21 @@
 
 
 (deftest ^:wip2 debug-test
-  (let [{:keys [origin-db illegal-entity blocks-to-delete blocks-to-move target-block sibling?]}
+  (let [{:keys [origin-db db illegal-entity other]}
         (dt/read-transit-str (str (fs-node/readFileSync "debug.json")))
-        conn (d/conn-from-db origin-db)
-        _ (prn :blocks-to-move blocks-to-move :blocks-to-delete blocks-to-delete)
-        block-entities (map #(d/entity @conn %) blocks-to-delete)
-        blocks-to-move (map #(d/entity @conn %) blocks-to-move)]
-    (prn "before transact" (d/pull origin-db '[*] illegal-entity))
-    (when (seq blocks-to-delete)
-      (prn :delete-blocks block-entities)
-      (outliner-tx/transact!
-       {:gen-undo-ops? false
-        :outliner-op :delete-blocks
-        :transact-opts {:repo test-helper/test-db-name-db-version
-                        :conn conn}}
-       (outliner-core/delete-blocks! test-helper/test-db-name-db-version conn
-                                     "MMM do, yyyy"
-                                     block-entities
-                                     {})))
-    (when (seq blocks-to-move)
-      (prn :move-blocks blocks-to-move :target target-block)
-      (outliner-tx/transact!
-       {:gen-undo-ops? false
-        :outliner-op :delete-blocks
-        :transact-opts {:repo test-helper/test-db-name-db-version
-                        :conn conn}}
-       (outliner-core/move-blocks! test-helper/test-db-name-db-version conn
-                                   blocks-to-move
-                                   (d/entity @conn target-block)
-                                   sibling?)))
-
-    (prn "after transact" (d/pull @conn '[*] illegal-entity))))
+        _ (prn :illegal-entity illegal-entity :other other)
+        illegal-entity1 (d/entity origin-db illegal-entity)
+        illegal-entity-left1 (:block/left illegal-entity1)
+        illegal-entity-parent1 (:block/parent illegal-entity1)]
+    (prn "before transact"
+         (select-keys illegal-entity1 [:db/id :block/left :block/parent])
+         (select-keys illegal-entity-left1 [:db/id :block/left :block/parent])
+         (select-keys illegal-entity-parent1 [:db/id :block/left :block/parent]))
+
+    (let [illegal-entity2 (d/entity db illegal-entity)
+          illegal-entity-left2 (:block/left illegal-entity2)
+          illegal-entity-parent2 (:block/parent illegal-entity2)]
+      (prn "after transact"
+           (select-keys illegal-entity2 [:db/id :block/left :block/parent])
+           (select-keys illegal-entity-left2 [:db/id :block/left :block/parent])
+           (select-keys illegal-entity-parent2 [:db/id :block/left :block/parent])))))