order.cljs 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
(ns relevance.test.order
  (:require [cljs.test :refer-macros [deftest testing is are]]
            [relevance.data :as data]
            [relevance.order :as order]
            [relevance.utils :as utils]))

(deftest empty-test
  (is (= 1 1)))

(deftest test-time-score
  ; A score for an unknown URL with no tab index is zero
  (is (= 0 (order/time-score {:url "http://google.com"}
                             {}
                             {}
                             {})))
  ; A score for an unknown URL with no tab index is the complement of its index
  (is (= -20 (order/time-score {:url   "http://google.com"
                                :index 20}
                               {}
                               {}
                               {})))
  ; A score for a known URL equals its time value
  (is (= 291 (order/time-score {:url "http://google.com"}
                               {(utils/url-key "http://google.com")
                                {:time 291}}
                               {}
                               {})))
  ; A score for a known URL gets added the time for its site
  (is (= 468 (order/time-score {:url "http://google.com/somepage"}
                               {(utils/url-key "http://google.com")          {:time 291}
                                (utils/url-key "http://google.com/somepage") {:time 345}}
                               {(utils/host-key "google.com") {:time 123}
                                (utils/host-key "apple.com")  {:time 987}}
                               {})))
  ; A score for a known URL is not affected by the score of other URLs for the same site
  (is (= 414 (order/time-score {:url "http://google.com/"}
                               {(utils/url-key "http://google.com")          {:time 291}
                                (utils/url-key "http://google.com/somepage") {:time 345}}
                               {(utils/host-key "google.com") {:time 123}
                                (utils/host-key "apple.com")  {:time 987}}
                               {})))
  ; A page inherits its site's score even if it's unknown
  (is (= 987 (order/time-score {:url "http://apple.com/mac"}
                               {(utils/url-key "http://google.com")          {:time 291}
                                (utils/url-key "http://google.com/somepage") {:time 345}}
                               {(utils/host-key "google.com") {:time 123}
                                (utils/host-key "apple.com")  {:time 987}}
                               {})))
  ; A page inherits its site's score even if it's unknown, but substracts the index so
  ; that they are placed at the end.
  (is (= 975 (order/time-score {:url "http://apple.com/mac" :index 12}
                               {(utils/url-key "http://google.com")          {:time 291}
                                (utils/url-key "http://google.com/somepage") {:time 345}}
                               {(utils/host-key "google.com") {:time 123}
                                (utils/host-key "apple.com")  {:time 987}}
                               {})))
  ; A page that has sound gets no extra score if the :sound-to-left? key isn't on settings
  (is (= 291 (order/time-score {:url     "http://google.com"
                                :audible true}
                               {(utils/url-key "http://google.com")
                                {:time 291}}
                               {}
                               {})))
  ; An audible page gets an extra score if the :sound-to-left? key is set to true on the settings,
  ; but based on the index, not the time spent
  (is (= (+ 5 123 order/sound-extra-score)
         (order/time-score {:url     "http://google.com/translate"
                            :index   5
                            :audible true}
                           {(utils/url-key "http://google.com/translate")
                            {:time 456}}
                           {(utils/host-key "google.com") {:time 123}
                            (utils/host-key "apple.com")  {:time 987}}
                           {:sound-to-left? true})))
  ; Non-http pages are penalized, but get a minimum score of 1 (they will
  ; likely have a time of 0 to begin with since they aren't tracked)
  (is (= (* 124 order/non-http-penalty)
         (order/time-score {:url   "chrome://google.com/translate"
                            :index 5}
                           {}
                           {(utils/host-key "google.com") {:time 123}
                            (utils/host-key "apple.com")  {:time 987}}
                           {}))))

(deftest test-score-tabs
  (let [url-times  {(utils/url-key "http://google.com")          {:time 2910}
                    (utils/url-key "http://google.com/somepage") {:time 345}
                    (utils/url-key "http://apple.com/osx")       {:time 10101}
                    (utils/url-key "http://apple.com/")          {:time 2120}}
        site-times {(utils/host-key "google.com") {:time 4295} ; Includes time from tabs which we have deleted
                    (utils/host-key "apple.com")  {:time 12221}}]
    ; We have spent the longest at Apple, so it gets prioritized
    ; The extension tab ends up at the end because it's not http
    (is (= [{:index 0 :id 23} {:index 1 :id 9} {:index 2 :id 1}]
           (order/score-tabs [{:url   "http://google.com"
                               :id    9
                               :index 15}
                              {:url   "chrome://extensions/"
                               :id    1
                               :index 1}
                              {:url   "https://apple.com/macbook"
                               :id    23
                               :index 912}]
                             url-times
                             site-times
                             {})))
    ; An unknown page ends up at the end
    (is (= [{:index 0 :id 23} {:index 1 :id 9} {:index 2 :id 1} {:index 3 :id 2}]
           (order/score-tabs [{:url   "http://google.com"
                               :id    9
                               :index 15}
                              {:url   "http://youtube.com/"
                               :id    2
                               :index 27}
                              {:url   "chrome://extensions/"
                               :id    1
                               :index 1}
                              {:url   "https://apple.com/macbook"
                               :id    23
                               :index 912}]
                             url-times
                             site-times
                             {})))
    ; Two unknown pages get sorted by their index order
    (is (= [{:index 0 :id 23} {:index 1 :id 9} {:index 2 :id 1} {:index 3 :id 123} {:index 4 :id 2}]
           (order/score-tabs [{:url   "http://google.com"
                               :id    9
                               :index 15}
                              {:url   "http://youtube.com/"
                               :id    2
                               :index 27}
                              {:url   "http://vimeo.com/"
                               :id    123
                               :index 26}
                              {:url   "chrome://extensions/"
                               :id    1
                               :index 1}
                              {:url   "https://apple.com/macbook"
                               :id    23
                               :index 912}]
                             url-times
                             site-times
                             {})))
    ; An unknown page that is playing sound gets prioritized according to the settings
    (is (= [{:index 0 :id 2} {:index 1 :id 23} {:index 2 :id 9} {:index 3 :id 1}]
           (order/score-tabs [{:url   "http://google.com"
                               :id    9
                               :index 15}
                              {:url     "http://youtube.com/"
                               :id      2
                               :audible true
                               :index   27}
                              {:url   "chrome://extensions/"
                               :id    1
                               :index 1}
                              {:url   "https://apple.com/macbook"
                               :id    23
                               :index 912}]
                             url-times
                             site-times
                             {:sound-to-left? true})))
    ; If for any reason the Apple URL is not http, it gets de-prioritized
    (is (= [{:index 0 :id 9} {:index 1 :id 1} {:index 2 :id 23}]
           (order/score-tabs [{:url   "http://google.com"
                               :id    9
                               :index 15}
                              {:url   "chrome://extensions/"
                               :id    1
                               :index 1}
                              {:url   "apple.com/macbook"
                               :id    23
                               :index 912}]
                             url-times
                             site-times
                             {})))

    )
  )