From 9acf67dd5b974a82c2449a2fea37ffc8dfa8fab7 Mon Sep 17 00:00:00 2001 From: Jamie Caprani Date: Fri, 9 Feb 2024 17:10:09 +0000 Subject: [PATCH] feat: add basic infrastructure for contract tests (#18665) --- Makefile | 9 +- shadow-cljs.edn | 1 - .../integration_test/wallet_test.cljs | 51 ----------- src/test_helpers/integration.cljs | 4 +- .../contract_test}/core_test.cljs | 2 +- src/tests/contract_test/utils.cljs | 14 +++ src/tests/contract_test/wallet_test.cljs | 86 +++++++++++++++++++ .../integration_test/chat_test.cljs | 2 +- .../integration_test/constants.cljs | 2 +- src/tests/integration_test/core_test.cljs | 34 ++++++++ .../integration_test/profile_test.cljs | 2 +- 11 files changed, 147 insertions(+), 60 deletions(-) delete mode 100644 src/status_im/integration_test/wallet_test.cljs rename src/{status_im/integration_test => tests/contract_test}/core_test.cljs (96%) create mode 100644 src/tests/contract_test/utils.cljs create mode 100644 src/tests/contract_test/wallet_test.cljs rename src/{status_im => tests}/integration_test/chat_test.cljs (98%) rename src/{status_im => tests}/integration_test/constants.cljs (74%) create mode 100644 src/tests/integration_test/core_test.cljs rename src/{status_im => tests}/integration_test/profile_test.cljs (97%) diff --git a/Makefile b/Makefile index beae50b12f..087a3de0ce 100644 --- a/Makefile +++ b/Makefile @@ -373,15 +373,20 @@ test-watch-for-repl: ##@test Watch all Clojure tests and support REPL connection "until [ -f $$SHADOW_OUTPUT_TO ] ; do sleep 1 ; done ; node --require ./test-resources/override.js $$SHADOW_OUTPUT_TO --repl" test-unit: export SHADOW_OUTPUT_TO := target/unit_test/test.js -test-unit: export SHADOW_NS_REGEXP := ^(?!status-im\.integration-test).*-test$$ +test-unit: export SHADOW_NS_REGEXP := ^(?!tests\.integration-test)(?!tests-im\.contract-test).*-test$$ test-unit: ##@test Run unit tests test-unit: _test-clojure test-integration: export SHADOW_OUTPUT_TO := target/integration_test/test.js -test-integration: export SHADOW_NS_REGEXP := ^status-im\.integration-test.*$$ +test-integration: export SHADOW_NS_REGEXP := ^tests\.integration-test.*$$ test-integration: ##@test Run integration tests test-integration: _test-clojure +test-contract: export SHADOW_OUTPUT_TO := target/contract_test/test.js +test-contract: export SHADOW_NS_REGEXP := ^tests\.contract-test.*$$ +test-contract: ##@test Run contract tests +test-contract: _test-clojure + android-test: jsbundle android-test: export TARGET := android android-test: diff --git a/shadow-cljs.edn b/shadow-cljs.edn index ad0e115585..750d4c8f11 100644 --- a/shadow-cljs.edn +++ b/shadow-cljs.edn @@ -176,7 +176,6 @@ ;; option setupFilesAfterEnv. test-helpers.component-tests-preload status-im.setup.schema-preload - quo.core-spec status-im.core-spec] :ns-regexp "component-spec$" diff --git a/src/status_im/integration_test/wallet_test.cljs b/src/status_im/integration_test/wallet_test.cljs deleted file mode 100644 index 026b68658b..0000000000 --- a/src/status_im/integration_test/wallet_test.cljs +++ /dev/null @@ -1,51 +0,0 @@ -(ns status-im.integration-test.wallet-test - (:require - [cljs.test :refer [is]] - [clojure.string :as string] - [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])) - -;; Workaround to skip test. Switch to `deftest` when test is fixed. -(defn create-wallet-account-test - [] - (h/log-headline :create-wallet-account-test) - (rf-test/run-test-async - (h/with-app-initialized - (h/with-account - (h/create-new-account!) - (rf-test/wait-for - [:wallet-legacy.accounts/account-stored] - (h/assert-new-account-created) - (h/logout) - (rf-test/wait-for [::logout/logout-method])))))) - -;; Workaround to skip test. Switch to `deftest` when test is fixed. -(defn back-up-seed-phrase-test - [] - (h/log-headline :back-up-seed-phrase-test) - (rf-test/run-test-async - (h/with-app-initialized - (h/with-account - (rf/dispatch-sync [:set-in [:my-profile/seed :step] :12-words]) ; display seed phrase to user - (rf/dispatch-sync [:my-profile/enter-two-random-words]) ; begin prompting user for seed words - (let [{:keys [mnemonic]} @(rf/subscribe [:profile/profile]) - seed @(rf/subscribe [:my-profile/seed]) - word1 (second (:first-word seed)) - word2 (second (:second-word seed))] - (is (= 12 (count (string/split mnemonic #" ")))) - (rf/dispatch-sync [:set-in [:my-profile/seed :word] word1]) - (rf/dispatch-sync [:my-profile/set-step :second-word]) - (rf/dispatch-sync [:set-in [:my-profile/seed :word] word2]) - (rf/dispatch [:my-profile/finish]) - (rf-test/wait-for - [:my-profile/finish-success] - (is (nil? @(rf/subscribe [:mnemonic]))) ; assert seed phrase has been removed - (h/logout) - (rf-test/wait-for [::logout/logout-method]))))))) diff --git a/src/test_helpers/integration.cljs b/src/test_helpers/integration.cljs index 33e45a9f13..36f0ef4881 100644 --- a/src/test_helpers/integration.cljs +++ b/src/test_helpers/integration.cljs @@ -7,10 +7,10 @@ [native-module.core :as native-module] [re-frame.core :as rf] status-im.events - [status-im.integration-test.constants :as constants] status-im.navigation.core status-im.subs.root - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [tests.integration-test.constants :as constants])) (defn initialize-app! [] diff --git a/src/status_im/integration_test/core_test.cljs b/src/tests/contract_test/core_test.cljs similarity index 96% rename from src/status_im/integration_test/core_test.cljs rename to src/tests/contract_test/core_test.cljs index 0cf7a1d23f..046f291b83 100644 --- a/src/status_im/integration_test/core_test.cljs +++ b/src/tests/contract_test/core_test.cljs @@ -1,4 +1,4 @@ -(ns status-im.integration-test.core-test +(ns tests.contract-test.core-test (:require [cljs.test :refer [deftest]] [day8.re-frame.test :as rf-test] diff --git a/src/tests/contract_test/utils.cljs b/src/tests/contract_test/utils.cljs new file mode 100644 index 0000000000..1a1d6e9c0a --- /dev/null +++ b/src/tests/contract_test/utils.cljs @@ -0,0 +1,14 @@ +(ns tests.contract-test.utils + (:require + [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})) diff --git a/src/tests/contract_test/wallet_test.cljs b/src/tests/contract_test/wallet_test.cljs new file mode 100644 index 0000000000..4ce5a58a0a --- /dev/null +++ b/src/tests/contract_test/wallet_test.cljs @@ -0,0 +1,86 @@ +(ns tests.contract-test.wallet-test + (:require + [cljs.test :refer [deftest is]] + [day8.re-frame.test :as rf-test] + legacy.status-im.events + [legacy.status-im.multiaccounts.logout.core :as logout] + legacy.status-im.subs.root + [status-im.common.emoji-picker.utils :as emoji-picker.utils] + [status-im.constants :as constants] + [status-im.contexts.wallet.data-store :as data-store] + status-im.events + status-im.navigation.core + status-im.subs.root + [test-helpers.integration :as h] + [tests.contract-test.utils :as contract-utils])) + +(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]))))) + +(defn get-default-account + [accounts] + (first (filter :wallet accounts))) + +(defn check-emoji-is-updated + [test-emoji accounts] + (let [default-account (get-default-account accounts)] + (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])))))) + +(def number-of-networks 3) + +(defn assert-ethereum-chains + [response] + (is (= number-of-networks (count response))) + (is (some #(= constants/ethereum-chain-id (get-in % [:Prod :chainId])) response)) + (is (some #(= constants/optimism-chain-id (get-in % [:Prod :chainId])) response)) + (is (some #(= constants/arbitrum-chain-id (get-in % [:Prod :chainId])) response)) + (is (some #(= constants/goerli-chain-id (get-in % [:Test :chainId])) response)) + (is (some #(= constants/arbitrum-testnet-chain-id (get-in % [:Test :chainId])) response)) + (is (some #(= constants/optimism-testnet-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]))))) diff --git a/src/status_im/integration_test/chat_test.cljs b/src/tests/integration_test/chat_test.cljs similarity index 98% rename from src/status_im/integration_test/chat_test.cljs rename to src/tests/integration_test/chat_test.cljs index 35cf7dd74d..2728b24a5c 100644 --- a/src/status_im/integration_test/chat_test.cljs +++ b/src/tests/integration_test/chat_test.cljs @@ -1,4 +1,4 @@ -(ns status-im.integration-test.chat-test +(ns tests.integration-test.chat-test (:require [cljs.test :refer [deftest is]] [day8.re-frame.test :as rf-test] diff --git a/src/status_im/integration_test/constants.cljs b/src/tests/integration_test/constants.cljs similarity index 74% rename from src/status_im/integration_test/constants.cljs rename to src/tests/integration_test/constants.cljs index eb935081f3..4f72fb3935 100644 --- a/src/status_im/integration_test/constants.cljs +++ b/src/tests/integration_test/constants.cljs @@ -1,4 +1,4 @@ -(ns status-im.integration-test.constants) +(ns tests.integration-test.constants) (def password "testabc") diff --git a/src/tests/integration_test/core_test.cljs b/src/tests/integration_test/core_test.cljs new file mode 100644 index 0000000000..bc98c70703 --- /dev/null +++ b/src/tests/integration_test/core_test.cljs @@ -0,0 +1,34 @@ +(ns tests.integration-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 + [legacy.status-im.utils.test :as utils.test] + [re-frame.core :as rf] + status-im.events + status-im.navigation.core + status-im.subs.root + [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))))) + +(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]))))) diff --git a/src/status_im/integration_test/profile_test.cljs b/src/tests/integration_test/profile_test.cljs similarity index 97% rename from src/status_im/integration_test/profile_test.cljs rename to src/tests/integration_test/profile_test.cljs index 6e2b1b8830..b59f06ae56 100644 --- a/src/status_im/integration_test/profile_test.cljs +++ b/src/tests/integration_test/profile_test.cljs @@ -1,4 +1,4 @@ -(ns status-im.integration-test.profile-test +(ns tests.integration-test.profile-test (:require [cljs.test :refer [deftest is]] [day8.re-frame.test :as rf-test]