utils.cljs 3.18 KB
Newer Older
1 2 3 4 5 6 7
(ns relevance.test.utils
  (:require [cljs.test :refer-macros [deftest testing is are]]
            [relevance.utils :as utils]
            ))


(deftest test-key-from-url
8 9
  (is (= (utils/url-key "http://localhost/")
         (utils/url-key "https://localhost/"))
10
      "Key should disregard protocol")
11 12
  (is (= (utils/url-key "https://localhost")
         (utils/url-key "https://localhost/"))
13
      "Key should disregard trailing slashes")
14 15
  (is (= (utils/url-key "https://localhost#hash")
         (utils/url-key "https://localhost/#hash"))
16
      "Key should disregard trailing hashtags")
17 18
  (is (= (utils/url-key "https://LOCALHOST")
         (utils/url-key "https://localhost"))
19
      "Key is not case-sensitive")
20
  (is (= (utils/url-key "https://LOCALHOST/someUrl#hash")
21 22
         (hash-string "localhost/someUrl"))
      "Our hash calculations are consistent with hash-string")
23 24
  (is (not= (utils/url-key "https://localhost?q=v")
            (utils/url-key "https://localhost?q="))
25
      "Key should respect query strings")
26 27
  (is (not= (utils/url-key "https://localhost.com/path")
            (utils/url-key "https://localhost.com/Path"))
28 29
      "Path is case-sensitive")
  ;; Let's confirm we actually return a consistent integer for some known values
30
  (are [k url] (= k (utils/url-key url))
31 32 33 34 35 36 37 38
               -20650657 "https://LOCALHOST/someUrl#hash"
               -380467869 "http://google.com"
               -380467869 "https://google.com"
               -380467869 "https://google.com/"
               -314744948 "https://www.google.com"
               -314744948 "https://www.google.com/#hash"
               -596725840 "https://www.google.com/somePath?q=v"
               -327774960 "http://numergent.com/tags/khroma/"
39
               -526558523 "http://numergent.com/opensource/"
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
               )
  )

(deftest test-time-display
  (are [time label] (= (utils/time-display time) label)
                    500 "< 1s"
                    999 "< 1s"
                    1000 "1s"
                    1001 "1s"
                    1999 "1s"
                    3742 "3s"
                    49231 "49s"
                    124076 "2min 4s"
                    762661 "12min 42s"
                    8659266 "2h 24min"
                    86592666 "1d 0h"
                    124076042 "1d 10h"
                    248996042 "2d 21h"
                    ))
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

(deftest test-host
  (are [url name] (= (utils/hostname url) name)
                  "https://www.google.com/some?q=v" "www.google.com"
                  "https://www.Google.com/some?q=v" "www.google.com"
                  "https://WWW.GOOGLE.COM/some?q=v" "www.google.com"
                  "http://WWW.GOOGLE.COM/some?q=v" "www.google.com"
                  "https://GOOGLE.COM:443/some?q=v" "google.com"
                  "https://GOOGLE.COM:3000/some?q=v" "google.com"     ; host would have included the port
                  "http://numergent.com/tag/khroma" "numergent.com"
                  "about:blank" ""
                  "chrome://extensions/?id=okhigbflgnbihoiokilagelkalkcigfp" "extensions"
                  "chrome-extension://okhigbflgnbihoiokilagelkalkcigfp/index.html" "okhigbflgnbihoiokilagelkalkcigfp"
                  "" ""
                  nil nil
                  ))