feat: inline search download status and headset transport
- mood detail carries the server's download jobs - search rows show Downloading… / Added / Failed, so the keyboard cannot hide it - headset and Bluetooth next/previous drive the app queue; previous keeps the after-two-seconds restart rule - 'Next batch' now explains what the preview is - version 0.3.1
This commit is contained in:
1 parent
cfab559e5e
commit
957ee3eb4b
12 files changed
+362
-19
No files matched your search
@@ -141,6 +141,76 @@ class AppStoreTest {
|
||||
store.release()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `search shows the download status and clears it when the job finishes`() = runTest {
|
||||
val storage = MemoryStorage()
|
||||
val cache = AppCache(storage)
|
||||
cache.saveSession(SessionState("http://music", "token", User("alice", admin = false)))
|
||||
var finished = false
|
||||
val engine = MockEngine { request ->
|
||||
when (request.url.encodedPath) {
|
||||
"/api/v1/me" -> respond("""{"name":"alice","admin":false}""", HttpStatusCode.OK, jsonHeaders)
|
||||
"/api/v1/moods" -> respond("""{"moods":[${moodJson(0)}]}""", HttpStatusCode.OK, jsonHeaders)
|
||||
"/api/v1/moods/mo-1" -> respond(
|
||||
detailJson(
|
||||
emptyList(),
|
||||
downloads = if (finished) listOf(downloadJson("done")) else emptyList(),
|
||||
),
|
||||
HttpStatusCode.OK,
|
||||
jsonHeaders,
|
||||
)
|
||||
"/api/v1/search" -> respond(
|
||||
"""{"results":[{"source":"youtube","externalId":"v1","title":"Fresh",
|
||||
"artist":"Artist","duration":180,"downloaded":false}]}""",
|
||||
HttpStatusCode.OK,
|
||||
jsonHeaders,
|
||||
)
|
||||
"/api/v1/moods/mo-1/songs" -> {
|
||||
finished = true
|
||||
respond(
|
||||
"""{"detail":${detailJson(emptyList(), downloads = listOf(downloadJson("queued")) )},
|
||||
"added":false,"downloading":true}""",
|
||||
HttpStatusCode.Accepted,
|
||||
jsonHeaders,
|
||||
)
|
||||
}
|
||||
else -> respondError(HttpStatusCode.NotFound)
|
||||
}
|
||||
}
|
||||
val store = store(storage, cache, FakePlayer(), engine)
|
||||
runCurrent()
|
||||
awaitState { store.state.value.moods.isNotEmpty() }
|
||||
|
||||
store.openSearch("mo-1")
|
||||
store.setSearchQuery("fresh")
|
||||
advanceTimeBy(350)
|
||||
runCurrent()
|
||||
awaitState("search results") { store.state.value.searchResults.isNotEmpty() }
|
||||
|
||||
store.addSearchResult(store.state.value.searchResults.single())
|
||||
awaitState("queued download") {
|
||||
store.state.value.searchDownloads["youtube|v1"]?.active == true
|
||||
}
|
||||
assertEquals("queued", store.state.value.searchDownloads["youtube|v1"]?.state)
|
||||
|
||||
// Each poll interval re-reads the detail. Advance until the finished job
|
||||
// lands; repeating also covers a coroutine that registered late.
|
||||
var ended = false
|
||||
repeat(10) {
|
||||
runCurrent()
|
||||
advanceTimeBy(4_100)
|
||||
// MockEngine answers off the test dispatcher, so give it real time.
|
||||
withContext(Dispatchers.Default) { delay(15) }
|
||||
runCurrent()
|
||||
if (store.state.value.searchDownloads["youtube|v1"]?.state == "done") {
|
||||
ended = true
|
||||
return@repeat
|
||||
}
|
||||
}
|
||||
assertTrue(ended, "downloads=${store.state.value.searchDownloads}")
|
||||
store.release()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `adding a recommendation refreshes the selected mood`() = runTest {
|
||||
val storage = MemoryStorage()
|
||||
@@ -323,6 +393,53 @@ class AppStoreTest {
|
||||
store.release()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `headset next and previous follow the queue instead of seeking`() = runTest {
|
||||
val storage = MemoryStorage()
|
||||
val cache = AppCache(storage)
|
||||
cache.saveSession(SessionState("http://music", "token", User("alice", admin = false)))
|
||||
val player = FakePlayer()
|
||||
val engine = MockEngine { request ->
|
||||
when (request.url.encodedPath) {
|
||||
"/api/v1/me" -> respond("""{"name":"alice","admin":false}""", HttpStatusCode.OK, jsonHeaders)
|
||||
"/api/v1/moods" -> respond("""{"moods":[${moodJson(3)}]}""", HttpStatusCode.OK, jsonHeaders)
|
||||
"/api/v1/moods/mo-1" -> respond(detailJson(listOf("a", "b", "c")), HttpStatusCode.OK, jsonHeaders)
|
||||
"/api/v1/moods/mo-1/preview" -> respond("""{"songs":[]}""", HttpStatusCode.OK, jsonHeaders)
|
||||
"/api/v1/moods/mo-1/sessions" -> respond(
|
||||
"""{"id":"s1","moodId":"mo-1","moodName":"Focus","startedAt":"now",
|
||||
"queue":[${songJson("a")},${songJson("b")},${songJson("c")}]}""",
|
||||
HttpStatusCode.Created,
|
||||
jsonHeaders,
|
||||
)
|
||||
else -> respondError(HttpStatusCode.NotFound)
|
||||
}
|
||||
}
|
||||
val store = store(storage, cache, player, engine)
|
||||
runCurrent()
|
||||
store.openMood("mo-1")
|
||||
awaitState { store.state.value.selectedMood != null }
|
||||
store.playMood("mo-1")
|
||||
awaitState { store.state.value.current?.id == "a" }
|
||||
|
||||
// A headset next moves the app queue: a -> b, not a seek inside one item.
|
||||
player.pressNext()
|
||||
awaitState { store.state.value.current?.id == "b" }
|
||||
assertEquals("b", store.state.value.current?.id)
|
||||
|
||||
// Within two seconds, previous restores the track that just finished.
|
||||
player.pressPrevious()
|
||||
awaitState { store.state.value.current?.id == "a" }
|
||||
assertEquals("a", store.state.value.current?.id)
|
||||
|
||||
// After two seconds it restarts the current track instead.
|
||||
store.seekTo(5.0)
|
||||
player.pressPrevious()
|
||||
runCurrent()
|
||||
assertEquals("a", store.state.value.current?.id)
|
||||
assertEquals(0.0, player.seeks.last())
|
||||
store.release()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `logout clears the session and the mood cache`() = runTest {
|
||||
val storage = MemoryStorage()
|
||||
@@ -355,13 +472,16 @@ class AppStoreTest {
|
||||
store.release()
|
||||
}
|
||||
|
||||
private suspend fun TestScope.awaitState(predicate: () -> Boolean) {
|
||||
private suspend fun TestScope.awaitState(predicate: () -> Boolean) =
|
||||
awaitState("state", predicate)
|
||||
|
||||
private suspend fun TestScope.awaitState(label: String, predicate: () -> Boolean) {
|
||||
repeat(500) {
|
||||
runCurrent()
|
||||
if (predicate()) return
|
||||
withContext(Dispatchers.Default) { delay(10) }
|
||||
}
|
||||
throw AssertionError("state did not settle")
|
||||
throw AssertionError("$label did not settle")
|
||||
}
|
||||
|
||||
private val jsonHeaders = headersOf(HttpHeaders.ContentType, "application/json")
|
||||
@@ -389,12 +509,21 @@ class AppStoreTest {
|
||||
songs: List<String>,
|
||||
recommendation: String? = null,
|
||||
source: String = "youtube",
|
||||
downloads: List<String> = emptyList(),
|
||||
) = """
|
||||
{"mood":${moodJson(songs.size, source)},
|
||||
"songs":[${songs.joinToString(",") { """{"song":${songJson(it)},"origin":"search","addedAt":"now"}""" }}],
|
||||
"recommendations":${recommendation?.let { "[$it]" } ?: "[]"}}
|
||||
"recommendations":${recommendation?.let { "[$it]" } ?: "[]"},
|
||||
"downloads":[${downloads.joinToString(",")}]}
|
||||
""".trimIndent()
|
||||
|
||||
private fun downloadJson(
|
||||
state: String,
|
||||
source: String = "youtube",
|
||||
externalId: String = "v1",
|
||||
) = """{"source":"$source","externalId":"$externalId","state":"$state",
|
||||
"attempts":1,"songId":null}""".trimIndent()
|
||||
|
||||
private fun song(id: String, duration: Int = 180) = Song(
|
||||
id = id,
|
||||
source = "youtube",
|
||||
@@ -423,6 +552,22 @@ class AppStoreTest {
|
||||
)
|
||||
|
||||
private class FakePlayer : AudioPlayer {
|
||||
private var onNext: (() -> Unit)? = null
|
||||
private var onPrevious: (() -> Unit)? = null
|
||||
|
||||
fun pressNext() {
|
||||
onNext?.invoke()
|
||||
}
|
||||
|
||||
fun pressPrevious() {
|
||||
onPrevious?.invoke()
|
||||
}
|
||||
|
||||
override fun setSkipCallbacks(onNext: (() -> Unit)?, onPrevious: (() -> Unit)?) {
|
||||
this.onNext = onNext
|
||||
this.onPrevious = onPrevious
|
||||
}
|
||||
|
||||
val played = mutableListOf<String>()
|
||||
val seeks = mutableListOf<Double>()
|
||||
var paused = false
|
||||
|
||||
Reference in new issue
Block a user