feat: move the client to searchable, downloadable moods
- new mood/song/search/recommendation models and API calls - AppCache v2 with moods and details only - Search and Downloads screens; remove Library and Longform - new mood detail with songs, add music and recommendation states - song-keyed covers and updated tests/docs
This commit is contained in:
1 parent
1bb23901eb
commit
dc270f863c
18 files changed
+1466
-1929
No files matched your search
@@ -8,17 +8,17 @@ import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.headersOf
|
||||
import io.ktor.utils.io.readAvailable
|
||||
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 kotlin.test.assertTrue
|
||||
import win.taptap.musicmanager.client.api.ApiClient
|
||||
import win.taptap.musicmanager.client.api.ApiException
|
||||
import win.taptap.musicmanager.client.api.Mood
|
||||
import win.taptap.musicmanager.client.api.configuredHttpClient
|
||||
import win.taptap.musicmanager.client.data.AppStorage
|
||||
import win.taptap.musicmanager.client.data.AppCache
|
||||
import win.taptap.musicmanager.client.data.CachedState
|
||||
import win.taptap.musicmanager.client.data.LibraryCache
|
||||
|
||||
class ApiClientTest {
|
||||
|
||||
@@ -31,15 +31,16 @@ class ApiClientTest {
|
||||
"/api/v1/auth/login" -> respond(
|
||||
"""
|
||||
{"token":"abc123","expiresAt":"2027-01-01T00:00:00Z",
|
||||
"user":{"name":"alice","admin":true,"musicFolders":[1],"longformFolders":[10]}}
|
||||
"user":{"name":"alice","admin":true}}
|
||||
""".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}]}
|
||||
{"moods":[{"id":"mo-1","name":"Focus","collectionPercent":60,
|
||||
"recommendationPercent":40,"recommendationCount":10,"refreshHours":24,
|
||||
"createdAt":"now","changedAt":"now","songCount":2,"recommendationReady":3}]}
|
||||
""".trimIndent(),
|
||||
HttpStatusCode.OK,
|
||||
jsonHeaders,
|
||||
@@ -56,7 +57,86 @@ class ApiClientTest {
|
||||
val moods = client.moods()
|
||||
assertEquals(1, moods.size)
|
||||
assertEquals("Focus", moods.first().name)
|
||||
assertEquals(2, moods.first().seedCount)
|
||||
assertEquals(2, moods.first().songCount)
|
||||
assertEquals(3, moods.first().recommendationReady)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `search returns badges and adding a video queues a download`() = runTest {
|
||||
val engine = MockEngine { request ->
|
||||
when (request.url.encodedPath) {
|
||||
"/api/v1/search" -> respond(
|
||||
"""
|
||||
{"results":[
|
||||
{"videoId":"v1","title":"Fresh","artist":"Artist","duration":180,
|
||||
"downloaded":false},
|
||||
{"videoId":"v2","title":"Old","artist":"Artist","duration":200,
|
||||
"songId":"sg-2","downloaded":true}
|
||||
]}
|
||||
""".trimIndent(),
|
||||
HttpStatusCode.OK,
|
||||
jsonHeaders,
|
||||
)
|
||||
"/api/v1/moods/mo-1/songs" -> respond(
|
||||
"""
|
||||
{"detail":{"mood":{"id":"mo-1","name":"Focus","createdAt":"now",
|
||||
"changedAt":"now","songCount":1},"songs":[],"recommendations":[]},
|
||||
"added":false,"downloading":true}
|
||||
""".trimIndent(),
|
||||
HttpStatusCode.Accepted,
|
||||
jsonHeaders,
|
||||
)
|
||||
else -> respondError(HttpStatusCode.NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
val client = ApiClient(configuredHttpClient(engine))
|
||||
client.restore("http://music", "token")
|
||||
|
||||
val results = client.search("fresh")
|
||||
assertEquals(2, results.size)
|
||||
assertNull(results[0].songId)
|
||||
assertEquals("sg-2", results[1].songId)
|
||||
assertTrue(results[1].downloaded)
|
||||
|
||||
val added = client.addSong("mo-1", videoId = "v1")
|
||||
assertTrue(added.downloading)
|
||||
assertEquals(1, added.detail.mood.songCount)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `adding and rejecting recommendations parse the decision`() = runTest {
|
||||
val engine = MockEngine { request ->
|
||||
when (request.url.encodedPath) {
|
||||
"/api/v1/moods/mo-1/recommendations/v1/add" -> respond(
|
||||
"""
|
||||
{"detail":{"mood":{"id":"mo-1","name":"Focus","createdAt":"now",
|
||||
"changedAt":"now","songCount":2},"songs":[],"recommendations":[]},
|
||||
"recommendation":{"videoId":"v1","title":"Pick","artist":"Artist",
|
||||
"state":"added"}}
|
||||
""".trimIndent(),
|
||||
HttpStatusCode.OK,
|
||||
jsonHeaders,
|
||||
)
|
||||
"/api/v1/moods/mo-1/recommendations/v2/reject" -> respond(
|
||||
"""
|
||||
{"detail":{"mood":{"id":"mo-1","name":"Focus","createdAt":"now",
|
||||
"changedAt":"now","songCount":2},"songs":[],"recommendations":[]},
|
||||
"recommendation":{"videoId":"v2","title":"Other","artist":"Artist",
|
||||
"state":"rejected"}}
|
||||
""".trimIndent(),
|
||||
HttpStatusCode.OK,
|
||||
jsonHeaders,
|
||||
)
|
||||
else -> respondError(HttpStatusCode.NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
val client = ApiClient(configuredHttpClient(engine))
|
||||
client.restore("http://music", "token")
|
||||
|
||||
assertEquals("added", client.addRecommendation("mo-1", "v1").recommendation.state)
|
||||
assertEquals("rejected", client.rejectRecommendation("mo-1", "v2").recommendation.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -116,23 +196,33 @@ class ApiClientTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `cache round-trips songs and moods`() = runTest {
|
||||
fun `cache round-trips moods and discards the old cache version`() = runTest {
|
||||
val storage = MemoryStorage()
|
||||
val cache = LibraryCache(storage)
|
||||
val cache = AppCache(storage)
|
||||
assertNull(cache.loadSession())
|
||||
|
||||
val mood = Mood(
|
||||
id = "mo-1",
|
||||
name = "Focus",
|
||||
createdAt = "now",
|
||||
changedAt = "now",
|
||||
songCount = 2,
|
||||
)
|
||||
cache.save(
|
||||
CachedState(
|
||||
serverUrl = "http://localhost:8080",
|
||||
userName = "alice",
|
||||
revision = "2026-11-14T00:00:00Z",
|
||||
songs = emptyList(),
|
||||
moods = emptyList(),
|
||||
moods = listOf(mood),
|
||||
),
|
||||
)
|
||||
|
||||
val loaded = cache.load()
|
||||
assertEquals("2026-11-14T00:00:00Z", loaded.revision)
|
||||
assertEquals(listOf("mo-1"), loaded.moods.map { it.id })
|
||||
assertEquals("alice", loaded.userName)
|
||||
|
||||
storage.write("mood-cache.json", """{"version":1,"songs":[{"id":"old"}]}""")
|
||||
val invalidated = cache.load()
|
||||
assertTrue(invalidated.moods.isEmpty())
|
||||
assertEquals(2, invalidated.version)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user