From 2d245295a7eb537969b41cccd12d396c5ca2ff2b Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Wed, 10 Jan 2024 04:15:09 +0800 Subject: [PATCH] fix: outliner core tests --- deps/db/src/logseq/db.cljs | 4 +- deps/outliner/src/logseq/outliner/core.cljs | 2 +- .../src/logseq/outliner/datascript.cljs | 4 +- src/main/frontend/util.cljc | 13 ++++- src/main/frontend/worker/util.cljc | 3 +- .../frontend/modules/outliner/core_test.cljs | 58 ++++++++++--------- 6 files changed, 49 insertions(+), 35 deletions(-) diff --git a/deps/db/src/logseq/db.cljs b/deps/db/src/logseq/db.cljs index b47502b7ae..3d54070dbf 100644 --- a/deps/db/src/logseq/db.cljs +++ b/deps/db/src/logseq/db.cljs @@ -60,8 +60,8 @@ (let [tx-data (common-util/fast-remove-nils tx-data)] (when (seq tx-data) - ;; (prn :debug :transact) - ;; (cljs.pprint/pprint tx-data) + ;; (prn :debug :transact) + ;; (cljs.pprint/pprint tx-data) (let [f (or @*transact-fn d/transact!)] (f conn tx-data tx-meta)))))) diff --git a/deps/outliner/src/logseq/outliner/core.cljs b/deps/outliner/src/logseq/outliner/core.cljs index cc2972074c..71d31acf47 100644 --- a/deps/outliner/src/logseq/outliner/core.cljs +++ b/deps/outliner/src/logseq/outliner/core.cljs @@ -387,7 +387,7 @@ (-get-children [this conn] (let [parent-id (otree/-get-id this conn) children (ldb/get-block-immediate-children @conn parent-id)] - (map (partial block @conn) children)))) + (map #(block @conn %) children)))) (defn get-right-sibling [db db-id] diff --git a/deps/outliner/src/logseq/outliner/datascript.cljs b/deps/outliner/src/logseq/outliner/datascript.cljs index 4548f0c360..ced15eb6f9 100644 --- a/deps/outliner/src/logseq/outliner/datascript.cljs +++ b/deps/outliner/src/logseq/outliner/datascript.cljs @@ -90,9 +90,11 @@ true (distinct))] + (when (and (seq txs) (or db-based? - (and (fn? unlinked-graph?) (not (unlinked-graph?))))) + (and (fn? unlinked-graph?) (not (unlinked-graph?))) + (exists? js/process))) ;; (prn :debug "DB transact") ;; (cljs.pprint/pprint txs) diff --git a/src/main/frontend/util.cljc b/src/main/frontend/util.cljc index 2de67a3c7a..34138587cf 100644 --- a/src/main/frontend/util.cljc +++ b/src/main/frontend/util.cljc @@ -1074,9 +1074,16 @@ [another-file-name]) string-join-path)))) -(defmacro profile - [k & body] - `(worker-util/profile ~k ~@body)) +#?(:clj + (defmacro profile + [k & body] + `(if goog.DEBUG + (let [k# ~k] + (.time js/console k#) + (let [res# (do ~@body)] + (.timeEnd js/console k#) + res#)) + (do ~@body)))) #?(:clj (defmacro with-time diff --git a/src/main/frontend/worker/util.cljc b/src/main/frontend/worker/util.cljc index 856cd7a428..e7cf1a2eeb 100644 --- a/src/main/frontend/worker/util.cljc +++ b/src/main/frontend/worker/util.cljc @@ -80,7 +80,8 @@ (defn post-message [type data] - (.postMessage js/self (bean/->js [type data]))))) + (when (exists? js/self) + (.postMessage js/self (bean/->js [type data])))))) ;; Copied from https://github.com/tonsky/datascript-todo #?(:clj diff --git a/src/test/frontend/modules/outliner/core_test.cljs b/src/test/frontend/modules/outliner/core_test.cljs index fd089849ca..b205cbefc3 100644 --- a/src/test/frontend/modules/outliner/core_test.cljs +++ b/src/test/frontend/modules/outliner/core_test.cljs @@ -32,9 +32,9 @@ ([id] (get-block id false)) ([id node?] - (cond-> (db/pull test-db '[*] [:block/uuid id]) + (cond->> (db/pull test-db '[*] [:block/uuid id]) node? - (partial outliner-core/block (db/get-db))))) + (outliner-core/block (db/get-db))))) (defn build-node-tree [col] @@ -105,10 +105,14 @@ (defn get-children [id] - (->> (get-block id true) - (otree/-get-children (db/get-db test-db false)) + (->> (otree/-get-children (get-block id true) (db/get-db test-db false)) (mapv #(-> % :data :block/uuid)))) +(defn- transact-opts + [] + {:transact-opts {:repo test-db + :conn (db/get-db test-db false)}}) + (deftest test-delete-block (testing " Insert a node between 6 and 9. @@ -124,7 +128,7 @@ " (transact-tree! tree) (let [block (get-block 6)] - (outliner-tx/transact! {:graph test-db} + (outliner-tx/transact! (transact-opts) (outliner-core/delete-blocks! test-db (db/get-db test-db false) (state/get-date-formatter) @@ -146,7 +150,7 @@ " (transact-tree! tree) (outliner-tx/transact! - {:graph test-db} + (transact-opts) (outliner-core/move-blocks! test-db (db/get-db test-db false) [(get-block 3)] (get-block 14) true)) @@ -169,7 +173,7 @@ " (transact-tree! tree) (outliner-tx/transact! - {:graph test-db} + (transact-opts) (outliner-core/move-blocks! test-db (db/get-db test-db false) [(get-block 3)] (get-block 12) false)) @@ -191,7 +195,7 @@ " (transact-tree! tree) (outliner-tx/transact! - {:graph test-db} + (transact-opts) (outliner-core/indent-outdent-blocks! test-db (db/get-db test-db false) [(get-block 6) (get-block 9)] true)) (is (= [4 5 6 9] (get-children 3))))) @@ -210,7 +214,7 @@ " (transact-tree! tree) (outliner-tx/transact! - {:graph test-db} + (transact-opts) (outliner-core/indent-outdent-blocks! test-db (db/get-db test-db false) [(get-block 13) (get-block 14) (get-block 15)] false)) (is (= [2 12 13 14 15 16] (get-children 22)))) (testing " @@ -227,7 +231,7 @@ " (transact-tree! tree) (outliner-tx/transact! - {:graph test-db} + (transact-opts) (outliner-core/indent-outdent-blocks! test-db (db/get-db test-db false) [(get-block 13) (get-block 14)] false)) (is (= [2 12 13 14 16] (get-children 22))))) @@ -277,7 +281,7 @@ " (transact-tree! tree) (outliner-tx/transact! - {:graph test-db} + (transact-opts) (outliner-core/indent-outdent-blocks! test-db (db/get-db test-db false) [(get-block 4) (get-block 5)] false)) (is (= [3 4 5 6 9] (get-children 2))))) @@ -296,7 +300,7 @@ " (transact-tree! tree) (outliner-tx/transact! - {:graph test-db} + (transact-opts) (outliner-core/delete-blocks! test-db (db/get-db test-db false) (state/get-date-formatter) [(get-block 6) (get-block 9)] {})) @@ -317,7 +321,7 @@ " (transact-tree! tree) (outliner-tx/transact! - {:graph test-db} + (transact-opts) (outliner-core/delete-blocks! test-db (db/get-db test-db false) (state/get-date-formatter) [(get-block 10) (get-block 13)] {})) @@ -338,7 +342,7 @@ " (transact-tree! tree) (outliner-tx/transact! - {:graph test-db} + (transact-opts) (outliner-core/move-blocks-up-down! test-db (db/get-db test-db false) [(get-block 9)] true)) (is (= [3 9 6] (get-children 2))))) @@ -361,7 +365,7 @@ [21]]) target-block (get-block 6)] (outliner-tx/transact! - {} + (transact-opts) (outliner-core/insert-blocks! test-db (db/get-db test-db false) @@ -387,7 +391,7 @@ :block/content ""}]) (let [target-block (get-block 22)] (outliner-tx/transact! - {} + (transact-opts) (outliner-core/insert-blocks! test-db (db/get-db test-db false) @@ -410,7 +414,7 @@ (let [new-blocks (build-blocks [[4 [5]]]) target-block (get-block 2)] (outliner-tx/transact! - {:graph test-db} + (transact-opts) (outliner-core/insert-blocks! test-db (db/get-db test-db false) new-blocks target-block {:sibling? false :keep-uuid? true :replace-empty-target? false}) @@ -486,7 +490,7 @@ (defn- save-block! [block] - (outliner-tx/transact! {:graph test-db} + (outliner-tx/transact! (transact-opts) (outliner-core/save-block! test-db (db/get-db test-db false) (state/get-date-formatter) block))) @@ -572,7 +576,7 @@ tags:: tag1, tag2 (defn insert-blocks! [blocks target] - (outliner-tx/transact! {:graph test-db} + (outliner-tx/transact! (transact-opts) (outliner-core/insert-blocks! test-db (db/get-db test-db false) blocks @@ -638,7 +642,7 @@ tags:: tag1, tag2 (insert-blocks! (gen-blocks) (get-random-block)) (let [blocks (get-random-successive-blocks)] (when (seq blocks) - (outliner-tx/transact! {:graph test-db} + (outliner-tx/transact! (transact-opts) (outliner-core/delete-blocks! test-db (db/get-db test-db false) (state/get-date-formatter) blocks {}))))))) @@ -657,7 +661,7 @@ tags:: tag1, tag2 (let [blocks (get-random-successive-blocks)] (when (seq blocks) (let [target (get-random-block)] - (outliner-tx/transact! {:graph test-db} + (outliner-tx/transact! (transact-opts) (outliner-core/move-blocks! test-db (db/get-db test-db false) blocks target (gen/generate gen/boolean))) (let [total (get-blocks-count)] (is (= total (count @*random-blocks))))))))))) @@ -675,7 +679,7 @@ tags:: tag1, tag2 (insert-blocks! blocks (get-random-block))) (let [blocks (get-random-successive-blocks)] (when (seq blocks) - (outliner-tx/transact! {:graph test-db} + (outliner-tx/transact! (transact-opts) (outliner-core/move-blocks-up-down! test-db (db/get-db test-db false) blocks (gen/generate gen/boolean))) (let [total (get-blocks-count)] (is (= total (count @*random-blocks)))))))))) @@ -694,7 +698,7 @@ tags:: tag1, tag2 (let [blocks (get-random-successive-blocks) indent? (gen/generate gen/boolean)] (when (seq blocks) - (outliner-tx/transact! {:graph test-db} + (outliner-tx/transact! (transact-opts) (outliner-core/indent-outdent-blocks! test-db (db/get-db test-db false) blocks indent?)) (let [total (get-blocks-count)] (is (= total (count @*random-blocks))))))))))) @@ -717,7 +721,7 @@ tags:: tag1, tag2 (when (seq blocks) (swap! *random-blocks (fn [old] (set/difference old (set (map :block/uuid blocks))))) - (outliner-tx/transact! {:graph test-db} + (outliner-tx/transact! (transact-opts) (outliner-core/delete-blocks! test-db (db/get-db test-db false) (state/get-date-formatter) blocks {}))))) @@ -726,7 +730,7 @@ tags:: tag1, tag2 (fn [] (let [blocks (get-random-successive-blocks)] (when (seq blocks) - (outliner-tx/transact! {:graph test-db} + (outliner-tx/transact! (transact-opts) (outliner-core/move-blocks! test-db (db/get-db test-db false) blocks (get-random-block) (gen/generate gen/boolean)))))) @@ -735,14 +739,14 @@ tags:: tag1, tag2 (fn [] (let [blocks (get-random-successive-blocks)] (when (seq blocks) - (outliner-tx/transact! {:graph test-db} + (outliner-tx/transact! (transact-opts) (outliner-core/move-blocks-up-down! test-db (db/get-db test-db false) blocks (gen/generate gen/boolean)))))) ;; indent outdent (fn [] (let [blocks (get-random-successive-blocks)] (when (seq blocks) - (outliner-tx/transact! {:graph test-db} + (outliner-tx/transact! (transact-opts) (outliner-core/indent-outdent-blocks! test-db (db/get-db test-db false) blocks (gen/generate gen/boolean))))))]] (dotimes [_i 100] ((rand-nth ops)))