1
0
Fork 0
mirror of https://github.com/heyarne/airsonic-ui.git synced 2026-05-06 18:33:38 +02:00

Cache API responses and make sure we remember more than just one

Closes #21.
Squashed commit of the following:

commit 964b29cf127cf51de86543d040bcb6c674b36d7e
Author: Arne Schlüter <arne@schlueter.is>
Date:   Wed Aug 22 17:56:48 2018 +0200

    Pass content for current route nicely to views

commit b469a0a4b69457ddf3a679ac1acc82fbaffdc8fd
Author: Arne Schlüter <arne@schlueter.is>
Date:   Wed Aug 22 16:01:04 2018 +0200

    Add response cache in app-db

commit da9faf89138f42ee544efc64c2e46787091b3dc7
Author: Arne Schlüter <arne@schlueter.is>
Date:   Wed Aug 22 13:40:57 2018 +0200

    Move api helpers and tests to own namespace
This commit is contained in:
Arne Schlüter 2018-08-22 17:58:03 +02:00
commit 2cdae0d683
13 changed files with 222 additions and 94 deletions

View file

@ -0,0 +1,50 @@
(ns airsonic-ui.api.events-test
(:require [cljs.test :refer-macros [deftest testing is]]
[airsonic-ui.api.events :as events]
[airsonic-ui.fixtures :as fixtures]))
(enable-console-print!)
(deftest api-failure-notifcations
(testing "Should show an error notification when airsonic responds with an error"
(let [fx (events/good-api-response {} [:api/good-response "ping" nil (:error fixtures/responses)])
ev (:dispatch fx)]
(is (= :notification/show (first ev)))
(is (= :error (second ev))))))
(deftest cached-api-requests
(letfn [(cache [fx [endpoint params]]
(get-in fx [:db :api/responses [endpoint params]]))]
(testing "Should be cached"
(testing "when the response was successful"
(let [endpoint "getScanStatus"
successful (events/good-api-response {} [:api/good-response endpoint nil (:ok fixtures/responses)])
unsuccessful (events/good-api-response {} [:api/good-response endpoint nil (:error fixtures/responses)])]
(is (map? (cache successful [endpoint])))
(is (nil? (cache unsuccessful [endpoint])))))
(testing "in an unwrapped format"
(let [endpoint "getScanStatus"
fx (events/good-api-response {} [:api/good-response endpoint nil (:ok fixtures/responses)])]
(is (= #{:count :scanning} (set (keys (cache fx [endpoint]))))))))
(testing "When being issued"
(let [endpoint "getScanStatus"
fx (events/api-request {:db {:credentials (select-keys fixtures/credentials [:server])}}
[:api/request endpoint])]
(testing "should send an http request"
(is (contains? fx :http-xhrio)))
(testing "should indicate that a request is ongoing"
(is (true? (:api/is-loading? (cache fx [endpoint]))) "for non-cached responses")
(is (true? (-> (events/good-api-response fx [:api/good-response endpoint nil (:ok fixtures/responses)])
(events/api-request [:api/request endpoint])
(cache [endpoint])
:api/is-loading?)) "for cached responses"))
(testing "should remove the indication that a request is ongoing when there is a response"
(is (not (:api/is-loading? (-> (events/good-api-response fx [:api/good-response endpoint nil (:ok fixtures/responses)])
(cache [endpoint])))) "for a good response")
(is (not (:api/is-loading? (-> (merge fx (events/good-api-response fx [:api/good-response endpoint nil (:error fixtures/responses)]))
(cache [endpoint])))) "when an error is returned")
(is (not (:api/is-loading? (-> (merge fx (events/failed-api-response fx [:api/failed-response endpoint]))
(cache [endpoint])))) "when communication with the server failed"))))
(testing "Should be able to avoid the cache"
;; FIXME: Implement this
)))

View file

@ -1,8 +1,8 @@
(ns airsonic-ui.utils.api-test
(ns airsonic-ui.api.helpers-test
(:require [cljs.test :refer [deftest testing is]]
[clojure.string :as str]
[airsonic-ui.fixtures :refer [responses]]
[airsonic-ui.utils.api :as api]))
[airsonic-ui.api.helpers :as api]))
(defn- url
"Construct a url with no params"

View file

@ -0,0 +1,29 @@
(ns airsonic-ui.api.subs-test
(:require [cljs.test :refer-macros [deftest testing is]]
[airsonic-ui.api.subs :as sub]))
(enable-console-print!)
(deftest endpoint-keywordification
(testing "Should strip prefixes"
(is (= :artist-info (sub/endpoint->kw "getArtistInfo")))
(is (= :jukebox-control (sub/endpoint->kw "jukeboxControl"))))
(testing "Should strip trailing numbers"
(is (= :album-list (sub/endpoint->kw "getAlbumList2")))
(is (= :search (sub/endpoint->kw "search3")))))
(deftest responses-for-route
(testing "Should return all cached responses for a route"
(let [route-events [[:api/request "getAlbumList2" {:type "recent", :size 18}]
[:event/should-be-ignored]
[:api/request "getArtistInfo" {:id "128"}]]
db {:api/responses {["getAlbumList2" {:type "recent" :size 18}]
{:album [{:genre "foo", :artistId "12345"}
{:genre "electronic", :artistId "9999"}]}
["getArtistInfo" {:id "128"}]
{:biography "Interesting bio"
:largeImageUrl "https://lastfm-img2.akamaized.net/i/u/300x300/fb416b59cd694587aca0b2dec8f41198.png"}}}]
(is (= {:album-list (get-in db [:api/responses ["getAlbumList2" {:type "recent" :size 18}]])
:artist-info (get-in db [:api/responses ["getArtistInfo" {:id "128"}]])}
(sub/route-data db [:api/route-data route-events]))))))

View file

@ -60,7 +60,7 @@
(testing "invokes correct callback on server response"
(is (= [:credentials/authentication-response fixtures/credentials] (:on-success request))))
(testing "invokes correct callback when server is not reachable"
(is (= [:api/bad-response] (:on-failure request))))))
(is (= [:api/failed-response] (:on-failure request))))))
(deftest authentication-response
(testing "On success"
@ -104,13 +104,6 @@
(is (= ::routes/login route-id))
(is (contains? query :redirect))))))
(deftest api-interaction
(testing "Should show an error notification when airsonic responds with an error"
(let [fx (events/good-api-response {} [:_ (:error fixtures/responses)])
ev (:dispatch fx)]
(is (= :notification/show (first ev)))
(is (= :error (second ev))))))
(defn- first-notification [fx]
(-> (get-in fx [:db :notifications]) vals first))

