test(rtc): use test-db-name-db-version

pull/11102/head
rcmerci 2024-03-06 12:36:00 +08:00
parent ddacb470b2
commit c53b65b6c1
7 changed files with 115 additions and 103 deletions

View File

@ -2,35 +2,35 @@
"Main ns for rtc related fns"
(:require-macros
[frontend.worker.rtc.macro :refer [with-sub-data-from-ws get-req-id get-result-ch]])
(:require [cljs-time.coerce :as tc]
(:require [cljs-bean.core :as bean]
[cljs-time.coerce :as tc]
[cljs-time.core :as t]
[cljs.core.async :as async :refer [<! >! chan go go-loop]]
[clojure.set :as set]
[clojure.string :as string]
[cognitect.transit :as transit]
[frontend.worker.async-util :include-macros true :refer [<?]]
[logseq.outliner.core :as outliner-core]
[logseq.outliner.transaction :as outliner-tx]
[frontend.worker.util :as worker-util]
[logseq.common.util :as common-util]
[logseq.common.config :as common-config]
[malli.core :as m]
[malli.util :as mu]
[logseq.db.frontend.property :as db-property]
[datascript.core :as d]
[logseq.graph-parser.whiteboard :as gp-whiteboard]
[frontend.worker.async-util :include-macros true :refer [<?]]
[frontend.worker.handler.page :as worker-page]
[frontend.worker.handler.page.rename :as worker-page-rename]
[frontend.worker.state :as worker-state]
[logseq.db :as ldb]
[frontend.worker.rtc.asset-sync :as asset-sync]
[frontend.worker.rtc.const :as rtc-const]
[frontend.worker.rtc.op-mem-layer :as op-mem-layer]
[frontend.worker.rtc.ws :as ws]
[frontend.worker.rtc.asset-sync :as asset-sync]
[promesa.core :as p]
[cljs-bean.core :as bean]
[frontend.worker.state :as worker-state]
[frontend.worker.util :as worker-util]
[logseq.common.config :as common-config]
[logseq.common.util :as common-util]
[logseq.common.util.page-ref :as page-ref]
[logseq.db :as ldb]
[logseq.db.frontend.content :as db-content]
[logseq.common.util.page-ref :as page-ref]))
[logseq.db.frontend.property :as db-property]
[logseq.graph-parser.whiteboard :as gp-whiteboard]
[logseq.outliner.core :as outliner-core]
[logseq.outliner.transaction :as outliner-tx]
[malli.core :as m]
[malli.util :as mu]
[promesa.core :as p]))
;; +-------------+
;; | |

View File

@ -252,13 +252,17 @@ This can be called in synchronous contexts as no async fns should be invoked"
[f & {:as start-opts}]
(start-and-destroy-db f (assoc start-opts :db-graph? true)))
(def db-based-start-and-destroy-db-map-fixture
"To avoid 'Fixtures may not be of mixed types' error
when use together with other map-type fixtures"
{:before #(start-test-db! {:db-graph? true})
:after #(destroy-test-db!)})
(def start-and-destroy-db-map-fixture
"To avoid 'Fixtures may not be of mixed types' error
when use together with other map-type fixtures"
{:before #(do (state/set-current-repo! test-db)
(start-test-db!))
:after #(do (state/set-current-repo! nil)
(destroy-test-db!))})
{:before #(start-test-db!)
:after #(destroy-test-db!)})
(defn save-block!
"Wrapper around editor-handler/save-block! that also adds tags"

View File

@ -9,7 +9,7 @@
[spy.core :as spy]))
(use-fixtures :each
test-helper/start-and-destroy-db-map-fixture
test-helper/db-based-start-and-destroy-db-map-fixture
rtc-fixture/listen-test-db-fixture
rtc-fixture/start-and-stop-asset-sync-loop-fixture
rtc-fixture/clear-op-mem-stores-fixture)

View File

