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

@ -41,7 +41,7 @@
"reg-fx" :arg1-pair "reg-fx" :arg1-pair
"testing" :arg1-body "testing" :arg1-body
"deftest-sub" :arg1-body "deftest-sub" :arg1-body
"h/integration-test" :arg1-body "test-async" :arg1-body
"wait-for" :arg1-body "wait-for" :arg1-body
"with-deps-check" :arg1-body "with-deps-check" :arg1-body
"schema/=>" :arg1-body "schema/=>" :arg1-body

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 (ns test-helpers.integration
(:require-macros [test-helpers.integration])
(:require (:require
[cljs.test :refer [is] :as test] [cljs.test :refer [is] :as test]
legacy.status-im.events legacy.status-im.events
@ -20,14 +19,16 @@
[utils.transforms :as transforms])) [utils.transforms :as transforms]))
(defn validate-mnemonic (defn validate-mnemonic
[mnemonic on-success on-error] [mnemonic]
(p/create
(fn [p-resolve p-reject]
(native-module/validate-mnemonic (native-module/validate-mnemonic
(security/safe-unmask-data mnemonic) (security/safe-unmask-data mnemonic)
(fn [result] (fn [result]
(let [{:keys [error keyUID]} (transforms/json->clj result)] (let [{:keys [error keyUID]} (transforms/json->clj result)]
(if (seq error) (if (seq error)
(when on-error (on-error error)) (p-reject error)
(on-success mnemonic keyUID)))))) (p-resolve [mnemonic keyUID]))))))))
(def default-re-frame-wait-for-timeout-ms (def default-re-frame-wait-for-timeout-ms
"Controls the maximum time allowed to wait for all events to be processed by "Controls the maximum time allowed to wait for all events to be processed by
@ -153,7 +154,7 @@
(-> (wait-for [:messenger-started]) (-> (wait-for [:messenger-started])
(.then #(assert-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. "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 `f` will be called with one argument, the `done` function exposed by the
@ -175,7 +176,8 @@
fail more often. fail more often.
" "
([test-name f] ([test-name f]
(integration-test test-name (test-async
test-name
{:fail-fast? true {:fail-fast? true
:timeout-ms default-integration-test-timeout-ms} :timeout-ms default-integration-test-timeout-ms}
f)) f))
@ -194,6 +196,29 @@
(restore-fn) (restore-fn)
(done)))))))) (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 ;;;; Fixtures
(defn fixture-session (defn fixture-session
@ -246,19 +271,14 @@
(defn enable-testnet! (defn enable-testnet!
[] []
(rf/dispatch [:profile.settings/profile-update :test-networks-enabled? (rf/dispatch [:profile.settings/profile-update :test-networks-enabled? true {}])
true {}])
(rf/dispatch [:wallet/initialize])) (rf/dispatch [:wallet/initialize]))
(defn recover-multiaccount! (defn recover-multiaccount!
[] []
(let [masked-seed-phrase (security/mask-data (:seed-phrase constants/recovery-account))] (p/let [masked-seed-phrase (security/mask-data (:seed-phrase constants/recovery-account))
(validate-mnemonic [mnemonic key-uid] (validate-mnemonic masked-seed-phrase)]
masked-seed-phrase (rf/dispatch [:onboarding/seed-phrase-validated (security/mask-data mnemonic) key-uid])
(fn [mnemonic key-uid]
(rf/dispatch [:onboarding/seed-phrase-validated
(security/mask-data mnemonic) key-uid])
(rf/dispatch [:pop-to-root :profiles]) (rf/dispatch [:pop-to-root :profiles])
(rf/dispatch [:profile/profile-selected key-uid]) (rf/dispatch [:profile/profile-selected key-uid])
(recover-and-login mnemonic)) (recover-and-login mnemonic)))
#())))

View File

@ -1,34 +1,15 @@
(ns tests.contract-test.core-test (ns tests.contract-test.core-test
(:require (:require
[cljs.test :refer [deftest]] [cljs.test :refer [deftest]]
[day8.re-frame.test :as rf-test]
legacy.status-im.events legacy.status-im.events
[legacy.status-im.multiaccounts.logout.core :as logout]
legacy.status-im.subs.root legacy.status-im.subs.root
[re-frame.core :as rf]
status-im.events status-im.events
status-im.navigation.core status-im.navigation.core
status-im.subs.root status-im.subs.root
[test-helpers.integration :as h] [test-helpers.integration :as h]))
[tests.test-utils :as utils.test]))
(deftest initialize-app-test (deftest initialize-app-test
(h/log-headline :initialize-app-test) (h/test-app-initialization))
(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)))))
(deftest create-account-test (deftest create-account-test
(h/log-headline :create-account-test) (h/test-account-creation))
(rf-test/run-test-async
(h/with-app-initialized
(h/with-account
(h/logout)
(rf-test/wait-for [::logout/logout-method])))))

View File

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

View File

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

View File

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

View File

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

View File

@ -2,33 +2,14 @@
(:require (:require
[cljs.test :refer [deftest]] [cljs.test :refer [deftest]]
legacy.status-im.events legacy.status-im.events
[legacy.status-im.multiaccounts.logout.core :as logout]
legacy.status-im.subs.root legacy.status-im.subs.root
[promesa.core :as p]
[re-frame.core :as rf]
status-im.events status-im.events
status-im.navigation.core status-im.navigation.core
status-im.subs.root status-im.subs.root
[test-helpers.integration :as h] [test-helpers.integration :as h]))
[tests.test-utils :as test-utils]))
(deftest initialize-app-test (deftest initialize-app-test
(h/integration-test ::initialize-app (h/test-app-initialization))
(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)))))
(deftest create-account-test (deftest create-account-test
(h/integration-test ::create-account (h/test-account-creation))
(fn []
(p/do
(h/setup-app)
(h/setup-account)
(h/logout)
(h/wait-for [::logout/logout-method])))))

View File

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