enhance(rtc): add testcase

This commit is contained in:
rcmerci
2023-11-01 18:12:12 +08:00
parent a2cba0bacb
commit 536852b103
3 changed files with 45 additions and 29 deletions

View File

@@ -10,6 +10,8 @@
[frontend.db.rtc.ops-idb-store :as ops-idb-store]))
(def *test-rtc-state (atom nil))
(def test-graph-uuid "e6d04ed7-bbc4-4ed2-a91b-69f3c0b9459d")
(def test-graph-init-local-t 1)
(defn- init-state-helper
[]

View File

@@ -4,7 +4,7 @@
[spy.core :as spy]))
;;; websocket
(defrecord Mock-WebSocket [onopen onmessage onclose onerror readyState push-data-chan ^:mutable push-data-fn]
(defrecord Mock-WebSocket [onopen onmessage onclose onerror readyState push-data-to-client-chan ^:mutable handler-fn]
Object
(close [_]
(prn :mock-ws :closed)
@@ -14,16 +14,16 @@
js/JSON.parse
(js->clj :keywordize-keys true)
rtc-const/data-to-ws-decoder)]
(async/put! onmessage msg)))
(handler-fn msg push-data-to-client-chan)))
(set-push-data-fn [_ f]
(set! push-data-fn f)))
(set-handler-fn [_ f]
(set! handler-fn f)))
(defn default-push-data-fn
[msg push-data-chan]
(defn default-handler
[msg push-data-to-client-chan]
(case (:action msg)
"register-graph-updates"
(async/offer! push-data-chan (select-keys msg [:req-id]))
(async/offer! push-data-to-client-chan (select-keys msg [:req-id]))
;; default
nil))
@@ -31,27 +31,12 @@
(defn mock-websocket
[data-from-ws-chan]
(let [stop-push-data-loop-ch (async/chan)
ws (->Mock-WebSocket nil (async/chan 10) nil nil 1
data-from-ws-chan (spy/spy default-push-data-fn))]
(async/go-loop []
(let [{:keys [stop msg]}
(async/alt!
stop-push-data-loop-ch {:stop true}
(.-onmessage ws) ([msg] {:msg msg}))]
(cond
(or stop (nil? msg))
(do (prn :mock-ws-loop-stop) nil)
msg
(do (when-let [push-data-fn (:push-data-fn ws)]
(push-data-fn msg (:push-data-chan ws)))
(recur)))))
ws))
(->Mock-WebSocket nil (async/chan 10) nil nil 1
data-from-ws-chan (spy/spy default-handler)))
;; (defn mock-ws-push-data-fn
;; (defn set-ws-handler-fn
;; [ws f]
;; (.set-push-data-fn ws f))
;; (.set-handler-fn ws f))
;;; websocket ends ;;;;

View File

@@ -3,7 +3,7 @@
These tests need to start the rtc-loop.
Other simple fn tests are located at `frontend.db.rtc.rtc-fns-test`"
(:require ["/frontend/idbkv" :as idb-keyval]
[cljs.core.async :as async :refer [<! go timeout]]
[cljs.core.async :as async :refer [<! go timeout >!]]
[clojure.test :as t :refer [deftest is use-fixtures]]
[datascript.core :as d]
[frontend.db.conn :as conn]
@@ -30,8 +30,8 @@
(deftest rtc-loop-init-test
(let [ws @(:*ws @rtc-fixture/*test-rtc-state)
push-data-fn (:push-data-fn ws)
last-ws-msg (first (spy/last-call push-data-fn))]
handler-fn (:handler-fn ws)
last-ws-msg (first (spy/last-call handler-fn))]
(is (= "register-graph-updates" (:action last-ws-msg)))))
@@ -80,3 +80,32 @@
(is (= ["update" {:block-uuid (str block-uuid2) :updated-attrs {:content nil}}] update-op-2)))))
(reset)
(done)))))
(deftest push-data-from-ws-test-1
(t/async
done
(idb-keyval-mock/with-reset-idb-keyval-mock reset
(go
(let [conn (conn/get-db test-helper/test-db false)
ws @(:*ws @rtc-fixture/*test-rtc-state)
push-data-to-client-chan (:push-data-to-client-chan ws)]
;; set local-t & graph-uuid in mock-indexeddb-store
(<! (rtc-op/<update-local-tx! test-helper/test-db rtc-fixture/test-graph-init-local-t))
(<! (rtc-op/<update-graph-uuid! test-helper/test-db rtc-fixture/test-graph-uuid))
(>! push-data-to-client-chan {:req-id "push-updates"
:t 2 :t-before 1
:affected-blocks
{"26c4b513-e251-4ce9-a421-364b774eb736"
{:op :update-page
:self "26c4b513-e251-4ce9-a421-364b774eb736"
:page-name "push-data-from-ws-test-1"
:original-name "Push-Data-From-Ws-Test-1"}}})
(<! (timeout 500))
(is (= {:block/uuid #uuid "26c4b513-e251-4ce9-a421-364b774eb736"
:block/original-name "Push-Data-From-Ws-Test-1"}
(select-keys (d/pull @conn '[*] [:block/name "push-data-from-ws-test-1"])
[:block/uuid :block/original-name])))
(reset)
(done))))))