mirror of
https://github.com/status-im/status-mobile.git
synced 2025-02-26 07:15:53 +00:00
tests
Former-commit-id: 84747296959ca9de8185c026967d61d12e60cace
This commit is contained in:
parent
6a9d1af886
commit
75649d46aa
@ -7,61 +7,6 @@
|
|||||||
[clojure.string :as s]
|
[clojure.string :as s]
|
||||||
[status-im.persistence.realm :as realm]))
|
[status-im.persistence.realm :as realm]))
|
||||||
|
|
||||||
;; commands loading flow
|
|
||||||
; ┌────────────────────────────┐
|
|
||||||
; │ user starts chat with dapp │
|
|
||||||
; └────────────────────────────┘
|
|
||||||
; │
|
|
||||||
; ▼
|
|
||||||
; ┌───────────────────────┐
|
|
||||||
; │ fetch current │
|
|
||||||
; │ `commands.js` hash │
|
|
||||||
; └───────────────────────┘
|
|
||||||
; │
|
|
||||||
; ▼
|
|
||||||
; ┌───────────────────────────┐
|
|
||||||
; │try to fetch `commands.js` │
|
|
||||||
; └───────────────────────────┘
|
|
||||||
; │
|
|
||||||
; ▼
|
|
||||||
;┌───┐ ┌─────────────────────────┐ ┌───┐
|
|
||||||
;│no ├────────│ there is `commands.js` │────┤yes│
|
|
||||||
;└─┬─┘ └─────────────────────────┘ └─┬─┘
|
|
||||||
; │ │
|
|
||||||
; │ ▼
|
|
||||||
; │ ┌───────────────────┐
|
|
||||||
; │ ┌────┐ │ Is file's hash │
|
|
||||||
; │ │nope├─────────────────│ equal to current? │
|
|
||||||
; │ └─┬──┘ └───────────────────┘
|
|
||||||
; │ │ │
|
|
||||||
; │ │ ┌─┴─┐
|
|
||||||
; │ │ │yes│
|
|
||||||
; │ │ └─┬─┘
|
|
||||||
; │ │ │
|
|
||||||
; │ │ ▼
|
|
||||||
; │ │ ┌────┐ ┌──────────────────────────┐ ┌────┐
|
|
||||||
; │ │ │fail├──│ ask `otto` to handle js │──┤succ│
|
|
||||||
; │ │ └──┬─┘ └──────────────────────────┘ └─┬──┘
|
|
||||||
; │ │ │ │
|
|
||||||
; │ │ │ ▼
|
|
||||||
; │ │ │ ┌────────────────────────────┐
|
|
||||||
; │ │ ▼ │ │
|
|
||||||
; │ │ ┌────────────────────────┐ │ save commands-js │
|
|
||||||
; │ │ │the dapp emit a message │ │ add some API object form │
|
|
||||||
; │ └─▶│ saying js is broken │ │ otto to app-db │
|
|
||||||
; │ └────────────────────────┘ └────────────────────────────┘
|
|
||||||
; │ │ │
|
|
||||||
; │ │ │
|
|
||||||
; │ ┌───────────────────┘ ▼
|
|
||||||
; │ │ ┌─────────────────────────┐
|
|
||||||
; │ │ │ if it is necessary show │
|
|
||||||
; │ ▼ │ as fullscreen │
|
|
||||||
; │ ┌───────────┐ └─────────────────────────┘
|
|
||||||
; │ │ │ │
|
|
||||||
; └─▶│ Fin' │◀──────────────────────────────────────────┘
|
|
||||||
; │ │
|
|
||||||
; └───────────┘
|
|
||||||
|
|
||||||
(defn reg-handler
|
(defn reg-handler
|
||||||
([name handler] (reg-handler name nil handler))
|
([name handler] (reg-handler name nil handler))
|
||||||
([name middleware handler]
|
([name middleware handler]
|
||||||
@ -88,7 +33,7 @@
|
|||||||
(dispatch [::parse-commands! identity file])
|
(dispatch [::parse-commands! identity file])
|
||||||
(dispatch [::loading-failed! identity ::wrong-hash])))
|
(dispatch [::loading-failed! identity ::wrong-hash])))
|
||||||
|
|
||||||
(defn get-hash-by-dentity
|
(defn get-hash-by-identity
|
||||||
[db identity]
|
[db identity]
|
||||||
(get-in db [:chats identity :dapp-hash]))
|
(get-in db [:chats identity :dapp-hash]))
|
||||||
|
|
||||||
@ -115,22 +60,21 @@
|
|||||||
|
|
||||||
(defn validate-hash
|
(defn validate-hash
|
||||||
[db [identity file]]
|
[db [identity file]]
|
||||||
(let [valid? (= (get-hash-by-dentity db identity)
|
(let [valid? (= (get-hash-by-identity db identity)
|
||||||
(get-hash-by-file file))]
|
(get-hash-by-file file))]
|
||||||
(assoc db ::valid-hash valid?)))
|
(assoc db ::valid-hash valid?)))
|
||||||
|
|
||||||
(defn add-commands
|
(defn add-commands
|
||||||
[db [id _ commands-obj]]
|
[db [id _ {:keys [commands responses]}]]
|
||||||
(let [commands (:commands commands-obj)]
|
|
||||||
(-> db
|
(-> db
|
||||||
(update-in [:chats id :commands] merge commands)
|
(update-in [:chats id :commands] merge commands)
|
||||||
(assoc db ::new-commands commands))))
|
(update-in [:chats id :responses] merge responses)))
|
||||||
|
|
||||||
(defn save-commands-js!
|
(defn save-commands-js!
|
||||||
[_ [id file]]
|
[_ [id file]]
|
||||||
(realm/create-object :commands {:chat-id id :file file}))
|
(realm/create-object :commands {:chat-id id :file file}))
|
||||||
|
|
||||||
(defn loading-failed
|
(defn loading-failed!
|
||||||
[db [id reason details]]
|
[db [id reason details]]
|
||||||
(let [url (get-in db [:chats id :dapp-url])]
|
(let [url (get-in db [:chats id :dapp-url])]
|
||||||
(toast (s/join "\n" ["commands.js loading failed"
|
(toast (s/join "\n" ["commands.js loading failed"
|
||||||
@ -152,5 +96,5 @@
|
|||||||
(after save-commands-js!)
|
(after save-commands-js!)
|
||||||
add-commands)
|
add-commands)
|
||||||
|
|
||||||
(reg-handler ::loading-failed! (u/side-effect! loading-failed))
|
(reg-handler ::loading-failed! (u/side-effect! loading-failed!))
|
||||||
|
|
||||||
|
20
test/cljs/status_im/test/commands/handlers.cljs
Normal file
20
test/cljs/status_im/test/commands/handlers.cljs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
(ns status-im.test.commands.handlers
|
||||||
|
(:require [cljs.test :refer-macros [deftest is]]
|
||||||
|
[status-im.commands.handlers :as h]))
|
||||||
|
|
||||||
|
(deftest test-validate-hash
|
||||||
|
(let [file "some-js"
|
||||||
|
db (-> {}
|
||||||
|
(assoc-in [:chats :user :dapp-hash] -731917028)
|
||||||
|
(assoc-in [:chats :user2 :dapp-hash] 123))]
|
||||||
|
(is (::h/valid-hash (h/validate-hash db [:user file])))
|
||||||
|
(is (not (::h/valid-hash (h/validate-hash db [:user2 file]))))
|
||||||
|
(is (not (::h/valid-hash (h/validate-hash db [:user3 file]))))))
|
||||||
|
|
||||||
|
(deftest test-add-commands
|
||||||
|
(let [obj {:commands {:test {:name "name"
|
||||||
|
:description "desc"}}
|
||||||
|
:responses {:test-r {:name "r"
|
||||||
|
:description "desc-r"}}}
|
||||||
|
db (h/add-commands {} [:user nil obj])]
|
||||||
|
(is (= obj (select-keys (get-in db [:chats :user]) [:commands :responses])))))
|
@ -1,5 +1,7 @@
|
|||||||
(ns status-im.test.runner
|
(ns status-im.test.runner
|
||||||
(:require [doo.runner :refer-macros [doo-tests]]
|
(:require [doo.runner :refer-macros [doo-tests]]
|
||||||
[status-im.test.handlers]))
|
[status-im.test.handlers]
|
||||||
|
[status-im.test.commands.handlers]))
|
||||||
|
|
||||||
(doo-tests 'status-im.test.handlers)
|
(doo-tests 'status-im.test.handlers
|
||||||
|
'status-im.test.commands.handlers)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user