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

Refactor playlist to use a sorted-map

This commit is contained in:
heyarne 2019-03-05 00:06:04 +01:00
commit 644939c618
9 changed files with 206 additions and 229 deletions

View file

@ -1,17 +1,20 @@
(ns airsonic-ui.audio.core-test
(:require [airsonic-ui.audio.core :as audio]
[airsonic-ui.audio.playlist-test :as p]
[airsonic-ui.fixtures :as fixtures]
#_[airsonic-ui.audio.playlist-test :as p]
#_[airsonic-ui.fixtures :as fixtures]
[cljs.test :refer [deftest testing is]]))
(enable-console-print!)
(deftest current-song-subscription
(letfn [(current-song [db]
;; NOTE: Should the subscription be moved to the playlist.cljs?
#_(testing "Should provide information about the 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 p/fixture)))))
(= fixtures/song (current-song p/fixture))))
(testing "Should work fine when no song is playing"
(is (nil? (audio/current-song nil [:audio/current-song])))))
(deftest playback-status-subscription
(letfn [(is-playing? [playback-status]

View file

@ -1,7 +1,6 @@
(ns airsonic-ui.audio.playlist-test
(:require [cljs.test :refer [deftest testing is]]
[airsonic-ui.audio.playlist :as playlist]
[airsonic-ui.helpers :refer [find-where]]
[airsonic-ui.fixtures :as fixtures]
[airsonic-ui.test-helpers :as helpers]
[debux.cs.core :refer-macros [dbg]]))
@ -33,14 +32,22 @@
(deftest playlist-creation
(testing "Playlist creation"
(testing "should give us the correct current song"
(testing "should give us the correct current song for linear playback-mode"
(let [queue (song-queue 10)]
(doseq [playback-mode [:linear :shuffled]
repeat-mode [:repeat-none :repeat-single :repeat-all]]
(doseq [repeat-mode [:repeat-none :repeat-single :repeat-all]]
(is (same-song? (first queue)
(-> (playlist/->playlist queue :playback-mode playback-mode :repeat-mode repeat-mode)
(playlist/peek)))
(str playback-mode ", " repeat-mode)))))
(-> (playlist/->playlist queue :playback-mode :linear :repeat-mode repeat-mode)
(playlist/current-song)))
(str "repeat-mode: " repeat-mode)))))
(testing "any current song for shuffled playback mode"
(let [queue (song-queue 10)]
(doseq [repeat-mode [:repeat-none :repeat-single :repeat-all]]
(is (some? ((set queue)
(-> (playlist/->playlist queue :playback-mode :linear :repeat-mode repeat-mode)
(playlist/current-song))))
(str "repeat-mode: " repeat-mode)))))
(testing "should give us a playlist with the correct number of tracks"
(let [queue (song-queue 100)]
(doseq [playback-mode [:linear :shuffled]
@ -56,9 +63,9 @@
linear (playlist/->playlist queue :playback-mode :linear :repeat-mode :repeat-none)
shuffled (playlist/set-playback-mode linear :shuffled)]
(testing "should re-order the tracks"
(is (not= (map :playlist/order (:items shuffled)) (map :playlist/order (:items linear)))))
(is (not= (:items shuffled) (:items linear))))
(testing "should not change the currently playing track"
(is (same-song? (playlist/peek linear) (playlist/peek shuffled))))
(is (same-song? (playlist/current-song linear) (playlist/current-song shuffled))))
(testing "should not change the repeat mode"
(is (= (:repeat-mode shuffled) (:repeat-mode linear))))))
(testing "from shuffled to linear"
@ -66,10 +73,11 @@
shuffled (playlist/->playlist queue :playback-mode :shuffled :repeat-mode :repeat-none)
linear (playlist/set-playback-mode shuffled :linear)]
(testing "should set the correct order for tracks"
(is (every? #(apply same-song? %) (interleave queue (:items linear))))
(is (< (:playlist/order (first (:items linear))) (:playlist/order (last (:items linear))))))
(is (every? #(apply same-song? %) (interleave queue (vals (:items linear)))))
(is (< (:playlist/linear-order (meta (first (vals (:items linear)))))
(:playlist/linear-order (meta (last (vals (:items linear))))))))
(testing "should not change the currently playing track"
(is (same-song? (playlist/peek linear) (playlist/peek shuffled))))
(is (same-song? (playlist/current-song linear) (playlist/current-song shuffled))))
(testing "should not change the repeat mode"
(is (= (:repeat-mode shuffled) (:repeat-mode linear))))))))
@ -91,17 +99,18 @@
(let [queue (song-queue 5)
playlist (playlist/->playlist queue :playback-mode :linear :repeat-mode repeat-mode)]
(is (same-song? (nth queue 1) (-> (playlist/next-song playlist)
(playlist/peek)))
(playlist/current-song)))
(str repeat-mode ", skipped once"))
(is (same-song? (nth queue 2) (-> (playlist/next-song playlist)
(playlist/next-song)
(playlist/peek)))
(playlist/current-song)))
(str repeat-mode ", skipped twice")))))
;; TODO: Write this test
(testing "Should go back to the first song when repeat-mode is all and we played the last song")
(testing "Should always give the same track when repeat-mode is single"
(let [queue (song-queue 3)
playlist (playlist/->playlist queue :playback-mode :linear :repeat-mode :repeat-single)
played-back (map playlist/peek (iterate playlist/next-song playlist))]
played-back (map playlist/current-song (iterate playlist/next-song playlist))]
(is (same-song? (first queue) (nth played-back 0)))
(is (same-song? (first queue) (nth played-back 1)))
(is (same-song? (first queue) (nth played-back 2)))
@ -110,7 +119,7 @@
(is (nil? (-> (song-queue 1)
(playlist/->playlist :playback-mode :linear :repeat-mode :repeat-none)
(playlist/next-song)
(playlist/peek))))))
(playlist/current-song))))))
(deftest shuffled-next-song
(testing "Should play every track once when called for the entire queue"
@ -118,35 +127,34 @@
(let [length 10
playlist (playlist/->playlist (song-queue length) :playback-mode :shuffled :repeat-mode repeat-mode)
played-tracks (->> (iterate playlist/next-song playlist)
(map playlist/peek)
(map playlist/current-song)
(take length))]
(is (= (count played-tracks) (count (set played-tracks)))
(str repeat-mode)))))
(testing "Should re-shuffle the playlist when wrapping around and repeat-mode is all"
(testing "Should keep the song order when wrapping around and repeat-mode is all"
(let [playlist (playlist/->playlist (song-queue 100) :playback-mode :shuffled :repeat-mode :repeat-all)
[last-idx _] (find-where #(= (:playlist/order %) 99) (:items playlist))]
(is (not= (map :playlist/order (:items playlist))
(map :playlist/order (:items (-> (playlist/set-current-song playlist last-idx)
(playlist/next-song))))))))
next-playlist (-> (playlist/set-current-song playlist 99)
(playlist/next-song))]
(= (playlist/current-song playlist)
(playlist/current-song next-playlist))))
(testing "Should always give the same track when repeat-mode is single"
(let [queue (song-queue 3)
playlist (playlist/->playlist queue :playback-mode :shuffled :repeat-mode :repeat-single)
played-back (map playlist/peek (iterate playlist/next-song playlist))]
(is (same-song? (first queue) (nth played-back 0)))
(is (same-song? (first queue) (nth played-back 1)))
(is (same-song? (first queue) (nth played-back 2)))
(is (same-song? (first queue) (nth played-back 3)) "wrapping around")))
(let [playlist (playlist/->playlist (song-queue 10) :playback-mode :shuffled :repeat-mode :repeat-single)
played-back (map playlist/current-song (iterate playlist/next-song playlist))]
(dotimes [i 3]
(is (same-song? (nth played-back i) (nth played-back (inc i)))))))
(testing "Should stop playing at the end of the queue when repeat-mode is none"
(is (nil? (-> (song-queue 1)
(playlist/->playlist :playback-mode :linear :repeat-mode :repeat-none)
(playlist/next-song)
(playlist/peek))))))
(playlist/current-song))))))
(deftest linear-previous-song
(testing "Should always give the same track when repeat-mode is single"
(let [queue (song-queue 3)
playlist (playlist/->playlist queue :playback-mode :linear :repeat-mode :repeat-single)
played-back (map playlist/peek (iterate playlist/next-song playlist))]
played-back (map playlist/current-song (iterate playlist/next-song playlist))]
(is (same-song? (first queue) (nth played-back 0)))
(is (same-song? (first queue) (nth played-back 1)))
(is (same-song? (first queue) (nth played-back 2)))
@ -158,61 +166,61 @@
(is (same-song? (nth queue 1) (-> (playlist/next-song playlist)
(playlist/next-song)
(playlist/previous-song)
(playlist/peek)))))))
(testing "Should repeatedly give the first song when repeat-mode is none"
(playlist/current-song)))))))
;; TODO: Should it?
#_(testing "Should repeatedly give the first song when repeat-mode is none"
(let [queue (song-queue 3)
playlist (playlist/->playlist queue :playback-mode :linear :repeat-mode :repeat-none)]
(is (same-song? (first queue) (-> (playlist/previous-song playlist)
(playlist/peek))))))
(playlist/current-song))))))
(testing "Should wrap around to last song when repeat-mode is all"
(let [queue (song-queue 3)
playlist (playlist/->playlist queue :playback-mode :linear :repeat-mode :repeat-all)]
(is (same-song? (last queue) (-> (playlist/previous-song playlist)
(playlist/peek)))))))
(playlist/current-song)))))))
(deftest shuffled-previous-song
(with-redefs [shuffle reverse]
(testing "Should always give the same track when repeat-mode is single"
(let [queue (song-queue 3)
playlist (playlist/->playlist queue :playback-mode :shuffled :repeat-mode :repeat-single)
played-back (map playlist/peek (iterate playlist/next-song playlist))]
(is (same-song? (first queue) (nth played-back 0)))
(is (same-song? (first queue) (nth played-back 1)))
(is (same-song? (first queue) (nth played-back 2)))
(is (same-song? (first queue) (nth played-back 3)) "wrapping around")))
played-back (map playlist/current-song (iterate playlist/next-song playlist))]
(dotimes [i 3]
(is (same-song? (nth played-back i) (nth played-back (inc i)))))))
(testing "Should keep the playing order when repeat-mode is not single"
(doseq [repeat-mode '(:repeat-none :repeat-all)]
(let [queue (song-queue 3)
playlist (playlist/->playlist queue :playback-mode :shuffled :repeat-mode repeat-mode)]
(is (same-song? (playlist/peek playlist)
(is (same-song? (playlist/current-song playlist)
(-> playlist
(playlist/next-song)
(playlist/previous-song)
(playlist/peek)))
(playlist/current-song)))
(str "for repeat mode " repeat-mode))
(is (same-song? (-> (playlist/next-song playlist)
(playlist/peek))
(playlist/current-song))
(-> (playlist/next-song playlist)
(playlist/next-song)
(playlist/previous-song)
(playlist/peek)))
(playlist/current-song)))
(str "for repeat mode " repeat-mode)))))
(testing "Should re-shuffle when repeat-mode is all and we go back to before the first track"
(let [playlist (with-redefs [shuffle identity]
(playlist/->playlist (song-queue 10) :playback-mode :shuffled :repeat-mode :repeat-all))
playlist' (with-redefs [shuffle reverse]
(playlist/previous-song playlist))]
(is (not= (map :playlist/order (:items playlist)) (map :playlist/order (:items playlist'))))))))
(testing "Should keep the song order when repeat-mode is all and we go back to before the first track"
(let [playlist (playlist/->playlist (song-queue 10) :playback-mode :shuffled :repeat-mode :repeat-all)
next-playlist (-> (playlist/previous-song playlist)
(playlist/set-current-song 0))]
(is (= (playlist/current-song playlist)
(playlist/current-song next-playlist)))))))
(deftest set-current-song
(testing "Should correctly set the new song"
(let [queue (song-queue 3)
playlist (playlist/->playlist queue :playback-mode :shuffled :repeat-mode :repeat-single)
current-track (first queue)
next-track (-> (playlist/set-current-song playlist 1)
(playlist/peek))]
(is (not (nil? next-track)))
(is (not (same-song? current-track next-track))))))
(doseq [repeat-mode [:repeat-all :repeat-none]]
(let [queue (song-queue 3)
playlist (playlist/->playlist queue :playback-mode :shuffled :repeat-mode repeat-mode)
next-track (-> (playlist/set-current-song playlist 1)
(playlist/current-song))]
(is (not (nil? next-track)))
(is (not (same-song? (playlist/current-song playlist)
next-track)))))))
(deftest enqueue-last
(testing "Should make sure the song is played last"
@ -223,12 +231,12 @@
(playlist/->playlist queue :playback-mode playback-mode :repeat-mode repeat-mode))
played-back (->> (iterate playlist/next-song playlist)
(take (dec length))
(map #(:id (playlist/peek %)))
(map #(:id (playlist/current-song %)))
(set))
to-enqueue (song)
playlist' (playlist/enqueue-last playlist to-enqueue)]
(is (nil? (played-back (-> (->> (iterate playlist/next-song playlist')
(map playlist/peek))
(map playlist/current-song))
(nth length)
(:id))))
(str "for " playback-mode ", " repeat-mode)))))
@ -240,7 +248,7 @@
played-back-songs (fn played-back-songs [playlist]
(->> (iterate playlist/next-song playlist)
(take length)
(map playlist/peek)
(map playlist/current-song)
(map :playlist/order)))
played-back (played-back-songs playlist)
played-back' (played-back-songs (playlist/enqueue-last playlist (song)))]
@ -256,4 +264,4 @@
next-song (song)]
(is (same-song? next-song (-> (playlist/enqueue-next playlist next-song)
(playlist/next-song)
(playlist/peek))))))))
(playlist/current-song))))))))

