feat: discover YT recommendations and tune the radio
CI / chart (push) Successful in 26s
CI / chart-push (push) Successful in 5s
CI / test (push) Successful in 2m6s
CI / image (push) Failing after 2m54s

yt-dlp now feeds mood discovery. Queries come from the free-text hint, seed
artists and genres; they run in parallel, are filtered to music-length videos,
matched against library songs by normalized title and artist, scored, and
stored as review candidates. Refresh, accept and reject endpoints are added:
accepting a library match adds a seed immediately, while accepted tracks that
are not owned wait for the future downloader. Decisions survive refreshes and
stale undecided candidates are pruned.

A background pass refreshes stale moods every hour, and the image ships a
pinned yt-dlp binary (YTDLP_PATH, also added to the Nix shell and chart).

Radio now avoids tracks played in the last 7 days when the pool is large
enough, spaces artists and albums across a batch, and weighs skips three times
as heavily as completions. The chart moves to 0.2.2.
This commit is contained in:
TapTap committed 2026-11-15 12:44:30 +00:00
1 parent 67eed8d262
commit c5aa000d1a
18 files changed
+1061 -37

No files matched your search

@@ -0,0 +1,137 @@
package win.taptap.musicmanager.radio
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import win.taptap.musicmanager.db.AffinityRecord
import win.taptap.musicmanager.db.SongRecord
class RadioServiceTest {
private val service = RadioService()
@Test
fun `recently played songs are penalized`() {
val song = song("s-1")
val fresh = service.scoreCandidate(
song, emptySet(), emptyList(), emptyList(), null, affinity = 0.0, jitter = 0.0,
)
val recent = service.scoreCandidate(
song, emptySet(), emptyList(), emptyList(), null, affinity = 0.0,
recent = setOf("s-1"), jitter = 0.0,
)
assertEquals(0.0, fresh)
assertEquals(-6.0, recent)
}
@Test
fun `skips outweigh completions`() {
assertEquals(1.0, AffinityRecord(completions = 1, skips = 0).score)
assertEquals(-2.0, AffinityRecord(completions = 1, skips = 1).score)
assertEquals(-5.0, AffinityRecord(completions = 1, skips = 2).score)
}
@Test
fun `seeds and matching genres are boosted`() {
val seeded = service.scoreCandidate(
song("s-1", genres = listOf("Ambient")),
seedIds = setOf("s-1"),
seedGenres = listOf("Ambient"),
seedArtistIds = emptyList(),
query = null,
affinity = 0.0,
jitter = 0.0,
)
val plain = service.scoreCandidate(
song("s-2"),
seedIds = setOf("s-1"),
seedGenres = listOf("Ambient"),
seedArtistIds = emptyList(),
query = null,
affinity = 0.0,
jitter = 0.0,
)
assertEquals(11.5, seeded)
assertEquals(0.0, plain)
}
@Test
fun `picks keep artists and albums apart`() {
val scored = listOf(
song("s1", artist = "A", albumId = "al1") to 0.0,
song("s2", artist = "B", albumId = "al2") to 0.0,
song("s3", artist = "C", albumId = "al3") to 0.0,
song("s4", artist = "D", albumId = "al4") to 0.0,
song("s5", artist = "A", albumId = "al5") to 0.0,
song("s6", artist = "B", albumId = "al6") to 0.0,
)
val picked = service.pickDiverse(scored, limit = 6)
assertEquals(6, picked.size)
picked.indices.forEach { index ->
val artists = picked.subList(index, minOf(index + 3, picked.size)).map { it.displayArtist }
assertEquals(artists.size, artists.distinct().size, "artist spacing at $index")
val albums = picked.subList(index, minOf(index + 4, picked.size)).map { it.albumId }
assertEquals(albums.size, albums.distinct().size, "album spacing at $index")
}
}
@Test
fun `picks relax spacing when the pool is too small`() {
val scored = listOf(
song("s1", artist = "A", albumId = "al1") to 0.0,
song("s2", artist = "A", albumId = "al1") to 0.0,
)
val picked = service.pickDiverse(scored, limit = 2)
assertEquals(listOf("s1", "s2"), picked.map { it.id })
}
@Test
fun `higher scores are picked first`() {
val scored = listOf(
song("low", artist = "A", albumId = "al1") to 1.0,
song("high", artist = "B", albumId = "al2") to 9.0,
)
assertTrue(service.pickDiverse(scored, limit = 2).first().id == "high")
}
private fun song(
id: String,
artist: String = "Artist $id",
albumId: String = "album-$id",
genres: List<String> = emptyList(),
) = SongRecord(
id = id,
albumId = albumId,
albumName = "Album",
artistId = "ar-$artist",
artistName = artist,
displayArtist = artist,
title = "Title $id",
trackNumber = 1,
discNumber = 1,
year = 2026,
duration = 180,
bitRate = 320,
size = 1_000,
suffix = "mp3",
contentType = "audio/mpeg",
coverArt = null,
folderId = 1,
relativePath = "$id.mp3",
created = "2026-01-01T00:00:00Z",
updatedAt = "2026-01-01T00:00:00Z",
starredAt = null,
rating = null,
playCount = 0,
lastPlayedAt = null,
artists = emptyList(),
genres = genres,
)
}