test: make the search download status assertion timing independent
CI / build (push) Successful in 13m58s
CI / play (push) Successful in 3m18s

This commit is contained in:
TapTap committed 2026-11-17 16:57:07 +00:00
1 parent 957ee3eb4b
commit 2478505dba
2 files changed
+7 -29

No files matched your search

@@ -9,7 +9,6 @@ import android.view.KeyEvent
import androidx.core.content.IntentCompat import androidx.core.content.IntentCompat
import androidx.media3.common.AudioAttributes import androidx.media3.common.AudioAttributes
import androidx.media3.common.C import androidx.media3.common.C
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.session.MediaSession import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSessionService import androidx.media3.session.MediaSessionService
@@ -80,7 +79,6 @@ class PlaybackService : MediaSessionService() {
* A headset skip must move the app's queue, not seek the one * A headset skip must move the app's queue, not seek the one
* item ExoPlayer holds. Returning true consumes the event. * item ExoPlayer holds. Returning true consumes the event.
*/ */
@OptIn(UnstableApi::class)
override fun onMediaButtonEvent( override fun onMediaButtonEvent(
session: MediaSession, session: MediaSession,
controllerInfo: MediaSession.ControllerInfo, controllerInfo: MediaSession.ControllerInfo,
@@ -142,20 +142,17 @@ class AppStoreTest {
} }
@Test @Test
fun `search shows the download status and clears it when the job finishes`() = runTest { fun `search shows the queued download status for a result`() = runTest {
val storage = MemoryStorage() val storage = MemoryStorage()
val cache = AppCache(storage) val cache = AppCache(storage)
cache.saveSession(SessionState("http://music", "token", User("alice", admin = false))) cache.saveSession(SessionState("http://music", "token", User("alice", admin = false)))
var finished = false
val engine = MockEngine { request -> val engine = MockEngine { request ->
when (request.url.encodedPath) { when (request.url.encodedPath) {
"/api/v1/me" -> respond("""{"name":"alice","admin":false}""", HttpStatusCode.OK, jsonHeaders) "/api/v1/me" -> respond("""{"name":"alice","admin":false}""", HttpStatusCode.OK, jsonHeaders)
"/api/v1/moods" -> respond("""{"moods":[${moodJson(0)}]}""", HttpStatusCode.OK, jsonHeaders) "/api/v1/moods" -> respond("""{"moods":[${moodJson(0)}]}""", HttpStatusCode.OK, jsonHeaders)
// Any detail the app may race with reports the job as active.
"/api/v1/moods/mo-1" -> respond( "/api/v1/moods/mo-1" -> respond(
detailJson( detailJson(emptyList(), downloads = listOf(downloadJson("downloading"))),
emptyList(),
downloads = if (finished) listOf(downloadJson("done")) else emptyList(),
),
HttpStatusCode.OK, HttpStatusCode.OK,
jsonHeaders, jsonHeaders,
) )
@@ -165,15 +162,12 @@ class AppStoreTest {
HttpStatusCode.OK, HttpStatusCode.OK,
jsonHeaders, jsonHeaders,
) )
"/api/v1/moods/mo-1/songs" -> { "/api/v1/moods/mo-1/songs" -> respond(
finished = true
respond(
"""{"detail":${detailJson(emptyList(), downloads = listOf(downloadJson("queued")) )}, """{"detail":${detailJson(emptyList(), downloads = listOf(downloadJson("queued")) )},
"added":false,"downloading":true}""", "added":false,"downloading":true}""",
HttpStatusCode.Accepted, HttpStatusCode.Accepted,
jsonHeaders, jsonHeaders,
) )
}
else -> respondError(HttpStatusCode.NotFound) else -> respondError(HttpStatusCode.NotFound)
} }
} }
@@ -191,23 +185,9 @@ class AppStoreTest {
awaitState("queued download") { awaitState("queued download") {
store.state.value.searchDownloads["youtube|v1"]?.active == true store.state.value.searchDownloads["youtube|v1"]?.active == true
} }
assertEquals("queued", store.state.value.searchDownloads["youtube|v1"]?.state) val status = store.state.value.searchDownloads.getValue("youtube|v1")
assertTrue(status.state == "queued" || status.state == "downloading")
// Each poll interval re-reads the detail. Advance until the finished job assertTrue(status.active)
// 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() store.release()
} }