client_steps.cljs 10 KB

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