View file

@ -1,7 +1,7 @@
(ns airsonic-ui.subs-test
(:require [cljs.test :refer [deftest testing is]]
[airsonic-ui.fixtures :as fixtures]
[airsonic-ui.utils.api :as api]
[airsonic-ui.api.helpers :as api]
[airsonic-ui.subs :as subs]))
(deftest booting
@ -10,17 +10,17 @@
is-booting? (fn is-booting? [db]
(subs/is-booting? db [:subs/is-booting?]))]
(testing "Should be false when we don't have previous credentials"
(is (not (is-booting? {:current-route route})))
(is (not (is-booting? {:current-route route
(is (not (is-booting? {:routes/current-route route})))
(is (not (is-booting? {:routes/current-route route
:credentials {}}))) )
(testing "Should be true when we have unverified credentials"
(is (true? (is-booting? {:current-route route
(is (true? (is-booting? {:routes/current-route route
:credentials fixtures/credentials}))))
(testing "Should be false when we have verified credentials"
(is (not (is-booting? {:current-route route
(is (not (is-booting? {:routes/current-route route
:credentials verified-credentials}))))
(testing "Should be true when routing is not yet set up"
(is (true? (is-booting? {:current-route nil
(is (true? (is-booting? {:routes/current-route nil
:credentials verified-credentials}))))))
(deftest cover-images