@ -21,14 +21,14 @@
[]
(let [data-from-ws-chan (chan (async/sliding-buffer 100))
ws (rtc-mock/mock-websocket data-from-ws-chan)]
(assoc (rtc-core/init-state ws data-from-ws-chan test-helper/test-db "")
(assoc (rtc-core/init-state ws data-from-ws-chan test-helper/test-db-name-db-version "")
:*auto-push-client-ops? (atom false))))
(defn- init-state-helper-for-asset-sync-loop
[]
(let [data-from-ws-chan (chan (async/sliding-buffer 100))
ws (rtc-mock/mock-websocket data-from-ws-chan)
rtc-state (rtc-core/init-state ws data-from-ws-chan test-helper/test-db "")]
rtc-state (rtc-core/init-state ws data-from-ws-chan test-helper/test-db-name-db-version "")]
(assoc (asset-sync/init-state-from-rtc-state rtc-state)
:*auto-push-assets-update-ops? (atom false))))
@ -36,7 +36,7 @@
[]
(go
(let [graph-uuid test-graph-uuid
repo test-helper/test-db
repo test-helper/test-db-name-db-version
state (init-state-helper)
loop-started-ch (chan)]
(reset! *test-rtc-state state)
@ -47,7 +47,7 @@
[]
(go
(let [graph-uuid test-graph-uuid
repo test-helper/test-db
repo test-helper/test-db-name-db-version
state (init-state-helper-for-asset-sync-loop)
loop-started-ch (chan)]
(reset! *test-asset-sync-state state)
@ -89,19 +89,19 @@
(def listen-test-db-fixture
{:before
#(let [test-db-conn (conn/get-db test-helper/test-db false)]
#(let [test-db-conn (conn/get-db test-helper/test-db-name-db-version false)]
(assert (some? test-db-conn))
(d/listen! test-db-conn
::gen-ops
(fn [{:keys [tx-data tx-meta db-before db-after]}]
(when (:persist-op? tx-meta true)
(db-listener/generate-rtc-ops test-helper/test-db db-before db-after tx-data)))))
(db-listener/generate-rtc-ops test-helper/test-db-name-db-version db-before db-after tx-data)))))
:after
#(when-let [test-db-conn (conn/get-db test-helper/test-db false)]
#(when-let [test-db-conn (conn/get-db test-helper/test-db-name-db-version false)]
(d/unlisten! test-db-conn ::gen-ops))})
(def clear-op-mem-stores-fixture
{:before #(do (op-mem-layer/remove-ops-store! test-helper/test-db)
(op-mem-layer/init-empty-ops-store! test-helper/test-db))
:after #(op-mem-layer/remove-ops-store! test-helper/test-db)})
{:before #(do (op-mem-layer/remove-ops-store! test-helper/test-db-name-db-version)
(op-mem-layer/init-empty-ops-store! test-helper/test-db-name-db-version))
:after #(op-mem-layer/remove-ops-store! test-helper/test-db-name-db-version)})

View File

@ -1,32 +1,33 @@
(ns frontend.worker.rtc.local-tx-to-remote-test
(:require [clojure.test :as t :refer [deftest is testing use-fixtures]]
[frontend.test.helper :as test-helper]
[frontend.worker.rtc.fixture :as rtc-fixture]
[datascript.core :as d]
[frontend.db.conn :as conn]
[frontend.handler.page :as page-handler]
[frontend.worker.rtc.core :as rtc-core]
[frontend.state :as state]
[frontend.test.helper :as test-helper]
[frontend.worker.rtc.const :as rtc-const]
[logseq.outliner.transaction :as outliner-tx]
[logseq.outliner.core :as outliner-core]
[datascript.core :as d]
[frontend.worker.rtc.core :as rtc-core]
[frontend.worker.rtc.fixture :as rtc-fixture]
[frontend.worker.state :as worker-state]
[logseq.common.config :as common-config]))
[logseq.common.config :as common-config]
[logseq.outliner.core :as outliner-core]
[logseq.outliner.transaction :as outliner-tx]))
(use-fixtures :each
test-helper/start-and-destroy-db-map-fixture
test-helper/db-based-start-and-destroy-db-map-fixture
rtc-fixture/listen-test-db-fixture
rtc-fixture/clear-op-mem-stores-fixture)
(deftest local-db-tx->remote-ops-test
(let [conn (conn/get-db test-helper/test-db false)
repo test-helper/test-db
(let [repo (state/get-current-repo)
conn (conn/get-db repo false)
[page1-uuid
uuid1 uuid2] (repeatedly random-uuid)
page1-name (str page1-uuid)
date-formatter (common-config/get-date-formatter (worker-state/get-config test-helper/test-db))
date-formatter (common-config/get-date-formatter (worker-state/get-config repo))
opts {:persist-op? true
:transact-opts {:repo test-helper/test-db
:transact-opts {:repo repo
:conn conn}}
gen-ops-fn (fn []
(let [r (rtc-const/to-ws-ops-decoder
@ -45,7 +46,7 @@
(outliner-tx/transact!
opts
(outliner-core/insert-blocks!
test-helper/test-db
repo
conn
[{:block/uuid uuid1 :block/content "uuid1-client"
:block/left [:block/uuid page1-uuid]
@ -66,7 +67,7 @@
(outliner-tx/transact!
opts
(outliner-core/delete-blocks!
test-helper/test-db
repo
conn
date-formatter
[(d/entity @conn [:block/uuid uuid1])]

View File

@ -7,17 +7,18 @@
[clojure.test :as t :refer [deftest is use-fixtures]]
[datascript.core :as d]
[frontend.db.conn :as conn]
[frontend.handler.page :as page-handler]
[frontend.state :as state]
[frontend.test.helper :as test-helper :include-macros true]
[frontend.worker.rtc.fixture :as rtc-fixture]
[frontend.worker.rtc.idb-keyval-mock :as idb-keyval-mock :include-macros true]
[frontend.worker.rtc.op-mem-layer :as op-mem-layer]
[frontend.handler.page :as page-handler]
[logseq.outliner.core :as outliner-core]
[logseq.outliner.transaction :as outliner-tx]
[frontend.test.helper :as test-helper :include-macros true]
[spy.core :as spy]))
(use-fixtures :each
test-helper/start-and-destroy-db-map-fixture
test-helper/db-based-start-and-destroy-db-map-fixture
rtc-fixture/listen-test-db-fixture
rtc-fixture/start-and-stop-rtc-loop-fixture
rtc-fixture/clear-op-mem-stores-fixture)
@ -38,22 +39,23 @@
(deftest gen-local-ops-test--create-page
(idb-keyval-mock/with-reset-idb-keyval-mock reset
(page-handler/create! "gen-local-ops-test-1" {:redirect? false :create-first-block? false})
(is (= 1 (op-mem-layer/get-unpushed-block-update-count test-helper/test-db)))
(is (= 1 (op-mem-layer/get-unpushed-block-update-count (state/get-current-repo))))
(reset)))
(deftest gen-local-ops-test-2--create-page&insert-blocks
(idb-keyval-mock/with-reset-idb-keyval-mock reset
(let [conn (conn/get-db test-helper/test-db false)]
(let [repo (state/get-current-repo)
conn (conn/get-db repo false)]
(page-handler/create! "gen-local-ops-test-2--create-page&insert-blocks"
{:redirect? false :create-first-block? false})
(let [page-block (d/pull @conn '[*] [:block/name "gen-local-ops-test-2--create-page&insert-blocks"])
[block-uuid1 block-uuid2] (repeatedly random-uuid)]
(outliner-tx/transact!
{:transact-opts {:repo test-helper/test-db
{:transact-opts {:repo repo
:conn conn}}
(outliner-core/insert-blocks!
test-helper/test-db
repo
conn
[{:block/uuid block-uuid1 :block/content "block1"}
{:block/uuid block-uuid2 :block/content "block2"
@ -61,7 +63,7 @@
:block/parent [:block/uuid (:block/uuid page-block)]}]
page-block
{:sibling? true :keep-uuid? true}))
(let [ops (op-mem-layer/get-all-ops test-helper/test-db)
(let [ops (op-mem-layer/get-all-ops repo)
ops* (sort-by (comp :epoch second) < ops)
[update-page-op move-op-1 update-op-1 move-op-2 update-op-2]
(map (fn [op] [(first op) (dissoc (second op) :epoch)]) ops*)]
@ -78,12 +80,13 @@
done
(idb-keyval-mock/with-reset-idb-keyval-mock reset
(go
(let [conn (conn/get-db test-helper/test-db false)
(let [repo (state/get-current-repo)
conn (conn/get-db repo false)
ws @(:*ws @rtc-fixture/*test-rtc-state)
push-data-to-client-chan (:push-data-to-client-chan ws)]
;; set local-t & graph-uuid in mock-indexeddb-store
(op-mem-layer/update-local-tx! test-helper/test-db rtc-fixture/test-graph-init-local-t)
(op-mem-layer/update-graph-uuid! test-helper/test-db rtc-fixture/test-graph-uuid)
(op-mem-layer/update-local-tx! repo rtc-fixture/test-graph-init-local-t)
(op-mem-layer/update-graph-uuid! repo rtc-fixture/test-graph-uuid)
(>! push-data-to-client-chan {:req-id "push-updates"
:t 2 :t-before 1
:affected-blocks

View File

@ -2,20 +2,21 @@
(:require [clojure.test :as t :refer [deftest is testing use-fixtures]]
[datascript.core :as d]
[frontend.db.conn :as conn]
[frontend.handler.page :as page-handler]
[frontend.state :as state]
[frontend.test.helper :as test-helper]
[frontend.worker.rtc.const :as rtc-const]
[frontend.worker.rtc.core :as rtc-core]
[frontend.worker.rtc.op-mem-layer :as op-mem-layer]
[frontend.handler.page :as page-handler]
[logseq.outliner.core :as outliner-core]
[logseq.outliner.transaction :as outliner-tx]
[frontend.test.helper :as test-helper]
[logseq.common.config :as common-config]
[frontend.worker.state :as worker-state]
[frontend.worker.rtc.const :as rtc-const]
[logseq.db :as ldb]))
[logseq.common.config :as common-config]
[logseq.db :as ldb]
[logseq.outliner.core :as outliner-core]
[logseq.outliner.transaction :as outliner-tx]))
(use-fixtures :each
test-helper/start-and-destroy-db-map-fixture)
test-helper/db-based-start-and-destroy-db-map-fixture)
(deftest filter-remote-data-by-local-unpushed-ops-test
(testing "case1"
@ -73,16 +74,17 @@
(deftest gen-remote-ops-test
(let [conn (conn/get-db test-helper/test-db false)
(let [repo (state/get-current-repo)
conn (conn/get-db repo false)
[uuid1 uuid2 uuid3 uuid4] (repeatedly random-uuid)
opts {:persist-op? false
:transact-opts {:repo test-helper/test-db
:transact-opts {:repo repo
:conn conn}}]
(page-handler/create! "gen-remote-ops-test" {:redirect? false :create-first-block? false :uuid uuid1})
(outliner-tx/transact!
opts
(outliner-core/insert-blocks!
test-helper/test-db
repo
conn
[{:block/uuid uuid2 :block/content "uuid2-block"}
{:block/uuid uuid3 :block/content "uuid3-block"
@ -94,31 +96,31 @@
(d/pull @conn '[*] [:block/name "gen-remote-ops-test"])
{:sibling? true :keep-uuid? true}))
(op-mem-layer/init-empty-ops-store! test-helper/test-db)
(op-mem-layer/add-ops! test-helper/test-db [["move" {:block-uuid (str uuid2) :epoch 1}]
["move" {:block-uuid (str uuid4) :epoch 2}]
["move" {:block-uuid (str uuid3) :epoch 3}]
["update" {:block-uuid (str uuid4) :epoch 4}]])
(let [_ (op-mem-layer/new-branch! test-helper/test-db)
r1 (rtc-core/gen-block-uuid->remote-ops test-helper/test-db conn :n 1)
_ (op-mem-layer/rollback! test-helper/test-db)
r2 (rtc-core/gen-block-uuid->remote-ops test-helper/test-db conn :n 2)]
(op-mem-layer/init-empty-ops-store! repo)
(op-mem-layer/add-ops! repo [["move" {:block-uuid (str uuid2) :epoch 1}]
["move" {:block-uuid (str uuid4) :epoch 2}]
["move" {:block-uuid (str uuid3) :epoch 3}]
["update" {:block-uuid (str uuid4) :epoch 4}]])
(let [_ (op-mem-layer/new-branch! repo)
r1 (rtc-core/gen-block-uuid->remote-ops repo conn :n 1)
_ (op-mem-layer/rollback! repo)
r2 (rtc-core/gen-block-uuid->remote-ops repo conn :n 2)]
(is (= {uuid2 [:move]}
(update-vals r1 keys)))
(is (= {uuid2 [:move]
uuid3 [:move]
uuid4 [:move :update]}
(update-vals r2 keys))))
(op-mem-layer/remove-ops-store! test-helper/test-db)))
(op-mem-layer/remove-ops-store! repo)))
(deftest apply-remote-move-ops-test
(let [conn (conn/get-db test-helper/test-db false)
repo test-helper/test-db
(let [repo (state/get-current-repo)
conn (conn/get-db repo false)
opts {:persist-op? false
:transact-opts {:repo test-helper/test-db
:transact-opts {:repo repo
:conn conn}}
date-formatter (common-config/get-date-formatter (worker-state/get-config test-helper/test-db))
date-formatter (common-config/get-date-formatter (worker-state/get-config repo))
page-name "apply-remote-move-ops-test"
[page-uuid
uuid1-client uuid2-client
@ -127,7 +129,7 @@
(outliner-tx/transact!
opts
(outliner-core/insert-blocks!
test-helper/test-db
repo
conn
[{:block/uuid uuid1-client :block/content "uuid1-client"
:block/left [:block/uuid page-uuid]
@ -149,7 +151,8 @@
:content "uuid1-remote"}}}
move-ops (#'rtc-core/move-ops-map->sorted-move-ops
(:move-ops-map
(#'rtc-core/affected-blocks->diff-type-ops repo (:affected-blocks data-from-ws))))]
(#'rtc-core/affected-blocks->diff-type-ops
repo (:affected-blocks data-from-ws))))]
(is (rtc-const/data-from-ws-validator data-from-ws))
(rtc-core/apply-remote-move-ops repo conn date-formatter move-ops)
(let [page-blocks (ldb/get-page-blocks @conn page-name {})]
@ -172,7 +175,8 @@
:left uuid2-remote}}}
move-ops (#'rtc-core/move-ops-map->sorted-move-ops
(:move-ops-map
(#'rtc-core/affected-blocks->diff-type-ops repo (:affected-blocks data-from-ws))))]
(#'rtc-core/affected-blocks->diff-type-ops
repo (:affected-blocks data-from-ws))))]
(is (rtc-const/data-from-ws-validator data-from-ws))
(rtc-core/apply-remote-move-ops repo conn date-formatter move-ops)
(let [page-blocks (ldb/get-page-blocks @conn page-name {})]
@ -182,12 +186,12 @@
(deftest ^:large-vars/cleanup-todo apply-remote-update-ops-test
(let [conn (conn/get-db test-helper/test-db false)
repo test-helper/test-db
(let [repo (state/get-current-repo)
conn (conn/get-db repo false)
opts {:persist-op? false
:transact-opts {:repo test-helper/test-db
:transact-opts {:repo repo
:conn conn}}
date-formatter (common-config/get-date-formatter (worker-state/get-config test-helper/test-db))
date-formatter (common-config/get-date-formatter (worker-state/get-config repo))
page-name "apply-remote-update-ops-test"
[page-uuid
uuid1-client uuid2-client
@ -198,7 +202,7 @@
(outliner-tx/transact!
opts
(outliner-core/insert-blocks!
test-helper/test-db
repo
conn
[{:block/uuid uuid1-client :block/content "uuid1-client"
:block/left [:block/uuid page-uuid]
@ -291,12 +295,12 @@
(is (= #{tag1-uuid} (set (map :block/uuid (:block/tags (d/entity @conn [:block/uuid uuid1-remote]))))))))))
(deftest apply-remote-remove-ops-test
(let [conn (conn/get-db test-helper/test-db false)
repo test-helper/test-db
(let [repo (state/get-current-repo)
conn (conn/get-db repo false)
opts {:persist-op? false
:transact-opts {:repo test-helper/test-db
:transact-opts {:repo repo
:conn conn}}
date-formatter (common-config/get-date-formatter (worker-state/get-config test-helper/test-db))
date-formatter (common-config/get-date-formatter (worker-state/get-config repo))
page-name "apply-remote-remove-ops-test"
[page-uuid
uuid1-client uuid2-client
@ -305,7 +309,7 @@
(outliner-tx/transact!
opts
(outliner-core/insert-blocks!
test-helper/test-db
repo
conn
[{:block/uuid uuid1-client :block/content "uuid1-client"
:block/left [:block/uuid page-uuid]
@ -342,9 +346,9 @@
(deftest apply-remote-update&remove-page-ops-test
(let [conn (conn/get-db test-helper/test-db false)
repo test-helper/test-db
date-formatter (common-config/get-date-formatter (worker-state/get-config test-helper/test-db))
(let [repo (state/get-current-repo)
conn (conn/get-db repo false)
date-formatter (common-config/get-date-formatter (worker-state/get-config repo))
[page1-uuid] (repeatedly random-uuid)]
(testing "apply-remote-update-page-ops-test1"
(let [data-from-ws {:req-id "req-id" :t 1 :t-before 0
@ -388,11 +392,11 @@
(deftest same-name-two-pages-merge-test
(let [conn (conn/get-db test-helper/test-db false)
repo test-helper/test-db
date-formatter (common-config/get-date-formatter (worker-state/get-config test-helper/test-db))
(let [repo (state/get-current-repo)
conn (conn/get-db repo false)
date-formatter (common-config/get-date-formatter (worker-state/get-config repo))
opts {:persist-op? false
:transact-opts {:repo test-helper/test-db
:transact-opts {:repo repo
:conn conn}}
page-name "same-name-page-test"
[page1-uuid page2-uuid
@ -402,7 +406,7 @@
(outliner-tx/transact!
opts
(outliner-core/insert-blocks!
test-helper/test-db
repo
conn
[{:block/uuid uuid1-client :block/content "uuid1-client"
:block/left [:block/uuid page1-uuid]