Files
AudioManagerClient/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/ApiClientTest.kt
T
TapTap c7c1d59b70
CI / build (push) Failing after 20m30s
feat: shuffle the whole library and add a sleep timer
Library screen: an "All music" card with Shuffle all, which builds a local
queue from every cached song so playback does not need a mood session.

Player bar: a sleep timer with 15/30/45/60/90 minute presets. The shared store
shows the countdown and pauses playback; on Android the pause is also scheduled
inside the media service through a custom session command, so it still fires
when the app is backgrounded or the task is swiped away.

Playback errors from the platform player now surface in the UI instead of
failing silently (for example when no audio device is available).

Tests cover shuffle-all, the empty library, timer expiry and cancelling it.
2026-11-15 08:09:02 +00:00

97 lines
3.4 KiB
Kotlin

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.test.runTest
import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNull
import win.taptap.musicmanager.client.api.ApiClient
import win.taptap.musicmanager.client.api.ApiException
import win.taptap.musicmanager.client.api.configuredHttpClient
import win.taptap.musicmanager.client.data.AppStorage
import win.taptap.musicmanager.client.data.CachedState
import win.taptap.musicmanager.client.data.LibraryCache
class ApiClientTest {
private val jsonHeaders = headersOf(HttpHeaders.ContentType, "application/json")
@Test
fun `login stores the token and parses moods`() = runTest {
val engine = MockEngine { request ->
when (request.url.encodedPath) {
"/api/v1/auth/login" -> respond(
"""
{"token":"abc123","expiresAt":"2027-01-01T00:00:00Z",
"user":{"name":"alice","admin":true,"musicFolders":[1],"longformFolders":[10]}}
""".trimIndent(),
HttpStatusCode.OK,
jsonHeaders,
)
"/api/v1/moods" -> respond(
"""
{"moods":[{"id":"mo-1","name":"Focus","targetSize":30,"refreshHours":24,
"ytEnabled":true,"createdAt":"now","changedAt":"now","seedCount":2}]}
""".trimIndent(),
HttpStatusCode.OK,
jsonHeaders,
)
else -> respondError(HttpStatusCode.NotFound)
}
}
val client = ApiClient(configuredHttpClient(engine))
val login = client.login("music.example.com", "alice", "secret", "test", "test")
assertEquals("abc123", login.token)
assertEquals("http://music.example.com", client.baseUrl)
val moods = client.moods()
assertEquals(1, moods.size)
assertEquals("Focus", moods.first().name)
assertEquals(2, moods.first().seedCount)
}
@Test
fun `api errors surface the server message`() = runTest {
val engine = MockEngine {
respond(
"""{"error":{"code":"unauthorized","message":"Missing or invalid token"}}""",
HttpStatusCode.Unauthorized,
jsonHeaders,
)
}
val client = ApiClient(configuredHttpClient(engine))
val error = assertFailsWith<ApiException> { client.moods() }
assertEquals(401, error.status)
assertEquals("unauthorized", error.code)
}
@Test
fun `cache round-trips songs and moods`() = runTest {
val storage = MemoryStorage()
val cache = LibraryCache(storage)
assertNull(cache.loadSession())
cache.save(
CachedState(
serverUrl = "http://localhost:8080",
userName = "alice",
revision = "2026-11-14T00:00:00Z",
songs = emptyList(),
moods = emptyList(),
),
)
val loaded = cache.load()
assertEquals("2026-11-14T00:00:00Z", loaded.revision)
assertEquals("alice", loaded.userName)
}
}