diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers.cljs index f7c713f5e5..66e2528ea5 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers.cljs @@ -7,61 +7,6 @@ [clojure.string :as s] [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 ([name handler] (reg-handler name nil handler)) ([name middleware handler] @@ -88,7 +33,7 @@ (dispatch [::parse-commands! identity file]) (dispatch [::loading-failed! identity ::wrong-hash]))) -(defn get-hash-by-dentity +(defn get-hash-by-identity [db identity] (get-in db [:chats identity :dapp-hash])) @@ -115,22 +60,21 @@ (defn validate-hash [db [identity file]] - (let [valid? (= (get-hash-by-dentity db identity) + (let [valid? (= (get-hash-by-identity db identity) (get-hash-by-file file))] (assoc db ::valid-hash valid?))) (defn add-commands - [db [id _ commands-obj]] - (let [commands (:commands commands-obj)] - (-> db - (update-in [:chats id :commands] merge commands) - (assoc db ::new-commands commands)))) + [db [id _ {:keys [commands responses]}]] + (-> db + (update-in [:chats id :commands] merge commands) + (update-in [:chats id :responses] merge responses))) (defn save-commands-js! [_ [id file]] (realm/create-object :commands {:chat-id id :file file})) -(defn loading-failed +(defn loading-failed! [db [id reason details]] (let [url (get-in db [:chats id :dapp-url])] (toast (s/join "\n" ["commands.js loading failed" @@ -152,5 +96,5 @@ (after save-commands-js!) add-commands) -(reg-handler ::loading-failed! (u/side-effect! loading-failed)) +(reg-handler ::loading-failed! (u/side-effect! loading-failed!)) diff --git a/test/cljs/status_im/test/commands/handlers.cljs b/test/cljs/status_im/test/commands/handlers.cljs new file mode 100644 index 0000000000..92225b5f9e --- /dev/null +++ b/test/cljs/status_im/test/commands/handlers.cljs @@ -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]))))) diff --git a/test/cljs/status_im/test/runner.cljs b/test/cljs/status_im/test/runner.cljs index 14bda7de00..fef2b15ecf 100644 --- a/test/cljs/status_im/test/runner.cljs +++ b/test/cljs/status_im/test/runner.cljs @@ -1,5 +1,7 @@ (ns status-im.test.runner (: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)