2022-09-14 16:45:08 +00:00
|
|
|
import { reactive } from 'vue'
|
2022-05-15 14:57:28 +00:00
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
|
|
|
import { expect, it } from 'vitest'
|
2022-07-23 10:00:14 +00:00
|
|
|
import factory from 'factoria'
|
2022-09-15 09:07:25 +00:00
|
|
|
import { http } from '@/services'
|
2022-09-14 16:45:08 +00:00
|
|
|
import { queueStore, songStore } from '.'
|
2022-05-15 14:57:28 +00:00
|
|
|
|
2022-12-02 16:17:37 +00:00
|
|
|
let songs: Song[]
|
2022-05-15 14:57:28 +00:00
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
|
|
|
protected beforeEach () {
|
|
|
|
super.beforeEach(() => {
|
2024-06-01 18:02:27 +00:00
|
|
|
songs = factory('song', 3)
|
2024-05-19 05:49:42 +00:00
|
|
|
queueStore.state.playables = reactive(songs)
|
2022-05-15 14:57:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
protected test () {
|
|
|
|
it('returns all queued songs', () => expect(queueStore.all).toEqual(songs))
|
|
|
|
|
2022-07-23 10:00:14 +00:00
|
|
|
it('returns the first queued song', () => expect(queueStore.first).toEqual(songs[0]))
|
|
|
|
|
|
|
|
it('returns the last queued song', () => expect(queueStore.last).toEqual(songs[2]))
|
|
|
|
|
|
|
|
it('queues to bottom', () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const song = factory('song')
|
2024-01-01 11:40:21 +00:00
|
|
|
const putMock = this.mock(http, 'put')
|
2022-07-23 10:00:14 +00:00
|
|
|
queueStore.queue(song)
|
|
|
|
|
|
|
|
expect(queueStore.all).toHaveLength(4)
|
|
|
|
expect(queueStore.last).toEqual(song)
|
2024-01-01 11:40:21 +00:00
|
|
|
expect(putMock).toHaveBeenCalledWith('queue/state', { songs: queueStore.all.map(song => song.id) })
|
2022-05-15 14:57:28 +00:00
|
|
|
})
|
|
|
|
|
2022-07-23 10:00:14 +00:00
|
|
|
it('queues to top', () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const song = factory('song')
|
2024-01-01 11:40:21 +00:00
|
|
|
const putMock = this.mock(http, 'put')
|
2022-07-23 10:00:14 +00:00
|
|
|
queueStore.queueToTop(song)
|
|
|
|
|
|
|
|
expect(queueStore.all).toHaveLength(4)
|
|
|
|
expect(queueStore.first).toEqual(song)
|
2024-01-01 11:40:21 +00:00
|
|
|
expect(putMock).toHaveBeenCalledWith('queue/state', { songs: queueStore.all.map(song => song.id) })
|
2022-05-15 14:57:28 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('replaces the whole queue', () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const newSongs = factory('song', 2)
|
2024-01-01 11:40:21 +00:00
|
|
|
const putMock = this.mock(http, 'put')
|
2022-07-23 10:00:14 +00:00
|
|
|
queueStore.replaceQueueWith(newSongs)
|
|
|
|
|
|
|
|
expect(queueStore.all).toEqual(newSongs)
|
2024-01-01 11:40:21 +00:00
|
|
|
expect(putMock).toHaveBeenCalledWith('queue/state', { songs: newSongs.map(song => song.id) })
|
2022-05-15 14:57:28 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('removes a song from queue', () => {
|
2024-01-01 11:40:21 +00:00
|
|
|
const putMock = this.mock(http, 'put')
|
2022-07-23 10:00:14 +00:00
|
|
|
queueStore.unqueue(songs[1])
|
|
|
|
|
|
|
|
expect(queueStore.all).toEqual([songs[0], songs[2]])
|
2024-01-01 11:40:21 +00:00
|
|
|
expect(putMock).toHaveBeenCalledWith('queue/state', { songs: queueStore.all.map(song => song.id) })
|
2022-05-15 14:57:28 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('removes multiple songs from queue', () => {
|
2024-01-01 11:40:21 +00:00
|
|
|
const putMock = this.mock(http, 'put')
|
2022-07-23 10:00:14 +00:00
|
|
|
queueStore.unqueue([songs[1], songs[0]])
|
|
|
|
|
|
|
|
expect(queueStore.all).toEqual([songs[2]])
|
2024-01-01 11:40:21 +00:00
|
|
|
expect(putMock).toHaveBeenCalledWith('queue/state', { songs: queueStore.all.map(song => song.id) })
|
2022-05-15 14:57:28 +00:00
|
|
|
})
|
|
|
|
|
2022-07-23 10:00:14 +00:00
|
|
|
it('clears the queue', () => {
|
2024-01-01 11:40:21 +00:00
|
|
|
const putMock = this.mock(http, 'put')
|
2022-05-15 14:57:28 +00:00
|
|
|
queueStore.clear()
|
2024-01-01 11:40:21 +00:00
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
expect(queueStore.state.playables).toHaveLength(0)
|
2024-01-01 11:40:21 +00:00
|
|
|
expect(putMock).toHaveBeenCalledWith('queue/state', { songs: [] })
|
2022-05-15 14:57:28 +00:00
|
|
|
})
|
|
|
|
|
2022-09-14 16:45:08 +00:00
|
|
|
it.each<[PlaybackState]>([['Playing'], ['Paused']])('identifies the current song by %s state', state => {
|
2024-05-19 05:49:42 +00:00
|
|
|
queueStore.state.playables[1].playback_state = state
|
|
|
|
expect(queueStore.current).toEqual(queueStore.state.playables[1])
|
2022-05-15 14:57:28 +00:00
|
|
|
})
|
|
|
|
|
2022-07-23 10:00:14 +00:00
|
|
|
it('gets the next song in queue', () => {
|
2024-05-19 05:49:42 +00:00
|
|
|
queueStore.state.playables[1].playback_state = 'Playing'
|
|
|
|
expect(queueStore.next).toEqual(queueStore.state.playables[2])
|
2022-07-23 10:00:14 +00:00
|
|
|
})
|
2022-05-15 14:57:28 +00:00
|
|
|
|
|
|
|
it('returns undefined as next song if at end of queue', () => {
|
2024-05-19 05:49:42 +00:00
|
|
|
queueStore.state.playables[2].playback_state = 'Playing'
|
2022-05-15 14:57:28 +00:00
|
|
|
expect(queueStore.next).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
2022-07-23 10:00:14 +00:00
|
|
|
it('gets the previous song in queue', () => {
|
2024-05-19 05:49:42 +00:00
|
|
|
queueStore.state.playables[1].playback_state = 'Playing'
|
|
|
|
expect(queueStore.previous).toEqual(queueStore.state.playables[0])
|
2022-07-23 10:00:14 +00:00
|
|
|
})
|
2022-05-15 14:57:28 +00:00
|
|
|
|
|
|
|
it('returns undefined as previous song if at beginning of queue', () => {
|
2024-05-19 05:49:42 +00:00
|
|
|
queueStore.state.playables[0].playback_state = 'Playing'
|
2022-05-15 14:57:28 +00:00
|
|
|
expect(queueStore.previous).toBeUndefined()
|
|
|
|
})
|
2022-07-23 10:00:14 +00:00
|
|
|
|
|
|
|
it('fetches random songs to queue', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const songs = factory('song', 3)
|
2022-09-15 09:07:25 +00:00
|
|
|
const getMock = this.mock(http, 'get').mockResolvedValue(songs)
|
2022-07-23 10:00:14 +00:00
|
|
|
const syncMock = this.mock(songStore, 'syncWithVault', songs)
|
2024-01-01 11:40:21 +00:00
|
|
|
const putMock = this.mock(http, 'put')
|
2022-07-23 11:07:14 +00:00
|
|
|
|
2022-07-23 10:00:14 +00:00
|
|
|
await queueStore.fetchRandom(3)
|
|
|
|
|
|
|
|
expect(getMock).toHaveBeenCalledWith('queue/fetch?order=rand&limit=3')
|
|
|
|
expect(syncMock).toHaveBeenCalledWith(songs)
|
|
|
|
expect(queueStore.all).toEqual(songs)
|
2024-01-01 11:40:21 +00:00
|
|
|
expect(putMock).toHaveBeenCalledWith('queue/state', { songs: songs.map(song => song.id) })
|
2022-07-23 10:00:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('fetches random songs to queue with a custom order', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const songs = factory('song', 3)
|
2022-09-15 09:07:25 +00:00
|
|
|
const getMock = this.mock(http, 'get').mockResolvedValue(songs)
|
2022-07-23 10:00:14 +00:00
|
|
|
const syncMock = this.mock(songStore, 'syncWithVault', songs)
|
2024-01-01 11:40:21 +00:00
|
|
|
const putMock = this.mock(http, 'put')
|
2022-07-23 10:00:14 +00:00
|
|
|
|
|
|
|
await queueStore.fetchInOrder('title', 'desc', 3)
|
|
|
|
|
|
|
|
expect(getMock).toHaveBeenCalledWith('queue/fetch?order=desc&sort=title&limit=3')
|
|
|
|
expect(syncMock).toHaveBeenCalledWith(songs)
|
|
|
|
expect(queueStore.all).toEqual(songs)
|
2024-01-01 11:40:21 +00:00
|
|
|
expect(putMock).toHaveBeenCalledWith('queue/state', { songs: songs.map(song => song.id) })
|
2022-07-23 10:00:14 +00:00
|
|
|
})
|
2022-05-15 14:57:28 +00:00
|
|
|
}
|
|
|
|
}
|