feat: base discovery on the whole mood collection
- seed from every artist (weighted by song count) and every album, not one track - walk Deezer's graph: artist top tracks, related artists, album deep cuts - score by artist frequency, collection album, catalog affinity and hint text - penalise repeated artists so a 30-slot mood spans artists, not one - refresh the pool when a song is added instead of waiting for refreshHours - skip discovery for a mood with no hint, genres or songs - extend tests and the smoke test with the artist graph
This commit is contained in:
1 parent
a78d359d0c
commit
62bcefad20
13 files changed
+911
-72
No files matched your search
@@ -179,6 +179,28 @@ class MoodRepositoryTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `adding a song makes discovery due again`() {
|
||||
withDatabase { mediaStore, _ ->
|
||||
val mood = createMood(recommendationCount = 2)
|
||||
assertTrue(MoodRepository.isDiscoveryDue(mood), "A never-seen mood is due")
|
||||
|
||||
MoodRepository.markDiscovered(mood.id)
|
||||
val fresh = assertNotNull(MoodRepository.get(mood.id, "alice"))
|
||||
assertFalse(MoodRepository.isDiscoveryDue(fresh), "Nothing changed since the pass")
|
||||
|
||||
// Timestamps are compared strictly; keep them distinguishable.
|
||||
Thread.sleep(5)
|
||||
val song = insertSong(mediaStore, MusicSource.YOUTUBE, "vid-1", "One")
|
||||
MoodRepository.addSong(mood.id, song.id, "search")
|
||||
val changed = assertNotNull(MoodRepository.get(mood.id, "alice"))
|
||||
assertTrue(
|
||||
MoodRepository.isDiscoveryDue(changed),
|
||||
"The collection is the discovery seed, so the pool is stale",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `download completion after a rejection does not attach the song`() {
|
||||
withDatabase { mediaStore, _ ->
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
package win.taptap.musicmanager.discovery
|
||||
|
||||
import com.sun.net.httpserver.HttpServer
|
||||
import java.net.InetSocketAddress
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/**
|
||||
* Exercises the Deezer similarity walk against a local stand-in API, so the
|
||||
* request shapes and the affinity weights stay pinned without network access.
|
||||
*/
|
||||
class DeezerDiscoverTest {
|
||||
|
||||
@Test
|
||||
fun `expands the collection through related artists and album deep cuts`() {
|
||||
val requests = CopyOnWriteArrayList<String>()
|
||||
val server = serve { path ->
|
||||
requests += path
|
||||
when (path) {
|
||||
"/search/artist" ->
|
||||
ok("""{"data":[{"id":27,"name":"Daft Punk"}]}""")
|
||||
"/search/album" ->
|
||||
ok(
|
||||
"""{"data":[{"id":5,"title":"Random Access Memories",
|
||||
"artist":{"name":"Daft Punk"}}]}""",
|
||||
)
|
||||
"/artist/27/top" ->
|
||||
ok(
|
||||
"""{"data":[
|
||||
{"id":1,"title":"Instant Crush","duration":337,"readable":true,
|
||||
"artist":{"name":"Daft Punk"},
|
||||
"album":{"title":"Random Access Memories"}},
|
||||
{"id":2,"title":"Too Long","duration":900,"readable":true,
|
||||
"artist":{"name":"Daft Punk"},
|
||||
"album":{"title":"Random Access Memories"}}]}""",
|
||||
)
|
||||
"/artist/27/related" ->
|
||||
ok("""{"data":[{"id":99,"name":"Daft Punk Tribute"}]}""")
|
||||
"/artist/99/top" ->
|
||||
ok(
|
||||
"""{"data":[
|
||||
{"id":3,"title":"Robot Rock","duration":300,"readable":true,
|
||||
"artist":{"name":"Daft Punk Tribute"}}]}""",
|
||||
)
|
||||
"/album/5/tracks" ->
|
||||
ok(
|
||||
"""{"data":[
|
||||
{"id":4,"title":"Contact","duration":382,"readable":true,
|
||||
"artist":{"name":"Daft Punk"},
|
||||
"album":{"title":"Random Access Memories"}}]}""",
|
||||
)
|
||||
else -> 404 to """{"error":{"type":"DataException","message":"not found"}}"""
|
||||
}
|
||||
}
|
||||
try {
|
||||
val found = catalog(server).discover(seeds(), limit = 50).associateBy { it.externalId }
|
||||
|
||||
// The 15-minute track is not music-length and never reaches the pool.
|
||||
assertEquals(setOf("1", "3", "4"), found.keys)
|
||||
assertEquals(0.5, found.getValue("1").affinity)
|
||||
assertEquals(1.5, found.getValue("3").affinity)
|
||||
assertEquals(1.0, found.getValue("4").affinity)
|
||||
// `/album/{id}/tracks` has no album object; the search result fills it in.
|
||||
assertEquals("Random Access Memories", found.getValue("4").album)
|
||||
assertTrue("/artist/27/related" in requests)
|
||||
assertTrue("/artist/99/top" in requests)
|
||||
assertTrue("/album/5/tracks" in requests)
|
||||
} finally {
|
||||
server.stop(0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a failing endpoint does not fail the whole expansion`() {
|
||||
val server = serve { path ->
|
||||
when (path) {
|
||||
"/search/artist" -> ok("""{"data":[{"id":27,"name":"Daft Punk"}]}""")
|
||||
"/artist/27/top" ->
|
||||
ok(
|
||||
"""{"data":[
|
||||
{"id":1,"title":"Instant Crush","duration":337,"readable":true,
|
||||
"artist":{"name":"Daft Punk"}}]}""",
|
||||
)
|
||||
else -> 500 to """{"error":{"type":"DataException","message":"boom"}}"""
|
||||
}
|
||||
}
|
||||
try {
|
||||
val found = catalog(server).discover(seeds(), limit = 50)
|
||||
|
||||
assertEquals(listOf("1"), found.map { it.externalId })
|
||||
} finally {
|
||||
server.stop(0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an empty collection makes no requests`() {
|
||||
val requests = CopyOnWriteArrayList<String>()
|
||||
val server = serve { path ->
|
||||
requests += path
|
||||
404 to """{"error":{"type":"DataException","message":"not found"}}"""
|
||||
}
|
||||
try {
|
||||
assertTrue(catalog(server).discover(DiscoverySeeds(), limit = 50).isEmpty())
|
||||
assertTrue(requests.isEmpty())
|
||||
} finally {
|
||||
server.stop(0)
|
||||
}
|
||||
}
|
||||
|
||||
private fun seeds() = DiscoverySeeds(
|
||||
artists = listOf(SeedArtist("Daft Punk", 4)),
|
||||
albums = listOf(SeedAlbum("Random Access Memories", "Daft Punk")),
|
||||
)
|
||||
|
||||
private fun catalog(server: HttpServer) = DeezerSearch(
|
||||
apiBase = "http://127.0.0.1:${server.address.port}",
|
||||
timeoutSeconds = 5,
|
||||
)
|
||||
|
||||
private fun ok(body: String) = 200 to body
|
||||
|
||||
private fun serve(route: (String) -> Pair<Int, String>): HttpServer {
|
||||
val server = HttpServer.create(InetSocketAddress("127.0.0.1", 0), 0)
|
||||
server.createContext("/") { exchange ->
|
||||
val (status, body) = route(exchange.requestURI.path)
|
||||
val bytes = body.toByteArray()
|
||||
exchange.responseHeaders.add("Content-Type", "application/json")
|
||||
exchange.sendResponseHeaders(status, bytes.size.toLong())
|
||||
exchange.responseBody.use { it.write(bytes) }
|
||||
}
|
||||
server.start()
|
||||
return server
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
import win.taptap.musicmanager.db.MoodRecord
|
||||
import win.taptap.musicmanager.db.SongRecord
|
||||
import win.taptap.musicmanager.source.MusicSource
|
||||
|
||||
class DiscoveryServiceTest {
|
||||
@@ -209,8 +210,8 @@ class DiscoveryServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `queries fall back to the mood name`() {
|
||||
assertEquals(listOf("Focus"), buildQueries(mood(), emptyList()))
|
||||
fun `a mood without a hint genres or songs has no query`() {
|
||||
assertTrue(buildQueries(mood(), emptyList()).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -223,12 +224,167 @@ class DiscoveryServiceTest {
|
||||
duration = 200,
|
||||
)
|
||||
|
||||
val plain = scoreCandidate(candidate, listOf("Alpha"), "focus")
|
||||
val seedArtist = scoreCandidate(candidate, listOf("Beta"), "focus")
|
||||
val plain = scoreCandidate(candidate, mapOf("alpha" to 1.0), emptySet(), "focus")
|
||||
val seedArtist = scoreCandidate(candidate, mapOf("beta" to 1.0), emptySet(), "focus")
|
||||
|
||||
assertTrue(seedArtist > plain)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an artist with more songs in the mood ranks higher`() {
|
||||
val candidate = TrackCandidate(
|
||||
source = MusicSource.DEEZER,
|
||||
externalId = "1",
|
||||
title = "Focus Two",
|
||||
artist = "Beta",
|
||||
duration = 200,
|
||||
)
|
||||
|
||||
val once = scoreCandidate(candidate, mapOf("beta" to 1.0), emptySet(), "")
|
||||
val often = scoreCandidate(candidate, mapOf("beta" to 12.0), emptySet(), "")
|
||||
|
||||
assertTrue(often > once)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `album deep cuts outrank tracks from unrelated albums`() {
|
||||
val candidate = TrackCandidate(
|
||||
source = MusicSource.DEEZER,
|
||||
externalId = "1",
|
||||
title = "Contact",
|
||||
artist = "Daft Punk",
|
||||
album = "Random Access Memories",
|
||||
duration = 382,
|
||||
)
|
||||
|
||||
val unrelated = scoreCandidate(candidate, emptyMap(), setOf("homework"), "")
|
||||
val sameAlbum = scoreCandidate(
|
||||
candidate,
|
||||
emptyMap(),
|
||||
setOf(matchKey("Random Access Memories")),
|
||||
"",
|
||||
)
|
||||
|
||||
assertEquals(1.0, sameAlbum - unrelated)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ranking spreads picks across artists instead of repeating one`() {
|
||||
val lead = (1..12).map {
|
||||
TrackCandidate(
|
||||
source = MusicSource.DEEZER,
|
||||
externalId = "lead-$it",
|
||||
title = "Lead $it",
|
||||
artist = "Alpha",
|
||||
duration = 200,
|
||||
)
|
||||
}
|
||||
val related = (1..6).map {
|
||||
TrackCandidate(
|
||||
source = MusicSource.DEEZER,
|
||||
externalId = "related-$it",
|
||||
title = "Related $it",
|
||||
artist = "Beta",
|
||||
duration = 200,
|
||||
affinity = 1.5,
|
||||
)
|
||||
}
|
||||
val scored = (lead + related).map {
|
||||
it to scoreCandidate(it, mapOf("alpha" to 1.0), emptySet(), "")
|
||||
}
|
||||
|
||||
val ranked = rankCandidates(scored, limit = 8)
|
||||
|
||||
assertEquals(8, ranked.size)
|
||||
assertTrue(ranked.any { it.artist == "Beta" }, "A related artist must take a slot")
|
||||
assertTrue(
|
||||
ranked.count { it.artist == "Alpha" } <= 5,
|
||||
"The lead artist must not fill every slot",
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `catalog affinity raises a related artist hit`() {
|
||||
val candidate = TrackCandidate(
|
||||
source = MusicSource.DEEZER,
|
||||
externalId = "1",
|
||||
title = "Robot Rock",
|
||||
artist = "Daft Punk Tribute",
|
||||
duration = 300,
|
||||
)
|
||||
|
||||
val plain = scoreCandidate(candidate, emptyMap(), emptySet(), "")
|
||||
val related = scoreCandidate(
|
||||
candidate.copy(affinity = 1.5),
|
||||
emptyMap(),
|
||||
emptySet(),
|
||||
"",
|
||||
)
|
||||
|
||||
assertEquals(1.5, related - plain)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `queries prefer collection artists over genres`() {
|
||||
val mood = mood(query = "late night", genres = listOf("Ambient", "Jazz"))
|
||||
val queries = buildQueries(mood, listOf("Alpha", "Beta", "Gamma", "Delta"))
|
||||
|
||||
assertEquals(listOf("late night", "Alpha", "Beta", "Gamma"), queries)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `collection artists are ordered by how many songs the mood has`() {
|
||||
val collection = listOf(
|
||||
song("Alpha"),
|
||||
song("Beta"),
|
||||
song("Alpha"),
|
||||
song("Beta"),
|
||||
song("Alpha"),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
listOf(SeedArtist("Alpha", 3), SeedArtist("Beta", 2)),
|
||||
artistsByFrequency(collection),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `albums are collected once per title and artist`() {
|
||||
val collection = listOf(
|
||||
song("Daft Punk", album = "Random Access Memories"),
|
||||
song("Daft Punk", album = "Random Access Memories"),
|
||||
song("Daft Punk", album = "Homework"),
|
||||
song("Other", album = null),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
listOf(
|
||||
SeedAlbum("Random Access Memories", "Daft Punk"),
|
||||
SeedAlbum("Homework", "Daft Punk"),
|
||||
),
|
||||
albumsOf(collection),
|
||||
)
|
||||
}
|
||||
|
||||
private fun song(artist: String, album: String? = "Album") = SongRecord(
|
||||
id = "sg-$artist-$album",
|
||||
source = MusicSource.DEEZER,
|
||||
externalId = "1",
|
||||
title = "Title",
|
||||
artist = artist,
|
||||
album = album,
|
||||
duration = 180,
|
||||
bitRate = 128,
|
||||
size = 1,
|
||||
suffix = "mp3",
|
||||
contentType = "audio/mpeg",
|
||||
relativePath = "deezer-1/Title.mp3",
|
||||
fileMtime = 0,
|
||||
thumbnail = null,
|
||||
createdAt = "2026-01-01T00:00:00Z",
|
||||
updatedAt = "2026-01-01T00:00:00Z",
|
||||
)
|
||||
|
||||
private fun mood(
|
||||
query: String? = null,
|
||||
genres: List<String> = emptyList(),
|
||||
|
||||
Reference in new issue
Block a user