Tienson Qin 1 неделя назад
Родитель
Сommit
1e51758b8d
2 измененных файлов с 77 добавлено и 9 удалено
  1. 46 9
      clj-e2e/src/logseq/e2e/graph.clj
  2. 31 0
      clj-e2e/test/logseq/e2e/graph_test.clj

+ 46 - 9
clj-e2e/src/logseq/e2e/graph.clj

@@ -15,12 +15,45 @@
   []
   (util/search-and-click "Go to all graphs"))
 
+(def ^:private e2ee-password-modal ".e2ee-password-modal-content")
+(def ^:private e2ee-new-password-input (str e2ee-password-modal " input[placeholder=\"Enter password\"]"))
+(def ^:private e2ee-new-password-confirm-input (str e2ee-password-modal " input[placeholder=\"Enter password again\"]"))
+(def ^:private e2ee-password-input (str e2ee-password-modal " .ls-toggle-password-input input"))
+(def ^:private e2ee-password-submit (str e2ee-password-modal " button:text(\"Submit\")"))
+(def ^:private cloud-ready-indicator "button.cloud.on.idle")
+
 (defn- input-e2ee-password
   []
-  (w/wait-for "input[type=\"password\"]" {:timeout 20000})
-  (w/click "input[type=\"password\"]")
-  (util/input "e2etest")
-  (w/click "button:text(\"Submit\")"))
+  (if (w/visible? e2ee-new-password-confirm-input)
+    (do
+      (w/click e2ee-new-password-input)
+      (util/input "e2etest")
+      (w/click e2ee-new-password-confirm-input)
+      (util/input "e2etest"))
+    (do
+      (w/click (.first (w/-query e2ee-password-input)))
+      (util/input "e2etest")))
+  (w/click e2ee-password-submit)
+  (w/wait-for-not-visible e2ee-password-modal))
+
+(defn- maybe-input-e2ee-password
+  []
+  ;; Password input prompt is optional for accounts with already initialized keys/password.
+  (loop [remaining-ms 20000]
+    (cond
+      (w/visible? e2ee-password-modal)
+      (input-e2ee-password)
+
+      (w/visible? cloud-ready-indicator)
+      nil
+
+      (<= remaining-ms 0)
+      nil
+
+      :else
+      (do
+        (util/wait-timeout 250)
+        (recur (- remaining-ms 250))))))
 
 (defn- new-graph-helper
   [graph-name enable-sync?]
@@ -30,10 +63,14 @@
   (util/input graph-name)
   (when enable-sync?
     (w/wait-for "button#rtc-sync" {:timeout 3000})
-    (w/click "button#rtc-sync")
-    (input-e2ee-password)
-    (w/wait-for "button.cloud.on.idle" {:timeout 20000}))
+    (w/click "button#rtc-sync"))
+
   (w/click "button:not([disabled]):text(\"Submit\")")
+
+  (when enable-sync?
+    (maybe-input-e2ee-password)
+    (w/wait-for cloud-ready-indicator {:timeout 20000}))
+
   ;; new graph can blocks the ui because the db need to be created and restored,
   ;; I have no idea why `search-and-click` failed to auto-wait sometimes.
   (util/wait-timeout 1000))
@@ -81,8 +118,8 @@
   (goto-all-graphs)
   (w/click (.last (w/-query (format "div[data-testid='logseq_db_%1$s'] span:has-text('%1$s')" to-graph-name))))
   (when wait-sync?
-    (when need-input-password? (input-e2ee-password))
-    (w/wait-for "button.cloud.on.idle" {:timeout 20000}))
+    (when need-input-password? (maybe-input-e2ee-password))
+    (w/wait-for cloud-ready-indicator {:timeout 20000}))
   (assert/assert-graph-loaded?))
 
 (defn validate-graph

+ 31 - 0
clj-e2e/test/logseq/e2e/graph_test.clj

@@ -0,0 +1,31 @@
+(ns logseq.e2e.graph-test
+  (:require [clojure.test :refer [deftest is]]
+            [logseq.e2e.graph]
+            [logseq.e2e.util :as util]
+            [wally.main :as w]))
+
+(deftest maybe-input-e2ee-password-skips-when-cloud-ready-test
+  (let [wait-calls (atom 0)
+        input-calls (atom 0)]
+    (with-redefs-fn {#'w/visible? (fn [q] (= q "button.cloud.on.idle"))
+                     #'util/wait-timeout (fn [_] (swap! wait-calls inc))
+                     #'logseq.e2e.graph/input-e2ee-password (fn [] (swap! input-calls inc))}
+      (fn []
+        ((var logseq.e2e.graph/maybe-input-e2ee-password))
+        (is (zero? @wait-calls))
+        (is (zero? @input-calls))))))
+
+(deftest maybe-input-e2ee-password-inputs-when-modal-appears-test
+  (let [ticks (atom 0)
+        input-calls (atom 0)]
+    (with-redefs-fn {#'w/visible? (fn [q]
+                                    (case q
+                                      ".e2ee-password-modal-content" (>= @ticks 2)
+                                      "button.cloud.on.idle" false
+                                      false))
+                     #'util/wait-timeout (fn [_] (swap! ticks inc))
+                     #'logseq.e2e.graph/input-e2ee-password (fn [] (swap! input-calls inc))}
+      (fn []
+        ((var logseq.e2e.graph/maybe-input-e2ee-password))
+        (is (= 2 @ticks))
+        (is (= 1 @input-calls))))))