2023-12-19 17:41:30 +00:00
|
|
|
(ns legacy.status-im.utils.transducers-test
|
2023-10-16 22:03:18 +00:00
|
|
|
(:require
|
|
|
|
[cljs.test :refer-macros [deftest is testing]]
|
2023-12-19 17:41:30 +00:00
|
|
|
[legacy.status-im.utils.transducers :as transducers]))
|
2017-10-24 10:51:32 +00:00
|
|
|
|
2020-05-05 14:18:23 +00:00
|
|
|
(def preview-call-1
|
2022-12-20 14:45:37 +00:00
|
|
|
{:jail-id 1
|
|
|
|
:path [:preview]
|
|
|
|
:params {:chat-id 1}
|
2017-10-24 10:51:32 +00:00
|
|
|
:callback (fn []
|
|
|
|
[[:msg-id 1]])})
|
|
|
|
|
2020-05-05 14:18:23 +00:00
|
|
|
(def preview-call-2
|
2022-12-20 14:45:37 +00:00
|
|
|
{:jail-id 1
|
|
|
|
:path [:preview]
|
|
|
|
:params {:chat-id 1}
|
2017-10-24 10:51:32 +00:00
|
|
|
:callback (fn []
|
|
|
|
[[:msg-id 2]])})
|
|
|
|
|
2020-05-05 14:18:23 +00:00
|
|
|
(def jail-calls
|
2017-10-24 10:51:32 +00:00
|
|
|
'({:jail-id 1
|
2022-12-20 14:45:37 +00:00
|
|
|
:path [:suggestions]
|
|
|
|
:params {:arg 0}}
|
2017-10-24 10:51:32 +00:00
|
|
|
{:jail-id 1
|
2022-12-20 14:45:37 +00:00
|
|
|
:path [:function]
|
|
|
|
:params {:sub :a}}
|
2017-10-24 10:51:32 +00:00
|
|
|
{:jail-id 1
|
2022-12-20 14:45:37 +00:00
|
|
|
:path [:function]
|
|
|
|
:params {:sub :b}}
|
2017-10-24 10:51:32 +00:00
|
|
|
{:jail-id 1
|
2022-12-20 14:45:37 +00:00
|
|
|
:path [:suggestions]
|
|
|
|
:params {:arg 1}}
|
2017-10-24 10:51:32 +00:00
|
|
|
{:jail-id 1
|
2022-12-20 14:45:37 +00:00
|
|
|
:path [:suggestions]
|
|
|
|
:params {:arg 2}}
|
2017-10-24 10:51:32 +00:00
|
|
|
preview-call-1
|
|
|
|
preview-call-2))
|
|
|
|
|
|
|
|
(deftest last-distinct-by-test
|
2022-12-20 14:45:37 +00:00
|
|
|
(testing
|
|
|
|
"Elements are removed from input according to provided `compare-fn`,
|
2017-10-24 10:51:32 +00:00
|
|
|
when duplicate elements are removed, the last one stays"
|
2018-07-23 12:14:06 +00:00
|
|
|
(is (= (sequence (transducers/last-distinct-by (fn [{:keys [jail-id path] :as call}]
|
|
|
|
(if (= :suggestions (last path))
|
|
|
|
[jail-id path]
|
|
|
|
call)))
|
|
|
|
jail-calls)
|
2017-10-24 10:51:32 +00:00
|
|
|
'({:jail-id 1
|
2022-12-20 14:45:37 +00:00
|
|
|
:path [:suggestions]
|
|
|
|
:params {:arg 2}}
|
2017-10-24 10:51:32 +00:00
|
|
|
{:jail-id 1
|
2022-12-20 14:45:37 +00:00
|
|
|
:path [:function]
|
|
|
|
:params {:sub :a}}
|
2017-10-24 10:51:32 +00:00
|
|
|
{:jail-id 1
|
2022-12-20 14:45:37 +00:00
|
|
|
:path [:function]
|
|
|
|
:params {:sub :b}}
|
2017-10-24 10:51:32 +00:00
|
|
|
preview-call-1
|
|
|
|
preview-call-2))))
|
|
|
|
(testing "Edge cases with input size `N=0` and `N=1` work as well"
|
|
|
|
(is (= (sequence (transducers/last-distinct-by identity) '())
|
|
|
|
'()))
|
|
|
|
(is (= (sequence (transducers/last-distinct-by identity) '(1))
|
|
|
|
'(1)))))
|