Procházet zdrojové kódy

test(undo): update generative testcases

rcmerci před 1 rokem
rodič
revize
b5c3757139

+ 62 - 50
src/main/frontend/worker/undo_redo.cljs

@@ -87,6 +87,10 @@ when undo this op, this original entity-map will be transacted back into db")
 
 (def ^:private undo-ops-validator (m/validator [:sequential undo-op-schema]))
 
+(def ^:dynamic *undo-redo-info-for-test*
+  "record undo-op info when running-test"
+  nil)
+
 (def ^:private entity-map-pull-pattern
   [:block/uuid
    {:block/left [:block/uuid]}
@@ -201,42 +205,44 @@ 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))]
-          (outliner-tx/transact!
-           {:gen-undo-op? false
-            :outliner-op :insert-blocks
-            :transact-opts {:repo repo
-                            :conn conn}}
-           (outliner-core/insert-blocks! repo conn
-                                         [(cond-> {:block/uuid block-uuid
-                                                   :block/content (:block/content block-entity-map)
-                                                   :block/format :markdown}
-                                            (:block/created-at block-entity-map)
-                                            (assoc :block/created-at (:block/created-at block-entity-map))
-
-                                            (:block/updated-at block-entity-map)
-                                            (assoc :block/updated-at (:block/updated-at block-entity-map))
-
-                                            (seq (:block/tags block-entity-map))
-                                            (assoc :block/tags (mapv (partial vector :block/uuid)
-                                                                     (:block/tags block-entity-map))))]
-                                         left-entity {:sibling? sibling? :keep-uuid? true}))
-          :push-undo-redo)))))
+          (some->>
+           (outliner-tx/transact!
+            {:gen-undo-op? false
+             :outliner-op :insert-blocks
+             :transact-opts {:repo repo
+                             :conn conn}}
+            (outliner-core/insert-blocks! repo conn
+                                          [(cond-> {:block/uuid block-uuid
+                                                    :block/content (:block/content block-entity-map)
+                                                    :block/format :markdown}
+                                             (:block/created-at block-entity-map)
+                                             (assoc :block/created-at (:block/created-at block-entity-map))
+
+                                             (:block/updated-at block-entity-map)
+                                             (assoc :block/updated-at (:block/updated-at block-entity-map))
+
+                                             (seq (:block/tags block-entity-map))
+                                             (assoc :block/tags (mapv (partial vector :block/uuid)
+                                                                      (:block/tags block-entity-map))))]
+                                          left-entity {:sibling? sibling? :keep-uuid? true}))
+           (conj [:push-undo-redo])))))))
 
 (defmethod reverse-apply-op ::insert-block
   [op conn repo]
   (let [[_ {:keys [block-uuid]}] op]
     (when-let [block-entity (d/entity @conn [:block/uuid block-uuid])]
       (when (empty? (:block/_parent block-entity)) ;if have children, skip
-        (outliner-tx/transact!
-         {:gen-undo-op? 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-entity]
-                                       {:children? false}))
-        :push-undo-redo))))
+        (some->>
+         (outliner-tx/transact!
+          {:gen-undo-op? 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-entity]
+                                        {:children? false}))
+         (conj [:push-undo-redo]))))))
 
 (defmethod reverse-apply-op ::move-block
   [op conn repo]
@@ -244,13 +250,14 @@ 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 [left-entity (d/entity @conn [:block/uuid block-origin-left])]
         (let [sibling? (not= block-origin-left block-origin-parent)]
-          (outliner-tx/transact!
-           {:gen-undo-op? false
-            :outliner-op :move-blocks
-            :transact-opts {:repo repo
-                            :conn conn}}
-           (outliner-core/move-blocks! repo conn [block-entity] left-entity sibling?))
-          :push-undo-redo)))))
+          (some->>
+           (outliner-tx/transact!
+            {:gen-undo-op? false
+             :outliner-op :move-blocks
+             :transact-opts {:repo repo
+                             :conn conn}}
+            (outliner-core/move-blocks! repo conn [block-entity] left-entity sibling?))
+           (conj [:push-undo-redo])))))))
 
 (defmethod reverse-apply-op ::update-block
   [op conn repo]
@@ -258,15 +265,16 @@ 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 (normal-block? block-entity)
         (let [new-block (assoc block-entity :block/content block-origin-content)]
-          (outliner-tx/transact!
-           {:gen-undo-op? false
-            :outliner-op :save-block
-            :transact-opts {:repo repo
-                            :conn conn}}
-           (outliner-core/save-block! repo conn
-                                      (common-config/get-date-formatter (worker-state/get-config repo))
-                                      new-block))
-          :push-undo-redo)))))
+          (some->>
+           (outliner-tx/transact!
+            {:gen-undo-op? false
+             :outliner-op :save-block
+             :transact-opts {:repo repo
+                             :conn conn}}
+            (outliner-core/save-block! repo conn
+                                       (common-config/get-date-formatter (worker-state/get-config repo))
+                                       new-block))
+           (conj [:push-undo-redo])))))))
 
 (defn undo
   [repo conn]
@@ -274,8 +282,10 @@ when undo this op, this original entity-map will be transacted back into db")
     (let [redo-ops-to-push (transient [])]
       (batch-tx/with-batch-tx-mode conn
         (doseq [op ops]
-          (let [rev-op (reverse-op @conn op)]
-            (when (= :push-undo-redo (reverse-apply-op op conn repo))
+          (let [rev-op (reverse-op @conn op)
+                r (reverse-apply-op op conn repo)]
+            (when (= :push-undo-redo (first r))
+              (some-> *undo-redo-info-for-test* (reset! {:op op :tx (second r)}))
               (conj! redo-ops-to-push rev-op)))))
       (when-let [rev-ops (not-empty (persistent! redo-ops-to-push))]
         (push-redo-ops repo (cons boundary rev-ops)))
@@ -291,8 +301,10 @@ when undo this op, this original entity-map will be transacted back into db")
     (let [undo-ops-to-push (transient [])]
       (batch-tx/with-batch-tx-mode conn
         (doseq [op ops]
-          (let [rev-op (reverse-op @conn op)]
-            (when (= :push-undo-redo (reverse-apply-op op conn repo))
+          (let [rev-op (reverse-op @conn op)
+                r (reverse-apply-op op conn repo)]
+            (when (= :push-undo-redo (first r))
+              (some-> *undo-redo-info-for-test* (reset! {:op op :tx (second r)}))
               (conj! undo-ops-to-push rev-op)))))
       (when-let [rev-ops (not-empty (persistent! undo-ops-to-push))]
         (push-undo-ops repo (cons boundary rev-ops)))

+ 64 - 33
src/test/frontend/worker/undo_redo_test.cljs

@@ -7,8 +7,8 @@
             [frontend.test.helper :as test-helper]
             [frontend.worker.undo-redo :as undo-redo]))
 
-(def init-data (test-helper/initial-test-page-and-blocks))
-(defn start-and-destroy-db
+(def ^:private init-data (test-helper/initial-test-page-and-blocks))
+(defn- start-and-destroy-db
   [f]
   (test-helper/db-based-start-and-destroy-db
    f
@@ -16,7 +16,7 @@
 
 (use-fixtures :each start-and-destroy-db)
 
-(def gen-non-exist-block-uuid gen/uuid)
+(def ^:private gen-non-exist-block-uuid gen/uuid)
 
 (defn- gen-block-uuid
   [db & {:keys [non-exist-frequency] :or {non-exist-frequency 1}}]
@@ -45,7 +45,7 @@
   [db]
   (gen/let [block-uuid (gen-block-uuid db {:non-exist-frequency 80})
             [parent left] (gen-parent-left-pair db)
-            content gen/string-ascii]
+            content gen/string-alphanumeric]
     [:frontend.worker.undo-redo/remove-block
      {:block-uuid block-uuid
       :block-entity-map
@@ -57,12 +57,12 @@
 (defn- gen-update-block-op
   [db]
   (gen/let [block-uuid (gen-block-uuid db)
-            content gen/string-ascii]
+            content gen/string-alphanumeric]
     [:frontend.worker.undo-redo/update-block
      {:block-uuid block-uuid
       :block-origin-content content}]))
 
-(def gen-boundary (gen/return [:frontend.worker.undo-redo/boundary]))
+(def ^:private gen-boundary (gen/return [:frontend.worker.undo-redo/boundary]))
 
 (defn- gen-op
   [db & {:keys [insert-block-op move-block-op remove-block-op update-block-op boundary-op]
@@ -79,41 +79,71 @@
 
 (defn- get-db-block-set
   [db]
-  (set (d/q '[:find ?uuid ?parent-uuid ?left-uuid
-              :where
-              [?b :block/uuid ?uuid]
-              [?b :block/parent ?parent]
-              [?b :block/left ?left]
-              [?parent :block/uuid ?parent-uuid]
-              [?left :block/uuid ?left-uuid]]
-            db)))
+  (set
+   (apply concat
+          (d/q '[:find ?uuid
+                 :where
+                 [?b :block/uuid ?uuid]
+                 [?b :block/parent ?parent]
+                 [?b :block/left ?left]
+                 [?parent :block/uuid ?parent-uuid]
+                 [?left :block/uuid ?left-uuid]]
+               db))))
+
+
+(defn- check-block-count
+  [{:keys [op tx]} current-db]
+  (case (first op)
+    :frontend.worker.undo-redo/move-block
+    (assert (= (:block-origin-left (second op))
+               (:block/uuid (:block/left (d/entity current-db [:block/uuid (:block-uuid (second op))]))))
+            {:op op :tx-data (:tx-data tx) :x (keys tx)})
+
+    :frontend.worker.undo-redo/update-block
+    (assert (some? (d/entity current-db [:block/uuid (:block-uuid (second op))]))
+            {:op op :tx-data (:tx-data tx)})
+
+    :frontend.worker.undo-redo/insert-block
+    (assert (nil? (d/entity current-db [:block/uuid (:block-uuid (second op))]))
+            {:op op :tx-data (:tx-data tx) :x (keys tx)})
+    :frontend.worker.undo-redo/remove-block
+    (assert (some? (d/entity current-db [:block/uuid (:block-uuid (second op))]))
+            {:op op :tx-data (:tx-data tx) :x (keys tx)})
+    ;; else
+    nil))
 
 (defn- undo-all-then-redo-all
   [conn]
-  (loop [i 0]
-    (if (not= :frontend.worker.undo-redo/empty-undo-stack
-              (undo-redo/undo test-helper/test-db-name-db-version conn))
-      (recur (inc i))
-      (prn :undo-count i)))
-
-  (loop []
-    (when (not= :frontend.worker.undo-redo/empty-redo-stack
-                (undo-redo/redo test-helper/test-db-name-db-version conn))
-      (recur))))
-
-(deftest undo-test
+  (binding [undo-redo/*undo-redo-info-for-test* (atom nil)]
+    (loop [i 0]
+      (let [r (undo-redo/undo test-helper/test-db-name-db-version conn)
+            current-db @conn]
+        (check-block-count @undo-redo/*undo-redo-info-for-test* current-db)
+        (if (not= :frontend.worker.undo-redo/empty-undo-stack r)
+          (recur (inc i))
+          (prn :undo-count i))))
+
+    (loop []
+      (let [r (undo-redo/redo test-helper/test-db-name-db-version conn)
+            current-db @conn]
+        (check-block-count @undo-redo/*undo-redo-info-for-test* current-db)
+        (when (not= :frontend.worker.undo-redo/empty-redo-stack r)
+          (recur))))))
+
+(deftest undo-redo-gen-test
   (let [conn (db/get-db false)
-        all-remove-ops (gen/generate (gen/vector (gen-op @conn {:remove-block-op 1000}) 100))]
+        all-remove-ops (gen/generate (gen/vector (gen-op @conn {:remove-block-op 1000}) 20))]
     (#'undo-redo/push-undo-ops test-helper/test-db-name-db-version all-remove-ops)
+    (prn :block-count-before-init (count (get-db-block-set @conn)))
     (loop [i 0]
-      (if (not= :frontend.worker.undo-redo/empty-undo-stack
-                (undo-redo/undo test-helper/test-db-name-db-version conn))
-        (recur (inc i))
-        (prn :undo-count i)))
+      (when (not= :frontend.worker.undo-redo/empty-undo-stack
+                  (undo-redo/undo test-helper/test-db-name-db-version conn))
+        (recur (inc i))))
+    (prn :block-count (count (get-db-block-set @conn)))
     (undo-redo/clear-undo-redo-stack)
     (testing "move blocks"
       (let [origin-graph-block-set (get-db-block-set @conn)
-            ops (gen/generate (gen/vector (gen-op @conn {:move-block-op 1000 :boundary-op 500}) 1000))]
+            ops (gen/generate (gen/vector (gen-op @conn {:move-block-op 1000 :boundary-op 500}) 300))]
         (prn :ops (count ops))
         (#'undo-redo/push-undo-ops test-helper/test-db-name-db-version ops)
 
@@ -133,4 +163,5 @@
         (undo-all-then-redo-all conn)
         (undo-all-then-redo-all conn)
 
-        (is (= origin-graph-block-set (get-db-block-set @conn)))))))
+        (is (= origin-graph-block-set (get-db-block-set @conn)))))
+    ))