Explorar el Código

test(rtc): add rtc-e2e-test/download_upload_test

rcmerci hace 1 año
padre
commit
3eecf04533

+ 8 - 1
shadow-cljs.edn

@@ -3,7 +3,8 @@
  :nrepl {:port 8701}
 
  ;; "." for /static
- :dev-http {3001 ["static" "."]}
+ :dev-http {3001 ["static" "."]
+            8021 "static/rtc-e2e-test"}
 
  :js-options {:js-package-dirs ["node_modules" "packages/tldraw/apps"]}
 
@@ -88,6 +89,12 @@
          :compiler-options {:static-fns false}
          :main frontend.test.frontend-node-test-runner/main}
 
+  :rtc-e2e-test {:target :browser-test
+                 :test-dir "static/rtc-e2e-test"
+                 :closure-defines {frontend.worker.rtc.const/RTC-E2E-TEST true}
+                 :devtools {:enabled false}
+                 :compiler-options {:static-fns false}}
+
   :gen-malli-kondo-config {:target :node-script
                            :closure-defines {frontend.util/NODETEST true}
                            :devtools {:enabled false}

+ 2 - 0
src/main/frontend/worker/rtc/const.cljs

@@ -5,6 +5,8 @@
             [malli.transform :as mt]
             [malli.util :as mu]))
 
