feat: rebuild AudioManager around downloadable moods
CI / chart (push) Successful in 3s
CI / test (push) Successful in 2m2s
CI / chart-push (push) Successful in 4s
CI / image (push) Successful in 3m20s

- V8 wipe migration and the shared song store keyed by YouTube video id
- media store, indexer, downloader, recommendation top-up and refcounted GC
- mood songs plus the recommendation lifecycle and mixed radio pools
- new search, song and recommendation API; remove library/longform/seed routes
- chart PVC/media env, docs, Nix, README and an end-to-end smoke test
This commit is contained in:
TapTap committed 2026-11-15 21:37:39 +00:00
1 parent 50d8b6c580
commit 824eb36dd2
59 files changed
+3838 -5276

No files matched your search

@@ -3,20 +3,18 @@ package win.taptap.musicmanager.db
import java.nio.file.Files
import java.nio.file.Path
import java.sql.DriverManager
import org.flywaydb.core.Flyway
import org.flywaydb.core.api.MigrationVersion
import java.time.Instant
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import org.flywaydb.core.Flyway
import org.flywaydb.core.api.MigrationVersion
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.jupiter.api.io.TempDir
import win.taptap.musicmanager.config.AppConfig
import win.taptap.musicmanager.config.FolderType
import win.taptap.musicmanager.config.LibraryConfig
import win.taptap.musicmanager.config.MusicFolder
import win.taptap.musicmanager.config.UserAccount
import win.taptap.musicmanager.config.UsersConfig
@@ -32,23 +30,24 @@ class DatabaseFactoryTest {
assertEquals("wal", pragma(db, "journal_mode"))
assertEquals("1", pragma(db, "foreign_keys"))
val migrations = db.dataSource.connection.use { connection ->
connection.createStatement().use { statement ->
statement.executeQuery("SELECT COUNT(*) FROM flyway_schema_history").use { result ->
result.next()
result.getInt(1)
}
}
}
assertTrue(migrations >= 2, "Expected at least the V1 and V2 migrations")
val migrations = queryInt(db, "SELECT COUNT(*) FROM flyway_schema_history")
assertTrue(migrations >= 8, "Expected at least the V1..V8 migrations")
// The new schema exists and the mounted-library tables do not.
assertTrue(tableExists(db, "songs"))
assertTrue(tableExists(db, "mood_songs"))
assertTrue(tableExists(db, "mood_recommendations"))
assertTrue(tableExists(db, "download_jobs"))
assertFalse(tableExists(db, "music_folders"))
assertFalse(tableExists(db, "artists"))
assertFalse(tableExists(db, "longform_progress"))
} finally {
db.close()
}
}
@Test
fun `upgrades legacy liked and downloaded recommendations`() {
fun `V8 wipes content but keeps users and device tokens`() {
val config = appConfig()
Files.createDirectories(config.dataDir)
val url = "jdbc:sqlite:${config.databasePath.toAbsolutePath()}"
@@ -56,7 +55,7 @@ class DatabaseFactoryTest {
Flyway.configure()
.dataSource(url, null, null)
.locations("classpath:db/migration")
.target(MigrationVersion.fromVersion("4"))
.target(MigrationVersion.fromVersion("7"))
.load()
.migrate()
@@ -66,62 +65,84 @@ class DatabaseFactoryTest {
"INSERT INTO users (name, is_admin, updated_at) VALUES ('alice', 1, 'now')",
)
statement.executeUpdate(
"INSERT INTO playlists (id, owner, name, created_at, changed_at) " +
"VALUES ('pl-1', 'alice', 'Mood', 'now', 'now')",
"INSERT INTO api_tokens " +
"(id, user_name, token_hash, device_name, platform, created_at, last_used_at, expires_at) " +
"VALUES ('tk-1', 'alice', 'hash', 'phone', 'android', 'now', 'now', 'later')",
)
statement.executeUpdate(
"INSERT INTO moods (playlist_id) VALUES ('pl-1')",
"INSERT INTO music_folders (id, name, type, root_path) VALUES (1, 'Music', 'music', '/m')",
)
statement.executeUpdate(
"INSERT INTO mood_recommendations " +
"(playlist_id, video_id, title, artist, state, first_seen, last_seen) " +
"VALUES ('pl-1', 'v-1', 'Title', 'Artist', 'liked', 'now', 'now')",
"INSERT INTO artists (id, name, sort_name, search_text) VALUES ('ar-1', 'Artist', 'artist', 'artist')",
)
statement.executeUpdate(
"INSERT INTO albums (id, name, created_at, search_text) VALUES ('al-1', 'Album', 'now', 'album')",
)
statement.executeUpdate(
"INSERT INTO songs " +
"(id, album_id, folder_id, title, display_artist, album_name, duration, bit_rate, " +
"size, suffix, content_type, relative_path, file_mtime, created_at, updated_at, search_text) " +
"VALUES ('sg-1', 'al-1', 1, 'Song', 'Artist', 'Album', 120, 128, 10, 'mp3', " +
"'audio/mpeg', 'a/b.mp3', 0, 'now', 'now', 'song')",
)
statement.executeUpdate(
"INSERT INTO moods " +
"(id, owner, name, genres, target_size, refresh_hours, yt_enabled, created_at, changed_at, seed_percent) " +
"VALUES ('mo-1', 'alice', 'Focus', '[]', 30, 24, 1, 'now', 'now', 50)",
)
statement.executeUpdate(
"INSERT INTO mood_seeds (mood_id, song_id, origin, added_at) VALUES ('mo-1', 'sg-1', 'manual', 'now')",
)
statement.executeUpdate(
"INSERT INTO playback_sessions (id, user_name, mood_id, created_at, last_seen_at) " +
"VALUES ('ps-1', 'alice', 'mo-1', 'now', 'now')",
)
statement.executeUpdate(
"INSERT INTO playback_session_items " +
"(session_id, position, song_id, state, queued_at, updated_at) " +
"VALUES ('ps-1', 0, 'sg-1', 'queued', 'now', 'now')",
)
}
}
val db = DatabaseFactory.connect(config)
try {
val state = db.dataSource.connection.use { connection ->
connection.createStatement().use { statement ->
statement.executeQuery(
"SELECT state FROM mood_recommendations WHERE video_id = 'v-1'",
).use { result ->
result.next()
result.getString(1)
}
}
}
assertEquals("new", state)
assertEquals(
1,
queryInt(db, "SELECT COUNT(*) FROM users WHERE name = 'alice'"),
"Users must survive the wipe",
)
assertEquals(
1,
queryInt(db, "SELECT COUNT(*) FROM api_tokens WHERE id = 'tk-1'"),
"Device tokens must survive the wipe",
)
assertEquals(0, queryInt(db, "SELECT COUNT(*) FROM songs"))
assertEquals(0, queryInt(db, "SELECT COUNT(*) FROM moods"))
assertEquals(0, queryInt(db, "SELECT COUNT(*) FROM playback_sessions"))
assertTrue(tableExists(db, "mood_songs"))
assertFalse(tableExists(db, "music_folders"))
} finally {
db.close()
}
}
@Test
fun `syncs folders and users idempotently`() {
fun `syncs users idempotently`() {
val db = DatabaseFactory.connect(appConfig())
try {
val library = library()
val users = UsersConfig(
listOf(
UserAccount("alice", "pw", admin = true, folders = setOf(1)),
UserAccount("bob", "pw", admin = false, folders = setOf(1)),
UserAccount("alice", "pw", admin = true),
UserAccount("bob", "pw", admin = false),
),
)
ConfigRepository.sync(library, users)
ConfigRepository.sync(library, users)
ConfigRepository.sync(users)
ConfigRepository.sync(users)
transaction(db.database) {
assertEquals(2L, MusicFoldersTable.selectAll().count())
assertEquals(2L, UsersTable.selectAll().count())
assertEquals(
"longform",
MusicFoldersTable.selectAll()
.where { MusicFoldersTable.id eq 10 }
.single()[MusicFoldersTable.type],
)
}
} finally {
db.close()
@@ -129,65 +150,69 @@ class DatabaseFactoryTest {
}
@Test
fun `removing a user cascades their per-user state`() {
fun `removing a user cascades their moods and playback state`() {
val db = DatabaseFactory.connect(appConfig())
try {
val library = library()
val aliceAndBob = UsersConfig(
listOf(
UserAccount("alice", "pw", admin = true, folders = setOf(1)),
UserAccount("bob", "pw", admin = false, folders = setOf(1)),
ConfigRepository.sync(
UsersConfig(
listOf(
UserAccount("alice", "pw", admin = true),
UserAccount("bob", "pw", admin = false),
),
),
)
ConfigRepository.sync(library, aliceAndBob)
insertSong(db, folderId = 1)
val now = Instant.now().toString()
val moodId = "mo-1"
transaction(db.database) {
UserSongStateTable.insert {
SongsTable.insert {
it[id] = "sg-1"
it[videoId] = "video-1"
it[title] = "Song"
it[artist] = "Artist"
it[relativePath] = "video-1/song.mp3"
it[createdAt] = now
it[updatedAt] = now
}
MoodsTable.insert {
it[id] = moodId
it[owner] = "alice"
it[name] = "Focus"
it[createdAt] = now
it[changedAt] = now
}
MoodSongsTable.insert {
it[this.moodId] = moodId
it[songId] = "sg-1"
it[addedAt] = now
}
PlaybackSessionsTable.insert {
it[id] = "ps-1"
it[userName] = "alice"
it[this.moodId] = moodId
it[createdAt] = now
it[lastSeenAt] = now
}
SessionItemsTable.insert {
it[sessionId] = "ps-1"
it[position] = 0
it[songId] = "sg-1"
it[playCount] = 3
it[starredAt] = Instant.now().toString()
it[queuedAt] = now
it[updatedAt] = now
}
assertEquals(1L, UserSongStateTable.selectAll().count())
}
ConfigRepository.sync(
library,
UsersConfig(listOf(UserAccount("bob", "pw", admin = false, folders = setOf(1)))),
UsersConfig(listOf(UserAccount("bob", "pw", admin = false))),
)
transaction(db.database) {
assertEquals(0L, UserSongStateTable.selectAll().count())
assertEquals(1L, UsersTable.selectAll().count())
}
} finally {
db.close()
}
}
@Test
fun `removing a folder cascades its songs`() {
val db = DatabaseFactory.connect(appConfig())
try {
val library = library()
val users = UsersConfig(listOf(UserAccount("alice", "pw", admin = true, folders = setOf(1))))
ConfigRepository.sync(library, users)
insertSong(db, folderId = 1)
transaction(db.database) {
assertEquals(0L, MoodsTable.selectAll().count())
assertEquals(0L, MoodSongsTable.selectAll().count())
assertEquals(0L, PlaybackSessionsTable.selectAll().count())
// Songs are shared and have no owner, so they survive.
assertEquals(1L, SongsTable.selectAll().count())
}
ConfigRepository.sync(
LibraryConfig(library.folders.filter { it.id == 10 }),
UsersConfig(listOf(UserAccount("alice", "pw", admin = true, folders = emptySet()))),
)
transaction(db.database) {
assertEquals(0L, SongsTable.selectAll().count())
assertEquals(1L, MusicFoldersTable.selectAll().count())
}
} finally {
db.close()
}
@@ -196,57 +221,10 @@ class DatabaseFactoryTest {
private fun appConfig(): AppConfig = AppConfig(
port = 8080,
dataDir = tempDir.resolve("data"),
libraryConfigPath = tempDir.resolve("library.yaml"),
usersFile = tempDir.resolve("users.yaml"),
ffmpegPath = "ffmpeg",
)
private fun library(): LibraryConfig {
val music = Files.createDirectories(tempDir.resolve("music"))
val longform = Files.createDirectories(tempDir.resolve("longform"))
return LibraryConfig(
listOf(
MusicFolder(1, "Music", FolderType.MUSIC, music),
MusicFolder(10, "Long-form", FolderType.LONGFORM, longform),
),
)
}
private fun insertSong(db: DatabaseHandle, folderId: Int) {
val now = Instant.now().toString()
transaction(db.database) {
ArtistsTable.insert {
it[id] = "ar-1"
it[name] = "Artist"
it[sortName] = "artist"
it[searchText] = "artist"
}
AlbumsTable.insert {
it[id] = "al-1"
it[name] = "Album"
it[createdAt] = now
it[searchText] = "album"
}
SongsTable.insert {
it[id] = "sg-1"
it[albumId] = "al-1"
it[this.folderId] = folderId
it[title] = "Song"
it[displayArtist] = "Artist"
it[albumName] = "Album"
it[duration] = 120
it[bitRate] = 128
it[size] = 1024
it[suffix] = "mp3"
it[contentType] = "audio/mpeg"
it[relativePath] = "artist/album/song.mp3"
it[fileMtime] = 0
it[createdAt] = now
it[searchText] = "song"
}
}
}
private fun pragma(db: DatabaseHandle, name: String): String =
db.dataSource.connection.use { connection ->
connection.createStatement().use { statement ->
@@ -256,4 +234,20 @@ class DatabaseFactoryTest {
}
}
}
private fun queryInt(db: DatabaseHandle, sql: String): Int =
db.dataSource.connection.use { connection ->
connection.createStatement().use { statement ->
statement.executeQuery(sql).use { result ->
assertTrue(result.next(), "$sql returned no row")
result.getInt(1)
}
}
}
private fun tableExists(db: DatabaseHandle, name: String): Boolean =
queryInt(
db,
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = '$name'",
) > 0
}