client_steps.cljs 9.2 KB

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