(ns strongbox.wowinterface-api-test
  (:require
   [clj-http.fake :refer [with-fake-routes-in-isolation]]
   [clojure.test :refer [deftest testing is use-fixtures]]
   [strongbox
    [test-helper :refer [fixture-path]]
    [wowinterface-api :as wowinterface-api]]
   ;;[taoensso.timbre :as log :refer [debug info warn error spy]]
   ))

(deftest expand-summary
  (testing "expand-summary correctly extracts and adds additional properties"
    (let [given {:url "",
                 :name "rotation-master",
                 :label "Rotation Master",
                 :updated-date "2019-07-29T21:37:00Z",
                 :download-count 80,
                 :tag-list [:dummy]
                 :source "wowinterface"
                 :source-id 25079
                 :game-track-list [:retail]}

          game-track :retail

          expected [{:download-url ""
                     :version "1.2.3"
                     :game-track game-track}]

          fixture (slurp (fixture-path "wowinterface-api--addon-details.json"))
          fake-routes {""
                       {:get (fn [_] {:status 200 :body fixture})}}]
      (with-fake-routes-in-isolation fake-routes
        (is (= expected (wowinterface-api/expand-summary given game-track)))))))

(deftest download-addon-404
  (testing "regular addon fetch that yields a 404 returns nil"
    (let [given {:url "",
                 :name "rotation-master",
                 :label "Rotation Master",
                 :updated-date "2019-07-29T21:37:00Z",
                 :download-count 80,
                 :tag-list [:dummy]
                 :source "wowinterface"
                 :source-id 25079
                 :game-track-list [:retail]}

          game-track :retail

          fake-routes {""
                       {:get (fn [req] {:status 404 :reason-phrase "Not Found" :body "<h1>Not Found</h1>"})}}]
      (with-fake-routes-in-isolation fake-routes
        (is (nil? (wowinterface-api/expand-summary given game-track)))))))

(deftest parse-user-string
  (testing "parsing wowinterface urls extracts the source-id"
    (let [cases [["" 8882]
                 ["-MasqueLiteStep.html" 8882]
                 ["-MasqueLiteStep" 8882]

                 ["-GSEGnomeSequencer-Enhanced-Advanc....html" 23921]

                 ;; path to an alternate download.
                 ;; would be nice to support but has nothing linking it to the catalogue
                 ["-9.0.6-bcc.zip?1621368578" nil]]]
      (doseq [[given expected] cases]
        (is (= expected (wowinterface-api/parse-user-string given)))))))

(deftest extract-aid
  (testing "the 'aid' query parameter can be extracted from a url if present"
    (let [cases [[nil nil]
                 ["" nil]
                 ["" nil]
                 ["" "119903"]
                 ["&aid=asdf" nil]]]
      (doseq [[given expected] cases]
        (is (= expected (wowinterface-api/extract-aid given)))))))

(deftest make-url
  (let [cases [[{} nil]
               [{:source-id nil} nil]
               [{:source-id 123} ""]]]
    (doseq [[given expected] cases]
      (is (= expected (wowinterface-api/make-url given))))))

[taoensso.timbre :as log :refer [debug info warn error spy]]
 path to an alternate download.
 would be nice to support but has nothing linking it to the catalogue
(ns strongbox.wowinterface-api-test
  (:require
   [clj-http.fake :refer [with-fake-routes-in-isolation]]
   [clojure.test :refer [deftest testing is use-fixtures]]
   [strongbox
    [test-helper :refer [fixture-path]]
    [wowinterface-api :as wowinterface-api]]
   ))

(deftest expand-summary
  (testing "expand-summary correctly extracts and adds additional properties"
    (let [given {:url "",
                 :name "rotation-master",
                 :label "Rotation Master",
                 :updated-date "2019-07-29T21:37:00Z",
                 :download-count 80,
                 :tag-list [:dummy]
                 :source "wowinterface"
                 :source-id 25079
                 :game-track-list [:retail]}

          game-track :retail

          expected [{:download-url ""
                     :version "1.2.3"
                     :game-track game-track}]

          fixture (slurp (fixture-path "wowinterface-api--addon-details.json"))
          fake-routes {""
                       {:get (fn [_] {:status 200 :body fixture})}}]
      (with-fake-routes-in-isolation fake-routes
        (is (= expected (wowinterface-api/expand-summary given game-track)))))))

(deftest download-addon-404
  (testing "regular addon fetch that yields a 404 returns nil"
    (let [given {:url "",
                 :name "rotation-master",
                 :label "Rotation Master",
                 :updated-date "2019-07-29T21:37:00Z",
                 :download-count 80,
                 :tag-list [:dummy]
                 :source "wowinterface"
                 :source-id 25079
                 :game-track-list [:retail]}

          game-track :retail

          fake-routes {""
                       {:get (fn [req] {:status 404 :reason-phrase "Not Found" :body "<h1>Not Found</h1>"})}}]
      (with-fake-routes-in-isolation fake-routes
        (is (nil? (wowinterface-api/expand-summary given game-track)))))))

(deftest parse-user-string
  (testing "parsing wowinterface urls extracts the source-id"
    (let [cases [["" 8882]
                 ["-MasqueLiteStep.html" 8882]
                 ["-MasqueLiteStep" 8882]

                 ["-GSEGnomeSequencer-Enhanced-Advanc....html" 23921]

                 ["-9.0.6-bcc.zip?1621368578" nil]]]
      (doseq [[given expected] cases]
        (is (= expected (wowinterface-api/parse-user-string given)))))))

(deftest extract-aid
  (testing "the 'aid' query parameter can be extracted from a url if present"
    (let [cases [[nil nil]
                 ["" nil]
                 ["" nil]
                 ["" "119903"]
                 ["&aid=asdf" nil]]]
      (doseq [[given expected] cases]
        (is (= expected (wowinterface-api/extract-aid given)))))))

(deftest make-url
  (let [cases [[{} nil]
               [{:source-id nil} nil]
               [{:source-id 123} ""]]]
    (doseq [[given expected] cases]
      (is (= expected (wowinterface-api/make-url given))))))
