test: make the search download status assertion timing independent
This commit is contained in:
1 parent
957ee3eb4b
commit
2478505dba
2 files changed
+12
-34
No files matched your search
@@ -142,20 +142,17 @@ class AppStoreTest {
|
||||
}
|
||||
|
||||
@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 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)
|
||||
// Any detail the app may race with reports the job as active.
|
||||
"/api/v1/moods/mo-1" -> respond(
|
||||
detailJson(
|
||||
emptyList(),
|
||||
downloads = if (finished) listOf(downloadJson("done")) else emptyList(),
|
||||
),
|
||||
detailJson(emptyList(), downloads = listOf(downloadJson("downloading"))),
|
||||
HttpStatusCode.OK,
|
||||
jsonHeaders,
|
||||
)
|
||||
@@ -165,15 +162,12 @@ class AppStoreTest {
|
||||
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,
|
||||
)
|
||||
}
|
||||
"/api/v1/moods/mo-1/songs" -> respond(
|
||||
"""{"detail":${detailJson(emptyList(), downloads = listOf(downloadJson("queued")) )},
|
||||
"added":false,"downloading":true}""",
|
||||
HttpStatusCode.Accepted,
|
||||
jsonHeaders,
|
||||
)
|
||||
else -> respondError(HttpStatusCode.NotFound)
|
||||
}
|
||||
}
|
||||
@@ -191,23 +185,9 @@ class AppStoreTest {
|
||||
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}")
|
||||
val status = store.state.value.searchDownloads.getValue("youtube|v1")
|
||||
assertTrue(status.state == "queued" || status.state == "downloading")
|
||||
assertTrue(status.active)
|
||||
store.release()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user