client_steps.cljs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. (ns client-steps
  2. (:require [cljs.test :as t :refer [is]]
  3. [const]
  4. [datascript.core :as d]
  5. [frontend.common.missionary-util :as c.m]
  6. [frontend.worker.rtc.client-op :as client-op]
  7. [frontend.worker.rtc.core :as rtc-core]
  8. [frontend.worker.rtc.log-and-state :as rtc-log-and-state]
  9. [helper]
  10. [logseq.db :as ldb]
  11. [logseq.outliner.batch-tx :as batch-tx]
  12. [missionary.core :as m]))
  13. (def ^:private step0
  14. {:client1
  15. (m/sp
  16. (let [conn (helper/get-downloaded-test-conn)
  17. tx-data (const/tx-data-map :create-page)]
  18. (batch-tx/with-batch-tx-mode conn {:e2e-test const/downloaded-test-repo :skip-store-conn true}
  19. (d/transact! conn tx-data))
  20. (is (=
  21. #{[:update-page const/page1-uuid]
  22. [:update const/page1-uuid
  23. [[:block/title "[\"~#'\",\"basic-edits-test\"]" true]
  24. [:block/created-at "[\"~#'\",1724836490809]" true]
  25. [:block/updated-at "[\"~#'\",1724836490809]" true]
  26. [:block/type "[\"~#'\",\"page\"]" true]]]
  27. [:move const/block1-uuid]
  28. [:update const/block1-uuid
  29. [[:block/updated-at "[\"~#'\",1724836490810]" true]
  30. [:block/created-at "[\"~#'\",1724836490810]" true]
  31. [:block/title "[\"~#'\",\"block1\"]" true]]]}
  32. (set (map helper/simplify-client-op (client-op/get-all-ops const/downloaded-test-repo)))))))
  33. :client2 nil})
  34. (def ^:private step1
  35. "client1: start rtc, wait page1, client1->remote
  36. client2: start rtc, wait page1, remote->client2"
  37. {:client1
  38. (m/sp
  39. (let [r (m/? (rtc-core/new-task--rtc-start const/downloaded-test-repo const/test-token))]
  40. (is (nil? r))
  41. (let [r (m/? (m/timeout
  42. (m/reduce (fn [_ v]
  43. (when (and (= :rtc.log/push-local-update (:type v))
  44. (empty? (client-op/get-all-ops const/downloaded-test-repo)))
  45. (is (nil? (:ex-data v)))
  46. (reduced v)))
  47. rtc-log-and-state/rtc-log-flow)
  48. 6000 :timeout))]
  49. (is (not= :timeout r)))))
  50. :client2
  51. (m/sp
  52. (let [r (m/? (rtc-core/new-task--rtc-start const/downloaded-test-repo const/test-token))]
  53. (is (nil? r)))
  54. (m/?
  55. (c.m/backoff
  56. (take 4 c.m/delays)
  57. (m/sp
  58. (let [conn (helper/get-downloaded-test-conn)
  59. page1 (d/pull @conn '[*] [:block/uuid const/page1-uuid])
  60. block1 (d/pull @conn '[*] [:block/uuid const/block1-uuid])]
  61. (when-not (:block/uuid page1)
  62. (throw (ex-info "wait page1 synced" {:missionary/retry true})))
  63. (is
  64. (= {:block/title "basic-edits-test"
  65. :block/name "basic-edits-test"
  66. :block/type "page"}
  67. (select-keys page1 [:block/title :block/name :block/type])))
  68. (is
  69. (= {:block/title "block1"
  70. :block/order "a0"
  71. :block/parent {:db/id (:db/id page1)}}
  72. (select-keys block1 [:block/title :block/order :block/parent]))))))))})
  73. (def ^:private step2
  74. "client1: insert 500 blocks, wait for changes to sync to remote
  75. client2: wait for blocks to sync from remote"
  76. {:client1
  77. (m/sp
  78. (let [conn (helper/get-downloaded-test-conn)]
  79. (batch-tx/with-batch-tx-mode conn {:e2e-test const/downloaded-test-repo :skip-store-conn true}
  80. (d/transact! conn (const/tx-data-map :insert-500-blocks)))
  81. (let [r (m/? (m/timeout
  82. (m/reduce (fn [_ v]
  83. (when (and (= :rtc.log/push-local-update (:type v))
  84. (empty? (client-op/get-all-ops const/downloaded-test-repo)))
  85. (is (nil? (:ex-data v)))
  86. (reduced v)))
  87. rtc-log-and-state/rtc-log-flow)
  88. 10000 :timeout))]
  89. (is (not= :timeout r)))))
  90. :client2
  91. (c.m/backoff
  92. (take 4 c.m/delays)
  93. (m/sp
  94. (let [conn (helper/get-downloaded-test-conn)
  95. page (d/pull @conn '[*] [:block/uuid const/page2-uuid])]
  96. (when-not (:block/uuid page)
  97. (throw (ex-info "wait page to be synced" {:missionary/retry true})))
  98. (let [blocks (ldb/sort-by-order (ldb/get-page-blocks @conn (:db/id page)))]
  99. (is (= 500 (count blocks)))
  100. (is (= (map #(str "x" %) (range 500))
  101. (map :block/title blocks)))))))})
  102. (def ^:private step3
  103. "client1:
  104. 1. add #task properties to block1 (`const/block1-uuid`)
  105. 2. wait to be synced
  106. client2:
  107. 1. wait the block&its properties to be synced"
  108. {:client1
  109. (m/sp
  110. (let [conn (helper/get-downloaded-test-conn)
  111. tx-data (const/tx-data-map :add-task-properties-to-block1)]
  112. (batch-tx/with-batch-tx-mode conn {:e2e-test const/downloaded-test-repo :skip-store-conn true}
  113. (d/transact! conn tx-data))
  114. (let [r (m/? (m/timeout
  115. (m/reduce (fn [_ v]
  116. (when (and (= :rtc.log/push-local-update (:type v))
  117. (empty? (client-op/get-all-ops const/downloaded-test-repo)))
  118. (is (nil? (:ex-data v)))
  119. (reduced v)))
  120. rtc-log-and-state/rtc-log-flow)
  121. 10000 :timeout))]
  122. (is (not= :timeout r)))))
  123. :client2
  124. (m/sp
  125. (let [conn (helper/get-downloaded-test-conn)
  126. block1 (d/pull @conn
  127. [{:block/tags [:db/ident]}
  128. {:logseq.task/status [:db/ident]}
  129. {:logseq.task/deadline [:block/journal-day]}]
  130. [:block/uuid const/block1-uuid])]
  131. (when-not (:logseq.task/status block1)
  132. (throw (ex-info "wait block1's task properties to be synced" {:missionary/retry true})))
  133. (is (= {:block/tags [{:db/ident :logseq.class/Task}],
  134. :logseq.task/status {:db/ident :logseq.task/status.done}
  135. :logseq.task/deadline {:block/journal-day 20240907}}
  136. block1))))})
  137. ;;; TODO
  138. #_(def ^:private step4
  139. "client1:
  140. - insert some blocks in page2
  141. - wait to be synced
  142. - wait a signal from client2
  143. - send a signal to client2
  144. - stop rtc
  145. - move some blocks
  146. - start rtc
  147. - wait to be synced
  148. - wait client2's message, which contains the result of client2's block tree,
  149. and compare them with blocks in client1
  150. client2:
  151. - wait inserted blocks synced
  152. - send a signal to client1
  153. - wait a signal from client1
  154. - stop rtc
  155. - move some blocks
  156. - start rtc
  157. - wait to be synced
  158. - send a message to client1 contains client2's block tree to client1"
  159. {:client1 nil
  160. :client2 nil})
  161. (defn- wrap-print-step-info
  162. [steps client]
  163. (map-indexed
  164. (fn [idx step]
  165. (m/sp
  166. (println (str "[" client "]") "start step" idx)
  167. (some-> (get step client) m/?)
  168. (println (str "[" client "]") "end step" idx)))
  169. steps))
  170. (def ^:private all-steps [step0 step1 step2 step3])
  171. (def client1-steps
  172. (wrap-print-step-info all-steps :client1))
  173. (def client2-steps
  174. (wrap-print-step-info all-steps :client2))