fix: harden scans, downloads, sync and radio edge cases
Scanner and library: - Keep the last library when a configured folder cannot be walked; the previous code deleted every song, cascading away mood seeds and progress. - Re-adding a file now clears its tombstone, and a re-deletion refreshes the timestamp instead of keeping the old one. - Tombstone timestamps are unique per row, and delta pages continue from the older of the song/tombstone page ends. - Snapshot pagination accepts a stable revision for later pages so songs inserted mid-sync are not skipped forever. - Reject "%" and "_" searches that normalize to an empty LIKE needle. Downloads and discovery: - Download promotion uses the deterministic path id, one folder per track so covers do not overwrite each other, removes failed promotions, and restarts the scan after a running one finishes. - Stale downloading jobs are recovered; reject cancels queued/in-flight jobs; markDone only owns downloading rows; rejecting no longer seeds the track. - Reuse an already completed download of the same video for another mood. - yt-dlp stdout and stderr are separated, empty discovery results no longer prune undecided recommendations, and filenames are capped by UTF-8 bytes. API and data: - Malformed or missing JSON returns 400 instead of 500; optional session bodies use receiveNullable. - Playback events must belong to the session, known song and open session. - Radio refills the candidate pool after recent-song pruning. - Seed percent is required in MoodRepository.update. - Normalize legacy V4 liked/downloaded recommendation states before V5, with an upgrade regression test. CI: check out without persisting credentials and require tests before chart publishing.
This commit is contained in:
1 parent
c75f2971b4
commit
4be502bb4e
23 files changed
+705
-66
No files matched your search
@@ -6,10 +6,13 @@ import java.nio.file.Path
|
||||
import java.time.Instant
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.insertIgnore
|
||||
import org.jetbrains.exposed.sql.update
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
import win.taptap.musicmanager.config.AppConfig
|
||||
@@ -24,6 +27,7 @@ import win.taptap.musicmanager.db.ArtistsTable
|
||||
import win.taptap.musicmanager.db.ConfigRepository
|
||||
import win.taptap.musicmanager.db.DatabaseFactory
|
||||
import win.taptap.musicmanager.db.DatabaseHandle
|
||||
import win.taptap.musicmanager.db.DownloadJobsTable
|
||||
import win.taptap.musicmanager.db.DownloadRepository
|
||||
import win.taptap.musicmanager.db.MoodRepository
|
||||
import win.taptap.musicmanager.db.RecommendationUpsert
|
||||
@@ -100,9 +104,84 @@ class DownloadServiceTest {
|
||||
assertTrue(MoodRepository.seedSongIds(mood.id).contains(songId))
|
||||
|
||||
val imports = library.folders.first().path.resolve("AudioManager Imports")
|
||||
val files = Files.list(imports).use { stream -> stream.toList() }
|
||||
val files = Files.walk(imports).use { stream ->
|
||||
stream.filter { Files.isRegularFile(it) }.toList()
|
||||
}
|
||||
assertEquals(1, files.size)
|
||||
assertEquals("Die drei ___ - Neue Folge.mp3", files.first().fileName.toString())
|
||||
assertEquals("Neue Folge.mp3", files.first().fileName.toString())
|
||||
assertEquals("Neue Folge", files.first().parent.fileName.toString())
|
||||
assertEquals("Die drei ___", files.first().parent.parent.fileName.toString())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun `downloaded file is promoted instead of an older same-title song`() {
|
||||
withDatabase { handle, library, users ->
|
||||
val mood = MoodRepository.create(
|
||||
owner = "alice",
|
||||
name = "Drei ???",
|
||||
description = null,
|
||||
query = null,
|
||||
genres = emptyList(),
|
||||
targetSize = 30,
|
||||
refreshHours = 24,
|
||||
ytEnabled = true,
|
||||
seedSongIds = emptyList(),
|
||||
)
|
||||
MoodRepository.storeRecommendations(
|
||||
mood.id,
|
||||
listOf(
|
||||
RecommendationUpsert(
|
||||
videoId = "v-dup",
|
||||
title = "Neue Folge",
|
||||
artist = "Die drei ???",
|
||||
album = null,
|
||||
duration = 300,
|
||||
thumbnail = null,
|
||||
matchedSongId = null,
|
||||
),
|
||||
),
|
||||
"2026-01-01T00:00:00Z",
|
||||
)
|
||||
DownloadRepository.enqueue("alice", mood.id, "v-dup", "Neue Folge", "Die drei ???")
|
||||
|
||||
val oldId = insertSong(
|
||||
handle,
|
||||
"Die drei ???",
|
||||
"Neue Folge",
|
||||
"Old/Die drei ??? - Neue Folge.mp3",
|
||||
)
|
||||
var newId: String? = null
|
||||
val service = DownloadService(
|
||||
users = users,
|
||||
library = library,
|
||||
downloader = AudioDownloader { videoId, _, _, dir ->
|
||||
val file = dir.resolve("$videoId.mp3")
|
||||
Files.write(file, "fake-mp3".toByteArray())
|
||||
file
|
||||
},
|
||||
tempDir = tempDir.resolve("downloads"),
|
||||
startScan = {
|
||||
if (newId == null) {
|
||||
newId = insertSong(
|
||||
handle,
|
||||
"Die drei ???",
|
||||
"Neue Folge",
|
||||
"AudioManager Imports/Die drei ___/Neue Folge/Neue Folge.mp3",
|
||||
)
|
||||
}
|
||||
true
|
||||
},
|
||||
isScanning = { false },
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
|
||||
val job = assertNotNull(DownloadRepository.forRecommendation("alice", mood.id, "v-dup"))
|
||||
assertEquals("done", job.state)
|
||||
assertEquals(newId, job.songId)
|
||||
assertNotEquals(oldId, job.songId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,6 +223,78 @@ class DownloadServiceTest {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
fun `stale downloading jobs are recovered`() {
|
||||
withDatabase { handle, library, users ->
|
||||
val mood = MoodRepository.create(
|
||||
owner = "alice",
|
||||
name = "Recovery",
|
||||
description = null,
|
||||
query = null,
|
||||
genres = emptyList(),
|
||||
targetSize = 30,
|
||||
refreshHours = 24,
|
||||
ytEnabled = true,
|
||||
seedSongIds = emptyList(),
|
||||
)
|
||||
DownloadRepository.enqueue("alice", mood.id, "v-stale", "Stale", "Nobody")
|
||||
val job = assertNotNull(DownloadRepository.forRecommendation("alice", mood.id, "v-stale"))
|
||||
transaction(handle.database) {
|
||||
DownloadJobsTable.update({ DownloadJobsTable.id eq job.id }) {
|
||||
it[state] = "downloading"
|
||||
it[updatedAt] = Instant.now().minusSeconds(3600).toString()
|
||||
}
|
||||
}
|
||||
|
||||
val claimed = assertNotNull(DownloadRepository.claimNext())
|
||||
assertEquals(job.id, claimed.id)
|
||||
assertEquals("downloading", claimed.state)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `failed verification removes the promoted file instead of duplicating it`() {
|
||||
withDatabase { _, library, users ->
|
||||
val mood = MoodRepository.create(
|
||||
owner = "alice",
|
||||
name = "Broken match",
|
||||
description = null,
|
||||
query = null,
|
||||
genres = emptyList(),
|
||||
targetSize = 30,
|
||||
refreshHours = 24,
|
||||
ytEnabled = true,
|
||||
seedSongIds = emptyList(),
|
||||
)
|
||||
DownloadRepository.enqueue("alice", mood.id, "v-nomatch", "No Match", "Nobody")
|
||||
val service = DownloadService(
|
||||
users = users,
|
||||
library = library,
|
||||
downloader = AudioDownloader { videoId, _, _, dir ->
|
||||
val file = dir.resolve("$videoId.mp3")
|
||||
Files.write(file, "fake-mp3".toByteArray())
|
||||
file
|
||||
},
|
||||
tempDir = tempDir.resolve("downloads"),
|
||||
startScan = { true },
|
||||
isScanning = { false },
|
||||
)
|
||||
|
||||
repeat(3) { assertTrue(service.processOne()) }
|
||||
|
||||
val job = assertNotNull(DownloadRepository.forRecommendation("alice", mood.id, "v-nomatch"))
|
||||
assertEquals("failed", job.state)
|
||||
val imports = library.folders.first().path.resolve("AudioManager Imports")
|
||||
if (Files.isDirectory(imports)) {
|
||||
val files = Files.walk(imports).use { stream ->
|
||||
stream.filter { Files.isRegularFile(it) }.toList()
|
||||
}
|
||||
assertTrue(files.isEmpty(), "leftover promoted files: $files")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `permission errors fail without burning retries`() {
|
||||
withDatabase { _, library, users ->
|
||||
@@ -205,10 +356,14 @@ class DownloadServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
private fun insertSong(handle: DatabaseHandle, artistName: String, title: String) {
|
||||
private fun insertSong(
|
||||
handle: DatabaseHandle,
|
||||
artistName: String,
|
||||
title: String,
|
||||
relativePath: String = "AudioManager Imports/$artistName - $title.mp3",
|
||||
): String {
|
||||
val artistId = IdGenerator.artistId(artistName)
|
||||
val albumId = IdGenerator.albumId(artistName, "Downloads")
|
||||
val relativePath = "AudioManager Imports/$artistName - $title.mp3"
|
||||
val songId = IdGenerator.songId(1, relativePath)
|
||||
val now = Instant.now().toString()
|
||||
|
||||
@@ -258,5 +413,6 @@ class DownloadServiceTest {
|
||||
it[genre] = "Hörspiel"
|
||||
}
|
||||
}
|
||||
return songId
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user