feat: add Deezer as the default download source
- V9 adds source/external_id to songs, moods, recommendations and jobs - Deezer public catalog for search, related album covers and metadata - streamrip downloader configured with DEEZER_ARL, 128/320 MP3 - per-mood source selector; switching clears and refills recommendations - keep YouTube Music as a full second catalog and downloader - media layout <source>-<externalId>, legacy YouTube folders still read - Docker/streamrip install, chart ARL secret, docs and dual-source smoke test
This commit is contained in:
1 parent
81a47f4cd2
commit
141698282d
43 files changed
+2072
-605
No files matched your search
@@ -24,7 +24,6 @@ import io.ktor.server.testing.ApplicationTestBuilder
|
||||
import io.ktor.server.testing.testApplication
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.time.Instant
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.int
|
||||
@@ -46,22 +45,20 @@ import win.taptap.musicmanager.db.DatabaseFactory
|
||||
import win.taptap.musicmanager.db.DatabaseHandle
|
||||
import win.taptap.musicmanager.db.DownloadRepository
|
||||
import win.taptap.musicmanager.db.MoodRepository
|
||||
import win.taptap.musicmanager.db.RecommendationUpsert
|
||||
import win.taptap.musicmanager.db.SongRecord
|
||||
import win.taptap.musicmanager.db.SongRepository
|
||||
import win.taptap.musicmanager.db.SongUpsert
|
||||
import win.taptap.musicmanager.discovery.DiscoveryService
|
||||
import win.taptap.musicmanager.discovery.MusicCatalog
|
||||
import win.taptap.musicmanager.discovery.MusicCatalogs
|
||||
import win.taptap.musicmanager.discovery.TopUpService
|
||||
import win.taptap.musicmanager.discovery.YtCandidate
|
||||
import win.taptap.musicmanager.discovery.YtResolver
|
||||
import win.taptap.musicmanager.discovery.YtSearch
|
||||
import win.taptap.musicmanager.discovery.TrackCandidate
|
||||
import win.taptap.musicmanager.media.MediaGcService
|
||||
import win.taptap.musicmanager.media.MediaStore
|
||||
import win.taptap.musicmanager.radio.RadioService
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.scanner.ScanStatus
|
||||
import win.taptap.musicmanager.source.MusicSource
|
||||
import win.taptap.musicmanager.transcoding.FfmpegTranscoder
|
||||
import win.taptap.musicmanager.util.IdGenerator
|
||||
|
||||
class ApiRoutesTest {
|
||||
|
||||
@@ -113,7 +110,7 @@ class ApiRoutesTest {
|
||||
bearer(token)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(
|
||||
"""{"name":"Focus","genres":["Ambient"],"collectionPercent":70,"recommendationCount":0}""",
|
||||
"""{"name":"Focus","genres":["Ambient"],"source":"youtube","collectionPercent":70,"recommendationCount":0}""",
|
||||
)
|
||||
}
|
||||
assertEquals(HttpStatusCode.Created, created.status)
|
||||
@@ -122,6 +119,7 @@ class ApiRoutesTest {
|
||||
assertEquals(0, body["mood"]!!.jsonObject.int("songCount"))
|
||||
assertEquals(70, body["mood"]!!.jsonObject.int("collectionPercent"))
|
||||
assertEquals(30, body["mood"]!!.jsonObject.int("recommendationPercent"))
|
||||
assertEquals("youtube", body["mood"]!!.jsonObject.string("source"))
|
||||
|
||||
val added = client.post("/api/v1/moods/$moodId/songs") {
|
||||
bearer(token)
|
||||
@@ -150,11 +148,12 @@ class ApiRoutesTest {
|
||||
val patched = client.patch("/api/v1/moods/$moodId") {
|
||||
bearer(token)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody("""{"name":"Deep Focus","collectionPercent":40}""")
|
||||
setBody("""{"name":"Deep Focus","collectionPercent":40,"source":"deezer"}""")
|
||||
}
|
||||
val patchedMood = parse(patched.bodyAsText())["mood"]!!.jsonObject
|
||||
assertEquals("Deep Focus", patchedMood.string("name"))
|
||||
assertEquals(40, patchedMood.int("collectionPercent"))
|
||||
assertEquals("deezer", patchedMood.string("source"))
|
||||
|
||||
val list = parse(client.get("/api/v1/moods") { bearer(token) }.bodyAsText())
|
||||
assertEquals(1, list["moods"]!!.jsonArray.size)
|
||||
@@ -186,7 +185,10 @@ class ApiRoutesTest {
|
||||
testApplication {
|
||||
setupApp(seed)
|
||||
val token = client.token("alice", "alice-pw")
|
||||
val moodId = client.createMood(token, """{"name":"Empty","recommendationCount":0}""")
|
||||
val moodId = client.createMood(
|
||||
token,
|
||||
"""{"name":"Empty","source":"youtube","recommendationCount":0}""",
|
||||
)
|
||||
|
||||
val rejected = client.post("/api/v1/moods/$moodId/sessions") {
|
||||
bearer(token)
|
||||
@@ -273,7 +275,10 @@ class ApiRoutesTest {
|
||||
setupApp(seed)
|
||||
val alice = client.token("alice", "alice-pw")
|
||||
val bob = client.token("bob", "bob-pw")
|
||||
val moodId = client.createMood(alice, """{"name":"Private","recommendationCount":0}""")
|
||||
val moodId = client.createMood(
|
||||
alice,
|
||||
"""{"name":"Private","source":"youtube","recommendationCount":0}""",
|
||||
)
|
||||
client.addSong(alice, moodId, seed.ambient1)
|
||||
val started = client.post("/api/v1/moods/$moodId/sessions") {
|
||||
bearer(alice)
|
||||
@@ -294,31 +299,37 @@ class ApiRoutesTest {
|
||||
fun `search hides long tracks and marks downloaded songs`() {
|
||||
seededContext().use { seed ->
|
||||
val results = listOf(
|
||||
YtCandidate("vid-ambient-1", "Already Here", "Artist", duration = 180),
|
||||
YtCandidate("vid-new", "Fresh Track", "New Artist", duration = 200),
|
||||
YtCandidate("vid-long", "Late Night Set", "Some DJ", duration = 3_600),
|
||||
YtCandidate("vid-short", "Interlude", "Some DJ", duration = 20),
|
||||
yt("vid-ambient-1", "Already Here", "Artist", 180),
|
||||
yt("vid-new", "Fresh Track", "New Artist", 200),
|
||||
yt("vid-long", "Late Night Set", "Some DJ", 3_600),
|
||||
yt("vid-short", "Interlude", "Some DJ", 20),
|
||||
)
|
||||
testApplication {
|
||||
setupApp(seed, search = FakeYt(results))
|
||||
setupApp(seed, youtubeResults = results)
|
||||
val token = client.token("alice", "alice-pw")
|
||||
val moodId = client.createMood(token, """{"name":"Search Mood","recommendationCount":0}""")
|
||||
val moodId = client.createMood(
|
||||
token,
|
||||
"""{"name":"Search Mood","source":"youtube","recommendationCount":0}""",
|
||||
)
|
||||
client.addSong(token, moodId, seed.ambient1)
|
||||
|
||||
val body = parse(
|
||||
client.get("/api/v1/search?q=ambient&moodId=$moodId") { bearer(token) }.bodyAsText(),
|
||||
client.get("/api/v1/search?q=ambient&source=youtube&moodId=$moodId") {
|
||||
bearer(token)
|
||||
}.bodyAsText(),
|
||||
)
|
||||
val found = body["results"]!!.jsonArray.map { it.jsonObject }
|
||||
assertEquals(2, found.size)
|
||||
assertFalse(found.any { it.string("videoId") == "vid-long" })
|
||||
assertFalse(found.any { it.string("videoId") == "vid-short" })
|
||||
assertFalse(found.any { it.string("externalId") == "vid-long" })
|
||||
assertFalse(found.any { it.string("externalId") == "vid-short" })
|
||||
assertTrue(found.all { it.string("source") == "youtube" })
|
||||
|
||||
val downloaded = found.first { it.string("videoId") == "vid-ambient-1" }
|
||||
val downloaded = found.first { it.string("externalId") == "vid-ambient-1" }
|
||||
assertTrue(downloaded["downloaded"]!!.jsonPrimitive.content.toBoolean())
|
||||
assertTrue(downloaded["inMood"]!!.jsonPrimitive.content.toBoolean())
|
||||
assertEquals(seed.ambient1, downloaded.string("songId"))
|
||||
assertFalse(
|
||||
found.first { it.string("videoId") == "vid-new" }["downloaded"]!!
|
||||
found.first { it.string("externalId") == "vid-new" }["downloaded"]!!
|
||||
.jsonPrimitive.content.toBoolean(),
|
||||
)
|
||||
}
|
||||
@@ -326,35 +337,77 @@ class ApiRoutesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `adding a search result downloads it and a URL is resolved`() {
|
||||
fun `Deezer search is the default source and marks songs in the mood`() {
|
||||
seededContext().use { seed ->
|
||||
val resolved = YtCandidate("vid-url", "From URL", "Artist", duration = 180)
|
||||
val search = FakeYt(
|
||||
results = emptyList(),
|
||||
resolved = mapOf(
|
||||
"https://youtu.be/vid-url" to resolved,
|
||||
"https://youtu.be/vid-long" to
|
||||
YtCandidate("vid-long", "Long", "Artist", duration = 3_600),
|
||||
),
|
||||
val results = listOf(
|
||||
dz("67238732", "Instant Crush", "Daft Punk", 337),
|
||||
dz("1", "Too Long", "DJ", 3_600),
|
||||
)
|
||||
testApplication {
|
||||
setupApp(seed, search = search)
|
||||
setupApp(seed, deezerResults = results)
|
||||
val token = client.token("alice", "alice-pw")
|
||||
val moodId = client.createMood(token, """{"name":"Adds","recommendationCount":0}""")
|
||||
val moodId = client.createMood(token, """{"name":"Deezer","recommendationCount":0}""")
|
||||
val added = client.post("/api/v1/moods/$moodId/songs") {
|
||||
bearer(token)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody("""{"source":"deezer","externalId":"67238732"}""")
|
||||
}
|
||||
assertEquals(HttpStatusCode.Accepted, added.status)
|
||||
|
||||
val body = parse(
|
||||
client.get("/api/v1/search?q=daft&moodId=$moodId") { bearer(token) }.bodyAsText(),
|
||||
)
|
||||
val found = body["results"]!!.jsonArray.map { it.jsonObject }
|
||||
assertEquals(1, found.size)
|
||||
assertEquals("deezer", found.single().string("source"))
|
||||
assertEquals("67238732", found.single().string("externalId"))
|
||||
assertEquals("Daft Punk", found.single().string("artist"))
|
||||
assertFalse(
|
||||
found.single()["downloaded"]!!.jsonPrimitive.content.toBoolean(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `adding a search result downloads it and URLs resolve per source`() {
|
||||
seededContext().use { seed ->
|
||||
val youtubeResolved = mapOf(
|
||||
"https://youtu.be/vid-url" to yt("vid-url", "From URL", "Artist", 180),
|
||||
"https://youtu.be/vid-long" to yt("vid-long", "Long", "Artist", 3_600),
|
||||
)
|
||||
val deezerResolved = mapOf(
|
||||
"https://www.deezer.com/track/67238732" to
|
||||
dz("67238732", "Instant Crush", "Daft Punk", 337),
|
||||
)
|
||||
testApplication {
|
||||
setupApp(
|
||||
seed,
|
||||
youtubeResolved = youtubeResolved,
|
||||
deezerResolved = deezerResolved,
|
||||
)
|
||||
val token = client.token("alice", "alice-pw")
|
||||
val moodId = client.createMood(
|
||||
token,
|
||||
"""{"name":"Adds","source":"youtube","recommendationCount":0}""",
|
||||
)
|
||||
|
||||
val queued = client.post("/api/v1/moods/$moodId/songs") {
|
||||
bearer(token)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody("""{"videoId":"vid-new"}""")
|
||||
setBody("""{"source":"youtube","externalId":"vid-new"}""")
|
||||
}
|
||||
assertEquals(HttpStatusCode.Accepted, queued.status)
|
||||
assertTrue(
|
||||
parse(queued.bodyAsText())["downloading"]!!.jsonPrimitive.content.toBoolean(),
|
||||
)
|
||||
assertEquals(
|
||||
"queued",
|
||||
DownloadRepository.find(moodId, "vid-new", DownloadRepository.KIND_SONG)?.state,
|
||||
val youtubeJob = DownloadRepository.find(
|
||||
moodId,
|
||||
"vid-new",
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
assertEquals("queued", youtubeJob?.state)
|
||||
assertEquals(MusicSource.YOUTUBE, youtubeJob?.source)
|
||||
|
||||
val byUrl = client.post("/api/v1/moods/$moodId/songs") {
|
||||
bearer(token)
|
||||
@@ -367,12 +420,33 @@ class ApiRoutesTest {
|
||||
DownloadRepository.find(moodId, "vid-url", DownloadRepository.KIND_SONG)?.state,
|
||||
)
|
||||
|
||||
val deezerUrl = client.post("/api/v1/moods/$moodId/songs") {
|
||||
bearer(token)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody("""{"url":"https://www.deezer.com/track/67238732"}""")
|
||||
}
|
||||
assertEquals(HttpStatusCode.Accepted, deezerUrl.status)
|
||||
val deezerJob = DownloadRepository.find(
|
||||
moodId,
|
||||
"67238732",
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
assertEquals("queued", deezerJob?.state)
|
||||
assertEquals(MusicSource.DEEZER, deezerJob?.source)
|
||||
|
||||
val long = client.post("/api/v1/moods/$moodId/songs") {
|
||||
bearer(token)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody("""{"url":"https://youtu.be/vid-long"}""")
|
||||
}
|
||||
assertEquals(HttpStatusCode.BadRequest, long.status)
|
||||
|
||||
val unknownSource = client.post("/api/v1/moods/$moodId/songs") {
|
||||
bearer(token)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody("""{"source":"spotify","externalId":"1"}""")
|
||||
}
|
||||
assertEquals(HttpStatusCode.BadRequest, unknownSource.status)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -381,14 +455,17 @@ class ApiRoutesTest {
|
||||
fun `recommendations can be added rejected and retried`() {
|
||||
seededContext().use { seed ->
|
||||
val results = listOf(
|
||||
YtCandidate("vid-ready", "Ready Track", "Artist", duration = 180),
|
||||
YtCandidate("vid-reject", "Rejected Track", "Artist", duration = 180),
|
||||
YtCandidate("vid-failed", "Failed Track", "Artist", duration = 180),
|
||||
yt("vid-ready", "Ready Track", "Artist", 180),
|
||||
yt("vid-reject", "Rejected Track", "Artist", 180),
|
||||
yt("vid-failed", "Failed Track", "Artist", 180),
|
||||
)
|
||||
testApplication {
|
||||
setupApp(seed, search = FakeYt(results))
|
||||
setupApp(seed, youtubeResults = results)
|
||||
val token = client.token("alice", "alice-pw")
|
||||
val moodId = client.createMood(token, """{"name":"Radio","recommendationCount":3}""")
|
||||
val moodId = client.createMood(
|
||||
token,
|
||||
"""{"name":"Radio","source":"youtube","recommendationCount":3}""",
|
||||
)
|
||||
client.addSong(token, moodId, seed.ambient1)
|
||||
|
||||
val refreshed = client.post("/api/v1/moods/$moodId/recommendations/refresh") {
|
||||
@@ -399,14 +476,20 @@ class ApiRoutesTest {
|
||||
.map { it.jsonObject }
|
||||
assertEquals(3, recommendations.size)
|
||||
assertTrue(recommendations.all { it.string("state") == "downloading" })
|
||||
assertTrue(recommendations.all { it.string("source") == "youtube" })
|
||||
|
||||
// Simulate a finished download for the first slot.
|
||||
val readySong = insertSong(seed, "vid-ready", "Ready Track")
|
||||
MoodRepository.completeDownload(moodId, "vid-ready", readySong)
|
||||
val readySong = insertSong(seed, MusicSource.YOUTUBE, "vid-ready", "Ready Track")
|
||||
MoodRepository.completeDownload(
|
||||
moodId,
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-ready",
|
||||
readySong,
|
||||
)
|
||||
|
||||
val detail = parse(client.get("/api/v1/moods/$moodId") { bearer(token) }.bodyAsText())
|
||||
val ready = detail["recommendations"]!!.jsonArray.map { it.jsonObject }
|
||||
.first { it.string("videoId") == "vid-ready" }
|
||||
.first { it.string("externalId") == "vid-ready" }
|
||||
assertEquals("ready", ready.string("state"))
|
||||
assertEquals(readySong, ready["song"]!!.jsonObject.string("id"))
|
||||
|
||||
@@ -425,7 +508,7 @@ class ApiRoutesTest {
|
||||
)
|
||||
assertFalse(
|
||||
addedBody["detail"]!!.jsonObject["recommendations"]!!.jsonArray
|
||||
.map { it.jsonObject.string("videoId") }
|
||||
.map { it.jsonObject.string("externalId") }
|
||||
.contains("vid-ready"),
|
||||
)
|
||||
|
||||
@@ -440,7 +523,7 @@ class ApiRoutesTest {
|
||||
// The third slot is visible but failed; it must be retried first.
|
||||
val remaining = MoodRepository.recommendations(moodId)
|
||||
assertEquals(1, remaining.size)
|
||||
assertEquals("vid-failed", remaining.single().videoId)
|
||||
assertEquals("vid-failed", remaining.single().externalId)
|
||||
MoodRepository.failRecommendation(moodId, "vid-failed", "boom", 3)
|
||||
|
||||
val cannotAdd = client.post("/api/v1/moods/$moodId/recommendations/vid-failed/add") {
|
||||
@@ -456,7 +539,7 @@ class ApiRoutesTest {
|
||||
assertEquals(
|
||||
"downloading",
|
||||
retryBody["recommendations"]!!.jsonArray.map { it.jsonObject }
|
||||
.first { it.string("videoId") == "vid-failed" }
|
||||
.first { it.string("externalId") == "vid-failed" }
|
||||
.string("state"),
|
||||
)
|
||||
assertEquals(
|
||||
@@ -502,9 +585,27 @@ class ApiRoutesTest {
|
||||
|
||||
private fun ApplicationTestBuilder.setupApp(
|
||||
seed: Seed,
|
||||
search: FakeYt = FakeYt(emptyList()),
|
||||
youtubeResults: List<TrackCandidate> = emptyList(),
|
||||
youtubeResolved: Map<String, TrackCandidate> = emptyMap(),
|
||||
deezerResults: List<TrackCandidate> = emptyList(),
|
||||
deezerResolved: Map<String, TrackCandidate> = emptyMap(),
|
||||
defaultSource: String = MusicSource.DEEZER,
|
||||
) {
|
||||
val discovery = DiscoveryService(search)
|
||||
val catalogs = MusicCatalogs(
|
||||
mapOf(
|
||||
MusicSource.YOUTUBE to FakeCatalog(
|
||||
MusicSource.YOUTUBE,
|
||||
youtubeResults,
|
||||
youtubeResolved,
|
||||
),
|
||||
MusicSource.DEEZER to FakeCatalog(
|
||||
MusicSource.DEEZER,
|
||||
deezerResults,
|
||||
deezerResolved,
|
||||
),
|
||||
),
|
||||
)
|
||||
val discovery = DiscoveryService(catalogs)
|
||||
application {
|
||||
install(ContentNegotiation) {
|
||||
json(Json { ignoreUnknownKeys = true; encodeDefaults = true })
|
||||
@@ -526,10 +627,10 @@ class ApiRoutesTest {
|
||||
radioService = RadioService(),
|
||||
discoveryService = discovery,
|
||||
topUpService = TopUpService(discovery),
|
||||
ytSearch = search,
|
||||
ytResolver = search,
|
||||
catalogs = catalogs,
|
||||
mediaStore = seed.media,
|
||||
gc = seed.gc,
|
||||
defaultSource = defaultSource,
|
||||
startScan = { false },
|
||||
scanStatus = { ScanStatus(false, 0) },
|
||||
)
|
||||
@@ -595,22 +696,29 @@ class ApiRoutesTest {
|
||||
coverArt = coverArt,
|
||||
gc = MediaGcService(media, coverArt),
|
||||
)
|
||||
seed.ambient1 = insertSong(seed, "vid-ambient-1", "Focus One", artist = "Alpha")
|
||||
seed.ambient2 = insertSong(seed, "vid-ambient-2", "Focus Two", artist = "Beta")
|
||||
seed.rock = insertSong(seed, "vid-rock", "Loud", artist = "Gamma")
|
||||
seed.bobSong = insertSong(seed, "vid-bob", "Bob Song", artist = "Bob")
|
||||
seed.ambient1 = insertSong(seed, MusicSource.YOUTUBE, "vid-ambient-1", "Focus One", "Alpha")
|
||||
seed.ambient2 = insertSong(seed, MusicSource.YOUTUBE, "vid-ambient-2", "Focus Two", "Beta")
|
||||
seed.rock = insertSong(seed, MusicSource.YOUTUBE, "vid-rock", "Loud", "Gamma")
|
||||
seed.bobSong = insertSong(seed, MusicSource.YOUTUBE, "vid-bob", "Bob Song", "Bob")
|
||||
seed.coverArt.saveCover(seed.ambient1, pngBytes())
|
||||
return seed
|
||||
}
|
||||
|
||||
/** Seeds a shared song directly (normally the downloader does this). */
|
||||
private fun insertSong(seed: Seed, videoId: String, title: String, artist: String = "Artist"): String {
|
||||
val file = seed.media.target(videoId, title)
|
||||
private fun insertSong(
|
||||
seed: Seed,
|
||||
source: String,
|
||||
externalId: String,
|
||||
title: String,
|
||||
artist: String = "Artist",
|
||||
): String {
|
||||
val file = seed.media.target(source, externalId, title)
|
||||
Files.createDirectories(file.parent)
|
||||
Files.write(file, "ID3-fake-audio".toByteArray())
|
||||
return SongRepository.upsert(
|
||||
SongUpsert(
|
||||
videoId = videoId,
|
||||
source = source,
|
||||
externalId = externalId,
|
||||
title = title,
|
||||
artist = artist,
|
||||
album = "Album",
|
||||
@@ -636,12 +744,31 @@ class ApiRoutesTest {
|
||||
|
||||
private fun JsonObject.int(name: String): Int = this[name]!!.jsonPrimitive.int
|
||||
|
||||
private class FakeYt(
|
||||
private val results: List<YtCandidate>,
|
||||
private val resolved: Map<String, YtCandidate> = emptyMap(),
|
||||
) : YtSearch, YtResolver {
|
||||
override fun search(query: String, limit: Int): List<YtCandidate> = results
|
||||
override fun resolve(url: String): YtCandidate? = resolved[url]
|
||||
private fun yt(externalId: String, title: String, artist: String, duration: Int) =
|
||||
TrackCandidate(
|
||||
source = MusicSource.YOUTUBE,
|
||||
externalId = externalId,
|
||||
title = title,
|
||||
artist = artist,
|
||||
duration = duration,
|
||||
)
|
||||
|
||||
private fun dz(externalId: String, title: String, artist: String, duration: Int) =
|
||||
TrackCandidate(
|
||||
source = MusicSource.DEEZER,
|
||||
externalId = externalId,
|
||||
title = title,
|
||||
artist = artist,
|
||||
duration = duration,
|
||||
)
|
||||
|
||||
private class FakeCatalog(
|
||||
override val source: String,
|
||||
private val results: List<TrackCandidate>,
|
||||
private val resolved: Map<String, TrackCandidate> = emptyMap(),
|
||||
) : MusicCatalog {
|
||||
override fun search(query: String, limit: Int): List<TrackCandidate> = results
|
||||
override fun resolve(url: String): TrackCandidate? = resolved[url]
|
||||
}
|
||||
|
||||
private data class Seed(
|
||||
|
||||
@@ -16,6 +16,11 @@ class AppConfigTest {
|
||||
assertEquals(Path.of("./config/users.yaml"), config.usersFile)
|
||||
assertEquals("ffmpeg", config.ffmpegPath)
|
||||
assertEquals("yt-dlp", config.ytDlpPath)
|
||||
assertEquals("rip", config.streamripPath)
|
||||
assertEquals("deezer", config.defaultSource)
|
||||
assertEquals(null, config.deezerArl)
|
||||
assertEquals(0, config.deezerQuality)
|
||||
assertEquals("https://api.deezer.com", config.deezerApiBase)
|
||||
assertEquals(Path.of("./data/media"), config.mediaDir)
|
||||
assertEquals(Path.of("./data/downloads"), config.downloadDir)
|
||||
assertEquals(Path.of("./data/musicmanager.db"), config.databasePath)
|
||||
@@ -30,6 +35,11 @@ class AppConfigTest {
|
||||
"USERS_FILE" to "/run/secrets/users.yaml",
|
||||
"FFMPEG_PATH" to "/usr/bin/ffmpeg",
|
||||
"YTDLP_PATH" to "/usr/local/bin/yt-dlp",
|
||||
"STREAMRIP_PATH" to "/usr/local/bin/rip",
|
||||
"DEFAULT_SOURCE" to "youtube",
|
||||
"DEEZER_ARL" to "test-arl",
|
||||
"DEEZER_QUALITY" to "1",
|
||||
"DEEZER_API_BASE" to "http://deezer.test",
|
||||
"MEDIA_DIR" to "/mnt/media",
|
||||
"DOWNLOAD_DIR" to "/var/tmp/downloads",
|
||||
),
|
||||
@@ -40,10 +50,25 @@ class AppConfigTest {
|
||||
assertEquals(Path.of("/run/secrets/users.yaml"), config.usersFile)
|
||||
assertEquals("/usr/bin/ffmpeg", config.ffmpegPath)
|
||||
assertEquals("/usr/local/bin/yt-dlp", config.ytDlpPath)
|
||||
assertEquals("/usr/local/bin/rip", config.streamripPath)
|
||||
assertEquals("youtube", config.defaultSource)
|
||||
assertEquals("test-arl", config.deezerArl)
|
||||
assertEquals(1, config.deezerQuality)
|
||||
assertEquals("http://deezer.test", config.deezerApiBase)
|
||||
assertEquals(Path.of("/mnt/media"), config.mediaDir)
|
||||
assertEquals(Path.of("/var/tmp/downloads"), config.downloadDir)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `rejects unknown sources and qualities`() {
|
||||
assertFailsWith<IllegalArgumentException> {
|
||||
AppConfig.fromEnv(mapOf("DEFAULT_SOURCE" to "spotify"))
|
||||
}
|
||||
assertFailsWith<IllegalArgumentException> {
|
||||
AppConfig.fromEnv(mapOf("DEEZER_QUALITY" to "2"))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `rejects a malformed port`() {
|
||||
assertFailsWith<IllegalArgumentException> {
|
||||
|
||||
@@ -31,7 +31,7 @@ class DatabaseFactoryTest {
|
||||
assertEquals("1", pragma(db, "foreign_keys"))
|
||||
|
||||
val migrations = queryInt(db, "SELECT COUNT(*) FROM flyway_schema_history")
|
||||
assertTrue(migrations >= 8, "Expected at least the V1..V8 migrations")
|
||||
assertTrue(migrations >= 9, "Expected at least the V1..V9 migrations")
|
||||
|
||||
// The new schema exists and the mounted-library tables do not.
|
||||
assertTrue(tableExists(db, "songs"))
|
||||
@@ -166,7 +166,8 @@ class DatabaseFactoryTest {
|
||||
transaction(db.database) {
|
||||
SongsTable.insert {
|
||||
it[id] = "sg-1"
|
||||
it[videoId] = "video-1"
|
||||
it[musicSource] = "youtube"
|
||||
it[externalId] = "video-1"
|
||||
it[title] = "Song"
|
||||
it[artist] = "Artist"
|
||||
it[relativePath] = "video-1/song.mp3"
|
||||
|
||||
@@ -16,6 +16,7 @@ import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.media.MediaGcService
|
||||
import win.taptap.musicmanager.media.MediaStore
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.source.MusicSource
|
||||
|
||||
class MoodRepositoryTest {
|
||||
|
||||
@@ -28,7 +29,7 @@ class MoodRepositoryTest {
|
||||
val mood = createMood(recommendationCount = 3)
|
||||
storeCandidates(mood.id, 5)
|
||||
|
||||
val promoted = MoodRepository.promoteCandidates(mood.id, 3)
|
||||
val promoted = MoodRepository.promoteCandidates(mood.id, MusicSource.YOUTUBE, 3)
|
||||
assertEquals(3, promoted.size)
|
||||
assertEquals(3, MoodRepository.activeCount(mood.id))
|
||||
assertEquals(
|
||||
@@ -37,34 +38,53 @@ class MoodRepositoryTest {
|
||||
)
|
||||
|
||||
val first = promoted.first()
|
||||
val song = insertSong(mediaStore, "vid-${first.videoId}", "Track ${first.videoId}")
|
||||
val song = insertSong(
|
||||
mediaStore,
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-${first.externalId}",
|
||||
"Track ${first.externalId}",
|
||||
)
|
||||
assertEquals(
|
||||
RecommendationCompletion.READY,
|
||||
MoodRepository.completeDownload(mood.id, first.videoId, song.id),
|
||||
MoodRepository.completeDownload(
|
||||
mood.id,
|
||||
MusicSource.YOUTUBE,
|
||||
first.externalId,
|
||||
song.id,
|
||||
),
|
||||
)
|
||||
assertEquals(
|
||||
"ready",
|
||||
MoodRepository.recommendation(mood.id, first.externalId)?.state,
|
||||
)
|
||||
assertEquals("ready", MoodRepository.recommendation(mood.id, first.videoId)?.state)
|
||||
|
||||
// Add turns the slot into a permanent mood song and frees it.
|
||||
assertEquals(
|
||||
RecommendationCompletion.ALREADY_ADDED,
|
||||
MoodRepository.markAdded(mood.id, first.videoId),
|
||||
MoodRepository.markAdded(mood.id, first.externalId),
|
||||
)
|
||||
assertTrue(MoodRepository.songIds(mood.id).contains(song.id))
|
||||
assertEquals("added", MoodRepository.recommendation(mood.id, first.videoId)?.state)
|
||||
assertEquals(
|
||||
"added",
|
||||
MoodRepository.recommendation(mood.id, first.externalId)?.state,
|
||||
)
|
||||
assertEquals(2, MoodRepository.activeCount(mood.id))
|
||||
|
||||
val refill = MoodRepository.promoteCandidates(mood.id, 1)
|
||||
val refill = MoodRepository.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
assertEquals(1, refill.size)
|
||||
assertEquals(3, MoodRepository.activeCount(mood.id))
|
||||
|
||||
// Reject is permanent and frees the slot again.
|
||||
MoodRepository.markRejected(mood.id, refill.first().videoId)
|
||||
MoodRepository.markRejected(mood.id, refill.first().externalId)
|
||||
assertEquals(2, MoodRepository.activeCount(mood.id))
|
||||
assertEquals(
|
||||
"rejected",
|
||||
MoodRepository.recommendation(mood.id, refill.first().videoId)?.state,
|
||||
MoodRepository.recommendation(mood.id, refill.first().externalId)?.state,
|
||||
)
|
||||
assertTrue(
|
||||
MoodRepository.decidedExternalIds(mood.id, MusicSource.YOUTUBE)
|
||||
.contains(refill.first().externalId),
|
||||
)
|
||||
assertTrue(MoodRepository.decidedVideoIds(mood.id).contains(refill.first().videoId))
|
||||
assertEquals(
|
||||
1,
|
||||
MoodRepository.allRecommendations(mood.id).count { it.state == "new" },
|
||||
@@ -76,10 +96,10 @@ class MoodRepositoryTest {
|
||||
fun `adding a song converts matching recommendations and drops hidden candidates`() {
|
||||
withDatabase { mediaStore, _ ->
|
||||
val mood = createMood(recommendationCount = 2)
|
||||
val one = insertSong(mediaStore, "v-1", "One")
|
||||
val two = insertSong(mediaStore, "v-2", "Two")
|
||||
val one = insertSong(mediaStore, MusicSource.YOUTUBE, "v-1", "One")
|
||||
val two = insertSong(mediaStore, MusicSource.YOUTUBE, "v-2", "Two")
|
||||
storeCandidates(mood.id, 2)
|
||||
MoodRepository.promoteCandidates(mood.id, 2)
|
||||
MoodRepository.promoteCandidates(mood.id, MusicSource.YOUTUBE, 2)
|
||||
|
||||
assertTrue(MoodRepository.addSong(mood.id, one.id, "search"))
|
||||
assertTrue(MoodRepository.addSong(mood.id, two.id, "search"))
|
||||
@@ -95,7 +115,7 @@ class MoodRepositoryTest {
|
||||
|
||||
// A still-hidden candidate for a video added manually is removed.
|
||||
val another = createMood(recommendationCount = 2, name = "Another")
|
||||
val three = insertSong(mediaStore, "v-3", "Three")
|
||||
val three = insertSong(mediaStore, MusicSource.YOUTUBE, "v-3", "Three")
|
||||
storeCandidates(another.id, 1, startAt = 3)
|
||||
assertNotNull(MoodRepository.recommendation(another.id, "v-3"))
|
||||
MoodRepository.addSong(another.id, three.id, "search")
|
||||
@@ -108,30 +128,76 @@ class MoodRepositoryTest {
|
||||
withDatabase { _, _ ->
|
||||
val mood = createMood(recommendationCount = 3)
|
||||
storeCandidates(mood.id, 3)
|
||||
val first = MoodRepository.promoteCandidates(mood.id, 1).single()
|
||||
MoodRepository.markRejected(mood.id, first.videoId)
|
||||
val first = MoodRepository
|
||||
.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
.single()
|
||||
MoodRepository.markRejected(mood.id, first.externalId)
|
||||
|
||||
// The same video must not come back as a hidden candidate.
|
||||
storeCandidates(mood.id, 3)
|
||||
assertEquals(
|
||||
"rejected",
|
||||
MoodRepository.recommendation(mood.id, first.videoId)?.state,
|
||||
MoodRepository.recommendation(mood.id, first.externalId)?.state,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `switching source clears active recommendations but keeps songs`() {
|
||||
withDatabase { mediaStore, _ ->
|
||||
val mood = createMood(recommendationCount = 2)
|
||||
val song = insertSong(mediaStore, MusicSource.YOUTUBE, "vid-1", "One")
|
||||
MoodRepository.addSong(mood.id, song.id, "search")
|
||||
storeCandidates(mood.id, 2)
|
||||
MoodRepository.promoteCandidates(mood.id, MusicSource.YOUTUBE, 2)
|
||||
assertEquals(2, MoodRepository.activeCount(mood.id))
|
||||
|
||||
val updated = assertNotNull(
|
||||
MoodRepository.update(
|
||||
id = mood.id,
|
||||
owner = "alice",
|
||||
name = mood.name,
|
||||
description = mood.description,
|
||||
query = mood.query,
|
||||
genres = mood.genres,
|
||||
source = MusicSource.DEEZER,
|
||||
collectionPercent = mood.collectionPercent,
|
||||
recommendationCount = mood.recommendationCount,
|
||||
refreshHours = mood.refreshHours,
|
||||
),
|
||||
)
|
||||
assertEquals(MusicSource.DEEZER, updated.source)
|
||||
assertEquals(0, MoodRepository.activeCount(mood.id))
|
||||
assertTrue(MoodRepository.songIds(mood.id).contains(song.id))
|
||||
assertNull(MoodRepository.recommendation(mood.id, "v-1"))
|
||||
|
||||
storeCandidates(mood.id, 2, source = MusicSource.DEEZER)
|
||||
val promoted = MoodRepository
|
||||
.promoteCandidates(mood.id, MusicSource.DEEZER, 2)
|
||||
assertEquals(2, promoted.size)
|
||||
assertEquals(MusicSource.DEEZER, promoted.first().source)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `download completion after a rejection does not attach the song`() {
|
||||
withDatabase { mediaStore, _ ->
|
||||
val mood = createMood(recommendationCount = 2)
|
||||
storeCandidates(mood.id, 2)
|
||||
val recommendation = MoodRepository.promoteCandidates(mood.id, 1).single()
|
||||
val song = insertSong(mediaStore, "vid-1", "One")
|
||||
val recommendation = MoodRepository
|
||||
.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
.single()
|
||||
val song = insertSong(mediaStore, MusicSource.YOUTUBE, "vid-1", "One")
|
||||
|
||||
MoodRepository.markRejected(mood.id, recommendation.videoId)
|
||||
MoodRepository.markRejected(mood.id, recommendation.externalId)
|
||||
assertEquals(
|
||||
RecommendationCompletion.REJECTED,
|
||||
MoodRepository.completeDownload(mood.id, recommendation.videoId, song.id),
|
||||
MoodRepository.completeDownload(
|
||||
mood.id,
|
||||
MusicSource.YOUTUBE,
|
||||
recommendation.externalId,
|
||||
song.id,
|
||||
),
|
||||
)
|
||||
assertTrue(MoodRepository.songIds(mood.id).isEmpty())
|
||||
}
|
||||
@@ -142,16 +208,23 @@ class MoodRepositoryTest {
|
||||
withDatabase { mediaStore, _ ->
|
||||
val mood = createMood(recommendationCount = 2)
|
||||
storeCandidates(mood.id, 2)
|
||||
val recommendation = MoodRepository.promoteCandidates(mood.id, 1).single()
|
||||
val song = insertSong(mediaStore, "vid-1", "One")
|
||||
val recommendation = MoodRepository
|
||||
.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
.single()
|
||||
val song = insertSong(mediaStore, MusicSource.YOUTUBE, "vid-1", "One")
|
||||
|
||||
// User presses Add while the download is still running.
|
||||
MoodRepository.markAdded(mood.id, recommendation.videoId)
|
||||
MoodRepository.markAdded(mood.id, recommendation.externalId)
|
||||
assertTrue(MoodRepository.songIds(mood.id).isEmpty())
|
||||
|
||||
assertEquals(
|
||||
RecommendationCompletion.ALREADY_ADDED,
|
||||
MoodRepository.completeDownload(mood.id, recommendation.videoId, song.id),
|
||||
MoodRepository.completeDownload(
|
||||
mood.id,
|
||||
MusicSource.YOUTUBE,
|
||||
recommendation.externalId,
|
||||
song.id,
|
||||
),
|
||||
)
|
||||
assertEquals(listOf(song.id), MoodRepository.songIds(mood.id))
|
||||
assertFalse(MoodRepository.addSong(mood.id, song.id, "recommendation"))
|
||||
@@ -162,12 +235,17 @@ class MoodRepositoryTest {
|
||||
fun `mood counts reflect songs and active recommendation states`() {
|
||||
withDatabase { mediaStore, _ ->
|
||||
val mood = createMood(recommendationCount = 3)
|
||||
val song = insertSong(mediaStore, "vid-1", "One")
|
||||
val song = insertSong(mediaStore, MusicSource.YOUTUBE, "vid-1", "One")
|
||||
MoodRepository.addSong(mood.id, song.id, "search")
|
||||
storeCandidates(mood.id, 3)
|
||||
val promoted = MoodRepository.promoteCandidates(mood.id, 2)
|
||||
val ready = insertSong(mediaStore, "vid-ready", "Ready")
|
||||
MoodRepository.completeDownload(mood.id, promoted.first().videoId, ready.id)
|
||||
val promoted = MoodRepository.promoteCandidates(mood.id, MusicSource.YOUTUBE, 2)
|
||||
val ready = insertSong(mediaStore, MusicSource.YOUTUBE, "vid-ready", "Ready")
|
||||
MoodRepository.completeDownload(
|
||||
mood.id,
|
||||
MusicSource.YOUTUBE,
|
||||
promoted.first().externalId,
|
||||
ready.id,
|
||||
)
|
||||
|
||||
val record = assertNotNull(MoodRepository.get(mood.id, "alice"))
|
||||
assertEquals(1, record.songCount)
|
||||
@@ -175,7 +253,12 @@ class MoodRepositoryTest {
|
||||
assertEquals(1, record.recommendationDownloading)
|
||||
assertEquals(0, record.recommendationFailed)
|
||||
|
||||
MoodRepository.failRecommendation(mood.id, promoted.last().videoId, "boom", 3)
|
||||
MoodRepository.failRecommendation(
|
||||
mood.id,
|
||||
promoted.last().externalId,
|
||||
"boom",
|
||||
3,
|
||||
)
|
||||
val failed = assertNotNull(MoodRepository.get(mood.id, "alice"))
|
||||
assertEquals(0, failed.recommendationDownloading)
|
||||
assertEquals(1, failed.recommendationFailed)
|
||||
@@ -186,7 +269,7 @@ class MoodRepositoryTest {
|
||||
fun `removing a song only detaches it`() {
|
||||
withDatabase { mediaStore, _ ->
|
||||
val mood = createMood(recommendationCount = 0)
|
||||
val song = insertSong(mediaStore, "vid-1", "One")
|
||||
val song = insertSong(mediaStore, MusicSource.YOUTUBE, "vid-1", "One")
|
||||
MoodRepository.addSong(mood.id, song.id, "search")
|
||||
|
||||
assertTrue(MoodRepository.removeSong(mood.id, song.id))
|
||||
@@ -200,7 +283,7 @@ class MoodRepositoryTest {
|
||||
withDatabase { mediaStore, gc ->
|
||||
val firstMood = createMood(recommendationCount = 0, name = "First")
|
||||
val secondMood = createMood(recommendationCount = 0, name = "Second")
|
||||
val song = insertSong(mediaStore, "vid-1", "One")
|
||||
val song = insertSong(mediaStore, MusicSource.YOUTUBE, "vid-1", "One")
|
||||
val file = assertNotNull(mediaStore.file(song))
|
||||
assertTrue(Files.isRegularFile(file))
|
||||
|
||||
@@ -221,7 +304,7 @@ class MoodRepositoryTest {
|
||||
RadioRepository.close(session.id, "alice")
|
||||
assertTrue(gc.collect(song.id))
|
||||
assertNull(SongRepository.byId(song.id))
|
||||
assertFalse(Files.exists(mediaStore.root.resolve("vid-1")))
|
||||
assertFalse(Files.exists(mediaStore.root.resolve("youtube-vid-1")))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,12 +313,19 @@ class MoodRepositoryTest {
|
||||
withDatabase { mediaStore, gc ->
|
||||
val mood = createMood(recommendationCount = 2)
|
||||
storeCandidates(mood.id, 2)
|
||||
val recommendation = MoodRepository.promoteCandidates(mood.id, 1).single()
|
||||
val song = insertSong(mediaStore, "vid-1", "One")
|
||||
MoodRepository.completeDownload(mood.id, recommendation.videoId, song.id)
|
||||
val recommendation = MoodRepository
|
||||
.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
.single()
|
||||
val song = insertSong(mediaStore, MusicSource.YOUTUBE, "vid-1", "One")
|
||||
MoodRepository.completeDownload(
|
||||
mood.id,
|
||||
MusicSource.YOUTUBE,
|
||||
recommendation.externalId,
|
||||
song.id,
|
||||
)
|
||||
|
||||
assertFalse(gc.collect(song.id))
|
||||
MoodRepository.markRejected(mood.id, recommendation.videoId)
|
||||
MoodRepository.markRejected(mood.id, recommendation.externalId)
|
||||
assertTrue(gc.collect(song.id))
|
||||
}
|
||||
}
|
||||
@@ -252,24 +342,35 @@ class MoodRepositoryTest {
|
||||
}
|
||||
}
|
||||
|
||||
private fun createMood(recommendationCount: Int, name: String = "Focus"): MoodRecord =
|
||||
MoodRepository.create(
|
||||
owner = "alice",
|
||||
name = name,
|
||||
description = null,
|
||||
query = null,
|
||||
genres = emptyList(),
|
||||
collectionPercent = 50,
|
||||
recommendationCount = recommendationCount,
|
||||
refreshHours = 24,
|
||||
)
|
||||
private fun createMood(
|
||||
recommendationCount: Int,
|
||||
name: String = "Focus",
|
||||
source: String = MusicSource.YOUTUBE,
|
||||
): MoodRecord = MoodRepository.create(
|
||||
owner = "alice",
|
||||
name = name,
|
||||
description = null,
|
||||
query = null,
|
||||
genres = emptyList(),
|
||||
source = source,
|
||||
collectionPercent = 50,
|
||||
recommendationCount = recommendationCount,
|
||||
refreshHours = 24,
|
||||
)
|
||||
|
||||
private fun storeCandidates(moodId: String, count: Int, startAt: Int = 1) {
|
||||
private fun storeCandidates(
|
||||
moodId: String,
|
||||
count: Int,
|
||||
startAt: Int = 1,
|
||||
source: String = MusicSource.YOUTUBE,
|
||||
) {
|
||||
MoodRepository.storeCandidates(
|
||||
moodId,
|
||||
source,
|
||||
(startAt until startAt + count).map { index ->
|
||||
RecommendationUpsert(
|
||||
videoId = "v-$index",
|
||||
source = source,
|
||||
externalId = "v-$index",
|
||||
title = "Track $index",
|
||||
artist = "Artist $index",
|
||||
album = null,
|
||||
@@ -283,15 +384,17 @@ class MoodRepositoryTest {
|
||||
|
||||
private fun insertSong(
|
||||
mediaStore: MediaStore,
|
||||
videoId: String,
|
||||
source: String,
|
||||
externalId: String,
|
||||
title: String,
|
||||
): SongRecord {
|
||||
val file = mediaStore.target(videoId, title)
|
||||
val file = mediaStore.target(source, externalId, title)
|
||||
Files.createDirectories(file.parent)
|
||||
Files.write(file, "fake-mp3".toByteArray())
|
||||
return SongRepository.upsert(
|
||||
SongUpsert(
|
||||
videoId = videoId,
|
||||
source = source,
|
||||
externalId = externalId,
|
||||
title = title,
|
||||
artist = "Artist",
|
||||
album = null,
|
||||
|
||||
@@ -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.source.MusicSource
|
||||
|
||||
class DiscoveryServiceTest {
|
||||
|
||||
@@ -24,11 +25,17 @@ class DiscoveryServiceTest {
|
||||
|
||||
assertEquals(3, parsed.size)
|
||||
assertEquals(
|
||||
YtCandidate("a", "Focus One", "Alpha", null, 123, "http://t/2.jpg"),
|
||||
TrackCandidate(MusicSource.YOUTUBE, "a", "Focus One", "Alpha", null, 123, "http://t/2.jpg"),
|
||||
parsed[0],
|
||||
)
|
||||
assertEquals(YtCandidate("b", "Some Song", "Some Artist", null, 61, null), parsed[1])
|
||||
assertEquals(YtCandidate("c", "Loud", "Gamma", null, 90, null), parsed[2])
|
||||
assertEquals(
|
||||
TrackCandidate(MusicSource.YOUTUBE, "b", "Some Song", "Some Artist", null, 61, null),
|
||||
parsed[1],
|
||||
)
|
||||
assertEquals(
|
||||
TrackCandidate(MusicSource.YOUTUBE, "c", "Loud", "Gamma", null, 90, null),
|
||||
parsed[2],
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -52,8 +59,9 @@ class DiscoveryServiceTest {
|
||||
|
||||
assertEquals(3, parsed.size)
|
||||
assertEquals(
|
||||
YtCandidate(
|
||||
videoId = "wU26xVT_vBU",
|
||||
TrackCandidate(
|
||||
source = MusicSource.YOUTUBE,
|
||||
externalId = "wU26xVT_vBU",
|
||||
title = "One More Time (Radio Edit)",
|
||||
artist = "Daft Punk",
|
||||
album = "One More Time",
|
||||
@@ -62,12 +70,75 @@ class DiscoveryServiceTest {
|
||||
),
|
||||
parsed[0],
|
||||
)
|
||||
// A dash inside a real song title must not be treated as an artist split.
|
||||
assertEquals("Get Lucky (feat. Pharrell Williams and Nile Rodgers)", parsed[1].title)
|
||||
assertEquals("Daft Punk, Pharrell Williams, Nile Rodgers", parsed[1].artist)
|
||||
assertEquals(null, parsed[2].album)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parses Deezer track results with album covers`() {
|
||||
val json = """
|
||||
{"data":[
|
||||
{"id":67238732,"title":"Instant Crush (feat. Julian Casablancas)","duration":337,
|
||||
"readable":true,"artist":{"name":"Daft Punk"},
|
||||
"album":{"title":"Random Access Memories","cover_big":"http://c/500.jpg"}},
|
||||
{"id":1,"title":"Unreadable","duration":200,"readable":false,
|
||||
"artist":{"name":"X"},"album":{"title":"Y"}},
|
||||
{"id":2,"title":"Too Long","duration":900,"readable":true,
|
||||
"artist":{"name":"X"},"album":{"title":"Y"}},
|
||||
{"id":3,"title":"Too Short","duration":10,"readable":true,
|
||||
"artist":{"name":"X"},"album":{"title":"Y"}}
|
||||
]}
|
||||
""".trimIndent()
|
||||
|
||||
val parsed = parseDeezerSearch(json)
|
||||
|
||||
assertEquals(1, parsed.size)
|
||||
assertEquals(
|
||||
TrackCandidate(
|
||||
source = MusicSource.DEEZER,
|
||||
externalId = "67238732",
|
||||
title = "Instant Crush (feat. Julian Casablancas)",
|
||||
artist = "Daft Punk",
|
||||
album = "Random Access Memories",
|
||||
duration = 337,
|
||||
thumbnail = "http://c/500.jpg",
|
||||
),
|
||||
parsed.single(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `resolved Deezer URLs keep long tracks so the API can reject them`() {
|
||||
val json = """
|
||||
{"id":1,"title":"Long Set","duration":900,"readable":true,
|
||||
"artist":{"name":"DJ"},"album":{"title":"Long"}}
|
||||
""".trimIndent()
|
||||
|
||||
assertEquals("Long Set", parseDeezerTrack(json)?.title)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parses a single resolved Deezer track`() {
|
||||
val json = """
|
||||
{"id":66609426,"title":"Get Lucky (Radio Edit)","duration":248,"readable":true,
|
||||
"artist":{"name":"Daft Punk"},"album":{"title":"Get Lucky","cover_xl":"http://c/xl.jpg"}}
|
||||
""".trimIndent()
|
||||
|
||||
assertEquals(
|
||||
TrackCandidate(
|
||||
source = MusicSource.DEEZER,
|
||||
externalId = "66609426",
|
||||
title = "Get Lucky (Radio Edit)",
|
||||
artist = "Daft Punk",
|
||||
album = "Get Lucky",
|
||||
duration = 248,
|
||||
thumbnail = "http://c/xl.jpg",
|
||||
),
|
||||
parseDeezerTrack(json),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parses a single resolved video`() {
|
||||
val json = """
|
||||
@@ -76,8 +147,9 @@ class DiscoveryServiceTest {
|
||||
""".trimIndent()
|
||||
|
||||
assertEquals(
|
||||
YtCandidate(
|
||||
videoId = "dQw4w9WgXcQ",
|
||||
TrackCandidate(
|
||||
source = MusicSource.YOUTUBE,
|
||||
externalId = "dQw4w9WgXcQ",
|
||||
title = "Never Gonna Give You Up",
|
||||
artist = "Rick Astley",
|
||||
album = null,
|
||||
@@ -94,6 +166,8 @@ class DiscoveryServiceTest {
|
||||
assertTrue(parseYtSearchOutput("not json").isEmpty())
|
||||
assertTrue(parseYtSearchOutput("""{"entries": "nope"}""").isEmpty())
|
||||
assertEquals(null, parseYtVideo("not json"))
|
||||
assertTrue(parseDeezerSearch("not json").isEmpty())
|
||||
assertEquals(null, parseDeezerTrack("not json"))
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -141,7 +215,13 @@ class DiscoveryServiceTest {
|
||||
|
||||
@Test
|
||||
fun `collection artists score higher`() {
|
||||
val candidate = YtCandidate("v", "Focus Two", "Beta", duration = 200)
|
||||
val candidate = TrackCandidate(
|
||||
source = MusicSource.DEEZER,
|
||||
externalId = "1",
|
||||
title = "Focus Two",
|
||||
artist = "Beta",
|
||||
duration = 200,
|
||||
)
|
||||
|
||||
val plain = scoreCandidate(candidate, listOf("Alpha"), "focus")
|
||||
val seedArtist = scoreCandidate(candidate, listOf("Beta"), "focus")
|
||||
@@ -159,6 +239,7 @@ class DiscoveryServiceTest {
|
||||
description = null,
|
||||
query = query,
|
||||
genres = genres,
|
||||
source = MusicSource.DEEZER,
|
||||
collectionPercent = 50,
|
||||
recommendationCount = 10,
|
||||
refreshHours = 24,
|
||||
|
||||
@@ -18,9 +18,10 @@ import win.taptap.musicmanager.db.ConfigRepository
|
||||
import win.taptap.musicmanager.db.DatabaseFactory
|
||||
import win.taptap.musicmanager.db.DatabaseHandle
|
||||
import win.taptap.musicmanager.db.DownloadRepository
|
||||
import win.taptap.musicmanager.db.MoodRepository
|
||||
import win.taptap.musicmanager.db.MoodRecord
|
||||
import win.taptap.musicmanager.db.MoodRepository
|
||||
import win.taptap.musicmanager.db.RecommendationUpsert
|
||||
import win.taptap.musicmanager.db.SongRecord
|
||||
import win.taptap.musicmanager.db.SongRepository
|
||||
import win.taptap.musicmanager.db.SongUpsert
|
||||
import win.taptap.musicmanager.media.MediaGcService
|
||||
@@ -28,6 +29,7 @@ import win.taptap.musicmanager.media.MediaStore
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.scanner.Mp3TagExtractor
|
||||
import win.taptap.musicmanager.scanner.SongIndexer
|
||||
import win.taptap.musicmanager.source.MusicSource
|
||||
|
||||
class DownloadServiceTest {
|
||||
|
||||
@@ -39,7 +41,12 @@ class DownloadServiceTest {
|
||||
TestAudio.assumeFfmpeg()
|
||||
withService { service, mediaStore, _ ->
|
||||
val mood = mood(recommendationCount = 0)
|
||||
DownloadRepository.enqueue("vid-manual", mood.id, DownloadRepository.KIND_SONG)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-manual",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
|
||||
@@ -51,20 +58,56 @@ class DownloadServiceTest {
|
||||
assertEquals(listOf(songId), MoodRepository.songIds(mood.id))
|
||||
|
||||
val song = assertNotNull(SongRepository.byId(songId))
|
||||
assertEquals("vid-manual", song.videoId)
|
||||
assertEquals(MusicSource.YOUTUBE, song.source)
|
||||
assertEquals("vid-manual", song.externalId)
|
||||
assertEquals("Song vid-manual", song.title)
|
||||
assertEquals("Artist", song.artist)
|
||||
assertEquals("vid-manual/Song vid-manual.mp3", song.relativePath)
|
||||
assertEquals("youtube-vid-manual/Song vid-manual.mp3", song.relativePath)
|
||||
assertTrue(Files.isRegularFile(assertNotNull(mediaStore.file(song))))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a Deezer job uses the Deezer downloader and stores its tags`() {
|
||||
var called = false
|
||||
val youtube = AudioDownloader { _, _ -> error("YouTube downloader must not run") }
|
||||
val deezer = AudioDownloader { externalId, dir ->
|
||||
called = true
|
||||
assertEquals("67238732", externalId)
|
||||
mp3("deezer-$externalId", dir, title = "Instant Crush", artist = "Daft Punk")
|
||||
}
|
||||
withService(youtube = youtube, deezer = deezer) { service, mediaStore, _ ->
|
||||
val mood = mood(recommendationCount = 0, source = MusicSource.DEEZER)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.DEEZER,
|
||||
"67238732",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
assertTrue(called)
|
||||
|
||||
val song = assertNotNull(SongRepository.byExternalId(MusicSource.DEEZER, "67238732"))
|
||||
assertEquals("Daft Punk", song.artist)
|
||||
assertEquals("deezer-67238732/Instant Crush.mp3", song.relativePath)
|
||||
assertTrue(Files.isRegularFile(assertNotNull(mediaStore.file(song))))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an already downloaded song attaches without another download`() {
|
||||
withService(downloader = AudioDownloader { _, _ -> error("downloader must not run") }) { service, mediaStore, _ ->
|
||||
withService(
|
||||
youtube = AudioDownloader { _, _ -> error("downloader must not run") },
|
||||
) { service, mediaStore, _ ->
|
||||
val mood = mood(recommendationCount = 0)
|
||||
val song = insertSong(mediaStore, "vid-existing")
|
||||
DownloadRepository.enqueue("vid-existing", mood.id, DownloadRepository.KIND_SONG)
|
||||
val song = insertSong(mediaStore, MusicSource.YOUTUBE, "vid-existing")
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-existing",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
|
||||
@@ -76,11 +119,23 @@ class DownloadServiceTest {
|
||||
fun `the same video is downloaded once for two moods`() {
|
||||
TestAudio.assumeFfmpeg()
|
||||
var downloads = 0
|
||||
withService(downloader = AudioDownloader { videoId, dir -> downloads++; mp3(videoId, dir) }) { service, _, _ ->
|
||||
withService(
|
||||
youtube = AudioDownloader { externalId, dir -> downloads++; mp3(externalId, dir) },
|
||||
) { service, _, _ ->
|
||||
val first = mood(recommendationCount = 0, name = "First")
|
||||
val second = mood(recommendationCount = 0, name = "Second")
|
||||
DownloadRepository.enqueue("vid-shared", first.id, DownloadRepository.KIND_SONG)
|
||||
DownloadRepository.enqueue("vid-shared", second.id, DownloadRepository.KIND_SONG)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-shared",
|
||||
first.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-shared",
|
||||
second.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
assertTrue(service.processOne())
|
||||
@@ -97,9 +152,12 @@ class DownloadServiceTest {
|
||||
withService { service, _, _ ->
|
||||
val mood = mood(recommendationCount = 1)
|
||||
storeRecommendation(mood.id, "vid-rec")
|
||||
val promoted = MoodRepository.promoteCandidates(mood.id, 1).single()
|
||||
val promoted = MoodRepository
|
||||
.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
.single()
|
||||
DownloadRepository.enqueue(
|
||||
promoted.videoId,
|
||||
promoted.source,
|
||||
promoted.externalId,
|
||||
mood.id,
|
||||
DownloadRepository.KIND_RECOMMENDATION,
|
||||
)
|
||||
@@ -121,8 +179,9 @@ class DownloadServiceTest {
|
||||
withService { service, _, _ ->
|
||||
val mood = mood(recommendationCount = 1)
|
||||
storeRecommendation(mood.id, "vid-rec")
|
||||
MoodRepository.promoteCandidates(mood.id, 1)
|
||||
MoodRepository.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-rec",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_RECOMMENDATION,
|
||||
@@ -143,8 +202,9 @@ class DownloadServiceTest {
|
||||
withService { service, mediaStore, _ ->
|
||||
val mood = mood(recommendationCount = 1)
|
||||
storeRecommendation(mood.id, "vid-rec")
|
||||
MoodRepository.promoteCandidates(mood.id, 1)
|
||||
MoodRepository.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-rec",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_RECOMMENDATION,
|
||||
@@ -154,18 +214,19 @@ class DownloadServiceTest {
|
||||
assertTrue(service.processOne())
|
||||
|
||||
assertTrue(MoodRepository.songIds(mood.id).isEmpty())
|
||||
assertNull(SongRepository.byVideoId("vid-rec"))
|
||||
assertTrue(Files.notExists(mediaStore.root.resolve("vid-rec")))
|
||||
assertNull(SongRepository.byExternalId(MusicSource.YOUTUBE, "vid-rec"))
|
||||
assertTrue(Files.notExists(mediaStore.root.resolve("youtube-vid-rec")))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `failures retry three times and leave the recommendation failed`() {
|
||||
withService(downloader = AudioDownloader { _, _ -> error("network down") }) { service, _, _ ->
|
||||
withService(youtube = AudioDownloader { _, _ -> error("network down") }) { service, _, _ ->
|
||||
val mood = mood(recommendationCount = 1)
|
||||
storeRecommendation(mood.id, "vid-broken")
|
||||
MoodRepository.promoteCandidates(mood.id, 1)
|
||||
MoodRepository.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-broken",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_RECOMMENDATION,
|
||||
@@ -176,7 +237,11 @@ class DownloadServiceTest {
|
||||
assertTrue(service.processOne())
|
||||
|
||||
val job = assertNotNull(
|
||||
DownloadRepository.find(mood.id, "vid-broken", DownloadRepository.KIND_RECOMMENDATION),
|
||||
DownloadRepository.find(
|
||||
mood.id,
|
||||
"vid-broken",
|
||||
DownloadRepository.KIND_RECOMMENDATION,
|
||||
),
|
||||
)
|
||||
assertEquals("failed", job.state)
|
||||
assertEquals(3, job.attempts)
|
||||
@@ -191,10 +256,15 @@ class DownloadServiceTest {
|
||||
@Test
|
||||
fun `permission errors fail without burning retries`() {
|
||||
withService(
|
||||
downloader = AudioDownloader { _, _ -> throw AccessDeniedException("/data/media") },
|
||||
youtube = AudioDownloader { _, _ -> throw AccessDeniedException("/data/media") },
|
||||
) { service, _, _ ->
|
||||
val mood = mood(recommendationCount = 0)
|
||||
DownloadRepository.enqueue("vid-denied", mood.id, DownloadRepository.KIND_SONG)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-denied",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
|
||||
@@ -207,41 +277,76 @@ class DownloadServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an unconfigured Deezer account fails permanently`() {
|
||||
val deezer = DeezerDownloader(arl = null)
|
||||
withService(deezer = deezer) { service, _, _ ->
|
||||
val mood = mood(recommendationCount = 0, source = MusicSource.DEEZER)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.DEEZER,
|
||||
"67238732",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
|
||||
val job = assertNotNull(
|
||||
DownloadRepository.find(mood.id, "67238732", DownloadRepository.KIND_SONG),
|
||||
)
|
||||
assertEquals("failed", job.state)
|
||||
assertEquals(1, job.attempts)
|
||||
assertTrue(job.error?.contains("Deezer is not configured") == true)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `manual adds outrank recommendation downloads`() {
|
||||
withDatabase { _, _ ->
|
||||
val mood = mood(recommendationCount = 1)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-rec",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_RECOMMENDATION,
|
||||
)
|
||||
DownloadRepository.enqueue("vid-manual", mood.id, DownloadRepository.KIND_SONG)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-manual",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
val claimed = assertNotNull(DownloadRepository.claimNext())
|
||||
assertEquals("vid-manual", claimed.videoId)
|
||||
assertEquals("vid-manual", claimed.externalId)
|
||||
assertEquals(DownloadRepository.KIND_SONG, claimed.kind)
|
||||
}
|
||||
}
|
||||
|
||||
private fun mood(recommendationCount: Int, name: String = "Focus"): MoodRecord =
|
||||
MoodRepository.create(
|
||||
owner = "alice",
|
||||
name = name,
|
||||
description = null,
|
||||
query = null,
|
||||
genres = emptyList(),
|
||||
collectionPercent = 50,
|
||||
recommendationCount = recommendationCount,
|
||||
refreshHours = 24,
|
||||
)
|
||||
private fun mood(
|
||||
recommendationCount: Int,
|
||||
name: String = "Focus",
|
||||
source: String = MusicSource.YOUTUBE,
|
||||
): MoodRecord = MoodRepository.create(
|
||||
owner = "alice",
|
||||
name = name,
|
||||
description = null,
|
||||
query = null,
|
||||
genres = emptyList(),
|
||||
source = source,
|
||||
collectionPercent = 50,
|
||||
recommendationCount = recommendationCount,
|
||||
refreshHours = 24,
|
||||
)
|
||||
|
||||
private fun storeRecommendation(moodId: String, videoId: String) {
|
||||
private fun storeRecommendation(moodId: String, externalId: String) {
|
||||
MoodRepository.storeCandidates(
|
||||
moodId,
|
||||
MusicSource.YOUTUBE,
|
||||
listOf(
|
||||
RecommendationUpsert(
|
||||
videoId = videoId,
|
||||
source = MusicSource.YOUTUBE,
|
||||
externalId = externalId,
|
||||
title = "Recommended",
|
||||
artist = "Artist",
|
||||
album = null,
|
||||
@@ -254,7 +359,8 @@ class DownloadServiceTest {
|
||||
}
|
||||
|
||||
private fun withService(
|
||||
downloader: AudioDownloader = AudioDownloader { videoId, dir -> mp3(videoId, dir) },
|
||||
youtube: AudioDownloader = AudioDownloader { externalId, dir -> mp3(externalId, dir) },
|
||||
deezer: AudioDownloader = AudioDownloader { externalId, dir -> mp3("deezer-$externalId", dir) },
|
||||
block: (DownloadService, MediaStore, MediaGcService) -> Unit,
|
||||
) {
|
||||
withDatabase { handle, store ->
|
||||
@@ -262,7 +368,12 @@ class DownloadServiceTest {
|
||||
val indexer = SongIndexer(store, coverArt)
|
||||
val gc = MediaGcService(store, coverArt)
|
||||
val service = DownloadService(
|
||||
downloader = downloader,
|
||||
downloaders = SourceDownloaders(
|
||||
mapOf(
|
||||
MusicSource.YOUTUBE to youtube,
|
||||
MusicSource.DEEZER to deezer,
|
||||
),
|
||||
),
|
||||
mediaStore = store,
|
||||
indexer = indexer,
|
||||
gc = gc,
|
||||
@@ -271,8 +382,8 @@ class DownloadServiceTest {
|
||||
)
|
||||
try {
|
||||
block(service, store, gc)
|
||||
} catch (t: Throwable) {
|
||||
throw t
|
||||
} finally {
|
||||
handle.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -288,26 +399,36 @@ class DownloadServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
private fun mp3(videoId: String, dir: Path): Path {
|
||||
val file = dir.resolve("$videoId.mp3")
|
||||
private fun mp3(
|
||||
externalId: String,
|
||||
dir: Path,
|
||||
title: String = "Song $externalId",
|
||||
artist: String = "Artist",
|
||||
): Path {
|
||||
val file = dir.resolve("$externalId.mp3")
|
||||
TestAudio.createMp3(
|
||||
file,
|
||||
metadata = mapOf(
|
||||
"title" to "Song $videoId",
|
||||
"artist" to "Artist",
|
||||
"title" to title,
|
||||
"artist" to artist,
|
||||
"album" to "Album",
|
||||
),
|
||||
)
|
||||
return file
|
||||
}
|
||||
|
||||
private fun insertSong(mediaStore: MediaStore, videoId: String): win.taptap.musicmanager.db.SongRecord {
|
||||
val file = mediaStore.target(videoId, "Existing")
|
||||
private fun insertSong(
|
||||
mediaStore: MediaStore,
|
||||
source: String,
|
||||
externalId: String,
|
||||
): SongRecord {
|
||||
val file = mediaStore.target(source, externalId, "Existing")
|
||||
Files.createDirectories(file.parent)
|
||||
Files.write(file, "fake-mp3".toByteArray())
|
||||
return SongRepository.upsert(
|
||||
SongUpsert(
|
||||
videoId = videoId,
|
||||
source = source,
|
||||
externalId = externalId,
|
||||
title = "Existing",
|
||||
artist = "Artist",
|
||||
album = null,
|
||||
|
||||
@@ -121,7 +121,8 @@ class RadioServiceTest {
|
||||
album: String? = "Album $id",
|
||||
) = SongRecord(
|
||||
id = id,
|
||||
videoId = "video-$id",
|
||||
source = "youtube",
|
||||
externalId = "video-$id",
|
||||
title = "Title $id",
|
||||
artist = artist,
|
||||
album = album,
|
||||
|
||||
@@ -14,6 +14,7 @@ import win.taptap.musicmanager.db.DatabaseFactory
|
||||
import win.taptap.musicmanager.db.DatabaseHandle
|
||||
import win.taptap.musicmanager.db.SongRepository
|
||||
import win.taptap.musicmanager.media.MediaStore
|
||||
import win.taptap.musicmanager.source.MusicSource
|
||||
import win.taptap.musicmanager.util.IdGenerator
|
||||
|
||||
class MediaScannerTest {
|
||||
@@ -50,8 +51,8 @@ class MediaScannerTest {
|
||||
assertEquals(2L, first.count)
|
||||
assertEquals(2L, SongRepository.count())
|
||||
|
||||
val song = assertNotNull(SongRepository.byVideoId("vid-one"))
|
||||
assertEquals(IdGenerator.songId("vid-one"), song.id)
|
||||
val song = assertNotNull(SongRepository.byExternalId(MusicSource.YOUTUBE, "vid-one"))
|
||||
assertEquals(IdGenerator.songId(MusicSource.YOUTUBE, "vid-one"), song.id)
|
||||
assertEquals("One", song.title)
|
||||
assertEquals("Alpha", song.artist)
|
||||
assertEquals("Album", song.album)
|
||||
@@ -73,13 +74,13 @@ class MediaScannerTest {
|
||||
val third = scanner.scanBlocking()
|
||||
assertNull(third.error)
|
||||
assertEquals(1L, third.count)
|
||||
assertNull(SongRepository.byVideoId("vid-two"))
|
||||
assertNull(SongRepository.byExternalId(MusicSource.YOUTUBE, "vid-two"))
|
||||
|
||||
// Re-adding it indexes it again.
|
||||
TestAudio.createMp3(two, metadata = mapOf("title" to "Two", "artist" to "Beta"))
|
||||
val fourth = scanner.scanBlocking()
|
||||
assertEquals(2L, fourth.count)
|
||||
assertNotNull(SongRepository.byVideoId("vid-two"))
|
||||
assertNotNull(SongRepository.byExternalId(MusicSource.YOUTUBE, "vid-two"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@ class IdGeneratorTest {
|
||||
|
||||
@Test
|
||||
fun `song IDs are deterministic, prefixed and 24 hex characters`() {
|
||||
val first = IdGenerator.songId("dQw4w9WgXcQ")
|
||||
val second = IdGenerator.songId(" dQw4w9WgXcQ ")
|
||||
val first = IdGenerator.songId("youtube", "dQw4w9WgXcQ")
|
||||
val second = IdGenerator.songId("youtube", " dQw4w9WgXcQ ")
|
||||
|
||||
assertEquals(first, second)
|
||||
assertTrue(first.startsWith("sg-"))
|
||||
@@ -27,8 +27,8 @@ class IdGeneratorTest {
|
||||
@Test
|
||||
fun `different videos produce different IDs`() {
|
||||
assertNotEquals(
|
||||
IdGenerator.songId("dQw4w9WgXcQ"),
|
||||
IdGenerator.songId("dQw4w9WgXcR"),
|
||||
IdGenerator.songId("youtube", "dQw4w9WgXcQ"),
|
||||
IdGenerator.songId("youtube", "dQw4w9WgXcR"),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user