View file

@ -2,17 +2,6 @@
(:require [cljs.test :refer [deftest testing is]]
[airsonic-ui.helpers :as helpers]))
(deftest find-where
(testing "Finds the correct item and index"
(is (= [0 1] (helpers/find-where (partial = 1) (range 1 10))))
(is (= [2 {:foo true, :bar false}] (helpers/find-where :foo '({}
{:foo false
:bar true}
{:foo true
:bar false})))))
(testing "Returns nil when nothing is found"
(is (nil? (helpers/find-where (partial = 2) (range 2))))))
(deftest add-classes
(testing "Should add classes to a simple hiccup keyword"
(is (= :div.foo (helpers/add-classes :div :foo)))
@ -43,9 +32,3 @@
(is (= "59:00" (helpers/format-duration (* 59 60) :brief? true)))
(is (= "01:00" (helpers/format-duration 60 :brief? true)))
(is (= "00:05" (helpers/format-duration 5 :brief? true)))))
(deftest vector-move
(testing "Should correctly move items in a vector"
(is (= [1] (helpers/vector-move [1] 0 0)))
(is (= [2 1] (helpers/vector-move [1 2] 0 1) (helpers/vector-move [1 2] 1 0)))
(is (= [1 4 2 3 5] (helpers/vector-move [1 2 3 4 5] 3 1)))))