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

Start restructuring audio playback, add some tests for audio

Fixes #15 where audio was not stopped on logout
This commit is contained in:
Arne Schlüter 2018-08-01 18:36:47 +02:00
commit 80225d46b1
10 changed files with 187 additions and 69 deletions

View file

@ -0,0 +1,40 @@
(ns airsonic-ui.audio-test
(:require [airsonic-ui.audio :as audio]
[airsonic-ui.fixtures :as fixtures]
[airsonic-ui.test-helpers :as helpers]
[cljs.test :refer [deftest testing is]]))
(enable-console-print!)
(defn- simulate-playlist [n ]
(repeatedly n #(hash-map :id (rand-int 9999)
:coverArt (rand-int 9999)
:year (+ 1900 (rand-int 118))
:artist (helpers/rand-str)
:aristId (rand-int 100000)
:title (helpers/rand-str)
:album (helpers/rand-str))))
(def fixture
{:audio {:current-song fixtures/song
:playlist (simulate-playlist 20)
:playback-status fixtures/playback-status}})
(deftest current-song
(letfn [(current-song [db]
(-> (audio/summary db [:audio/summary])
(audio/current-song [:audio/current-song])))]
(testing "Should provide information about the song"
(= fixtures/song (current-song fixture)))))
(deftest playback-status
(letfn [(is-playing? [playback-status]
(audio/is-playing? playback-status [:audio/is-playing?]))]
(testing "Should be shown as not playing when the song is paused or has ended"
(is (not (is-playing? {:paused? true, :ended? false})))
(is (not (is-playing? {:paused? false, :ended? true}))))
(testing "Should be shown as playing when the song is not paused or finished"
(is (is-playing? {:paused? false, :ended? false})))))
#_(deftest current-playlist
(testing "Should show the complete playlist"))

View file

@ -93,7 +93,9 @@
(testing "Should redirect to the login screen"
(is (dispatches? fx [:routes/do-navigation [::routes/login]])))
(testing "Should reset the app-db"
(is (= db/default-db (:db fx)))))
(is (= db/default-db (:db fx))))
(testing "Should stop currently playing songs"
(is (contains? fx :audio/stop))))
(testing "Should be able to keep a redirection parameter"
(let [redirect [:route {:with-data #{1 2 3 4 5}}]
navigation-event (:dispatch (events/logout {} [:_ :redirect-to redirect]))]
@ -102,13 +104,15 @@
(is (= ::routes/login route-id))
(is (contains? query :redirect))))))
(defn- first-notification [fx]
(-> (get-in fx [:db :notifications]) vals first))
(deftest api-interaction
(testing "Should show an error notification when airsonic responds with an error"
(let [fx (events/good-api-response {} [:_ (:error fixtures/responses)])]
(is (= :error (-> fx :dispatch second))))))
(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))
(deftest user-notifications
(testing "Should be able to display a message with an assigned level"

View file

@ -43,3 +43,11 @@
:contentType "audio/mpeg",
:album "Reincarnations, Pt. 2 - The Remix Chapter 2009 - 2014",
:track 14})
(def playback-status
{:ended? false
:loop? false
:muted? false
:paused? false
:current-src "https://londe.arnes.space/rest/stream?f=json&c=airsonic-ui-cljs&v=1.15.0&id=9574&u=arne&p=27h-%25bO%5B8-.ys%40SQ%7Bg%24-%5B5NZkX%7Dw%24NNwY%263DPATi%2CgaFoH%40e"
:current-time 3.477029})

View file

@ -6,3 +6,14 @@
[cofx ev]
(let [all-events (conj (get cofx :dispatch-n []) (:dispatch cofx))]
(boolean (some #(= ev (if (vector? ev) % (first %))) all-events))))
(defn rand-str
"Generates a random string; ported from https://stackoverflow.com/a/27747377/2345852"
([] (rand-str 40))
([len]
(let [arr (js/Uint8Array. (/ len 2))]
(.. js/window -crypto (getRandomValues arr))
(.. js/Array
(from arr #(-> (str 0 (.toString % 16))
(.substr -2)))
(join "")))))

View file

@ -1,17 +1,24 @@
(ns airsonic-ui.test-helpers-test
(:require [cljs.test :refer [deftest testing is]]
[airsonic-ui.test-helpers :refer [dispatches?]]))
[airsonic-ui.test-helpers :as h]))
(deftest dispatch-helper
(testing "single dispatch"
(is (false? (dispatches? {} :foo)))
(is (true? (dispatches? {:dispatch [:foo 1 2 3]} :foo)))
(is (false? (dispatches? {:dispatch [:foo 1 2 3]} :bar)))
(is (true? (dispatches? {:dispatch [:foo 1 2 3]} [:foo 1 2 3])))
(is (false? (dispatches? {:dispatch [:foo 1 2 3]} [:bar 2 3]))))
(testing "multiple dispatch"
(is (false? (dispatches? {:dispatch-n [[:bar]]} :foo)))
(is (true? (dispatches? {:dispatch-n [[:foo 1 2 3]]} :foo)))
(is (false? (dispatches? {:dispatch-n [[:foo 1 2 3]]} :bar)))
(is (dispatches? {:dispatch-n [[:foo 1 2 3]]} [:foo 1 2 3]))
(is (false? (dispatches? {:dispatch-n [[:foo 1 2 3]]} [:bar 2 3])))))
(testing "Should identify singly dispatched events"
(is (false? (h/dispatches? {} :foo)))
(is (true? (h/dispatches? {:dispatch [:foo 1 2 3]} :foo)))
(is (false? (h/dispatches? {:dispatch [:foo 1 2 3]} :bar)))
(is (true? (h/dispatches? {:dispatch [:foo 1 2 3]} [:foo 1 2 3])))
(is (false? (h/dispatches? {:dispatch [:foo 1 2 3]} [:bar 2 3]))))
(testing "Should identify an event along multiple dispatched events"
(is (false? (h/dispatches? {:dispatch-n [[:bar]]} :foo)))
(is (true? (h/dispatches? {:dispatch-n [[:foo 1 2 3]]} :foo)))
(is (false? (h/dispatches? {:dispatch-n [[:foo 1 2 3]]} :bar)))
(is (h/dispatches? {:dispatch-n [[:foo 1 2 3]]} [:foo 1 2 3]))
(is (false? (h/dispatches? {:dispatch-n [[:foo 1 2 3]]} [:bar 2 3])))))
(deftest rand-str
(testing "Generates strings"
(is (string? (h/rand-str)))
(is (string? (h/rand-str 20))))
(testing "Should respect the length for even lengths"
(is (= 124 (count (h/rand-str 124))))))