fix: harden scans, downloads, sync and radio edge cases
CI / chart (push) Successful in 3s
CI / test (push) Successful in 2m2s
CI / chart-push (push) Successful in 4s
CI / image (push) Successful in 3m13s

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:
TapTap committed 2026-11-15 18:18:21 +00:00
1 parent c75f2971b4
commit 4be502bb4e
23 files changed
+705 -66

No files matched your search

@@ -53,6 +53,7 @@ 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.LibraryTombstonesTable
import win.taptap.musicmanager.db.SongArtistsTable
import win.taptap.musicmanager.db.SongGenresTable
import win.taptap.musicmanager.db.SongsTable
@@ -81,6 +82,13 @@ class ApiRoutesTest {
assertEquals("alice", ok["user"]!!.jsonObject.string("name"))
assertEquals(listOf(1), ok["user"]!!.jsonObject["musicFolders"]!!.jsonArray.map { it.jsonPrimitive.int })
val malformed = client.post("/api/v1/moods") {
bearer(parse(client.login("alice", "alice-pw").bodyAsText()).string("token"))
contentType(ContentType.Application.Json)
setBody("not-json")
}
assertEquals(HttpStatusCode.BadRequest, malformed.status)
val bad = client.login("alice", "wrong")
assertEquals(HttpStatusCode.Unauthorized, bad.status)
assertEquals("invalid_credentials", parse(bad.bodyAsText())["error"]!!.jsonObject.string("code"))
@@ -110,6 +118,15 @@ class ApiRoutesTest {
assertFalse(seed.bobSong in ids)
assertTrue(page.string("revision").isNotBlank())
// A snapshot revision is echoed so multi-page syncs share one cutoff.
val snapshot = "2026-01-01T00:00:00Z"
val paged = parseBody(
client.get("/api/v1/library/songs?scope=music&limit=1&revision=$snapshot") {
bearer(token)
}.bodyAsText(),
)
assertEquals(snapshot, paged.string("revision"))
val longform = parseBody(
client.get("/api/v1/library/songs?scope=longform") {
bearer(token)
@@ -131,6 +148,54 @@ class ApiRoutesTest {
val forbidden = client.get("/api/v1/songs/${seed.bobSong}/stream?token=$token")
assertEquals(HttpStatusCode.NotFound, forbidden.status)
// A wildcard-only search must not return the whole library.
val wildcard = parseBody(
client.get("/api/v1/library/search?q=%25&scope=music") { bearer(token) }.bodyAsText(),
)
assertTrue(wildcard["songs"]!!.jsonArray.isEmpty())
}
}
}
@Test
fun `delta sync continues when only tombstones fill the page`() {
seededContext().use { seed ->
testApplication {
setupApp(seed)
val token = client.token("alice", "alice-pw")
val since = Instant.now().plusSeconds(1)
transaction(seed.db.database) {
listOf("gone-1" to 2L, "gone-2" to 3L).forEach { (id, offset) ->
LibraryTombstonesTable.insert {
it[entity] = "song"
it[entityId] = id
it[deletedAt] = since.plusSeconds(offset).toString()
}
}
}
val first = parseBody(
client.get("/api/v1/library/songs?limit=1&since=$since") {
bearer(token)
}.bodyAsText(),
)
assertEquals(
listOf("gone-1"),
first["deletedSongIds"]!!.jsonArray.map { it.jsonPrimitive.content },
)
val next = first["nextSince"]!!.jsonPrimitive.content
val second = parseBody(
client.get("/api/v1/library/songs?limit=1&since=$next") {
bearer(token)
}.bodyAsText(),
)
assertEquals(
listOf("gone-2"),
second["deletedSongIds"]!!.jsonArray.map { it.jsonPrimitive.content },
)
}
}
}
@@ -189,6 +254,22 @@ class ApiRoutesTest {
assertFalse(queue.any { it.string("id") == seed.bobSong })
assertTrue(queue.all { it.string("folderId") == "1" })
// Unknown ids must not break event recording with a foreign-key error.
val unknownEvent = client.post("/api/v1/sessions/$sessionId/events") {
bearer(token)
contentType(ContentType.Application.Json)
setBody("""{"events":[{"songId":"sg-does-not-exist","type":"completed"}]}""")
}
assertEquals(HttpStatusCode.OK, unknownEvent.status)
assertEquals(0, parse(unknownEvent.bodyAsText()).int("accepted"))
val badNext = client.post("/api/v1/sessions/$sessionId/next") {
bearer(token)
contentType(ContentType.Application.Json)
setBody("not-json")
}
assertEquals(HttpStatusCode.BadRequest, badNext.status)
val second = client.post("/api/v1/sessions/$sessionId/next") {
bearer(token)
contentType(ContentType.Application.Json)