feat: add the Kotlin Multiplatform AudioManager client
A shared Compose Multiplatform client for Android, desktop and web on the native mood-first API. - Bearer login with persisted, restorable sessions. - Mood grid and full editor (create, settings, seeds, delete). - Server-driven radio sessions with automatic started/completed/skipped events and prefetching. - Add to current mood or choose another mood from the player. - Library search, original MP3 downloads, offline playback of downloads. - Offline cache of moods, songs and the last sync revision. - Platform audio: Media3 on Android, MP3SPI on desktop, HTML audio on web. - Platform storage: filesystem on Android/desktop, localStorage on web. - Desktop tests for API parsing, errors and cache persistence. - Gitea CI image with Android SDK 35, build pipeline and tagged releases.
This commit is contained in:
commit
3281e46104
40 files changed
+7021
No files matched your search
@@ -0,0 +1,114 @@
|
||||
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)
|
||||
}
|
||||
|
||||
private class MemoryStorage : AppStorage {
|
||||
private val files = mutableMapOf<String, String>()
|
||||
override fun read(name: String): String? = files[name]
|
||||
override fun write(name: String, text: String) {
|
||||
files[name] = text
|
||||
}
|
||||
|
||||
override fun delete(name: String) {
|
||||
files.remove(name)
|
||||
}
|
||||
|
||||
override fun saveDownload(songId: String, bytes: ByteArray): String? = null
|
||||
override fun downloadPath(songId: String): String? = null
|
||||
override fun listDownloads(): Map<String, Long> = emptyMap()
|
||||
override fun deleteDownload(songId: String) = Unit
|
||||
override fun storageUsedBytes(): Long = 0
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user