Rewrite contract tests to use new promise-based API (#19208)

This commit is contained in:
Icaro Motta 2024-03-13 09:38:32 -03:00 committed by GitHub
parent dbcdb18818
commit 4fa9a27cd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 146 additions and 225 deletions

View File

@ -22,37 +22,37 @@
:multi-lhs-hang]
:fn-map
{"reg-sub" :arg1-pair
"h/describe" :arg1-body
"h/describe-skip" :arg1-body
"h/describe-only" :arg1-body
"h/test" :arg1-body
"h/test-skip" :arg1-body
"h/test-only" :arg1-body
"test/async" :arg1-body
"test/use-fixtures" :arg1-body
"global.describe" :arg1-body
"global.test" :arg1-body
"list-comp" :binding
"defview" :arg1-body
"letsubs" :binding
"with-let" "let"
"reg-event-fx" :arg1-pair
"reg-fx" :arg1-pair
"testing" :arg1-body
"deftest-sub" :arg1-body
"h/integration-test" :arg1-body
"wait-for" :arg1-body
"with-deps-check" :arg1-body
"schema/=>" :arg1-body
"->" [:noarg1-body
{:list {:constant-pair? false :force-nl? false}
:next-inner-restore [[:list :constant-pair?]]}]
"set!" "reset!"
"assoc-when" "assoc"
"assoc-some" "assoc"
"conj-when" "conj"
"conj-some" "conj"}
{"reg-sub" :arg1-pair
"h/describe" :arg1-body
"h/describe-skip" :arg1-body
"h/describe-only" :arg1-body
"h/test" :arg1-body
"h/test-skip" :arg1-body
"h/test-only" :arg1-body
"test/async" :arg1-body
"test/use-fixtures" :arg1-body
"global.describe" :arg1-body
"global.test" :arg1-body
"list-comp" :binding
"defview" :arg1-body
"letsubs" :binding
"with-let" "let"
"reg-event-fx" :arg1-pair
"reg-fx" :arg1-pair
"testing" :arg1-body
"deftest-sub" :arg1-body
"test-async" :arg1-body
"wait-for" :arg1-body
"with-deps-check" :arg1-body
"schema/=>" :arg1-body
"->" [:noarg1-body
{:list {:constant-pair? false :force-nl? false}
:next-inner-restore [[:list :constant-pair?]]}]
"set!" "reset!"
"assoc-when" "assoc"
"assoc-some" "assoc"
"conj-when" "conj"
"conj-some" "conj"}
:style-map
{:no-comma {:map {:comma? false}}

View File

@ -1,36 +0,0 @@
(ns test-helpers.integration
(:require [day8.re-frame.test :as rf-test]
[re-frame.core :as rf]))
(defmacro with-app-initialized
[& body]
`(do
(tests.test-utils/init!)
(if (test-helpers.integration/app-initialized)
(do ~@body)
(do
(rf/dispatch [:app-started])
(rf-test/wait-for [:profile/get-profiles-overview-success]
~@body)))))
(defmacro with-account
[& body]
`(if (test-helpers.integration/messenger-started)
(do ~@body)
(do
(test-helpers.integration/create-multiaccount!)
(rf-test/wait-for [:messenger-started]
(test-helpers.integration/assert-messenger-started)
~@body))))
(defmacro with-recovered-account
[& body]
`(if (test-helpers.integration/messenger-started)
(do ~@body)
(do
(test-helpers.integration/recover-multiaccount!)
(rf-test/wait-for
[:messenger-started]
(test-helpers.integration/enable-testnet!)
(test-helpers.integration/assert-messenger-started)
~@body))))

View File

@ -1,5 +1,4 @@
(ns test-helpers.integration
(:require-macros [test-helpers.integration])
(:require
[cljs.test :refer [is] :as test]
legacy.status-im.events
@ -20,14 +19,16 @@
[utils.transforms :as transforms]))
(defn validate-mnemonic
[mnemonic on-success on-error]
(native-module/validate-mnemonic
(security/safe-unmask-data mnemonic)
(fn [result]
(let [{:keys [error keyUID]} (transforms/json->clj result)]
(if (seq error)
(when on-error (on-error error))
(on-success mnemonic keyUID))))))
[mnemonic]
(p/create
(fn [p-resolve p-reject]
(native-module/validate-mnemonic
(security/safe-unmask-data mnemonic)
(fn [result]
(let [{:keys [error keyUID]} (transforms/json->clj result)]
(if (seq error)
(p-reject error)
(p-resolve [mnemonic keyUID]))))))))
(def default-re-frame-wait-for-timeout-ms
"Controls the maximum time allowed to wait for all events to be processed by
@ -153,7 +154,7 @@
(-> (wait-for [:messenger-started])
(.then #(assert-messenger-started))))))
(defn integration-test
(defn test-async
"Runs `f` inside `cljs.test/async` macro in a restorable re-frame checkpoint.
`f` will be called with one argument, the `done` function exposed by the
@ -175,10 +176,11 @@
fail more often.
"
([test-name f]
(integration-test test-name
{:fail-fast? true
:timeout-ms default-integration-test-timeout-ms}
f))
(test-async
test-name
{:fail-fast? true
:timeout-ms default-integration-test-timeout-ms}
f))
([test-name {:keys [fail-fast? timeout-ms]} f]
(test/async
done
@ -194,6 +196,29 @@
(restore-fn)
(done))))))))
(defn test-app-initialization
[]
(test-async ::initialize-app
(fn []
(p/do
(test-utils/init!)
(rf/dispatch [:app-started])
;; Use initialize-view because it has the longest avg. time and is
;; dispatched by initialize-multiaccounts (last non-view event).
(wait-for [:profile/get-profiles-overview-success
:font/init-font-file-for-initials-avatar])
(assert-app-initialized)))))
(defn test-account-creation
[]
(test-async ::create-account
(fn []
(p/do
(setup-app)
(setup-account)
(logout)
(wait-for [::logout/logout-method])))))
;;;; Fixtures
(defn fixture-session
@ -246,19 +271,14 @@
(defn enable-testnet!
[]
(rf/dispatch [:profile.settings/profile-update :test-networks-enabled?
true {}])
(rf/dispatch [:profile.settings/profile-update :test-networks-enabled? true {}])
(rf/dispatch [:wallet/initialize]))
(defn recover-multiaccount!
[]
(let [masked-seed-phrase (security/mask-data (:seed-phrase constants/recovery-account))]
(validate-mnemonic
masked-seed-phrase
(fn [mnemonic key-uid]
(rf/dispatch [:onboarding/seed-phrase-validated
(security/mask-data mnemonic) key-uid])
(rf/dispatch [:pop-to-root :profiles])
(rf/dispatch [:profile/profile-selected key-uid])
(recover-and-login mnemonic))
#())))
(p/let [masked-seed-phrase (security/mask-data (:seed-phrase constants/recovery-account))
[mnemonic key-uid] (validate-mnemonic masked-seed-phrase)]
(rf/dispatch [:onboarding/seed-phrase-validated (security/mask-data mnemonic) key-uid])
(rf/dispatch [:pop-to-root :profiles])
(rf/dispatch [:profile/profile-selected key-uid])
(recover-and-login mnemonic)))

View File

@ -1,34 +1,15 @@
(ns tests.contract-test.core-test
(:require
[cljs.test :refer [deftest]]
[day8.re-frame.test :as rf-test]
legacy.status-im.events
[legacy.status-im.multiaccounts.logout.core :as logout]
legacy.status-im.subs.root
[re-frame.core :as rf]
status-im.events
status-im.navigation.core
status-im.subs.root
[test-helpers.integration :as h]
[tests.test-utils :as utils.test]))
[test-helpers.integration :as h]))
(deftest initialize-app-test
(h/log-headline :initialize-app-test)
(rf-test/run-test-async
(utils.test/init!)
(rf/dispatch [:app-started])
(rf-test/wait-for
;; use initialize-view because it has the longest avg. time and
;; is dispatched by initialize-multiaccounts (last non-view event)
[:profile/get-profiles-overview-success]
(rf-test/wait-for
[:font/init-font-file-for-initials-avatar]
(h/assert-app-initialized)))))
(h/test-app-initialization))
(deftest create-account-test
(h/log-headline :create-account-test)
(rf-test/run-test-async
(h/with-app-initialized
(h/with-account
(h/logout)
(rf-test/wait-for [::logout/logout-method])))))
(h/test-account-creation))

View File

@ -1,25 +1,19 @@
(ns tests.contract-test.profile-test
(:require
[cljs.test :refer [deftest is]]
[day8.re-frame.test :as rf-test]
[cljs.test :refer [deftest is use-fixtures]]
legacy.status-im.events
[legacy.status-im.multiaccounts.logout.core :as logout]
legacy.status-im.subs.root
[promesa.core :as p]
status-im.events
status-im.navigation.core
status-im.subs.root
[test-helpers.integration :as h]
[tests.contract-test.utils :as contract-utils]))
(use-fixtures :each (h/fixture-session))
(deftest profile-set-bio-contract
(h/log-headline :contract/wakuext_setBio)
(rf-test/run-test-async
(h/with-app-initialized
(h/with-account
(contract-utils/call-rpc-endpoint
{:rpc-endpoint "wakuext_setBio"
:params ["new bio"]
:on-error #(is (nil? %) "Set bio RPC call should have succeeded")})
(h/logout)
(rf-test/wait-for
[::logout/logout-method])))))
(h/test-async :contract/wakuext_setBio
(fn []
(-> (contract-utils/call-rpc "wakuext_setBio" "new bio")
(p/catch #(is (nil? %) "Set bio RPC call should have succeeded"))))))

View File

@ -1,14 +1,14 @@
(ns tests.contract-test.utils
(:require
[promesa.core :as p]
[status-im.common.json-rpc.events :as rpc-events]
[utils.number]))
(defn call-rpc-endpoint
[{:keys [rpc-endpoint
params
action
on-error]}]
(rpc-events/call {:method rpc-endpoint
:params params
:on-success action
:on-error on-error}))
(defn call-rpc
[method & args]
(p/create
(fn [p-resolve p-reject]
(rpc-events/call {:method method
:params args
:on-success p-resolve
:on-error p-reject}))))

View File

@ -1,11 +1,10 @@
(ns tests.contract-test.wallet-test
(:require
[cljs.test :refer [deftest is]]
[day8.re-frame.test :as rf-test]
[cljs.test :refer [deftest is use-fixtures]]
legacy.status-im.events
[legacy.status-im.multiaccounts.logout.core :as logout]
legacy.status-im.subs.root
[native-module.core :as native-module]
[promesa.core :as p]
[status-im.common.emoji-picker.utils :as emoji-picker.utils]
[status-im.constants :as constants]
[status-im.contexts.wallet.data-store :as data-store]
@ -16,23 +15,19 @@
[tests.contract-test.utils :as contract-utils]
[tests.integration-test.constants :as integration-constants]))
(use-fixtures :each (h/fixture-session))
(defn assert-accounts-get-accounts
[result]
(is (true? (some :wallet result)))
(is (true? (some :chat result)))
(is (= 2 (count result))))
(deftest accounts-get-accounts-contract
(h/log-headline :contract/accounts-get-accounts)
(rf-test/run-test-async
(h/with-app-initialized
(h/with-account
(contract-utils/call-rpc-endpoint
{:rpc-endpoint "accounts_getAccounts"
:action assert-accounts-get-accounts})
(h/logout)
(rf-test/wait-for
[::logout/logout-method])))))
(deftest accounts-get-accounts-contract-test
(h/test-async :contract/accounts-get-accounts
(fn []
(p/let [result (contract-utils/call-rpc "accounts_getAccounts")]
(assert-accounts-get-accounts result)))))
(defn get-default-account
[accounts]
@ -44,24 +39,16 @@
(is (= (:emoji default-account) test-emoji))))
(deftest accounts-save-accounts-contract
(h/log-headline :contract/accounts-save-account)
(rf-test/run-test-async
(h/with-app-initialized
(h/with-account
(let [test-emoji (emoji-picker.utils/random-emoji)
account (contract-utils/call-rpc-endpoint
{:rpc-endpoint "accounts_getAccounts"
:action get-default-account})]
(contract-utils/call-rpc-endpoint
{:rpc-endpoint "accounts_saveAccount"
:action identity
:params [(data-store/<-account (merge account {:emoji test-emoji}))]})
(contract-utils/call-rpc-endpoint
{:rpc-endpoint "accounts_getAccounts"
:action #(check-emoji-is-updated test-emoji %)})
(h/logout)
(rf-test/wait-for
[::logout/logout-method]))))))
(h/test-async :contract/accounts-save-account
(fn []
(p/let [test-emoji (emoji-picker.utils/random-emoji)
account (contract-utils/call-rpc "accounts_getAccounts")
default-account (get-default-account account)
_ (contract-utils/call-rpc
"accounts_saveAccount"
(data-store/<-account (merge default-account {:emoji test-emoji})))
accounts (contract-utils/call-rpc "accounts_getAccounts")]
(check-emoji-is-updated test-emoji accounts)))))
(def number-of-networks 3)
@ -76,16 +63,10 @@
(is (some #(= constants/optimism-sepolia-chain-id (get-in % [:Test :chainId])) response)))
(deftest accounts-get-chains-contract
(h/log-headline :contract/wallet_get-ethereum-chains)
(rf-test/run-test-async
(h/with-app-initialized
(h/with-account
(contract-utils/call-rpc-endpoint
{:rpc-endpoint "wallet_getEthereumChains"
:action assert-ethereum-chains})
(h/logout)
(rf-test/wait-for
[::logout/logout-method])))))
(h/test-async :contract/wallet_get-ethereum-chains
(fn []
(p/let [response (contract-utils/call-rpc "wallet_getEthereumChains")]
(assert-ethereum-chains response)))))
(defn get-main-account
[accounts]
@ -99,18 +80,18 @@
(is (= (:public-key response) (:public-key response)))
(is (= "m/43'/60'/1581'/0'/0" (:path (first response)))))
(deftest wallet-get-derived-addressess-contract
(h/log-headline :wallet/create-derived-addresses)
(rf-test/run-test-async
(h/with-app-initialized
(h/with-recovered-account
(let [sha3-pwd (native-module/sha3 test-password)
derivation-path ["m/43'/60'/1581'/0'/0"]
main-account (contract-utils/call-rpc-endpoint
{:rpc-endpoint "accounts_getAccounts"
:action get-main-account})]
(contract-utils/call-rpc-endpoint
{:rpc-endpoint "wallet_getDerivedAddresses"
:params [sha3-pwd main-account derivation-path]
:action assert-derived-account}))))))
(deftest wallet-get-derived-addressess-contract-test
(h/test-async :wallet/create-derived-addresses
(fn []
(p/let [_ (h/enable-testnet!)
_ (h/recover-multiaccount!)
sha3-pwd (native-module/sha3 test-password)
derivation-path ["m/43'/60'/1581'/0'/0"]
accounts (contract-utils/call-rpc "accounts_getAccounts")
main-account (get-main-account accounts)
response (contract-utils/call-rpc
"wallet_getDerivedAddresses"
sha3-pwd
main-account
derivation-path)]
(assert-derived-account response)))))

View File

@ -17,7 +17,7 @@
"0x0402905bed83f0bbf993cee8239012ccb1a8bc86907ead834c1e38476a0eda71414eed0e25f525f270592a2eebb01c9119a4ed6429ba114e51f5cb0a28dae1adfd")
(deftest one-to-one-chat-test
(h/integration-test ::one-to-one-chat
(h/test-async ::one-to-one-chat
(fn []
(p/do
(rf/dispatch-sync [:chat.ui/start-chat chat-id])
@ -26,7 +26,7 @@
(is (= chat-id @(rf/subscribe [:chats/current-chat-id])))))))
(deftest delete-chat-test
(h/integration-test ::delete-chat
(h/test-async ::delete-chat
(fn []
(p/do
(rf/dispatch-sync [:chat.ui/start-chat chat-id])
@ -38,7 +38,7 @@
(rf/dispatch-sync [:chat.ui/remove-chat chat-id])))))
(deftest mute-chat-test
(h/integration-test ::mute-chat
(h/test-async ::mute-chat
(fn []
(p/do
(rf/dispatch-sync [:chat.ui/start-chat chat-id])
@ -57,7 +57,7 @@
(is (not @(rf/subscribe [:chats/muted chat-id])))))))
(deftest add-contact-test
(h/integration-test ::add-contact
(h/test-async ::add-contact
(fn []
(let [compressed-key "zQ3shMwgSMKHVznoowceZMxWde9HUnkQEVSGvvex8UFpFNErL"
public-key (str "0x0407e9dc435fe366cb0b4c4f35cbd925438c0f46fe0"

View File

@ -2,33 +2,14 @@
(:require
[cljs.test :refer [deftest]]
legacy.status-im.events
[legacy.status-im.multiaccounts.logout.core :as logout]
legacy.status-im.subs.root
[promesa.core :as p]
[re-frame.core :as rf]
status-im.events
status-im.navigation.core
status-im.subs.root
[test-helpers.integration :as h]
[tests.test-utils :as test-utils]))
[test-helpers.integration :as h]))
(deftest initialize-app-test
(h/integration-test ::initialize-app
(fn []
(p/do
(test-utils/init!)
(rf/dispatch [:app-started])
;; Use initialize-view because it has the longest avg. time and is
;; dispatched by initialize-multiaccounts (last non-view event).
(h/wait-for [:profile/get-profiles-overview-success
:font/init-font-file-for-initials-avatar])
(h/assert-app-initialized)))))
(h/test-app-initialization))
(deftest create-account-test
(h/integration-test ::create-account
(fn []
(p/do
(h/setup-app)
(h/setup-account)
(h/logout)
(h/wait-for [::logout/logout-method])))))
(h/test-account-creation))

View File

@ -10,7 +10,7 @@
(use-fixtures :each (h/fixture-session))
(deftest edit-profile-name-test
(h/integration-test ::edit-profile-name
(h/test-async ::edit-profile-name
(fn []
(let [new-name "John Doe"]
(p/do
@ -21,7 +21,7 @@
(is (= new-name display-name))))))))
(deftest edit-profile-picture-test
(h/integration-test ::edit-profile-picture
(h/test-async ::edit-profile-picture
(fn []
(let [mock-image "resources/images/mock2/monkey.png"
absolute-path (.resolve test-utils/path mock-image)]
@ -32,7 +32,7 @@
(is (not (nil? (:images profile))))))))))
(deftest delete-profile-picture-test
(h/integration-test ::delete-profile-picture
(h/test-async ::delete-profile-picture
(fn []
(p/do
(rf/dispatch [:profile/delete-picture])
@ -41,7 +41,7 @@
(is (nil? (:image profile))))))))
(deftest edit-profile-bio-test
(h/integration-test ::edit-profile-bio
(h/test-async ::edit-profile-bio
(fn []
(let [new-bio "New bio text"]
(p/do