+(goog-define RTC-E2E-TEST false)
+
 (def block-pos-schema
   [:catn
    [:parent-uuid [:maybe :uuid]]

+ 1 - 1
src/main/frontend/worker/rtc/exception.cljs

@@ -1,6 +1,6 @@
 (ns frontend.worker.rtc.exception
   "Exception list"
-  (:require [frontend.common.schema-register :as sr]))
+  (:require [frontend.common.schema-register :include-macros true :as sr]))
 
 (sr/defkeyword :rtc.exception/remote-graph-not-exist
   "Remote exception. e.g. push client-updates to a deleted graph.")

+ 8 - 4
src/main/frontend/worker/rtc/full_upload_download_graph.cljs

@@ -6,6 +6,7 @@
             [datascript.core :as d]
             [frontend.common.missionary-util :as c.m]
             [frontend.worker.rtc.client-op :as client-op]
+            [frontend.worker.rtc.const :as rtc-const]
             [frontend.worker.rtc.log-and-state :as rtc-log-and-state]
             [frontend.worker.rtc.ws-util :as ws-util]
             [frontend.worker.state :as worker-state]
@@ -129,15 +130,17 @@
                                                         :s3-key key
                                                         :graph-name remote-graph-name}))]
         (if-let [graph-uuid (:graph-uuid upload-resp)]
-          (let [^js worker-obj (:worker/object @worker-state/*state)]
+          (do
             (ldb/transact! conn
                            [{:db/ident :logseq.kv/graph-uuid :kv/value graph-uuid}
                             {:db/ident :logseq.kv/graph-local-tx :kv/value "0"}])
-            (m/? (c.m/await-promise (.storeMetadata worker-obj repo (pr-str {:kv/value graph-uuid}))))
             (client-op/update-graph-uuid repo graph-uuid)
+            (when-not rtc-const/RTC-E2E-TEST
+              (let [^js worker-obj (:worker/object @worker-state/*state)]
+                (m/? (c.m/await-promise (.storeMetadata worker-obj repo (pr-str {:kv/value graph-uuid}))))))
             (rtc-log-and-state/rtc-log :rtc.log/upload {:sub-type :upload-completed
                                                         :message "upload-graph completed"})
-            nil)
+            {:graph-uuid graph-uuid})
           (throw (ex-info "upload-graph failed" {:upload-resp upload-resp})))))))
 
 (def page-of-block
@@ -322,7 +325,8 @@
           (let [all-blocks (ldb/read-transit-str body)]
             (m/? (new-task--transact-remote-all-blocks all-blocks repo graph-uuid))
             (client-op/update-graph-uuid repo graph-uuid)
-            (m/? (c.m/await-promise (.storeMetadata worker-obj repo (pr-str {:kv/value graph-uuid}))))
+            (when-not rtc-const/RTC-E2E-TEST
+              (m/? (c.m/await-promise (.storeMetadata worker-obj repo (pr-str {:kv/value graph-uuid})))))
             (rtc-log-and-state/rtc-log :rtc.log/download {:sub-type :download-completed
                                                           :message "download completed"
                                                           :graph-uuid graph-uuid})

+ 5 - 0
src/rtc_e2e_test/const.cljs

@@ -0,0 +1,5 @@
+(ns const
+  "Consts for rtc e2e tests")
+
+(def test-token "TEST-TOKEN")
+(def test-repo "TEST-REPO")

+ 20 - 1
src/rtc_e2e_test/download_upload_test.cljs

@@ -1,2 +1,21 @@
 (ns download-upload-test
-  "RTC e2e tests for download-graph and upload-graph")
+  "RTC e2e tests for download-graph and upload-graph"
+  (:require [cljs.test :as t :refer [deftest]]
+            [const]
+            [example]
+            [fixture]
+            [frontend.common.missionary-util :as c.m]
+            [helper]
+            [missionary.core :as m]))
+
+(t/use-fixtures :each fixture/install-example-db-fixture)
+
+(deftest upload-graph-test
+  (t/async
+   done
+   (c.m/run-task
+    (m/sp
+      (let [{:keys [graph-uuid]} (m/? (helper/new-task--upload-example-graph))]
+        (m/? (helper/new-task--wait-creating-graph graph-uuid)))
+      (done))
+    :upload-graph-test)))

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 4 - 0
src/rtc_e2e_test/example.cljs


+ 18 - 0
src/rtc_e2e_test/fixture.cljs

@@ -0,0 +1,18 @@
+(ns fixture
+  (:require [const]
+            [datascript.core :as d]
+            [example]
+            [frontend.worker.rtc.client-op :as client-op]
+            [frontend.worker.state :as worker-state]))
+
+(def install-example-db-fixture
+  {:before
+   (fn []
+     (reset! worker-state/*rtc-ws-url "wss://ws-dev.logseq.com/rtc-sync?token=%s")
+     (swap! worker-state/*client-ops-conns assoc const/test-repo (d/create-conn client-op/schema-in-db))
+     (let [conn (d/conn-from-db example/example-db)]
+       (swap! worker-state/*datascript-conns assoc const/test-repo conn)))
+   :after
+   (fn []
+     (swap! worker-state/*datascript-conns dissoc const/test-repo)
+     (swap! worker-state/*client-ops-conns dissoc const/test-repo))})

+ 44 - 0
src/rtc_e2e_test/helper.clj

@@ -0,0 +1,44 @@
+(ns helper)
+
+;; Copied from https://clojurians.slack.com/archives/C03S1L9DN/p1631221014001900?thread_ts=1631219838.001500&cid=C03S1L9DN
+(defmacro with-reset
+  "Like cljs.core/with-redefs, but bindings persist until the `reset` fn is
+ called, allowing bindings to be used in async contexts."
+  [reset bindings & body]
+  ;; code adapted from https://cljs.github.io/api/cljs.core/with-redefs
+  (let [names (take-nth 2 bindings)
+        vals (take-nth 2 (drop 1 bindings))
+        orig-val-syms (map (comp gensym #(str % "-orig-val__") name) names)
+        temp-val-syms (map (comp gensym #(str % "-temp-val__") name) names)
+        binds (map vector names temp-val-syms)
+        redefs (reverse (map vector names orig-val-syms))
+        bind-value (fn [[k v]] (list 'set! k v))]
+    `(let [~@(interleave orig-val-syms names)
+           ~@(interleave temp-val-syms vals)
+           ~reset #(do ~@(map bind-value redefs))]
+       ~@(map bind-value binds)
+       ~@body)))
+
+;; Copied from https://github.com/babashka/nbb/blob/e5d84b0fac59774f5d7a4a9e807240cce04bf252/test/nbb/test_macros.clj
+(defmacro deftest-async
+  "A wrapper around deftest that handles async and done in all cases.
+  Importantly, it prevents unexpected failures in an async test from abruptly
+  ending a test suite"
+  [name opts & body]
+  (let [[opts body]
+        (if (map? opts)
+          [opts body]
+          [nil (cons opts body)])]
+    `(cljs.test/deftest ~name
+       ~@(when-let [pre (:before opts)]
+           [pre])
+       (cljs.test/async
+        ~'done
+        (-> (do ~@body)
+            (.catch (fn [err#]
+                      (cljs.test/is (= 1 0) (str err# (.-stack err#)))))
+            (.finally
+             (fn []
+               ~@(when-let [post (:after opts)]
+                   [post])
+               (~'done))))))))

+ 22 - 0
src/rtc_e2e_test/helper.cljs

@@ -0,0 +1,22 @@
+(ns helper
+  (:require [const]
+            [frontend.common.missionary-util :as c.m]
+            [frontend.worker.rtc.core :as rtc.core]
+            [missionary.core :as m]))
+
+(defn new-task--upload-example-graph
+  []
+  (rtc.core/new-task--upload-graph const/test-token const/test-repo const/test-repo))
+
+(defn new-task--wait-creating-graph
+  [graph-uuid]
+  (c.m/backoff
+   (take 4 c.m/delays)
+   (m/sp
+     (let [graphs (m/? (rtc.core/new-task--get-graphs const/test-token))
+           graph (some (fn [graph] (when (= graph-uuid (:graph-uuid graph)) graph)) graphs)]
+       (when-not graph
+         (throw (ex-info "graph not exist" {:graph-uuid graph-uuid})))
+       (prn "waiting for graph " graph-uuid " finish creating")
+       (when (= "creating" (:graph-status graph))
+         (throw (ex-info "wait creating-graph" {:missionary/retry true})))))))

Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio