1
0
Fork 0
mirror of https://github.com/heyarne/airsonic-ui.git synced 2026-05-06 18:33:38 +02:00
airsonic-ui/test/cljs/airsonic_ui/api/helpers_test.cljs
Arne Schlüter fa485bbf42 Merge incomplete podcast support
commit 4ac35d6530f7770e7b80307321c72541a55e2c8e
Author: Arne Schlüter <arne@schlueter.is>
Date:   Mon Oct 8 21:09:04 2018 +0200

    Stub out podcast detail view

commit 60742a22e93bfe6f432e06d56d3e4da671184559
Author: Arne Schlüter <arne@schlueter.is>
Date:   Tue Sep 18 23:02:39 2018 +0200

    Simplify api helpers; closes #16

commit 8bbc79ebf4dbbe3dbfa08cb4c7c1edd341d507eb
Author: Arne Schlüter <arne@schlueter.is>
Date:   Tue Sep 18 19:39:17 2018 +0200

    Adjust `stream-url` to work with podcast episodes

commit 991ba5b65230a7429c160ca1b7968ecbb8595e0b
Author: Arne Schlüter <arne@schlueter.is>
Date:   Tue Sep 18 19:14:08 2018 +0200

    Fix breadcrumbs for podcasts

commit 37c3a894eded2fe37f9af031d3132c7175702266
Author: Arne Schlüter <arne@schlueter.is>
Date:   Tue Sep 18 15:11:54 2018 +0200

    Stub out overview for podcasts
2018-10-08 21:15:56 +02:00

76 lines
3.6 KiB
Clojure

(ns airsonic-ui.api.helpers-test
(:require [cljs.test :refer [deftest testing is]]
[clojure.string :as str]
[airsonic-ui.fixtures :as fixtures :refer [responses]]
[airsonic-ui.api.helpers :as api]))
(defn- url
"Construct a url with no params"
[server endpoint]
(api/url server endpoint {}))
(def fixtures
{:default-url (url {:server "http://localhost:8080"} "ping")})
(deftest general-url-construction
(testing "Handles missing slashes"
(is (true? (str/starts-with? (url {:server "http://localhost:8080"} "ping") "http://localhost:8080/rest/ping")))
(is (true? (str/starts-with? (url {:server "http://localhost:8080/"} "ping") "http://localhost:8080/rest/ping"))))
(testing "Should set correct default parameters"
(is (string? (re-find #"f=json" (fixtures :default-url))))
(is (string? (re-find #"v=1\.15\.0" (fixtures :default-url))))))
(deftest parameter-encoding
(testing "Should escape url parameters"
(let [query "äöüß"
encoded-str (js/encodeURIComponent query)]
(is (str/includes? (api/url {:server "http://localhost"} "search3" {:query query}) encoded-str)))))
(deftest stream-urls
(testing "Should construct the url based on a song's id"
(let [stream-url (api/stream-url {:server "http://localhost"} fixtures/song)]
(is (str/includes? stream-url (str "id=" (:id fixtures/song))))))
(testing "Should also work for podcasts"
(let [stream-url (api/stream-url {:server "http://localhost"} fixtures/podcast-episode)]
(is (str/includes? stream-url (str "id=" (:streamId fixtures/podcast-episode)))))))
(deftest cover-urls
(let [album {:coverArt "cover-99999"}]
(testing "Should construct the url based on an item's cover-id"
(is (true? (str/includes? (api/cover-url {:server "http://server.tld"} album -1) (str "id=" (:coverArt album))))))
(testing "Should scale an image to a given size"
(is (true? (str/includes? (api/cover-url {:server "http://server.tld"} album 48) "size=48"))))))
(deftest response-handling
(testing "Should unwrap responses"
(let [response (:ok responses)]
(is (= (get-in response [:subsonic-response :scanStatus])
(api/unwrap-response response)))))
(testing "Should detect errors"
(is (true? (api/is-error? (:error responses))))
(is (false? (api/is-error? (:ok responses)))))
(testing "Should throw an informative error when trying to unwrap an erroneous response"
(let [error-response (:error responses)]
(is (thrown? ExceptionInfo (api/unwrap-response error-response)))
(try
(api/unwrap-response error-response)
(catch ExceptionInfo e
(is (= (get-in error-response [:subsonic-response :error]) (ex-data e))))))))
(deftest error-recognition
(testing "Should detect error responses"
(is (true? (api/is-error? (:error responses))))
(is (true? (api/is-error? (:auth-failure responses)))))
(testing "Should pass on good responses"
(is (false? (api/is-error? (:ok responses))))
(is (false? (api/is-error? (:ping-success responses))))))
(deftest content-type
(testing "Should detect whether the data we look at represents a song"
(is (= :content-type/song (api/content-type fixtures/song))))
(testing "Should detect whether the data we look at represents an artist"
(is (= :content-type/artist (api/content-type fixtures/artist)))
(is (= :content-type/artist (api/content-type (dissoc fixtures/artist :coverArt)))))
(testing "Should detect whether the data we look at represents an album"
(is (= :content-type/album (api/content-type fixtures/album)))
(is (= :content-type/album (api/content-type (dissoc fixtures/album :coverArt))))))