Fix unbounded number of calls to fetch notifications (#14250)

This commit is contained in:
Icaro Motta 2022-10-27 16:21:41 -03:00 committed by GitHub
parent 69303cd7d1
commit 91b237d3d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 512 additions and 527 deletions

View File

@ -5,25 +5,17 @@
[status-im.constants :as const]
[status-im.ethereum.json-rpc :as json-rpc]
status-im.events
[status-im.test-helpers :as h]))
[status-im.test-helpers :as h]
[status-im.utils.config :as config]))
(defn setup []
(h/register-helper-events)
(rf/dispatch [:init/app-started]))
(defn remove-color-key
"Remove `:color` key from notifications because they have random values that are
inconvenient to assert against."
[grouped-notifications {:keys [type status]}]
(update-in grouped-notifications
[type status :data]
(fn [old _]
(map #(dissoc % :color) old))
nil))
;;;; Contact verification
(deftest contact-verification-decline-test
(with-redefs [config/new-activity-center-enabled? true]
(testing "successfully declines and reconciles returned notification"
(rf-test/run-test-sync
(setup)
@ -86,12 +78,7 @@
const/activity-center-notification-type-contact-verification
{:read {:data [expected-notification]}
:unread {:data []}}}
(-> (h/db)
(get-in [:activity-center :notifications])
(remove-color-key {:type const/activity-center-notification-type-no-type
:status :read})
(remove-color-key {:type const/activity-center-notification-type-contact-verification
:status :read})))))))
(get-in (h/db) [:activity-center :notifications]))))))
(testing "logs failure"
(rf-test/run-test-sync
@ -107,11 +94,12 @@
{:contact-verification-id contact-verification-id
:error :fake-error}]
:level :warn}
(last @logs)))))))))
(last @logs))))))))))
;;;; Notification reconciliation
(deftest notifications-reconcile-test
(with-redefs [config/new-activity-center-enabled? true]
(testing "does nothing when there are no new notifications"
(rf-test/run-test-sync
(setup)
@ -350,11 +338,12 @@
:read false
:type const/activity-center-notification-type-one-to-one-chat
:timestamp 50}]}}}
(get-in (h/db) [:activity-center :notifications]))))))
(get-in (h/db) [:activity-center :notifications])))))))
;;;; Notifications fetching and pagination
(deftest notifications-fetch-test
(with-redefs [config/new-activity-center-enabled? true]
(testing "fetches first page"
(rf-test/run-test-sync
(setup)
@ -387,9 +376,7 @@
:read false
:reply-message nil
:type const/activity-center-notification-type-one-to-one-chat}]}}}
(remove-color-key (get-in (h/db) [:activity-center :notifications])
{:status :unread
:type const/activity-center-notification-type-one-to-one-chat}))))))
(get-in (h/db) [:activity-center :notifications]))))))
(testing "does not fetch next page when pagination cursor reached the end"
(rf-test/run-test-sync
@ -461,9 +448,7 @@
:read false
:reply-message nil
:type const/activity-center-notification-type-mention}]}}}
(remove-color-key (get-in (h/db) [:activity-center :notifications])
{:status :unread
:type const/activity-center-notification-type-mention}))))))
(get-in (h/db) [:activity-center :notifications]))))))
(testing "does not fetch next page while it is still loading"
(rf-test/run-test-sync
@ -504,4 +489,4 @@
const/activity-center-notification-type-one-to-one-chat
:unread
:fake-error]
(:args (last @spy-queue))))))))
(:args (last @spy-queue)))))))))

View File

@ -2,7 +2,8 @@
(:require [clojure.set :as set]
[quo.design-system.colors :as colors]
[status-im.constants :as constants]
[status-im.data-store.messages :as messages]))
[status-im.data-store.messages :as messages]
[status-im.utils.config :as config]))
(defn- rpc->type [{:keys [type name] :as chat}]
(case type
@ -33,14 +34,15 @@
chat))
(defn <-rpc [item]
(-> item
(cond-> (-> item
rpc->type
(set/rename-keys {:lastMessage :last-message
:replyMessage :reply-message
:chatId :chat-id
:contactVerificationStatus :contact-verification-status})
(assoc :color (rand-nth colors/chat-colors))
(update :last-message #(when % (messages/<-rpc %)))
(update :message #(when % (messages/<-rpc %)))
(update :reply-message #(when % (messages/<-rpc %)))
(dissoc :chatId)))
(dissoc :chatId))
(not config/new-activity-center-enabled?)
(assoc :color (rand-nth colors/chat-colors))))

View File

@ -1,8 +1,8 @@
(ns status-im.data-store.activities-test
(:require [cljs.test :refer [deftest is testing]]
[quo.design-system.colors :as colors]
[status-im.constants :as constants]
[status-im.data-store.activities :as store]))
[status-im.data-store.activities :as store]
[status-im.utils.config :as config]))
(def chat-id
"0x04c66155")
@ -18,16 +18,14 @@
:replyMessage {}})
(deftest <-rpc-test
(testing "assocs random chat color"
(is (contains? (set colors/chat-colors) (:color (store/<-rpc raw-notification)))))
(with-redefs [config/new-activity-center-enabled? true]
(testing "renames keys"
(is (= {:name chat-name
:chat-id chat-id
:contact-verification-status constants/contact-verification-state-pending}
(-> raw-notification
store/<-rpc
(dissoc :color :last-message :message :reply-message)))))
(dissoc :last-message :message :reply-message)))))
(testing "transforms messages from RPC response"
(is (= {:last-message {:quoted-message nil
@ -98,4 +96,4 @@
(-> raw-notification
(assoc :type constants/activity-center-notification-type-one-to-one-chat)
store/<-rpc
(select-keys [:name :chat-type :chat-name :public? :group-chat]))))))
(select-keys [:name :chat-type :chat-name :public? :group-chat])))))))