The mood screen now shows recommendation cards with thumbnails, a refresh action, and accept/reject buttons. Accepting a library match adds it as a seed immediately; other accepted tracks are marked as queued for download. The player persists the session, queue and current track, restores them paused on the next launch and starts the track on the next tap. While offline the store retries the library sync with exponential backoff, downloads retry three times before failing, and covers plus YT thumbnails are cached for the app's lifetime.
This commit is contained in:
1 parent
2234192dc7
commit
dc719f3e1a
8 files changed
+582
-91
No files matched your search
@@ -1,9 +1,16 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import io.ktor.client.engine.mock.MockEngine
|
||||
import io.ktor.client.engine.mock.respond
|
||||
import io.ktor.client.engine.mock.respondError
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.headersOf
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.test.TestScope
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.test.advanceTimeBy
|
||||
import kotlinx.coroutines.test.runCurrent
|
||||
import kotlinx.coroutines.test.runTest
|
||||
@@ -15,9 +22,12 @@ import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
import win.taptap.musicmanager.client.api.ApiClient
|
||||
import win.taptap.musicmanager.client.api.Song
|
||||
import win.taptap.musicmanager.client.api.User
|
||||
import win.taptap.musicmanager.client.api.configuredHttpClient
|
||||
import win.taptap.musicmanager.client.data.CachedState
|
||||
import win.taptap.musicmanager.client.data.LibraryCache
|
||||
import win.taptap.musicmanager.client.data.PlaybackSnapshot
|
||||
import win.taptap.musicmanager.client.data.SessionState
|
||||
import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
import win.taptap.musicmanager.client.store.AppStore
|
||||
|
||||
@@ -110,6 +120,134 @@ class AppStoreTest {
|
||||
store.release()
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun `bootstrap restores the saved queue and starts it on play`() = runTest {
|
||||
val storage = MemoryStorage()
|
||||
val cache = LibraryCache(storage)
|
||||
cache.savePlayback(
|
||||
PlaybackSnapshot(
|
||||
queue = listOf(song("b")),
|
||||
current = song("a"),
|
||||
),
|
||||
)
|
||||
val player = FakePlayer()
|
||||
val store = store(storage, cache, player)
|
||||
runCurrent()
|
||||
|
||||
val restored = store.state.value
|
||||
assertEquals("a", restored.current?.id)
|
||||
assertEquals(listOf("b"), restored.queue.map { it.id })
|
||||
assertFalse(restored.playing)
|
||||
assertTrue(player.played.isEmpty())
|
||||
|
||||
store.togglePlayPause()
|
||||
runCurrent()
|
||||
|
||||
assertTrue(store.state.value.playing)
|
||||
assertEquals(listOf("a"), player.played)
|
||||
store.release()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `accepting a recommendation replaces the selected mood`() = runTest {
|
||||
val storage = MemoryStorage()
|
||||
val cache = LibraryCache(storage)
|
||||
cache.saveSession(
|
||||
SessionState("http://music", "token", User("alice", admin = false)),
|
||||
)
|
||||
val player = FakePlayer()
|
||||
val detail = moodDetailJson(state = "new", seedCount = 1)
|
||||
val acceptedDetail = moodDetailJson(state = "accepted", seedCount = 2)
|
||||
val engine = MockEngine { request ->
|
||||
when (request.url.encodedPath) {
|
||||
"/api/v1/moods" -> respond("""{"moods":[]}""", HttpStatusCode.OK, jsonHeaders)
|
||||
"/api/v1/library/songs" -> respond(
|
||||
"""{"songs":[],"revision":"r1"}""",
|
||||
HttpStatusCode.OK,
|
||||
jsonHeaders,
|
||||
)
|
||||
"/api/v1/moods/mo-1" -> respond(detail, HttpStatusCode.OK, jsonHeaders)
|
||||
"/api/v1/moods/mo-1/preview" -> respond(
|
||||
"""{"songs":[]}""",
|
||||
HttpStatusCode.OK,
|
||||
jsonHeaders,
|
||||
)
|
||||
"/api/v1/moods/mo-1/recommendations/v-1/accept" -> respond(
|
||||
"""{"detail":$acceptedDetail,"recommendation":${recommendationJson("accepted")},"seedAdded":true}""",
|
||||
HttpStatusCode.OK,
|
||||
jsonHeaders,
|
||||
)
|
||||
"/api/v1/moods/mo-1/recommendations/v-1/reject" -> respond(
|
||||
"""{"detail":$detail,"recommendation":${recommendationJson("rejected")},"seedAdded":false}""",
|
||||
HttpStatusCode.OK,
|
||||
jsonHeaders,
|
||||
)
|
||||
else -> respondError(HttpStatusCode.NotFound)
|
||||
}
|
||||
}
|
||||
val store = AppStore(
|
||||
scope = this,
|
||||
api = ApiClient(configuredHttpClient(engine)),
|
||||
storage = storage,
|
||||
player = player,
|
||||
cache = cache,
|
||||
)
|
||||
runCurrent()
|
||||
store.openMood("mo-1")
|
||||
awaitState { store.state.value.selectedMood != null || store.state.value.message != null }
|
||||
|
||||
assertEquals(
|
||||
"new",
|
||||
store.state.value.selectedMood?.recommendations?.single()?.state,
|
||||
"message=" + store.state.value.message,
|
||||
)
|
||||
|
||||
store.acceptRecommendation("v-1")
|
||||
awaitState {
|
||||
store.state.value.selectedMood?.recommendations?.single()?.state == "accepted"
|
||||
}
|
||||
|
||||
val state = store.state.value
|
||||
assertEquals("accepted", state.selectedMood?.recommendations?.single()?.state)
|
||||
assertEquals(2, state.selectedMood?.mood?.seedCount)
|
||||
assertEquals("Added to Focus", state.message)
|
||||
assertFalse(state.messageIsError)
|
||||
store.release()
|
||||
}
|
||||
|
||||
/**
|
||||
* Ktor's mock engine responds on a real dispatcher, so queued test work has
|
||||
* to be run between short real waits.
|
||||
*/
|
||||
private suspend fun TestScope.awaitState(predicate: () -> Boolean) {
|
||||
repeat(500) {
|
||||
runCurrent()
|
||||
if (predicate()) return
|
||||
withContext(Dispatchers.Default) { delay(10) }
|
||||
}
|
||||
throw AssertionError("state did not settle")
|
||||
}
|
||||
|
||||
private val jsonHeaders = headersOf(HttpHeaders.ContentType, "application/json")
|
||||
|
||||
private fun moodJson(seedCount: Int) = """
|
||||
{"id":"mo-1","name":"Focus","targetSize":30,"refreshHours":24,"ytEnabled":true,
|
||||
"createdAt":"2026-01-01T00:00:00Z","changedAt":"2026-01-01T00:00:00Z",
|
||||
"seedCount":$seedCount,"newRecommendations":1}
|
||||
""".trimIndent()
|
||||
|
||||
private fun recommendationJson(state: String) = """
|
||||
{"videoId":"v-1","title":"Soft Light","artist":"Beta","duration":180,
|
||||
"state":"$state","timesAdded":0,
|
||||
"firstSeen":"2026-01-01T00:00:00Z","lastSeen":"2026-01-01T00:00:00Z",
|
||||
"matchedSong":${song("a").let { """{"id":"${it.id}","title":"${it.title}","artist":"${it.artist}","album":"${it.album}","albumId":"${it.albumId}","duration":${it.duration},"bitRate":${it.bitRate},"size":${it.size},"suffix":"${it.suffix}","folderId":${it.folderId},"relativePath":"${it.relativePath}","updatedAt":"${it.updatedAt}"}""" }}}
|
||||
""".trimIndent()
|
||||
|
||||
private fun moodDetailJson(state: String, seedCount: Int) = """
|
||||
{"mood":${moodJson(seedCount)},"seeds":[],"recommendations":[${recommendationJson(state)}]}
|
||||
""".trimIndent()
|
||||
|
||||
private fun kotlinx.coroutines.test.TestScope.store(
|
||||
storage: MemoryStorage,
|
||||
cache: LibraryCache,
|
||||
|
||||
Reference in new issue
Block a user